cups_exporter/internal/printers.go
Kumi a96c9d88b7
Refactor printerMetrics function to use int instead of int8 for printer states
- Replace int8 with int in the states map
- Cast the printer state values to int when updating the states map
- Update the metrics values to use int(ipp.PrinterState<state>) with the corresponding state names as labels
2023-09-03 09:19:56 +02:00

31 lines
1.1 KiB
Go

package internal
import (
"github.com/phin1x/go-ipp"
"github.com/prometheus/client_golang/prometheus"
)
func (e *Exporter) printerMetrics(ch chan<- prometheus.Metric) error {
printers, err := e.client.GetPrinters([]string{"printer-state"})
if err != nil {
e.log.Error(err, "failed to fetch completed jobs")
return err
}
states := map[int]int{
int(ipp.PrinterStateIdle): 0,
int(ipp.PrinterStateProcessing): 0,
int(ipp.PrinterStateStopped): 0,
}
for _, attr := range printers {
states[attr["printer-state"][0].Value.(int)]++
}
ch <- prometheus.MustNewConstMetric(e.printersTotal, prometheus.GaugeValue, float64(len(printers)))
ch <- prometheus.MustNewConstMetric(e.printerStateTotal, prometheus.GaugeValue, float64(states[int(ipp.PrinterStateIdle)]), "idle")
ch <- prometheus.MustNewConstMetric(e.printerStateTotal, prometheus.GaugeValue, float64(states[int(ipp.PrinterStateProcessing)]), "processing")
ch <- prometheus.MustNewConstMetric(e.printerStateTotal, prometheus.GaugeValue, float64(states[int(ipp.PrinterStateStopped)]), "stopped")
return nil
}