@@ -16,18 +16,19 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
16
package main
17
17
18
18
import (
19
- "io/ioutil"
20
- "os/exec"
21
- "log"
22
- "strings"
23
- "strconv"
24
- "regexp"
25
- "github.com/prometheus/client_golang/prometheus"
19
+ "io/ioutil"
20
+ "log"
21
+ "os/exec"
22
+ "regexp"
23
+ "strconv"
24
+ "strings"
25
+
26
+ "github.com/prometheus/client_golang/prometheus"
26
27
)
27
28
28
29
func AccountsData () []byte {
29
- cmd := exec .Command ("squeue" ,"-a" ,"-r" ,"-h" ,"-o %A|%a|%T|%C" )
30
- stdout , err := cmd .StdoutPipe ()
30
+ cmd := exec .Command ("squeue" , "-a" , "-r" , "-h" , "-o %A|%a|%T|%C" )
31
+ stdout , err := cmd .StdoutPipe ()
31
32
if err != nil {
32
33
log .Fatal (err )
33
34
}
@@ -42,80 +43,80 @@ func AccountsData() []byte {
42
43
}
43
44
44
45
type JobMetrics struct {
45
- pending float64
46
- running float64
47
- running_cpus float64
48
- suspended float64
46
+ pending float64
47
+ running float64
48
+ running_cpus float64
49
+ suspended float64
49
50
}
50
51
51
52
func 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
53
+ accounts := make (map [string ]* JobMetrics )
54
+ lines := strings .Split (string (input ), "\n " )
55
+ for _ , line := range lines {
56
+ if strings .Contains (line , "|" ) {
57
+ account := strings .Split (line , "|" )[1 ]
58
+ _ , key := accounts [account ]
59
+ if ! key {
60
+ accounts [account ] = & JobMetrics {0 , 0 , 0 , 0 }
61
+ }
62
+ state := strings .Split (line , "|" )[2 ]
63
+ state = strings .ToLower (state )
64
+ cpus , _ := strconv .ParseFloat (strings .Split (line , "|" )[3 ], 64 )
65
+ pending := regexp .MustCompile (`^pending` )
66
+ running := regexp .MustCompile (`^running` )
67
+ suspended := regexp .MustCompile (`^suspended` )
68
+ switch {
69
+ case pending .MatchString (state ):
70
+ accounts [account ].pending ++
71
+ case running .MatchString (state ):
72
+ accounts [account ].running ++
73
+ accounts [account ].running_cpus += cpus
74
+ case suspended .MatchString (state ):
75
+ accounts [account ].suspended ++
76
+ }
77
+ }
78
+ }
79
+ return accounts
79
80
}
80
81
81
82
type AccountsCollector struct {
82
- pending * prometheus.Desc
83
- running * prometheus.Desc
84
- running_cpus * prometheus.Desc
85
- suspended * prometheus.Desc
83
+ pending * prometheus.Desc
84
+ running * prometheus.Desc
85
+ running_cpus * prometheus.Desc
86
+ suspended * prometheus.Desc
86
87
}
87
88
88
89
func 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
- }
90
+ labels := []string {"account" }
91
+ return & AccountsCollector {
92
+ pending : prometheus .NewDesc ("slurm_account_jobs_pending" , "Pending jobs for account" , labels , nil ),
93
+ running : prometheus .NewDesc ("slurm_account_jobs_running" , "Running jobs for account" , labels , nil ),
94
+ running_cpus : prometheus .NewDesc ("slurm_account_cpus_running" , "Running cpus for account" , labels , nil ),
95
+ suspended : prometheus .NewDesc ("slurm_account_jobs_suspended" , "Suspended jobs for account" , labels , nil ),
96
+ }
96
97
}
97
98
98
99
func (ac * AccountsCollector ) Describe (ch chan <- * prometheus.Desc ) {
99
- ch <- ac .pending
100
- ch <- ac .running
101
- ch <- ac .running_cpus
102
- ch <- ac .suspended
100
+ ch <- ac .pending
101
+ ch <- ac .running
102
+ ch <- ac .running_cpus
103
+ ch <- ac .suspended
103
104
}
104
105
105
106
func (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
- }
107
+ am := ParseAccountsMetrics (AccountsData ())
108
+ for a := range am {
109
+ if am [a ].pending > 0 {
110
+ ch <- prometheus .MustNewConstMetric (ac .pending , prometheus .GaugeValue , am [a ].pending , a )
111
+ }
112
+ if am [a ].running > 0 {
113
+ ch <- prometheus .MustNewConstMetric (ac .running , prometheus .GaugeValue , am [a ].running , a )
114
+ }
115
+ if am [a ].running_cpus > 0 {
116
+ ch <- prometheus .MustNewConstMetric (ac .running_cpus , prometheus .GaugeValue , am [a ].running_cpus , a )
117
+ }
118
+ if am [a ].suspended > 0 {
119
+ ch <- prometheus .MustNewConstMetric (ac .suspended , prometheus .GaugeValue , am [a ].suspended , a )
120
+ }
121
+ }
121
122
}
0 commit comments