Skip to content

Commit

Permalink
Refactored node.go to use subprocess.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Rovanion committed Aug 4, 2021
1 parent 9a3570b commit 9afe3af
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 18 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go 1.12
require (
github.com/prometheus/client_golang v1.2.1
github.com/prometheus/common v0.7.0
github.com/stretchr/testify v1.3.0 // indirect
)
19 changes: 3 additions & 16 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */
package main

import (
"log"
"os/exec"
"sort"
"strconv"
"strings"
Expand All @@ -36,8 +34,8 @@ type NodeMetrics struct {
nodeStatus string
}

func NodeGetMetrics() map[string]*NodeMetrics {
return ParseNodeMetrics(NodeData())
func GetNodeMetrics() map[string]*NodeMetrics {
return ParseNodeMetrics(Subprocess("sinfo", "-h", "-N", "-O", "NodeList,AllocMem,Memory,CPUsState,StateLong"))
}

// ParseNodeMetrics takes the output of sinfo with node data
Expand Down Expand Up @@ -79,17 +77,6 @@ func ParseNodeMetrics(input []byte) map[string]*NodeMetrics {
return nodes
}

// NodeData executes the sinfo command to get data for each node
// It returns the output of the sinfo command
func NodeData() []byte {
cmd := exec.Command("sinfo", "-h", "-N", "-O", "NodeList,AllocMem,Memory,CPUsState,StateLong")
out, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
return out
}

type NodeCollector struct {
cpuAlloc *prometheus.Desc
cpuIdle *prometheus.Desc
Expand Down Expand Up @@ -125,7 +112,7 @@ func (nc *NodeCollector) Describe(ch chan<- *prometheus.Desc) {
}

func (nc *NodeCollector) Collect(ch chan<- prometheus.Metric) {
nodes := NodeGetMetrics()
nodes := GetNodeMetrics()
for node := range nodes {
ch <- prometheus.MustNewConstMetric(nc.cpuAlloc, prometheus.GaugeValue, float64(nodes[node].cpuAlloc), node, nodes[node].nodeStatus)
ch <- prometheus.MustNewConstMetric(nc.cpuIdle, prometheus.GaugeValue, float64(nodes[node].cpuIdle), node, nodes[node].nodeStatus)
Expand Down
5 changes: 3 additions & 2 deletions node_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* Copyright 2021 Chris Read
// +build unit
/* Copyright 2021 Chris Read, Rovanion Luckey
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
Expand Down Expand Up @@ -45,7 +46,7 @@ func TestNodeMetrics(t *testing.T) {
t.Fatalf("Can not open test data: %v", err)
}
metrics := ParseNodeMetrics(data)
t.Logf("%+v", metrics)
// t.Logf("%+v", metrics)

assert.Contains(t, metrics, "b001")
assert.Equal(t, uint64(327680), metrics["b001"].memAlloc)
Expand Down
1 change: 1 addition & 0 deletions nodes.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* Copyright 2017 Victor Penso, Matteo Dessalvi
Copyright 2021 Rovanion Luckey
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
Expand Down

0 comments on commit 9afe3af

Please sign in to comment.