cups-exporter/pkg/jobs.go

83 lines
2.8 KiB
Go

/*
** Copyright 2022 Dolysis Consulting Limited
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
** For changes see the git log
*/
package pkg
import (
"strconv"
ipp "git.st8l.com/dolysis/go-ipp"
"github.com/prometheus/client_golang/prometheus"
)
func (e *Exporter) jobsMetrics(ch chan<- prometheus.Metric) error {
printers, err := e.client.GetPrinters([]string{""})
if err != nil {
e.log.Error(err, "failed to fetch printers")
return err
}
for _, attr := range printers {
if len(attr["printer-name"]) == 1 {
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 := map[int8]int{}
for _, attr := range jobs {
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("Unknown 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.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")
} else {
e.log.Info("printer name attribute missing")
}
}
return nil
}