cups-exporter/pkg/jobs.go

83 lines
2.8 KiB
Go
Raw Permalink Normal View History

/*
2022-11-18 09:08:01 +00:00
** 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
*/
2019-12-26 20:52:54 +00:00
package pkg
import (
2022-11-17 13:59:06 +00:00
"strconv"
2022-11-17 13:20:46 +00:00
ipp "git.st8l.com/dolysis/go-ipp"
2019-12-26 20:52:54 +00:00
"github.com/prometheus/client_golang/prometheus"
)
func (e *Exporter) jobsMetrics(ch chan<- prometheus.Metric) error {
2021-01-15 12:49:33 +00:00
printers, err := e.client.GetPrinters([]string{""})
2019-12-26 20:52:54 +00:00
if err != nil {
e.log.Error(err, "failed to fetch printers")
2019-12-26 20:52:54 +00:00
return err
}
for _, attr := range printers {
2019-12-26 20:52:54 +00:00
2021-01-15 12:49:33 +00:00
if len(attr["printer-name"]) == 1 {
2019-12-26 20:52:54 +00:00
2021-01-15 12:49:33 +00:00
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
}
2019-12-26 20:52:54 +00:00
2021-01-15 12:49:33 +00:00
ch <- prometheus.MustNewConstMetric(e.jobsTotal, prometheus.CounterValue, float64(len(jobs)), printer)
2019-12-26 20:52:54 +00:00
2022-11-17 13:59:06 +00:00
states := map[int8]int{}
2019-12-26 20:52:54 +00:00
2021-01-15 12:49:33 +00:00
for _, attr := range jobs {
2019-12-26 20:52:54 +00:00
2021-01-15 12:49:33 +00:00
if len(attr["job-state"]) == 1 {
2022-11-17 13:59:06 +00:00
value := int8(attr["job-state"][0].Value.(int))
2021-01-15 12:49:33 +00:00
if value <= 9 && value >= 3 {
states[value]++
} else {
2022-11-17 14:07:58 +00:00
e.log.Info("Unknown job state : " + strconv.Itoa(int(value)))
2021-01-15 12:49:33 +00:00
}
} else {
2021-01-15 12:49:33 +00:00
e.log.Info("job state attribute missing")
}
}
2021-01-15 12:49:33 +00:00
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")
2021-01-15 12:49:33 +00:00
} else {
e.log.Info("printer name attribute missing")
}
2019-12-26 20:52:54 +00:00
}
return nil
2019-12-26 20:52:54 +00:00
}