forked from vpenso/prometheus-slurm-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobs.go
134 lines (119 loc) · 3.26 KB
/
jobs.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
/* Copyright 2025 Scalableminds
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 (
"encoding/json"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"io/ioutil"
"os/exec"
)
func JobsData() []byte {
cmd := exec.Command("squeue", "--json")
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 SqueueResult struct {
Jobs []SqueueJob `json:"jobs"`
}
type SqueueJob struct {
Account string `json:"acccount"`
Id int `json:"job_id"`
Name string `json:"name"`
Resources SqueueJobResources `json:"job_resources"`
State []string `json:"job_state"`
Nodes string `json:"nodes"`
Partition string `json:"partition"`
GroupId int `json:"group_id"`
GroupName string `json:"group_name"`
UserId int `json:"user_id"`
UserName string `json:"user_name"`
}
type SqueueJobResources struct {
Cpus int `json:"cpus"`
Nodes struct {
Allocation []SqueueJobResourcesAllocation `json:"allocation"`
} `json:"nodes"`
}
type SqueueJobResourcesAllocation struct {
Memory struct {
Allocated int `json:"allocated"`
} `json:"memory"`
}
func InstrumentJobs() SqueueResult {
jobs := JobsData()
var result SqueueResult
if err := json.Unmarshal(jobs, &result); err != nil {
log.Fatal(err)
}
return result
}
type JobsCollector struct {
jobs *prometheus.Desc
}
func NewJobsCollector() *JobsCollector {
labels := []string{
"account",
"job_id",
"name",
"cpus",
"memory",
"state",
"nodes",
"partition",
"group_id",
"group_name",
"user_id",
"user_name",
}
return &JobsCollector{
jobs: prometheus.NewDesc("slurm_jobs", "Description of running Slurm jobs", labels, nil),
}
}
func (jc *JobsCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- jc.jobs
}
func (jc *JobsCollector) Collect(ch chan<- prometheus.Metric) {
jm := InstrumentJobs()
for _, job := range jm.Jobs {
allocatedMemory := 0
for _, allocation := range job.Resources.Nodes.Allocation {
allocatedMemory += allocation.Memory.Allocated
}
labels := []string{
job.Account,
fmt.Sprintf("%d", job.Id),
job.Name,
fmt.Sprintf("%d", job.Resources.Cpus),
fmt.Sprintf("%d", allocatedMemory),
fmt.Sprintf("%s", job.State),
job.Nodes,
job.Partition,
fmt.Sprintf("%d", job.GroupId),
job.GroupName,
fmt.Sprintf("%d", job.UserId),
job.UserName,
}
ch <- prometheus.MustNewConstMetric(jc.jobs, prometheus.GaugeValue, 1.0, labels...)
}
}