test printers response before parsing

This commit is contained in:
lconsuegra 2021-01-15 13:49:33 +01:00
parent 86ea2a3389
commit c8a17fbb7d
1 changed files with 38 additions and 33 deletions

View File

@ -8,7 +8,7 @@ import (
func (e *Exporter) jobsMetrics(ch chan<- prometheus.Metric) error { func (e *Exporter) jobsMetrics(ch chan<- prometheus.Metric) error {
printers, err := e.client.GetPrinters([]string{"printer-state"}) printers, err := e.client.GetPrinters([]string{""})
if err != nil { if err != nil {
e.log.Error(err, "failed to fetch printers") e.log.Error(err, "failed to fetch printers")
@ -17,6 +17,8 @@ func (e *Exporter) jobsMetrics(ch chan<- prometheus.Metric) error {
for _, attr := range printers { for _, attr := range printers {
if len(attr["printer-name"]) == 1 {
printer := attr["printer-name"][0].Value.(string) printer := attr["printer-name"][0].Value.(string)
jobs, err := e.client.GetJobs(printer, "", ipp.JobStateFilterAll, false, 0, 0, []string{"job-state"}) jobs, err := e.client.GetJobs(printer, "", ipp.JobStateFilterAll, false, 0, 0, []string{"job-state"})
@ -27,13 +29,13 @@ func (e *Exporter) jobsMetrics(ch chan<- prometheus.Metric) error {
ch <- prometheus.MustNewConstMetric(e.jobsTotal, prometheus.CounterValue, float64(len(jobs)), printer) ch <- prometheus.MustNewConstMetric(e.jobsTotal, prometheus.CounterValue, float64(len(jobs)), printer)
states := map[int8]int{} states := map[ipp.JobState]int{}
for _, attr := range jobs { for _, attr := range jobs {
if len(attr["job-state"]) == 1 { if len(attr["job-state"]) == 1 {
value := int8(attr["job-state"][0].Value.(int)) value := ipp.JobState(attr["job-state"][0].Value.(int))
if value <= 9 && value >= 3 { if value <= 9 && value >= 3 {
states[value]++ states[value]++
@ -53,6 +55,9 @@ func (e *Exporter) jobsMetrics(ch chan<- prometheus.Metric) error {
ch <- prometheus.MustNewConstMetric(e.jobStateTotal, prometheus.GaugeValue, float64(states[ipp.JobStateAborted]), printer, "aborted") 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") ch <- prometheus.MustNewConstMetric(e.jobStateTotal, prometheus.GaugeValue, float64(states[ipp.JobStateCompleted]), printer, "completed")
} else {
e.log.Info("printer name attribute missing")
}
} }
return nil return nil