From 3ae05b86900052f11db01e3382c6a5c845b754bc Mon Sep 17 00:00:00 2001 From: lconsuegra Date: Thu, 7 Jan 2021 11:46:17 +0100 Subject: [PATCH] jobs: Expose real jobs state and add a printer label --- pkg/jobs.go | 66 +++++++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/pkg/jobs.go b/pkg/jobs.go index 9d82a63..cb95c3b 100644 --- a/pkg/jobs.go +++ b/pkg/jobs.go @@ -6,42 +6,48 @@ import ( ) func (e *Exporter) jobsMetrics(ch chan<- prometheus.Metric) error { - jobs, err := e.client.GetJobs("", "", ipp.JobStateFilterNotCompleted, false, 0, 0, []string{}) + + 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 } - activeJobs := len(jobs) + for _, attr := range printers { + + printer := attr["printer-name"][0].Value.(string) + + 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 := make(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 { + + states[int8(attr["job-state"][0].Value.(int))]++ + } + + 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") - jobs, err = e.client.GetJobs("", "", ipp.JobStateFilterAll, false, e.lastJobId, 0, []string{}) - if err != nil { - e.log.Error(err, "failed to fetch all jobs") - return err } - lastJobId := getLastJobId(jobs) - if lastJobId > e.lastJobId { - e.lastJobId = lastJobId - } - - ch <- prometheus.MustNewConstMetric(e.jobsTotal, prometheus.CounterValue, float64(lastJobId)) - ch <- prometheus.MustNewConstMetric(e.jobsActiveTotal, prometheus.GaugeValue, float64(activeJobs)) - return nil } - -/* - returns the last job id, the last completed job id and the current active jobs -*/ -func getLastJobId(m map[int]ipp.Attributes) int { - lastJobId := 0 - - for k := range m { - if k > lastJobId { - lastJobId = k - } - } - - return lastJobId -}