Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
Merge pull request #84 from jacobmarble/monitor-local
Browse files Browse the repository at this point in the history
Add privet printer quantity to monitor output.
  • Loading branch information
vitalybuka committed Sep 22, 2015
2 parents 5fd4f7c + 8a854a4 commit 0734f2a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
14 changes: 11 additions & 3 deletions connector-monitor/connector-monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"os"
"time"

"github.com/golang/glog"
"github.com/google/cups-connector/lib"
)

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
15 changes: 11 additions & 4 deletions monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ 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"
)

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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -120,14 +123,18 @@ 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
}

stats := fmt.Sprintf(
monitorFormat,
cupsPrinterQuantity, rawPrinterQuantity, gcpPrinterQuantity,
cupsPrinterQuantity, rawPrinterQuantity, gcpPrinterQuantity, privetPrinterQuantity,
cupsConnOpen, cupsConnMax,
jobsDone, jobsError, jobsProcessing)

Expand Down
7 changes: 7 additions & 0 deletions privet/privet.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,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)
}

0 comments on commit 0734f2a

Please sign in to comment.