Files
ucs-exporter/internal/exporters/UcsmTemperatures.go
Nathan Coad 88c8109391
All checks were successful
continuous-integration/drone/push Build is passing
include vnic stats
2023-09-29 14:09:39 +10:00

177 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"
)
// Define the fields we will send back to the collector
type UcsmTemperatures struct {
Server string
Component string
Description string
Value float64
}
/*
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) *UcsExporters {
return &UcsExporters{
ucsClient: client,
ctx: ctx,
UcsmTemperatures: prometheus.NewDesc("ucsm_temperature_sensor",
"UCSM temperature sensor for motherboard/cpu/psu",
[]string{"server", "component", "description"}, nil,
),
}
}
// Describe prometheus describe
func (u *UcsExporters) Describe(ch chan<- *prometheus.Desc) {
log.Printf("Running Describe for UcsmTemperaturesCollector\n")
ch <- u.UcsmTemperatures
}
*/
// Collect prometheus collect
func (u *UcsExporters) GetUcsmTemperatures(ctx context.Context, ucsmTempsChannel chan []UcsmTemperatures) {
/*
wg.Add(1)
defer wg.Done()
defer close(ucsmTempsChannel)
*/
temperatureResults := make([]UcsmTemperatures, 0)
// The type into which we unmarshal the result data
type temps struct {
XMLName xml.Name
Temperatures []mo.ComputeMbTempStats `xml:"computeMbTempStats"`
}
// Define the request we are making to UCS Manager
tempRequest := api.ConfigResolveClassRequest{
Cookie: u.ucsClient.Cookie,
ClassId: "computeMbTempStats",
InHierarchical: "false",
}
var boardTempList temps
//cswg := new(sync.WaitGroup)
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)
boardTempFront := UcsmTemperatures{
Server: bladeDn,
Component: temps.Dn,
Description: "motherboard_front_temperature",
Value: temps.FmTempSenIo,
}
boardTempRear := UcsmTemperatures{
Server: bladeDn,
Component: temps.Dn,
Description: "motherboard_rear_temperature",
Value: temps.FmTempSenRear,
}
temperatureResults = append(temperatureResults, boardTempFront)
temperatureResults = append(temperatureResults, boardTempRear)
/*
// TODO - work out a better way of running this twice, once for each temperature sensor
metric1 := prometheus.MustNewConstMetric(u.UcsmTemperatures, prometheus.GaugeValue, temps.FmTempSenIo, bladeDn, temps.Dn, "motherboard_rear_temperature")
// send the data
ch <- metric1
// generate the metric
metric2 := prometheus.MustNewConstMetric(u.UcsmTemperatures, prometheus.GaugeValue, temps.FmTempSenRear, bladeDn, temps.Dn, "motherboard_front_temperature")
// send the data
ch <- metric2
*/
}
//cswg.Wait()
log.Printf("GetUcsmTemperatures sending results to channel\n")
ucsmTempsChannel <- temperatureResults
//return
}
/*
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
*/