@@ -16,18 +16,18 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */
1616package main
1717
1818import (
19- "io/ioutil "
20- "os/exec "
21- "log"
22- "strings "
23- "strconv "
24- "regexp "
25- "github.com/prometheus/client_golang/prometheus "
19+ "github.com/prometheus/client_golang/prometheus "
20+ "io/ioutil "
21+ "log"
22+ "os/exec "
23+ "regexp "
24+ "strconv "
25+ "strings "
2626)
2727
2828func AccountsData () []byte {
29- cmd := exec .Command ("squeue" ,"-a" ,"-r" ,"-h" ,"-o %A|%a|%T|%C" )
30- stdout , err := cmd .StdoutPipe ()
29+ cmd := exec .Command ("squeue" , "-a" , "-r" , "-h" , "-o %A|%a|%T|%C" )
30+ stdout , err := cmd .StdoutPipe ()
3131 if err != nil {
3232 log .Fatal (err )
3333 }
@@ -42,80 +42,80 @@ func AccountsData() []byte {
4242}
4343
4444type JobMetrics struct {
45- pending float64
46- running float64
47- running_cpus float64
48- suspended float64
45+ pending float64
46+ running float64
47+ running_cpus float64
48+ suspended float64
4949}
5050
5151func ParseAccountsMetrics (input []byte ) map [string ]* JobMetrics {
52- accounts := make (map [string ]* JobMetrics )
53- lines := strings .Split (string (input ), "\n " )
54- for _ , line := range lines {
55- if strings .Contains (line ,"|" ) {
56- account := strings .Split (line ,"|" )[1 ]
57- _ , key := accounts [account ]
58- if ! key {
59- accounts [account ] = & JobMetrics {0 ,0 , 0 , 0 }
60- }
61- state := strings .Split (line ,"|" )[2 ]
62- state = strings .ToLower (state )
63- cpus ,_ := strconv .ParseFloat (strings .Split (line ,"|" )[3 ],64 )
64- pending := regexp .MustCompile (`^pending` )
65- running := regexp .MustCompile (`^running` )
66- suspended := regexp .MustCompile (`^suspended` )
67- switch {
68- case pending .MatchString (state ) == true :
69- accounts [account ].pending ++
70- case running .MatchString (state ) == true :
71- accounts [account ].running ++
72- accounts [account ].running_cpus += cpus
73- case suspended .MatchString (state ) == true :
74- accounts [account ].suspended ++
75- }
76- }
77- }
78- return accounts
52+ accounts := make (map [string ]* JobMetrics )
53+ lines := strings .Split (string (input ), "\n " )
54+ for _ , line := range lines {
55+ if strings .Contains (line , "|" ) {
56+ account := strings .Split (line , "|" )[1 ]
57+ _ , key := accounts [account ]
58+ if ! key {
59+ accounts [account ] = & JobMetrics {0 , 0 , 0 , 0 }
60+ }
61+ state := strings .Split (line , "|" )[2 ]
62+ state = strings .ToLower (state )
63+ cpus , _ := strconv .ParseFloat (strings .Split (line , "|" )[3 ], 64 )
64+ pending := regexp .MustCompile (`^pending` )
65+ running := regexp .MustCompile (`^running` )
66+ suspended := regexp .MustCompile (`^suspended` )
67+ switch {
68+ case pending .MatchString (state ) == true :
69+ accounts [account ].pending ++
70+ case running .MatchString (state ) == true :
71+ accounts [account ].running ++
72+ accounts [account ].running_cpus += cpus
73+ case suspended .MatchString (state ) == true :
74+ accounts [account ].suspended ++
75+ }
76+ }
77+ }
78+ return accounts
7979}
8080
8181type AccountsCollector struct {
82- pending * prometheus.Desc
83- running * prometheus.Desc
84- running_cpus * prometheus.Desc
85- suspended * prometheus.Desc
82+ pending * prometheus.Desc
83+ running * prometheus.Desc
84+ running_cpus * prometheus.Desc
85+ suspended * prometheus.Desc
8686}
8787
8888func NewAccountsCollector () * AccountsCollector {
89- labels := []string {"account" }
90- return & AccountsCollector {
91- pending : prometheus .NewDesc ("slurm_account_jobs_pending" , "Pending jobs for account" , labels , nil ),
92- running : prometheus .NewDesc ("slurm_account_jobs_running" , "Running jobs for account" , labels , nil ),
93- running_cpus : prometheus .NewDesc ("slurm_account_cpus_running" , "Running cpus for account" , labels , nil ),
94- suspended : prometheus .NewDesc ("slurm_account_jobs_suspended" , "Suspended jobs for account" , labels , nil ),
95- }
89+ labels := []string {"account" }
90+ return & AccountsCollector {
91+ pending : prometheus .NewDesc ("slurm_account_jobs_pending" , "Pending jobs for account" , labels , nil ),
92+ running : prometheus .NewDesc ("slurm_account_jobs_running" , "Running jobs for account" , labels , nil ),
93+ running_cpus : prometheus .NewDesc ("slurm_account_cpus_running" , "Running cpus for account" , labels , nil ),
94+ suspended : prometheus .NewDesc ("slurm_account_jobs_suspended" , "Suspended jobs for account" , labels , nil ),
95+ }
9696}
9797
9898func (ac * AccountsCollector ) Describe (ch chan <- * prometheus.Desc ) {
99- ch <- ac .pending
100- ch <- ac .running
101- ch <- ac .running_cpus
102- ch <- ac .suspended
99+ ch <- ac .pending
100+ ch <- ac .running
101+ ch <- ac .running_cpus
102+ ch <- ac .suspended
103103}
104104
105105func (ac * AccountsCollector ) Collect (ch chan <- prometheus.Metric ) {
106- am := ParseAccountsMetrics (AccountsData ())
107- for a := range am {
108- if am [a ].pending > 0 {
109- ch <- prometheus .MustNewConstMetric (ac .pending , prometheus .GaugeValue , am [a ].pending , a )
110- }
111- if am [a ].running > 0 {
112- ch <- prometheus .MustNewConstMetric (ac .running , prometheus .GaugeValue , am [a ].running , a )
113- }
114- if am [a ].running_cpus > 0 {
115- ch <- prometheus .MustNewConstMetric (ac .running_cpus , prometheus .GaugeValue , am [a ].running_cpus , a )
116- }
117- if am [a ].suspended > 0 {
118- ch <- prometheus .MustNewConstMetric (ac .suspended , prometheus .GaugeValue , am [a ].suspended , a )
119- }
120- }
106+ am := ParseAccountsMetrics (AccountsData ())
107+ for a := range am {
108+ if am [a ].pending > 0 {
109+ ch <- prometheus .MustNewConstMetric (ac .pending , prometheus .GaugeValue , am [a ].pending , a )
110+ }
111+ if am [a ].running > 0 {
112+ ch <- prometheus .MustNewConstMetric (ac .running , prometheus .GaugeValue , am [a ].running , a )
113+ }
114+ if am [a ].running_cpus > 0 {
115+ ch <- prometheus .MustNewConstMetric (ac .running_cpus , prometheus .GaugeValue , am [a ].running_cpus , a )
116+ }
117+ if am [a ].suspended > 0 {
118+ ch <- prometheus .MustNewConstMetric (ac .suspended , prometheus .GaugeValue , am [a ].suspended , a )
119+ }
120+ }
121121}
0 commit comments