From c8a17fbb7d2ecaec34d0acc98cdd79941ae66782 Mon Sep 17 00:00:00 2001 From: lconsuegra Date: Fri, 15 Jan 2021 13:49:33 +0100 Subject: [PATCH] test printers response before parsing --- pkg/jobs.go | 71 ++++++++++++++++++++++++++++------------------------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/pkg/jobs.go b/pkg/jobs.go index 5153861..d3d9a18 100644 --- a/pkg/jobs.go +++ b/pkg/jobs.go @@ -8,7 +8,7 @@ import ( func (e *Exporter) jobsMetrics(ch chan<- prometheus.Metric) error { - printers, err := e.client.GetPrinters([]string{"printer-state"}) + printers, err := e.client.GetPrinters([]string{""}) if err != nil { e.log.Error(err, "failed to fetch printers") @@ -17,42 +17,47 @@ func (e *Exporter) jobsMetrics(ch chan<- prometheus.Metric) error { for _, attr := range printers { - printer := attr["printer-name"][0].Value.(string) + if len(attr["printer-name"]) == 1 { - jobs, err := e.client.GetJobs(printer, "", ipp.JobStateFilterAll, false, 0, 0, []string{"job-state"}) - if err != nil { - e.log.Error(err, "failed to fetch all jobs states") - return err - } + printer := attr["printer-name"][0].Value.(string) - ch <- prometheus.MustNewConstMetric(e.jobsTotal, prometheus.CounterValue, float64(len(jobs)), printer) - - states := map[int8]int{} - - for _, attr := range jobs { - - if len(attr["job-state"]) == 1 { - - value := int8(attr["job-state"][0].Value.(int)) - - if value <= 9 && value >= 3 { - states[value]++ - } else { - e.log.Info("Unknow job state : " + strconv.Itoa(int(value))) - } - } else { - e.log.Info("job state attribute missing") + jobs, err := e.client.GetJobs(printer, "", ipp.JobStateFilterAll, false, 0, 0, []string{"job-state"}) + if err != nil { + e.log.Error(err, "failed to fetch all jobs states") + return err } + + ch <- prometheus.MustNewConstMetric(e.jobsTotal, prometheus.CounterValue, float64(len(jobs)), printer) + + states := map[ipp.JobState]int{} + + for _, attr := range jobs { + + if len(attr["job-state"]) == 1 { + + value := ipp.JobState(attr["job-state"][0].Value.(int)) + + if value <= 9 && value >= 3 { + states[value]++ + } else { + e.log.Info("Unknow job state : " + strconv.Itoa(int(value))) + } + } else { + e.log.Info("job state attribute missing") + } + } + + ch <- prometheus.MustNewConstMetric(e.jobStateTotal, prometheus.GaugeValue, float64(states[ipp.JobStatePending]), printer, "pending") + ch <- prometheus.MustNewConstMetric(e.jobStateTotal, prometheus.GaugeValue, float64(states[ipp.JobStateHeld]), printer, "held") + ch <- prometheus.MustNewConstMetric(e.jobStateTotal, prometheus.GaugeValue, float64(states[ipp.JobStateProcessing]), printer, "processing") + ch <- prometheus.MustNewConstMetric(e.jobStateTotal, prometheus.GaugeValue, float64(states[ipp.JobStateStopped]), printer, "stopped") + ch <- prometheus.MustNewConstMetric(e.jobStateTotal, prometheus.GaugeValue, float64(states[ipp.JobStateCanceled]), printer, "canceled") + ch <- prometheus.MustNewConstMetric(e.jobStateTotal, prometheus.GaugeValue, float64(states[ipp.JobStateAborted]), printer, "aborted") + ch <- prometheus.MustNewConstMetric(e.jobStateTotal, prometheus.GaugeValue, float64(states[ipp.JobStateCompleted]), printer, "completed") + + } else { + e.log.Info("printer name attribute missing") } - - ch <- prometheus.MustNewConstMetric(e.jobStateTotal, prometheus.GaugeValue, float64(states[ipp.JobStatePending]), printer, "pending") - ch <- prometheus.MustNewConstMetric(e.jobStateTotal, prometheus.GaugeValue, float64(states[ipp.JobStateHeld]), printer, "held") - ch <- prometheus.MustNewConstMetric(e.jobStateTotal, prometheus.GaugeValue, float64(states[ipp.JobStateProcessing]), printer, "processing") - ch <- prometheus.MustNewConstMetric(e.jobStateTotal, prometheus.GaugeValue, float64(states[ipp.JobStateStopped]), printer, "stopped") - ch <- prometheus.MustNewConstMetric(e.jobStateTotal, prometheus.GaugeValue, float64(states[ipp.JobStateCanceled]), printer, "canceled") - ch <- prometheus.MustNewConstMetric(e.jobStateTotal, prometheus.GaugeValue, float64(states[ipp.JobStateAborted]), printer, "aborted") - ch <- prometheus.MustNewConstMetric(e.jobStateTotal, prometheus.GaugeValue, float64(states[ipp.JobStateCompleted]), printer, "completed") - } return nil