Skip to content

Commit ccf39b6

Browse files
committed
Revert e7064fd, undoing style fixes
To decrease the size of the pull request
1 parent 9afe3af commit ccf39b6

File tree

7 files changed

+235
-236
lines changed

7 files changed

+235
-236
lines changed

accounts.go

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -17,91 +17,91 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */
1717
package main
1818

1919
import (
20-
"github.com/prometheus/client_golang/prometheus"
21-
"regexp"
22-
"strconv"
23-
"strings"
20+
"strings"
21+
"strconv"
22+
"regexp"
23+
"github.com/prometheus/client_golang/prometheus"
2424
)
2525

2626
type JobMetrics struct {
27-
pending float64
28-
running float64
29-
running_cpus float64
30-
suspended float64
27+
pending float64
28+
running float64
29+
running_cpus float64
30+
suspended float64
3131
}
3232

33-
func ParseAccountsMetrics(squeueOutput []byte) map[string]*JobMetrics {
34-
accounts := make(map[string]*JobMetrics)
35-
lines := strings.Split(string(squeueOutput), "\n")
36-
for _, line := range lines {
37-
if strings.Contains(line, "|") {
38-
account := strings.Split(line, "|")[1]
39-
_, key := accounts[account]
40-
if !key {
41-
accounts[account] = &JobMetrics{0, 0, 0, 0}
42-
}
43-
state := strings.Split(line, "|")[2]
44-
state = strings.ToLower(state)
45-
cpus, _ := strconv.ParseFloat(strings.Split(line, "|")[3], 64)
46-
pending := regexp.MustCompile(`^pending`)
47-
running := regexp.MustCompile(`^running`)
48-
suspended := regexp.MustCompile(`^suspended`)
49-
switch {
50-
case pending.MatchString(state) == true:
51-
accounts[account].pending++
52-
case running.MatchString(state) == true:
53-
accounts[account].running++
54-
accounts[account].running_cpus += cpus
55-
case suspended.MatchString(state) == true:
56-
accounts[account].suspended++
57-
}
58-
}
59-
}
60-
return accounts
33+
func ParseAccountsMetrics(input []byte) map[string]*JobMetrics {
34+
accounts := make(map[string]*JobMetrics)
35+
lines := strings.Split(string(input), "\n")
36+
for _, line := range lines {
37+
if strings.Contains(line,"|") {
38+
account := strings.Split(line,"|")[1]
39+
_,key := accounts[account]
40+
if !key {
41+
accounts[account] = &JobMetrics{0,0,0,0}
42+
}
43+
state := strings.Split(line,"|")[2]
44+
state = strings.ToLower(state)
45+
cpus,_ := strconv.ParseFloat(strings.Split(line,"|")[3],64)
46+
pending := regexp.MustCompile(`^pending`)
47+
running := regexp.MustCompile(`^running`)
48+
suspended := regexp.MustCompile(`^suspended`)
49+
switch {
50+
case pending.MatchString(state) == true:
51+
accounts[account].pending++
52+
case running.MatchString(state) == true:
53+
accounts[account].running++
54+
accounts[account].running_cpus += cpus
55+
case suspended.MatchString(state) == true:
56+
accounts[account].suspended++
57+
}
58+
}
59+
}
60+
return accounts
6161
}
6262

6363
func GetAccountsMetrics() map[string]*JobMetrics {
6464
return ParseAccountsMetrics(Subprocess("squeue", "-a", "-r", "-h", "-o %A|%a|%T|%C"))
6565
}
6666

6767
type AccountsCollector struct {
68-
pending *prometheus.Desc
69-
running *prometheus.Desc
70-
running_cpus *prometheus.Desc
71-
suspended *prometheus.Desc
68+
pending *prometheus.Desc
69+
running *prometheus.Desc
70+
running_cpus *prometheus.Desc
71+
suspended *prometheus.Desc
7272
}
7373

7474
func NewAccountsCollector() *AccountsCollector {
75-
labels := []string{"account"}
76-
return &AccountsCollector{
77-
pending: prometheus.NewDesc("slurm_account_jobs_pending", "Pending jobs for account", labels, nil),
78-
running: prometheus.NewDesc("slurm_account_jobs_running", "Running jobs for account", labels, nil),
79-
running_cpus: prometheus.NewDesc("slurm_account_cpus_running", "Running cpus for account", labels, nil),
80-
suspended: prometheus.NewDesc("slurm_account_jobs_suspended", "Suspended jobs for account", labels, nil),
81-
}
75+
labels := []string{"account"}
76+
return &AccountsCollector{
77+
pending: prometheus.NewDesc("slurm_account_jobs_pending", "Pending jobs for account", labels, nil),
78+
running: prometheus.NewDesc("slurm_account_jobs_running", "Running jobs for account", labels, nil),
79+
running_cpus: prometheus.NewDesc("slurm_account_cpus_running", "Running cpus for account", labels, nil),
80+
suspended: prometheus.NewDesc("slurm_account_jobs_suspended", "Suspended jobs for account", labels, nil),
81+
}
8282
}
8383

8484
func (ac *AccountsCollector) Describe(ch chan<- *prometheus.Desc) {
85-
ch <- ac.pending
86-
ch <- ac.running
87-
ch <- ac.running_cpus
88-
ch <- ac.suspended
85+
ch <- ac.pending
86+
ch <- ac.running
87+
ch <- ac.running_cpus
88+
ch <- ac.suspended
8989
}
9090

9191
func (ac *AccountsCollector) Collect(ch chan<- prometheus.Metric) {
92-
am := GetAccountsMetrics()
93-
for a := range am {
94-
if am[a].pending > 0 {
95-
ch <- prometheus.MustNewConstMetric(ac.pending, prometheus.GaugeValue, am[a].pending, a)
96-
}
97-
if am[a].running > 0 {
98-
ch <- prometheus.MustNewConstMetric(ac.running, prometheus.GaugeValue, am[a].running, a)
99-
}
100-
if am[a].running_cpus > 0 {
101-
ch <- prometheus.MustNewConstMetric(ac.running_cpus, prometheus.GaugeValue, am[a].running_cpus, a)
102-
}
103-
if am[a].suspended > 0 {
104-
ch <- prometheus.MustNewConstMetric(ac.suspended, prometheus.GaugeValue, am[a].suspended, a)
105-
}
106-
}
92+
am := GetAccountsMetrics()
93+
for a := range am {
94+
if am[a].pending > 0 {
95+
ch <- prometheus.MustNewConstMetric(ac.pending, prometheus.GaugeValue, am[a].pending, a)
96+
}
97+
if am[a].running > 0 {
98+
ch <- prometheus.MustNewConstMetric(ac.running, prometheus.GaugeValue, am[a].running, a)
99+
}
100+
if am[a].running_cpus > 0 {
101+
ch <- prometheus.MustNewConstMetric(ac.running_cpus, prometheus.GaugeValue, am[a].running_cpus, a)
102+
}
103+
if am[a].suspended > 0 {
104+
ch <- prometheus.MustNewConstMetric(ac.suspended, prometheus.GaugeValue, am[a].suspended, a)
105+
}
106+
}
107107
}

gpus.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ func GetGPUsMetrics() *GPUsMetrics {
9292

9393
func NewGPUsCollector() *GPUsCollector {
9494
return &GPUsCollector{
95-
alloc: prometheus.NewDesc("slurm_gpus_alloc", "Allocated GPUs", nil, nil),
96-
idle: prometheus.NewDesc("slurm_gpus_idle", "Idle GPUs", nil, nil),
97-
total: prometheus.NewDesc("slurm_gpus_total", "Total GPUs", nil, nil),
95+
alloc: prometheus.NewDesc("slurm_gpus_alloc", "Allocated GPUs", nil, nil),
96+
idle: prometheus.NewDesc("slurm_gpus_idle", "Idle GPUs", nil, nil),
97+
total: prometheus.NewDesc("slurm_gpus_total", "Total GPUs", nil, nil),
9898
utilization: prometheus.NewDesc("slurm_gpus_utilization", "Total GPU utilization", nil, nil),
9999
}
100100
}

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ func init() {
2727
// Metrics have to be registered to be exposed
2828
prometheus.MustRegister(NewAccountsCollector()) // from accounts.go
2929
prometheus.MustRegister(NewCPUsCollector()) // from cpus.go
30+
prometheus.MustRegister(NewGPUsCollector()) // from gpus.go
3031
prometheus.MustRegister(NewNodesCollector()) // from nodes.go
3132
prometheus.MustRegister(NewNodeCollector()) // from node.go
32-
prometheus.MustRegister(NewGPUsCollector()) // from gpus.go
3333
prometheus.MustRegister(NewPartitionsCollector()) // from partitions.go
3434
prometheus.MustRegister(NewQueueCollector()) // from queue.go
3535
prometheus.MustRegister(NewSchedulerCollector()) // from scheduler.go

node_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ func TestNodeMetrics(t *testing.T) {
4646
t.Fatalf("Can not open test data: %v", err)
4747
}
4848
metrics := ParseNodeMetrics(data)
49-
// t.Logf("%+v", metrics)
5049

5150
assert.Contains(t, metrics, "b001")
5251
assert.Equal(t, uint64(327680), metrics["b001"].memAlloc)

partitions.go

Lines changed: 72 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -23,46 +23,46 @@ import (
2323
)
2424

2525
type 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

3333
func 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

6868
func GetPartitionsMetrics() map[string]*PartitionMetrics {
@@ -73,49 +73,49 @@ func GetPartitionsMetrics() map[string]*PartitionMetrics {
7373
}
7474

7575
type 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

8383
func 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

9494
func (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

102102
func (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

Comments
 (0)