@@ -23,46 +23,46 @@ import (
2323)
2424
2525type PartitionMetrics struct {
26- allocated float64
27- idle float64
28- other float64
29- pending float64
30- total float64
26+ allocated float64
27+ idle float64
28+ other float64
29+ pending float64
30+ total float64
3131}
3232
3333func ParsePartitionsMetrics (sinfoOutput []byte , squeueOutput []byte ) map [string ]* PartitionMetrics {
34- partitions := make (map [string ]* PartitionMetrics )
35- lines := strings .Split (string (sinfoOutput ), "\n " )
36- for _ , line := range lines {
37- if strings .Contains (line , "," ) {
38- // name of a partition
39- partition := strings .Split (line , "," )[0 ]
40- _ , key := partitions [partition ]
41- if ! key {
42- partitions [partition ] = & PartitionMetrics {0 , 0 , 0 , 0 , 0 }
43- }
44- states := strings .Split (line , "," )[1 ]
45- allocated , _ := strconv .ParseFloat (strings .Split (states , "/" )[0 ], 64 )
46- idle , _ := strconv .ParseFloat (strings .Split (states , "/" )[1 ], 64 )
47- other , _ := strconv .ParseFloat (strings .Split (states , "/" )[2 ], 64 )
48- total , _ := strconv .ParseFloat (strings .Split (states , "/" )[3 ], 64 )
49- partitions [partition ].allocated = allocated
50- partitions [partition ].idle = idle
51- partitions [partition ].other = other
52- partitions [partition ].total = total
53- }
54- }
55- // get list of pending jobs by partition name
56- list := strings .Split (string (squeueOutput ), "\n " )
57- for _ , partition := range list {
58- // accumulate the number of pending jobs
59- _ , key := partitions [partition ]
60- if key {
61- partitions [partition ].pending += 1
62- }
63- }
34+ partitions := make (map [string ]* PartitionMetrics )
35+ lines := strings .Split (string (sinfoOutput ),"\n " )
36+ for _ , line := range lines {
37+ if strings .Contains (line ,"," ) {
38+ // name of a partition
39+ partition := strings .Split (line ,"," )[0 ]
40+ _ , key := partitions [partition ]
41+ if ! key {
42+ partitions [partition ] = & PartitionMetrics {0 ,0 , 0 , 0 , 0 }
43+ }
44+ states := strings .Split (line ,"," )[1 ]
45+ allocated ,_ := strconv .ParseFloat (strings .Split (states ,"/" )[0 ],64 )
46+ idle ,_ := strconv .ParseFloat (strings .Split (states ,"/" )[1 ],64 )
47+ other ,_ := strconv .ParseFloat (strings .Split (states ,"/" )[2 ],64 )
48+ total ,_ := strconv .ParseFloat (strings .Split (states ,"/" )[3 ],64 )
49+ partitions [partition ].allocated = allocated
50+ partitions [partition ].idle = idle
51+ partitions [partition ].other = other
52+ partitions [partition ].total = total
53+ }
54+ }
55+ // get list of pending jobs by partition name
56+ list := strings .Split (string (squeueOutput ), "\n " )
57+ for _ ,partition := range list {
58+ // accumulate the number of pending jobs
59+ _ , key := partitions [partition ]
60+ if key {
61+ partitions [partition ].pending += 1
62+ }
63+ }
6464
65- return partitions
65+ return partitions
6666}
6767
6868func GetPartitionsMetrics () map [string ]* PartitionMetrics {
@@ -73,49 +73,49 @@ func GetPartitionsMetrics() map[string]*PartitionMetrics {
7373}
7474
7575type PartitionsCollector struct {
76- allocated * prometheus.Desc
77- idle * prometheus.Desc
78- other * prometheus.Desc
79- pending * prometheus.Desc
80- total * prometheus.Desc
76+ allocated * prometheus.Desc
77+ idle * prometheus.Desc
78+ other * prometheus.Desc
79+ pending * prometheus.Desc
80+ total * prometheus.Desc
8181}
8282
8383func NewPartitionsCollector () * PartitionsCollector {
84- labels := []string {"partition" }
85- return & PartitionsCollector {
86- allocated : prometheus .NewDesc ("slurm_partition_cpus_allocated" , "Allocated CPUs for partition" , labels , nil ),
87- idle : prometheus .NewDesc ("slurm_partition_cpus_idle" , "Idle CPUs for partition" , labels , nil ),
88- other : prometheus .NewDesc ("slurm_partition_cpus_other" , "Other CPUs for partition" , labels , nil ),
89- pending : prometheus .NewDesc ("slurm_partition_jobs_pending" , "Pending jobs for partition" , labels , nil ),
90- total : prometheus .NewDesc ("slurm_partition_cpus_total" , "Total CPUs for partition" , labels , nil ),
91- }
84+ labels := []string {"partition" }
85+ return & PartitionsCollector {
86+ allocated : prometheus .NewDesc ("slurm_partition_cpus_allocated" , "Allocated CPUs for partition" , labels ,nil ),
87+ idle : prometheus .NewDesc ("slurm_partition_cpus_idle" , "Idle CPUs for partition" , labels ,nil ),
88+ other : prometheus .NewDesc ("slurm_partition_cpus_other" , "Other CPUs for partition" , labels ,nil ),
89+ pending : prometheus .NewDesc ("slurm_partition_jobs_pending" , "Pending jobs for partition" , labels ,nil ),
90+ total : prometheus .NewDesc ("slurm_partition_cpus_total" , "Total CPUs for partition" , labels ,nil ),
91+ }
9292}
9393
9494func (pc * PartitionsCollector ) Describe (ch chan <- * prometheus.Desc ) {
95- ch <- pc .allocated
96- ch <- pc .idle
97- ch <- pc .other
98- ch <- pc .pending
99- ch <- pc .total
95+ ch <- pc .allocated
96+ ch <- pc .idle
97+ ch <- pc .other
98+ ch <- pc .pending
99+ ch <- pc .total
100100}
101101
102102func (pc * PartitionsCollector ) Collect (ch chan <- prometheus.Metric ) {
103- pm := GetPartitionsMetrics ()
104- for p := range pm {
105- if pm [p ].allocated > 0 {
106- ch <- prometheus .MustNewConstMetric (pc .allocated , prometheus .GaugeValue , pm [p ].allocated , p )
107- }
108- if pm [p ].idle > 0 {
109- ch <- prometheus .MustNewConstMetric (pc .idle , prometheus .GaugeValue , pm [p ].idle , p )
110- }
111- if pm [p ].other > 0 {
112- ch <- prometheus .MustNewConstMetric (pc .other , prometheus .GaugeValue , pm [p ].other , p )
113- }
114- if pm [p ].pending > 0 {
115- ch <- prometheus .MustNewConstMetric (pc .pending , prometheus .GaugeValue , pm [p ].pending , p )
116- }
117- if pm [p ].total > 0 {
118- ch <- prometheus .MustNewConstMetric (pc .total , prometheus .GaugeValue , pm [p ].total , p )
119- }
120- }
103+ pm := GetPartitionsMetrics ()
104+ for p := range pm {
105+ if pm [p ].allocated > 0 {
106+ ch <- prometheus .MustNewConstMetric (pc .allocated , prometheus .GaugeValue , pm [p ].allocated , p )
107+ }
108+ if pm [p ].idle > 0 {
109+ ch <- prometheus .MustNewConstMetric (pc .idle , prometheus .GaugeValue , pm [p ].idle , p )
110+ }
111+ if pm [p ].other > 0 {
112+ ch <- prometheus .MustNewConstMetric (pc .other , prometheus .GaugeValue , pm [p ].other , p )
113+ }
114+ if pm [p ].pending > 0 {
115+ ch <- prometheus .MustNewConstMetric (pc .pending , prometheus .GaugeValue , pm [p ].pending , p )
116+ }
117+ if pm [p ].total > 0 {
118+ ch <- prometheus .MustNewConstMetric (pc .total , prometheus .GaugeValue , pm [p ].total , p )
119+ }
120+ }
121121}
0 commit comments