make sure we have a job state in the response before parsing

This commit is contained in:
lconsuegra 2021-01-15 09:26:58 +01:00
parent 87d67327c9
commit 9c81fabc19
1 changed files with 14 additions and 14 deletions

View File

@ -9,6 +9,7 @@ import (
func (e *Exporter) jobsMetrics(ch chan<- prometheus.Metric) error { func (e *Exporter) jobsMetrics(ch chan<- prometheus.Metric) error {
printers, err := e.client.GetPrinters([]string{"printer-state"}) printers, err := e.client.GetPrinters([]string{"printer-state"})
if err != nil { if err != nil {
e.log.Error(err, "failed to fetch printers") e.log.Error(err, "failed to fetch printers")
return err return err
@ -18,7 +19,7 @@ func (e *Exporter) jobsMetrics(ch chan<- prometheus.Metric) error {
printer := attr["printer-name"][0].Value.(string) 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 { if err != nil {
e.log.Error(err, "failed to fetch all jobs states") e.log.Error(err, "failed to fetch all jobs states")
return err 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) ch <- prometheus.MustNewConstMetric(e.jobsTotal, prometheus.CounterValue, float64(len(jobs)), printer)
states := make(map[int8]int, 7) states := map[int8]int{}
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
for _, attr := range jobs { for _, attr := range jobs {
if attr["job-state"][0].Value.(int) <= 9 && attr["job-state"][0].Value.(int) >= 3 { if len(attr["job-state"]) == 1 {
states[int8(attr["job-state"][0].Value.(int))]++
} else {
e.log.Info("Unknow job state : " + strconv.Itoa(attr["job-state"][0].Value.(int)))
}
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") ch <- prometheus.MustNewConstMetric(e.jobStateTotal, prometheus.GaugeValue, float64(states[ipp.JobStatePending]), printer, "pending")