Skip to content

Commit c7e9b13

Browse files
committed
first fairshare collector prototype
1 parent 4440740 commit c7e9b13

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

sshare.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ import (
1919
"io/ioutil"
2020
"os/exec"
2121
"log"
22+
"strings"
23+
"strconv"
24+
"github.com/prometheus/client_golang/prometheus"
2225
)
2326

2427
func FairShareData() []byte {
25-
cmd := exec.Command("sshare", "-o account,fairshare")
28+
cmd := exec.Command("sshare", "-nPo account,fairshare", "|", "grep '^ [a-z]'", "|", "tr -d ' '" )
2629
stdout, err := cmd.StdoutPipe()
2730
if err != nil {
2831
log.Fatal(err)
@@ -36,3 +39,46 @@ func FairShareData() []byte {
3639
}
3740
return out
3841
}
42+
43+
type FairShareMetrics struct {
44+
fairshare float64
45+
}
46+
47+
func ParseFairShareMetrics() map[string]*FairShareMetrics {
48+
accounts := make(map[string]*FairShareMetrics)
49+
lines := strings.Split(string(FairShareData()), "\n")
50+
for _, line := range lines {
51+
if strings.Contains(line,"|") {
52+
account := strings.Split(line,"|")[0]
53+
_,key := accounts[account]
54+
if !key {
55+
accounts[account] = &FairShareMetrics{0}
56+
}
57+
fairshare,_ := strconv.ParseFloat(strings.Split(line,"|")[1],64)
58+
accounts[account].fairshare = fairshare
59+
}
60+
}
61+
return accounts
62+
}
63+
64+
type FairShareCollector struct {
65+
fairshare *prometheus.Desc
66+
}
67+
68+
func NewFairShareCollector() *FairShareCollector {
69+
labels := []string{"account"}
70+
return &FairShareCollector{
71+
fairshare: prometheus.NewDesc("slurm_account_fairshare","FairShare for account" , labels,nil),
72+
}
73+
}
74+
75+
func (fsc *FairShareCollector) Describe(ch chan<- *prometheus.Desc) {
76+
ch <- fsc.fairshare
77+
}
78+
79+
func (fsc *FairShareCollector) Collect(ch chan<- prometheus.Metric) {
80+
fsm := ParseFairShareMetrics()
81+
for f := range fsm {
82+
ch <- prometheus.MustNewConstMetric(fsc.fairshare, prometheus.GaugeValue, fsm[f].fairshare, f)
83+
}
84+
}

0 commit comments

Comments
 (0)