From 51c7d41aaa8166c76ec012a6f0e5cb1139c60406 Mon Sep 17 00:00:00 2001 From: lconsuegra Date: Thu, 7 Jan 2021 11:47:44 +0100 Subject: [PATCH] printers: Add label to differentiate each printers metrics --- pkg/printers.go | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/pkg/printers.go b/pkg/printers.go index f3de9d9..68d1361 100644 --- a/pkg/printers.go +++ b/pkg/printers.go @@ -8,23 +8,28 @@ import ( 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") + e.log.Error(err, "failed to fetch printers") return err } - states := make(map[ipp.PrinterState]int) - states[ipp.PrinterStateIdle] = 0 - states[ipp.PrinterStateProcessing] = 0 - states[ipp.PrinterStateStopped] = 0 + ch <- prometheus.MustNewConstMetric(e.printersTotal, prometheus.GaugeValue, float64(len(printers))) for _, attr := range printers { - states[ipp.PrinterState(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[ipp.PrinterStateIdle]), "idle") - ch <- prometheus.MustNewConstMetric(e.printerStateTotal, prometheus.GaugeValue, float64(states[ipp.PrinterStateProcessing]), "processing") - ch <- prometheus.MustNewConstMetric(e.printerStateTotal, prometheus.GaugeValue, float64(states[ipp.PrinterStateStopped]), "stopped") + printer := attr["printer-name"][0].Value.(string) + + states := make(map[int8]int) + states[ipp.PrinterStateIdle] = 0 + states[ipp.PrinterStateProcessing] = 0 + states[ipp.PrinterStateStopped] = 0 + + states[int8(attr["printer-state"][0].Value.(int))]++ + + ch <- prometheus.MustNewConstMetric(e.printerStateTotal, prometheus.GaugeValue, float64(states[ipp.PrinterStateIdle]), printer, "idle") + ch <- prometheus.MustNewConstMetric(e.printerStateTotal, prometheus.GaugeValue, float64(states[ipp.PrinterStateProcessing]), printer, "processing") + ch <- prometheus.MustNewConstMetric(e.printerStateTotal, prometheus.GaugeValue, float64(states[ipp.PrinterStateStopped]), printer, "stopped") + + } return nil }