|
| 1 | +/* Copyright 2017 Victor Penso, Matteo Dessalvi |
| 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 | + "github.com/prometheus/client_golang/prometheus" |
| 20 | + "io/ioutil" |
| 21 | + "log" |
| 22 | + "os/exec" |
| 23 | + "strconv" |
| 24 | + "strings" |
| 25 | +) |
| 26 | + |
| 27 | +type CPUsMetrics struct { |
| 28 | + alloc float64 |
| 29 | + idle float64 |
| 30 | + other float64 |
| 31 | + total float64 |
| 32 | +} |
| 33 | + |
| 34 | +func CPUsGetMetrics() *CPUsMetrics { |
| 35 | + return ParseCPUsMetrics(CPUsData()) |
| 36 | +} |
| 37 | + |
| 38 | +func ParseCPUsMetrics(input []byte) *CPUsMetrics { |
| 39 | + var cm CPUsMetrics |
| 40 | + if strings.Contains(string(input), "/") { |
| 41 | + splitted := strings.Split(strings.TrimSpace(string(input)), "/") |
| 42 | + cm.alloc, _ = strconv.ParseFloat(splitted[0], 64) |
| 43 | + cm.idle, _ = strconv.ParseFloat(splitted[1], 64) |
| 44 | + cm.other, _ = strconv.ParseFloat(splitted[2], 64) |
| 45 | + cm.total, _ = strconv.ParseFloat(splitted[3], 64) |
| 46 | + } |
| 47 | + return &cm |
| 48 | +} |
| 49 | + |
| 50 | +// Execute the sinfo command and return its output |
| 51 | +func CPUsData() []byte { |
| 52 | + cmd := exec.Command("sinfo", "-h", "-o %C") |
| 53 | + stdout, err := cmd.StdoutPipe() |
| 54 | + if err != nil { |
| 55 | + log.Fatal(err) |
| 56 | + } |
| 57 | + if err := cmd.Start(); err != nil { |
| 58 | + log.Fatal(err) |
| 59 | + } |
| 60 | + out, _ := ioutil.ReadAll(stdout) |
| 61 | + if err := cmd.Wait(); err != nil { |
| 62 | + log.Fatal(err) |
| 63 | + } |
| 64 | + return out |
| 65 | +} |
| 66 | + |
| 67 | +/* |
| 68 | + * Implement the Prometheus Collector interface and feed the |
| 69 | + * Slurm scheduler metrics into it. |
| 70 | + * https://godoc.org/github.com/prometheus/client_golang/prometheus#Collector |
| 71 | + */ |
| 72 | + |
| 73 | +func NewCPUsCollector() *CPUsCollector { |
| 74 | + return &CPUsCollector{ |
| 75 | + alloc: prometheus.NewDesc("slurm_cpus_alloc", "Allocated CPUs", nil, nil), |
| 76 | + idle: prometheus.NewDesc("slurm_cpus_idle", "Idle CPUs", nil, nil), |
| 77 | + other: prometheus.NewDesc("slurm_cpus_other", "Mix CPUs", nil, nil), |
| 78 | + total: prometheus.NewDesc("slurm_cpus_total", "Total CPUs", nil, nil), |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +type CPUsCollector struct { |
| 83 | + alloc *prometheus.Desc |
| 84 | + idle *prometheus.Desc |
| 85 | + other *prometheus.Desc |
| 86 | + total *prometheus.Desc |
| 87 | +} |
| 88 | + |
| 89 | +// Send all metric descriptions |
| 90 | +func (cc *CPUsCollector) Describe(ch chan<- *prometheus.Desc) { |
| 91 | + ch <- cc.alloc |
| 92 | + ch <- cc.idle |
| 93 | + ch <- cc.other |
| 94 | + ch <- cc.total |
| 95 | +} |
| 96 | +func (cc *CPUsCollector) Collect(ch chan<- prometheus.Metric) { |
| 97 | + cm := CPUsGetMetrics() |
| 98 | + ch <- prometheus.MustNewConstMetric(cc.alloc, prometheus.GaugeValue, cm.alloc) |
| 99 | + ch <- prometheus.MustNewConstMetric(cc.idle, prometheus.GaugeValue, cm.idle) |
| 100 | + ch <- prometheus.MustNewConstMetric(cc.other, prometheus.GaugeValue, cm.other) |
| 101 | + ch <- prometheus.MustNewConstMetric(cc.total, prometheus.GaugeValue, cm.total) |
| 102 | +} |
0 commit comments