diff --git a/pkg/jobs.go b/pkg/jobs.go index 51ec0f7..5153861 100644 --- a/pkg/jobs.go +++ b/pkg/jobs.go @@ -9,6 +9,7 @@ import ( func (e *Exporter) jobsMetrics(ch chan<- prometheus.Metric) error { printers, err := e.client.GetPrinters([]string{"printer-state"}) + if err != nil { e.log.Error(err, "failed to fetch printers") return err @@ -18,7 +19,7 @@ func (e *Exporter) jobsMetrics(ch chan<- prometheus.Metric) error { printer := attr["printer-name"][0].Value.(string) - jobs, err := e.client.GetJobs(printer, "", "all", false, 0, 0, []string{"job-state"}) + 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 @@ -26,23 +27,22 @@ func (e *Exporter) jobsMetrics(ch chan<- prometheus.Metric) error { ch <- prometheus.MustNewConstMetric(e.jobsTotal, prometheus.CounterValue, float64(len(jobs)), printer) - states := make(map[int8]int, 7) - states[ipp.JobStatePending] = 0 - states[ipp.JobStateHeld] = 0 - states[ipp.JobStateProcessing] = 0 - states[ipp.JobStateStopped] = 0 - states[ipp.JobStateCanceled] = 0 - states[ipp.JobStateAborted] = 0 - states[ipp.JobStateCompleted] = 0 + states := map[int8]int{} for _, attr := range jobs { - if attr["job-state"][0].Value.(int) <= 9 && attr["job-state"][0].Value.(int) >= 3 { - states[int8(attr["job-state"][0].Value.(int))]++ - } else { - e.log.Info("Unknow job state : " + strconv.Itoa(attr["job-state"][0].Value.(int))) - } + 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") + } } ch <- prometheus.MustNewConstMetric(e.jobStateTotal, prometheus.GaugeValue, float64(states[ipp.JobStatePending]), printer, "pending")