forked from vpenso/prometheus-slurm-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodes.go
187 lines (169 loc) · 5.48 KB
/
nodes.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* Copyright 2017 Victor Penso, Matteo Dessalvi
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 (
"github.com/prometheus/client_golang/prometheus"
"io/ioutil"
"log"
"os/exec"
"regexp"
"sort"
"strconv"
"strings"
)
type NodesMetrics struct {
alloc float64
comp float64
down float64
drain float64
err float64
fail float64
idle float64
maint float64
mix float64
resv float64
}
func NodesGetMetrics() *NodesMetrics {
return ParseNodesMetrics(NodesData())
}
func RemoveDuplicates(s []string) []string {
m := map[string]bool{}
t := []string{}
// Walk through the slice 's' and for each value we haven't seen so far, append it to 't'.
for _, v := range s {
if _, seen := m[v]; !seen {
t = append(t, v)
m[v] = true
}
}
return t
}
func ParseNodesMetrics(input []byte) *NodesMetrics {
var nm NodesMetrics
lines := strings.Split(string(input), "\n")
// Sort and remove all the duplicates from the 'sinfo' output
sort.Strings(lines)
lines_uniq := RemoveDuplicates(lines)
for _, line := range lines_uniq {
if strings.Contains(line, ",") {
split := strings.Split(line, ",")
count, _ := strconv.ParseFloat(strings.TrimSpace(split[0]), 64)
state := split[1]
alloc := regexp.MustCompile(`^alloc`)
comp := regexp.MustCompile(`^comp`)
down := regexp.MustCompile(`^down`)
drain := regexp.MustCompile(`^drain`)
fail := regexp.MustCompile(`^fail`)
err := regexp.MustCompile(`^err`)
idle := regexp.MustCompile(`^idle`)
maint := regexp.MustCompile(`^maint`)
mix := regexp.MustCompile(`^mix`)
resv := regexp.MustCompile(`^res`)
switch {
case alloc.MatchString(state) == true:
nm.alloc += count
case comp.MatchString(state) == true:
nm.comp += count
case down.MatchString(state) == true:
nm.down += count
case drain.MatchString(state) == true:
nm.drain += count
case fail.MatchString(state) == true:
nm.fail += count
case err.MatchString(state) == true:
nm.err += count
case idle.MatchString(state) == true:
nm.idle += count
case maint.MatchString(state) == true:
nm.maint += count
case mix.MatchString(state) == true:
nm.mix += count
case resv.MatchString(state) == true:
nm.resv += count
}
}
}
return &nm
}
// Execute the sinfo command and return its output
func NodesData() []byte {
cmd := exec.Command("sinfo", "-h", "-o %D,%T")
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
}
/*
* Implement the Prometheus Collector interface and feed the
* Slurm scheduler metrics into it.
* https://godoc.org/github.com/prometheus/client_golang/prometheus#Collector
*/
func NewNodesCollector() *NodesCollector {
return &NodesCollector{
alloc: prometheus.NewDesc("slurm_nodes_alloc", "Allocated nodes", nil, nil),
comp: prometheus.NewDesc("slurm_nodes_comp", "Completing nodes", nil, nil),
down: prometheus.NewDesc("slurm_nodes_down", "Down nodes", nil, nil),
drain: prometheus.NewDesc("slurm_nodes_drain", "Drain nodes", nil, nil),
err: prometheus.NewDesc("slurm_nodes_err", "Error nodes", nil, nil),
fail: prometheus.NewDesc("slurm_nodes_fail", "Fail nodes", nil, nil),
idle: prometheus.NewDesc("slurm_nodes_idle", "Idle nodes", nil, nil),
maint: prometheus.NewDesc("slurm_nodes_maint", "Maint nodes", nil, nil),
mix: prometheus.NewDesc("slurm_nodes_mix", "Mix nodes", nil, nil),
resv: prometheus.NewDesc("slurm_nodes_resv", "Reserved nodes", nil, nil),
}
}
type NodesCollector struct {
alloc *prometheus.Desc
comp *prometheus.Desc
down *prometheus.Desc
drain *prometheus.Desc
err *prometheus.Desc
fail *prometheus.Desc
idle *prometheus.Desc
maint *prometheus.Desc
mix *prometheus.Desc
resv *prometheus.Desc
}
// Send all metric descriptions
func (nc *NodesCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- nc.alloc
ch <- nc.comp
ch <- nc.down
ch <- nc.drain
ch <- nc.err
ch <- nc.fail
ch <- nc.idle
ch <- nc.maint
ch <- nc.mix
ch <- nc.resv
}
func (nc *NodesCollector) Collect(ch chan<- prometheus.Metric) {
nm := NodesGetMetrics()
ch <- prometheus.MustNewConstMetric(nc.alloc, prometheus.GaugeValue, nm.alloc)
ch <- prometheus.MustNewConstMetric(nc.comp, prometheus.GaugeValue, nm.comp)
ch <- prometheus.MustNewConstMetric(nc.down, prometheus.GaugeValue, nm.down)
ch <- prometheus.MustNewConstMetric(nc.drain, prometheus.GaugeValue, nm.drain)
ch <- prometheus.MustNewConstMetric(nc.err, prometheus.GaugeValue, nm.err)
ch <- prometheus.MustNewConstMetric(nc.fail, prometheus.GaugeValue, nm.fail)
ch <- prometheus.MustNewConstMetric(nc.idle, prometheus.GaugeValue, nm.idle)
ch <- prometheus.MustNewConstMetric(nc.maint, prometheus.GaugeValue, nm.maint)
ch <- prometheus.MustNewConstMetric(nc.mix, prometheus.GaugeValue, nm.mix)
ch <- prometheus.MustNewConstMetric(nc.resv, prometheus.GaugeValue, nm.resv)
}