-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
162 lines (136 loc) · 3.11 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"time"
"github.com/ziutek/rrd"
)
var logger *log.Logger
// Printer holds the host name
type Printer struct {
Host string `json:"host"`
Port uint16 `json:"port,omitempty"`
}
type Configuration struct {
Printer Printer `json:"printer"`
CertFile string `json:"ca-cert"`
LogFile string `json:"logfile"`
RRDFile string `json:"rrdfile"`
}
func main() {
conf := loadConfiguration()
setUpLogging(conf.LogFile)
setUpRRD(conf.RRDFile)
u := getHTML(false, conf)
updateUsage(u, conf.RRDFile)
}
func loadConfiguration() *Configuration {
conf, _ := os.Open("conf.json")
defer conf.Close()
decoder := json.NewDecoder(conf)
configuration := &Configuration{}
err := decoder.Decode(configuration)
if err != nil {
log.Fatal(err)
}
return configuration
}
func setUpLogging(logfile string) {
// open log file
file, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatal(err)
}
logger = log.New(file, "hp-monitor: ", log.Lshortfile|log.LstdFlags)
logger.Println("Starting hp-monitor")
}
func getHTML(test bool, conf *Configuration) *usage {
if test {
/* testing mock */
file, _ := os.Open("usage.html")
defer file.Close()
return newUsage(file)
} else {
/* actual */
p := x509.NewCertPool()
cert, err := ioutil.ReadFile(conf.CertFile)
if err != nil {
log.Fatal(err)
}
p.AppendCertsFromPEM(cert)
tr := &http.Transport{
TLSClientConfig: &tls.Config{RootCAs: p},
}
client := &http.Client{Transport: tr}
res, err := client.Get(conf.Printer.URL())
if err != nil {
logger.Fatal(err)
}
return newUsage(res.Body)
}
}
func (p *Printer) URL() string {
baseURL := fmt.Sprintf("https://%s", p.Host)
URL, _ := url.Parse(baseURL)
if p.Port != 443 {
URL.Path += fmt.Sprintf(":%d", p.Port)
}
URL.Path += "/hp/device/this.LCDispatcher"
/* append query params*/
v := url.Values{}
v.Set("nav", "hp.Usage")
URL.RawQuery = v.Encode()
return URL.String()
}
func setUpRRD(dbfile string) {
start := time.Date(2015, time.July, 1, 0, 0, 0, 0, time.UTC)
c := rrd.NewCreator(dbfile, start, 3600)
/* setup DSes */
c.DS("letter-simplex", "GAUGE",
7200, /* heartbeat */
"U", /* min */
"U", /* max */
)
c.DS("letter-duplex", "GAUGE",
7200, /* heartbeat */
"U", /* min */
"U", /* max */
)
/* setup RRAs*/
c.RRA("AVERAGE",
0.5, /* xff */
1, /* steps */
168, /* rows */
)
if err := c.Create(false); os.IsNotExist(err) {
logger.Fatal(err)
}
}
func updateUsage(u *usage, dbfile string) {
delta := sinceLastUpdate(dbfile)
if delta.Seconds() > 3300 {
logger.Println("Updating RRD with usage...")
up := rrd.NewUpdater(dbfile)
up.SetTemplate("letter-simplex:letter-duplex")
err := up.Update(time.Now(), u.simplex(), u.duplex())
if err != nil {
logger.Fatal(err)
}
} else {
logger.Println("Too soon...")
}
}
func sinceLastUpdate(dbfile string) time.Duration {
ri, _ := rrd.Info(dbfile)
lastUpdate := int64(ri["last_update"].(uint))
t := time.Unix(lastUpdate, 0)
delta := time.Since(t)
return delta
}