-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
132 lines (116 loc) · 3.5 KB
/
util.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
// Copyright 2020 University at Buffalo. All rights reserved.
//
// This file is part of SlurmExporter.
//
// SlurmExporter 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.
//
// SlurmExporter 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 SlurmExporter. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"regexp"
"strconv"
"strings"
"sync"
kingpin "github.com/alecthomas/kingpin/v2"
"github.com/dustin/go-humanize"
"github.com/prometheus/client_golang/prometheus"
)
var (
ignorePartitions = kingpin.Flag("collector.partition.ignore",
"Regexp of partitions to ignore").Default("^$").String()
// Regexp to parse GPU Gres and GresUsed strings. Example looks like this:
// gpu:tesla_v100-pcie-16gb:2(S:0-1)
gpuGresPattern = regexp.MustCompile(`^gpu\:([^\:]+)\:?(\d+)?`)
collectError = prometheus.NewDesc("slurm_exporter_collect_error",
"Indicates if an error has occurred during collection", []string{"collector"}, nil)
allocPattern = regexp.MustCompile(`(?i)^ALLOC`)
compPattern = regexp.MustCompile(`(?i)^COMP`)
downPattern = regexp.MustCompile(`(?i)^DOWN`)
drainPattern = regexp.MustCompile(`(?i)^DRAIN`)
failPattern = regexp.MustCompile(`(?i)^FAIL`)
errPattern = regexp.MustCompile(`(?i)^ERR`)
idlePattern = regexp.MustCompile(`(?i)^IDLE`)
invalPattern = regexp.MustCompile(`(?i)^INVAL`)
maintPattern = regexp.MustCompile(`(?i)^MAINT`)
mixPattern = regexp.MustCompile(`(?i)^MIX`)
plannedPattern = regexp.MustCompile(`(?i)^PLANNED`)
rebootPattern = regexp.MustCompile(`(?i)^REBOOT`)
resvPattern = regexp.MustCompile(`(?i)^RES`)
unknownPattern = regexp.MustCompile(`(?i)^UNKNOWN`)
)
type Tres struct {
Memory uint64
CPU int
Node int
Billing int
GresGpu int
}
type Token struct {
sync.RWMutex
token string
created int64
}
type rpcStat struct {
count float64
aveTime float64
totalTime float64
}
// gpuCountFromGres parses Slurm GRES (generic resource) line and returns the
// count of GPUs
func gpuCountFromGres(line string) int {
value := 0
tres := strings.Split(line, ",")
for _, g := range tres {
if !strings.HasPrefix(g, "gpu:") {
continue
}
matches := gpuGresPattern.FindStringSubmatch(g)
if len(matches) == 3 {
if matches[2] != "" {
value, _ = strconv.Atoi(matches[2])
} else {
value, _ = strconv.Atoi(matches[1])
}
}
}
return value
}
// parseTres parses Slurm TRES (Trackable RESources) line. Example looks like:
// cpu=32,mem=187000M,billing=32,gres/gpu=2
func parseTres(line string) *Tres {
var tres Tres
items := strings.Split(line, ",")
for _, item := range items {
kv := strings.Split(item, "=")
switch kv[0] {
case "cpu":
tres.CPU, _ = strconv.Atoi(kv[1])
case "node":
tres.Node, _ = strconv.Atoi(kv[1])
case "billing":
tres.Billing, _ = strconv.Atoi(kv[1])
case "gres/gpu":
tres.GresGpu, _ = strconv.Atoi(kv[1])
case "mem":
tres.Memory, _ = humanize.ParseBytes(kv[1])
}
}
return &tres
}
func sliceContains(slice []string, str string) bool {
for _, s := range slice {
if str == s {
return true
}
}
return false
}