cups-exporter/pkg/printers.go

53 lines
1.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 (
ipp "git.st8l.com/dolysis/go-ipp"
"github.com/prometheus/client_golang/prometheus"
)
func (e *Exporter) printerMetrics(ch chan<- prometheus.Metric) error {
printers, err := e.client.GetPrinters([]string{"printer-state"})
if err != nil {
e.log.Error(err, "failed to fetch printers")
return err
}
ch <- prometheus.MustNewConstMetric(e.printersTotal, prometheus.GaugeValue, float64(len(printers)))
for _, attr := range printers {
printer := attr["printer-name"][0].Value.(string)
states := make(map[int8]int)
states[ipp.PrinterStateIdle] = 0
states[ipp.PrinterStateProcessing] = 0
states[ipp.PrinterStateStopped] = 0
states[int8(attr["printer-state"][0].Value.(int))]++
ch <- prometheus.MustNewConstMetric(e.printerStateTotal, prometheus.GaugeValue, float64(states[ipp.PrinterStateIdle]), printer, "idle")
ch <- prometheus.MustNewConstMetric(e.printerStateTotal, prometheus.GaugeValue, float64(states[ipp.PrinterStateProcessing]), printer, "processing")
ch <- prometheus.MustNewConstMetric(e.printerStateTotal, prometheus.GaugeValue, float64(states[ipp.PrinterStateStopped]), printer, "stopped")
}
return nil
}