From 8a854a4e8e3d91b0f31b2a9b356a74fc547b0798 Mon Sep 17 00:00:00 2001 From: Jacob Marble Date: Mon, 21 Sep 2015 16:19:14 -0700 Subject: [PATCH] Add privet printer quantity to monitor output. --- connector-monitor/connector-monitor.go | 14 +++++++++++--- connector/connector.go | 2 +- monitor/monitor.go | 15 +++++++++++---- privet/privet.go | 7 +++++++ 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/connector-monitor/connector-monitor.go b/connector-monitor/connector-monitor.go index 29306e7..4f41add 100644 --- a/connector-monitor/connector-monitor.go +++ b/connector-monitor/connector-monitor.go @@ -15,6 +15,7 @@ import ( "os" "time" + "github.com/golang/glog" "github.com/google/cups-connector/lib" ) @@ -26,9 +27,16 @@ func main() { flag.Parse() fmt.Println(lib.FullName) - config, err := lib.ConfigFromFile() - if err != nil { - panic(err) + var config *lib.Config + if lib.ConfigFileExists() { + var err error + config, err = lib.ConfigFromFile() + if err != nil { + glog.Fatal(err) + } + } else { + config = &lib.DefaultConfig + glog.Info("No config file was found, so using defaults") } if _, err := os.Stat(config.MonitorSocketFilename); err != nil { diff --git a/connector/connector.go b/connector/connector.go index 784181d..fd7ba7e 100644 --- a/connector/connector.go +++ b/connector/connector.go @@ -132,7 +132,7 @@ func main() { } defer pm.Quit() - m, err := monitor.NewMonitor(c, g, pm, config.MonitorSocketFilename) + m, err := monitor.NewMonitor(c, g, priv, pm, config.MonitorSocketFilename) if err != nil { glog.Fatal(err) } diff --git a/monitor/monitor.go b/monitor/monitor.go index c44bac3..ad69060 100644 --- a/monitor/monitor.go +++ b/monitor/monitor.go @@ -15,6 +15,7 @@ import ( "github.com/google/cups-connector/gcp" "github.com/google/cups-connector/lib" "github.com/google/cups-connector/manager" + "github.com/google/cups-connector/privet" "github.com/golang/glog" ) @@ -22,6 +23,7 @@ import ( const monitorFormat = `cups-printers=%d cups-raw-printers=%d gcp-printers=%d +local-printers=%d cups-conn-qty=%d cups-conn-max-qty=%d jobs-done=%d @@ -32,12 +34,13 @@ jobs-in-progress=%d type Monitor struct { cups *cups.CUPS gcp *gcp.GoogleCloudPrint + p *privet.Privet pm *manager.PrinterManager listenerQuit chan bool } -func NewMonitor(cups *cups.CUPS, gcp *gcp.GoogleCloudPrint, pm *manager.PrinterManager, socketFilename string) (*Monitor, error) { - m := Monitor{cups, gcp, pm, make(chan bool)} +func NewMonitor(cups *cups.CUPS, gcp *gcp.GoogleCloudPrint, p *privet.Privet, pm *manager.PrinterManager, socketFilename string) (*Monitor, error) { + m := Monitor{cups, gcp, p, pm, make(chan bool)} listener, err := net.ListenUnix("unix", &net.UnixAddr{socketFilename, "unix"}) if err != nil { @@ -99,7 +102,7 @@ func (m *Monitor) Quit() { } func (m *Monitor) getStats() (string, error) { - var cupsPrinterQuantity, rawPrinterQuantity, gcpPrinterQuantity int + var cupsPrinterQuantity, rawPrinterQuantity, gcpPrinterQuantity, privetPrinterQuantity int if cupsPrinters, err := m.cups.GetPrinters(); err != nil { return "", err @@ -120,6 +123,10 @@ func (m *Monitor) getStats() (string, error) { } } + if m.p != nil { + privetPrinterQuantity = m.p.Size() + } + jobsDone, jobsError, jobsProcessing, err := m.pm.GetJobStats() if err != nil { return "", err @@ -127,7 +134,7 @@ func (m *Monitor) getStats() (string, error) { stats := fmt.Sprintf( monitorFormat, - cupsPrinterQuantity, rawPrinterQuantity, gcpPrinterQuantity, + cupsPrinterQuantity, rawPrinterQuantity, gcpPrinterQuantity, privetPrinterQuantity, cupsConnOpen, cupsConnMax, jobsDone, jobsError, jobsProcessing) diff --git a/privet/privet.go b/privet/privet.go index 0b409d8..8f77885 100644 --- a/privet/privet.go +++ b/privet/privet.go @@ -124,3 +124,10 @@ func (p *Privet) Quit() { delete(p.apis, cupsPrinterName) } } + +func (p *Privet) Size() int { + p.apisMutex.RLock() + defer p.apisMutex.RUnlock() + + return len(p.apis) +}