cups_exporter/internal/jobs.go

48 lines
1.1 KiB
Go
Raw Normal View History

2023-04-30 21:22:42 +00:00
package internal
2019-12-26 20:52:54 +00:00
import (
"github.com/phin1x/go-ipp"
"github.com/prometheus/client_golang/prometheus"
)
func (e *Exporter) jobsMetrics(ch chan<- prometheus.Metric) error {
jobs, err := e.client.GetJobs("", "", ipp.JobStateFilterNotCompleted, false, 0, 0, []string{})
if err != nil {
e.log.Error(err, "failed to fetch completed jobs")
return err
}
activeJobs := len(jobs)
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
}
/*
2023-04-30 21:22:42 +00:00
returns the last job id, the last completed job id and the current active jobs
2019-12-26 20:52:54 +00:00
*/
func getLastJobId(m map[int]ipp.Attributes) int {
lastJobId := 0
for k := range m {
if k > lastJobId {
lastJobId = k
}
}
return lastJobId
}