Skip to content

Commit 7548b3a

Browse files
committed
new collector for partition metrics added
1 parent 0941f6c commit 7548b3a

File tree

3 files changed

+99
-7
lines changed

3 files changed

+99
-7
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ PROJECT_NAME = prometheus-slurm-exporter
22
ifndef GOPATH
33
GOPATH=$(shell pwd):/usr/share/gocode
44
endif
5-
GOFILES=accounts.go cpus.go main.go nodes.go queue.go scheduler.go users.go
5+
GOFILES=accounts.go cpus.go main.go nodes.go partitions.go queue.go scheduler.go users.go
66
GOBIN=bin/$(PROJECT_NAME)
77

88
build:

main.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ import (
2525

2626
func init() {
2727
// Metrics have to be registered to be exposed
28-
prometheus.MustRegister(NewSchedulerCollector()) // from scheduler.go
29-
prometheus.MustRegister(NewQueueCollector()) // from queue.go
30-
prometheus.MustRegister(NewNodesCollector()) // from nodes.go
31-
prometheus.MustRegister(NewCPUsCollector()) // from cpus.go
32-
prometheus.MustRegister(NewAccountsCollector()) // from accounts.go
33-
prometheus.MustRegister(NewUsersCollector()) // from users.go
28+
prometheus.MustRegister(NewSchedulerCollector()) // from scheduler.go
29+
prometheus.MustRegister(NewQueueCollector()) // from queue.go
30+
prometheus.MustRegister(NewNodesCollector()) // from nodes.go
31+
prometheus.MustRegister(NewCPUsCollector()) // from cpus.go
32+
prometheus.MustRegister(NewAccountsCollector()) // from accounts.go
33+
prometheus.MustRegister(NewUsersCollector()) // from users.go
34+
prometheus.MustRegister(NewPartitionsCollector()) // from partitions.go
3435
}
3536

3637
var listenAddress = flag.String(

partitions.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* Copyright 2020 Victor Penso
2+
3+
This program is free software: you can redistribute it and/or modify
4+
it under the terms of the GNU General Public License as published by
5+
the Free Software Foundation, either version 3 of the License, or
6+
(at your option) any later version.
7+
8+
This program is distributed in the hope that it will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
GNU General Public License for more details.
12+
13+
You should have received a copy of the GNU General Public License
14+
along with this program. If not, see <http://www.gnu.org/licenses/>. */
15+
16+
package main
17+
18+
import (
19+
"io/ioutil"
20+
"os/exec"
21+
"log"
22+
"strings"
23+
"strconv"
24+
"github.com/prometheus/client_golang/prometheus"
25+
)
26+
27+
func PartitionsData() []byte {
28+
cmd := exec.Command("sinfo", "-h", "-o%R,%C")
29+
stdout, err := cmd.StdoutPipe()
30+
if err != nil {
31+
log.Fatal(err)
32+
}
33+
if err := cmd.Start(); err != nil {
34+
log.Fatal(err)
35+
}
36+
out, _ := ioutil.ReadAll(stdout)
37+
if err := cmd.Wait(); err != nil {
38+
log.Fatal(err)
39+
}
40+
return out
41+
}
42+
43+
type PartitionMetrics struct {
44+
allocated float64
45+
idle float64
46+
other float64
47+
total float64
48+
}
49+
50+
func ParsePartitionsMetrics(input []byte) map[string]*PartitionMetrics {
51+
partitions := make(map[string]*PartitionMetrics)
52+
lines := strings.Split(string(input), "\n")
53+
for _, line := range lines {
54+
if strings.Contains(line,",") {
55+
// name of a partition
56+
partition := strings.Split(line,",")[0]
57+
_,key := partitions[partition]
58+
if !key {
59+
partitions[partition] = &PartitionMetrics{0,0,0,0}
60+
}
61+
states := strings.Split(line,",")[1]
62+
allocated,_ := strconv.ParseFloat(strings.Split(states,"/")[0],64)
63+
partitions[partition].allocated = allocated
64+
}
65+
}
66+
return partitions
67+
}
68+
69+
type PartitionsCollector struct {
70+
allocated *prometheus.Desc
71+
}
72+
73+
func NewPartitionsCollector() *PartitionsCollector {
74+
labels := []string{"partition"}
75+
return &PartitionsCollector{
76+
allocated: prometheus.NewDesc("slurm_partition_cpus_allocated", "Allocated CPUs for partition", labels,nil),
77+
}
78+
}
79+
80+
func (pc *PartitionsCollector) Describe(ch chan<- *prometheus.Desc) {
81+
ch <- pc.allocated
82+
}
83+
84+
func (pc *PartitionsCollector) Collect(ch chan<- prometheus.Metric) {
85+
pm := ParsePartitionsMetrics(PartitionsData())
86+
for p := range pm {
87+
if pm[p].allocated > 0 {
88+
ch <- prometheus.MustNewConstMetric(pc.allocated, prometheus.GaugeValue, pm[p].allocated, p)
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)