jobs: Expose real jobs state and add a printer label

This commit is contained in:
lconsuegra 2021-01-07 11:46:17 +01:00
parent 5b3260d47c
commit 3ae05b8690
1 changed files with 36 additions and 30 deletions

View File

@ -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
}