-
Notifications
You must be signed in to change notification settings - Fork 149
/
queue.go
169 lines (155 loc) · 5.57 KB
/
queue.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
/* 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"
"strings"
)
type QueueMetrics struct {
pending float64
pending_dep float64
running float64
suspended float64
cancelled float64
completing float64
completed float64
configuring float64
failed float64
timeout float64
preempted float64
node_fail float64
}
// Returns the scheduler metrics
func QueueGetMetrics() *QueueMetrics {
return ParseQueueMetrics(QueueData())
}
func ParseQueueMetrics(input []byte) *QueueMetrics {
var qm QueueMetrics
lines := strings.Split(string(input), "\n")
for _, line := range lines {
if strings.Contains(line, ",") {
splitted := strings.Split(line, ",")
state := splitted[1]
switch state {
case "PENDING":
qm.pending++
if len(splitted) > 2 && splitted[2] == "Dependency" {
qm.pending_dep++
}
case "RUNNING":
qm.running++
case "SUSPENDED":
qm.suspended++
case "CANCELLED":
qm.cancelled++
case "COMPLETING":
qm.completing++
case "COMPLETED":
qm.completed++
case "CONFIGURING":
qm.configuring++
case "FAILED":
qm.failed++
case "TIMEOUT":
qm.timeout++
case "PREEMPTED":
qm.preempted++
case "NODE_FAIL":
qm.node_fail++
}
}
}
return &qm
}
// Execute the squeue command and return its output
func QueueData() []byte {
cmd := exec.Command("squeue", "-a", "-r", "-h", "-o %A,%T,%r", "--states=all")
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 queue metrics into it.
* https://godoc.org/github.com/prometheus/client_golang/prometheus#Collector
*/
func NewQueueCollector() *QueueCollector {
return &QueueCollector{
pending: prometheus.NewDesc("slurm_queue_pending", "Pending jobs in queue", nil, nil),
pending_dep: prometheus.NewDesc("slurm_queue_pending_dependency", "Pending jobs because of dependency in queue", nil, nil),
running: prometheus.NewDesc("slurm_queue_running", "Running jobs in the cluster", nil, nil),
suspended: prometheus.NewDesc("slurm_queue_suspended", "Suspended jobs in the cluster", nil, nil),
cancelled: prometheus.NewDesc("slurm_queue_cancelled", "Cancelled jobs in the cluster", nil, nil),
completing: prometheus.NewDesc("slurm_queue_completing", "Completing jobs in the cluster", nil, nil),
completed: prometheus.NewDesc("slurm_queue_completed", "Completed jobs in the cluster", nil, nil),
configuring: prometheus.NewDesc("slurm_queue_configuring", "Configuring jobs in the cluster", nil, nil),
failed: prometheus.NewDesc("slurm_queue_failed", "Number of failed jobs", nil, nil),
timeout: prometheus.NewDesc("slurm_queue_timeout", "Jobs stopped by timeout", nil, nil),
preempted: prometheus.NewDesc("slurm_queue_preempted", "Number of preempted jobs", nil, nil),
node_fail: prometheus.NewDesc("slurm_queue_node_fail", "Number of jobs stopped due to node fail", nil, nil),
}
}
type QueueCollector struct {
pending *prometheus.Desc
pending_dep *prometheus.Desc
running *prometheus.Desc
suspended *prometheus.Desc
cancelled *prometheus.Desc
completing *prometheus.Desc
completed *prometheus.Desc
configuring *prometheus.Desc
failed *prometheus.Desc
timeout *prometheus.Desc
preempted *prometheus.Desc
node_fail *prometheus.Desc
}
func (qc *QueueCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- qc.pending
ch <- qc.pending_dep
ch <- qc.running
ch <- qc.suspended
ch <- qc.cancelled
ch <- qc.completing
ch <- qc.completed
ch <- qc.configuring
ch <- qc.failed
ch <- qc.timeout
ch <- qc.preempted
ch <- qc.node_fail
}
func (qc *QueueCollector) Collect(ch chan<- prometheus.Metric) {
qm := QueueGetMetrics()
ch <- prometheus.MustNewConstMetric(qc.pending, prometheus.GaugeValue, qm.pending)
ch <- prometheus.MustNewConstMetric(qc.pending_dep, prometheus.GaugeValue, qm.pending_dep)
ch <- prometheus.MustNewConstMetric(qc.running, prometheus.GaugeValue, qm.running)
ch <- prometheus.MustNewConstMetric(qc.suspended, prometheus.GaugeValue, qm.suspended)
ch <- prometheus.MustNewConstMetric(qc.cancelled, prometheus.GaugeValue, qm.cancelled)
ch <- prometheus.MustNewConstMetric(qc.completing, prometheus.GaugeValue, qm.completing)
ch <- prometheus.MustNewConstMetric(qc.completed, prometheus.GaugeValue, qm.completed)
ch <- prometheus.MustNewConstMetric(qc.configuring, prometheus.GaugeValue, qm.configuring)
ch <- prometheus.MustNewConstMetric(qc.failed, prometheus.GaugeValue, qm.failed)
ch <- prometheus.MustNewConstMetric(qc.timeout, prometheus.GaugeValue, qm.timeout)
ch <- prometheus.MustNewConstMetric(qc.preempted, prometheus.GaugeValue, qm.preempted)
ch <- prometheus.MustNewConstMetric(qc.node_fail, prometheus.GaugeValue, qm.node_fail)
}