-
Notifications
You must be signed in to change notification settings - Fork 149
/
partitions.go
149 lines (134 loc) · 5.37 KB
/
partitions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* Copyright 2020 Victor Penso
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package main
import (
"io/ioutil"
"os/exec"
"log"
"strings"
"strconv"
"github.com/prometheus/client_golang/prometheus"
)
func PartitionsData() []byte {
cmd := exec.Command("sinfo", "-h", "-o%R,%C")
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
out, _ := ioutil.ReadAll(stdout)
if err := cmd.Wait(); err != nil {
log.Fatal(err)
}
return out
}
func PartitionsPendingJobsData() []byte {
cmd := exec.Command("squeue","-a","-r","-h","-o%P","--states=PENDING")
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
out, _ := ioutil.ReadAll(stdout)
if err := cmd.Wait(); err != nil {
log.Fatal(err)
}
return out
}
type PartitionMetrics struct {
allocated float64
idle float64
other float64
pending float64
total float64
}
func ParsePartitionsMetrics() map[string]*PartitionMetrics {
partitions := make(map[string]*PartitionMetrics)
lines := strings.Split(string(PartitionsData()), "\n")
for _, line := range lines {
if strings.Contains(line,",") {
// name of a partition
partition := strings.Split(line,",")[0]
_,key := partitions[partition]
if !key {
partitions[partition] = &PartitionMetrics{0,0,0,0,0}
}
states := strings.Split(line,",")[1]
allocated,_ := strconv.ParseFloat(strings.Split(states,"/")[0],64)
idle,_ := strconv.ParseFloat(strings.Split(states,"/")[1],64)
other,_ := strconv.ParseFloat(strings.Split(states,"/")[2],64)
total,_ := strconv.ParseFloat(strings.Split(states,"/")[3],64)
partitions[partition].allocated = allocated
partitions[partition].idle = idle
partitions[partition].other = other
partitions[partition].total = total
}
}
// get list of pending jobs by partition name
list := strings.Split(string(PartitionsPendingJobsData()),"\n")
for _,partition := range list {
// accumulate the number of pending jobs
_,key := partitions[partition]
if key {
partitions[partition].pending += 1
}
}
return partitions
}
type PartitionsCollector struct {
allocated *prometheus.Desc
idle *prometheus.Desc
other *prometheus.Desc
pending *prometheus.Desc
total *prometheus.Desc
}
func NewPartitionsCollector() *PartitionsCollector {
labels := []string{"partition"}
return &PartitionsCollector{
allocated: prometheus.NewDesc("slurm_partition_cpus_allocated", "Allocated CPUs for partition", labels,nil),
idle: prometheus.NewDesc("slurm_partition_cpus_idle", "Idle CPUs for partition", labels,nil),
other: prometheus.NewDesc("slurm_partition_cpus_other", "Other CPUs for partition", labels,nil),
pending: prometheus.NewDesc("slurm_partition_jobs_pending", "Pending jobs for partition", labels,nil),
total: prometheus.NewDesc("slurm_partition_cpus_total", "Total CPUs for partition", labels,nil),
}
}
func (pc *PartitionsCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- pc.allocated
ch <- pc.idle
ch <- pc.other
ch <- pc.pending
ch <- pc.total
}
func (pc *PartitionsCollector) Collect(ch chan<- prometheus.Metric) {
pm := ParsePartitionsMetrics()
for p := range pm {
if pm[p].allocated > 0 {
ch <- prometheus.MustNewConstMetric(pc.allocated, prometheus.GaugeValue, pm[p].allocated, p)
}
if pm[p].idle > 0 {
ch <- prometheus.MustNewConstMetric(pc.idle, prometheus.GaugeValue, pm[p].idle, p)
}
if pm[p].other > 0 {
ch <- prometheus.MustNewConstMetric(pc.other, prometheus.GaugeValue, pm[p].other, p)
}
if pm[p].pending > 0 {
ch <- prometheus.MustNewConstMetric(pc.pending, prometheus.GaugeValue, pm[p].pending, p)
}
if pm[p].total > 0 {
ch <- prometheus.MustNewConstMetric(pc.total, prometheus.GaugeValue, pm[p].total, p)
}
}
}