Skip to content

Commit

Permalink
test printers response before parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
lconsuegra committed Jan 15, 2021
1 parent 86ea2a3 commit c8a17fb
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions pkg/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

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 {
e.log.Error(err, "failed to fetch printers")
Expand All @@ -17,42 +17,47 @@ func (e *Exporter) jobsMetrics(ch chan<- prometheus.Metric) error {

for _, attr := range printers {

printer := attr["printer-name"][0].Value.(string)
if len(attr["printer-name"]) == 1 {

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
}
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)
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 {
states[value]++
if value <= 9 && value >= 3 {
states[value]++
} else {
e.log.Info("Unknow job state : " + strconv.Itoa(int(value)))
}
} else {
e.log.Info("Unknow job state : " + strconv.Itoa(int(value)))
e.log.Info("job state attribute missing")
}
} 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")
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
Expand Down

0 comments on commit c8a17fb

Please sign in to comment.