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

Commit 0734f2a

Browse files
committed
Merge pull request #84 from jacobmarble/monitor-local
Add privet printer quantity to monitor output.
2 parents 5fd4f7c + 8a854a4 commit 0734f2a

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

connector-monitor/connector-monitor.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"os"
1616
"time"
1717

18+
"github.com/golang/glog"
1819
"github.com/google/cups-connector/lib"
1920
)
2021

@@ -26,9 +27,16 @@ func main() {
2627
flag.Parse()
2728
fmt.Println(lib.FullName)
2829

29-
config, err := lib.ConfigFromFile()
30-
if err != nil {
31-
panic(err)
30+
var config *lib.Config
31+
if lib.ConfigFileExists() {
32+
var err error
33+
config, err = lib.ConfigFromFile()
34+
if err != nil {
35+
glog.Fatal(err)
36+
}
37+
} else {
38+
config = &lib.DefaultConfig
39+
glog.Info("No config file was found, so using defaults")
3240
}
3341

3442
if _, err := os.Stat(config.MonitorSocketFilename); err != nil {

connector/connector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func main() {
132132
}
133133
defer pm.Quit()
134134

135-
m, err := monitor.NewMonitor(c, g, pm, config.MonitorSocketFilename)
135+
m, err := monitor.NewMonitor(c, g, priv, pm, config.MonitorSocketFilename)
136136
if err != nil {
137137
glog.Fatal(err)
138138
}

monitor/monitor.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ import (
1515
"github.com/google/cups-connector/gcp"
1616
"github.com/google/cups-connector/lib"
1717
"github.com/google/cups-connector/manager"
18+
"github.com/google/cups-connector/privet"
1819

1920
"github.com/golang/glog"
2021
)
2122

2223
const monitorFormat = `cups-printers=%d
2324
cups-raw-printers=%d
2425
gcp-printers=%d
26+
local-printers=%d
2527
cups-conn-qty=%d
2628
cups-conn-max-qty=%d
2729
jobs-done=%d
@@ -32,12 +34,13 @@ jobs-in-progress=%d
3234
type Monitor struct {
3335
cups *cups.CUPS
3436
gcp *gcp.GoogleCloudPrint
37+
p *privet.Privet
3538
pm *manager.PrinterManager
3639
listenerQuit chan bool
3740
}
3841

39-
func NewMonitor(cups *cups.CUPS, gcp *gcp.GoogleCloudPrint, pm *manager.PrinterManager, socketFilename string) (*Monitor, error) {
40-
m := Monitor{cups, gcp, pm, make(chan bool)}
42+
func NewMonitor(cups *cups.CUPS, gcp *gcp.GoogleCloudPrint, p *privet.Privet, pm *manager.PrinterManager, socketFilename string) (*Monitor, error) {
43+
m := Monitor{cups, gcp, p, pm, make(chan bool)}
4144

4245
listener, err := net.ListenUnix("unix", &net.UnixAddr{socketFilename, "unix"})
4346
if err != nil {
@@ -99,7 +102,7 @@ func (m *Monitor) Quit() {
99102
}
100103

101104
func (m *Monitor) getStats() (string, error) {
102-
var cupsPrinterQuantity, rawPrinterQuantity, gcpPrinterQuantity int
105+
var cupsPrinterQuantity, rawPrinterQuantity, gcpPrinterQuantity, privetPrinterQuantity int
103106

104107
if cupsPrinters, err := m.cups.GetPrinters(); err != nil {
105108
return "", err
@@ -120,14 +123,18 @@ func (m *Monitor) getStats() (string, error) {
120123
}
121124
}
122125

126+
if m.p != nil {
127+
privetPrinterQuantity = m.p.Size()
128+
}
129+
123130
jobsDone, jobsError, jobsProcessing, err := m.pm.GetJobStats()
124131
if err != nil {
125132
return "", err
126133
}
127134

128135
stats := fmt.Sprintf(
129136
monitorFormat,
130-
cupsPrinterQuantity, rawPrinterQuantity, gcpPrinterQuantity,
137+
cupsPrinterQuantity, rawPrinterQuantity, gcpPrinterQuantity, privetPrinterQuantity,
131138
cupsConnOpen, cupsConnMax,
132139
jobsDone, jobsError, jobsProcessing)
133140

privet/privet.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,10 @@ func (p *Privet) Quit() {
121121
delete(p.apis, cupsPrinterName)
122122
}
123123
}
124+
125+
func (p *Privet) Size() int {
126+
p.apisMutex.RLock()
127+
defer p.apisMutex.RUnlock()
128+
129+
return len(p.apis)
130+
}

0 commit comments

Comments
 (0)