All checks were successful
continuous-integration/drone/push Build is passing
174 lines
5.4 KiB
Go
174 lines
5.4 KiB
Go
package exporters
|
|
|
|
import (
|
|
"context"
|
|
"encoding/xml"
|
|
"log"
|
|
"strings"
|
|
|
|
"github.com/dnaeon/go-ucs/api"
|
|
"github.com/dnaeon/go-ucs/mo"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
)
|
|
|
|
type UcsmTemperaturesCollector struct {
|
|
ucsClient *api.Client
|
|
ctx context.Context
|
|
metrics []*prometheus.Desc
|
|
}
|
|
|
|
var metrics []prometheus.Desc
|
|
|
|
//var ucsClient *api.Client
|
|
//var ctx context.Context
|
|
|
|
/*
|
|
type ComputeMbTempStats struct {
|
|
XMLName xml.Name `xml:"computeMbTempStats"`
|
|
Dn string `xml:"dn,attr,omitempty"`
|
|
FmTempSenIo string `xml:"fmTempSenIo,attr,omitempty"`
|
|
FmTempSenRear string `xml:"fmTempSenRear,attr,omitempty"`
|
|
}
|
|
*/
|
|
func NewUcsmTemperatureCollector(client *api.Client, ctx context.Context) *UcsmTemperaturesCollector {
|
|
|
|
/*
|
|
ucsClient = client
|
|
ctx = ctx
|
|
*/
|
|
/*
|
|
return &main.Collector{
|
|
UcsmTemperature: prometheus.NewDesc("ucsm_temperature_sensor",
|
|
"UCSM temperature sensor for motherboard/cpu/psu",
|
|
[]string{"server", "component", "description"}, nil,
|
|
),
|
|
}
|
|
*/
|
|
|
|
return &UcsmTemperaturesCollector{
|
|
ucsClient: client,
|
|
ctx: ctx,
|
|
metrics: []*prometheus.Desc{
|
|
prometheus.NewDesc(
|
|
"ucsm_temperature_sensor",
|
|
"UCSM temperature sensor for motherboard/cpu/psu",
|
|
[]string{"server", "component", "description"}, nil,
|
|
),
|
|
},
|
|
}
|
|
|
|
}
|
|
|
|
// Describe prometheus describe
|
|
func (u *UcsmTemperaturesCollector) Describe(ch chan<- *prometheus.Desc) {
|
|
log.Printf("Running Describe for UcsmTemperaturesCollector\n")
|
|
|
|
// Describe the metrics and send them on the channel
|
|
for _, desc := range u.metrics {
|
|
ch <- desc
|
|
}
|
|
|
|
/*
|
|
metricDesc := prometheus.NewDesc("ucsm_temperature_sensor",
|
|
"UCSM temperature sensor for motherboard/cpu/psu",
|
|
[]string{"server", "component", "description"}, nil)
|
|
|
|
fmt.Printf("metricDesc: %v\n", *metricDesc)
|
|
|
|
metrics = append(metrics, *metricDesc)
|
|
ch <- metricDesc
|
|
*/
|
|
}
|
|
|
|
// Collect prometheus collect
|
|
func (u *UcsmTemperaturesCollector) Collect(ch chan<- prometheus.Metric) {
|
|
// The type into which we unmarshal the result data
|
|
type temps struct {
|
|
XMLName xml.Name
|
|
Temperatures []mo.ComputeMbTempStats `xml:"computeMbTempStats"`
|
|
}
|
|
|
|
tempRequest := api.ConfigResolveClassRequest{
|
|
Cookie: u.ucsClient.Cookie,
|
|
ClassId: "computeMbTempStats",
|
|
InHierarchical: "false",
|
|
}
|
|
|
|
var boardTempList temps
|
|
|
|
log.Println("Retrieving managed objects with class `computeMbTempStats`")
|
|
|
|
if err := u.ucsClient.ConfigResolveClass(u.ctx, tempRequest, &boardTempList); err != nil {
|
|
log.Fatalf("Unable to retrieve `computeMbTempStats` managed object: %s", err)
|
|
}
|
|
|
|
log.Printf("Retrieved temps for %d compute blades\n", len(boardTempList.Temperatures))
|
|
for _, temps := range boardTempList.Temperatures {
|
|
// Substring the Dn for the temperatures to get the Dn for the parent blade itself
|
|
bladeDn := strings.Replace(temps.Dn, "/board/temp-stats", "", -1)
|
|
|
|
log.Printf("Temps for blade %s:\n", bladeDn)
|
|
log.Printf("Front Temperature: %v\n", temps.FmTempSenIo)
|
|
log.Printf("Rear Temperature: %v\n", temps.FmTempSenRear)
|
|
|
|
// TODO - work out a better way of running this twice, once for each temperature sensor
|
|
|
|
// Collect the actual metric values and send them on the channel
|
|
for _, desc := range u.metrics {
|
|
|
|
// populate the labels array
|
|
//labelValues := []string{bladeDn, temps.Dn, "motherboard_rear_temperature"}
|
|
|
|
// generate the metric
|
|
metric1 := prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, temps.FmTempSenIo, bladeDn, temps.Dn, "motherboard_rear_temperature")
|
|
// send the data
|
|
ch <- metric1
|
|
|
|
// generate the metric
|
|
metric2 := prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, temps.FmTempSenRear, bladeDn, temps.Dn, "motherboard_front_temperature")
|
|
// send the data
|
|
ch <- metric2
|
|
}
|
|
|
|
//ch <- prometheus.MustNewConstMetric()
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
logger = logging.getLogger("UcsmTemperatureSensorCollector")
|
|
|
|
class UcsmTemperatureSensorCollector(BaseCollector):
|
|
def get_metrics(self):
|
|
return {
|
|
"sensor": GaugeMetricFamily("ucsm_temperature_sensor",
|
|
"UCSM temperature sensor for motherboard/cpu/psu",
|
|
labels=['server', 'component',
|
|
'description'])
|
|
}
|
|
|
|
def collect_metrics(self, server, handle):
|
|
logger.debug("Collecting Metrics ")
|
|
g = self.get_metrics()['sensor']
|
|
|
|
mb_temp = handle.query_classid(NamingId.COMPUTE_MB_TEMP_STATS)
|
|
for temp in mb_temp:
|
|
value_rear = math.nan if temp.fm_temp_sen_rear == 'not-applicable' else temp.fm_temp_sen_rear
|
|
g.add_metric(labels=[server, temp.dn, "motherboard_rear_temperature"],
|
|
value=value_rear)
|
|
value_io = math.nan if temp.fm_temp_sen_io == 'not-applicable' else temp.fm_temp_sen_io
|
|
g.add_metric(labels=[server, temp.dn, "motherboard_front_temperature"],
|
|
value=value_io)
|
|
|
|
cpu_temp = handle.query_classid(NamingId.PROCESSOR_ENV_STATS)
|
|
for temp in cpu_temp:
|
|
g.add_metric(labels=[server, temp.dn, "cpu_temperature"],
|
|
value=temp.temperature)
|
|
|
|
psu_temp = handle.query_classid(NamingId.EQUIPMENT_PSU_STATS)
|
|
for temp in psu_temp:
|
|
g.add_metric(labels=[server, temp.dn, "psu_temperature"],
|
|
value=temp.ambient_temp)
|
|
yield g
|
|
*/
|