-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
46 lines (39 loc) · 1.1 KB
/
collector.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
package main
import (
"github.com/prometheus/client_golang/prometheus"
"log"
"os/exec"
"runtime"
"strings"
"strconv"
"time"
"math/rand"
)
var powerConsumption = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "power_Consumption", Help: "Displays the power consumption in Watts of an specific VM"})
func getPower(){
cmd := exec.Command("/bin/sh","-c","sudo ipmi-sensors -h localhost --no-sensor-type-output --no-header-output --comma-separated-output --sensor-types Current")
if runtime.GOOS == "windows" {
cmd = exec.Command("tasklist")
}
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
// Output from the previous ipmi sensor: 52,PSU_Input_Power,204.00,W,'OK'
// We are only interested in the watts, so we only store that part.
watts,err := strconv.ParseFloat(strings.Split(string(out), ",")[2],64)
powerConsumption.Set(watts)
}
// This is the first function to execute
func init() {
prometheus.MustRegister(powerConsumption)
getPower()
}
func self_update(){
rand.Seed(time.Now().Unix())
for {
getPower()
time.Sleep(time.Second)
}
}