|
| 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