This repository has been archived by the owner on Jun 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharris_mon.go
239 lines (202 loc) · 6.07 KB
/
arris_mon.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Arris Monitor Server 2.0
// 2018
package main
import (
"flag"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"time"
prom "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
htmlquery "github.com/antchfx/xquery/html"
"golang.org/x/net/html"
statik "github.com/rakyll/statik/fs"
_ "github.com/realitycheck/arris_mon/statik" // register res files as embedded static.
)
var (
addr = "127.0.0.1:8081"
sourceURL = "http://192.168.100.1/cgi-bin/status_cgi"
downstreamTable = "//table[2]/tbody"
upstreamTable = "//table[4]/tbody"
delay = 1 * time.Second
verbosity = false
resDir = ""
downstreamFreq = newGauge("arris_downstream_freq", "Downstream Frequency")
downstreamPower = newGauge("arris_downstream_power", "Downstream Power")
downstreamSnr = newGauge("arris_downstream_snr", "Downstream SNR")
downstreamOctets = newGauge("arris_downstream_octets", "Downstream Octets")
downstreamCorrecteds = newGauge("arris_downstream_correcteds", "Downstream Correcteds")
downstreamUncorrectables = newGauge("arris_downstream_uncorrectables", "Downstream Uncorrectables")
upstreamFreq = newGauge("arris_upstream_freq", "Upstream Frequency")
upstreamPower = newGauge("arris_upstream_power", "Upstream Power")
)
// fallbackFS defines a list of http.FileSystem objects
// as a single http.FileSystem object.
type fallbackFS []http.FileSystem
func (ffs fallbackFS) Open(name string) (f http.File, err error) {
for _, fs := range ffs {
f, err = fs.Open(name)
if err == nil && f != nil {
return
}
}
if f == nil && err == nil {
err = fmt.Errorf("fallbackFS: %s not found", name)
}
return
}
func logVerbose(format string, v ...interface{}) {
if verbosity {
log.Printf(format, v)
}
}
func newGauge(name, help string) *prom.GaugeVec {
return prom.NewGaugeVec(prom.GaugeOpts{
Name: name,
Help: help,
}, []string{
"id", "name",
})
}
func init() {
flag.StringVar(&addr, "addr", addr, "Server address")
flag.StringVar(&sourceURL, "src", sourceURL, "Source URL")
flag.BoolVar(&verbosity, "v", verbosity, "Verbose print")
flag.StringVar(&downstreamTable, "downstream", downstreamTable, "Source downstream HTML element")
flag.StringVar(&upstreamTable, "upstream", upstreamTable, "Source upstream HTML element")
flag.DurationVar(&delay, "d", delay, "Delay of source reading")
flag.StringVar(&resDir, "res", resDir, "Path to local res dir for static files (optional)")
prom.MustRegister(downstreamFreq)
prom.MustRegister(downstreamPower)
prom.MustRegister(downstreamSnr)
prom.MustRegister(downstreamOctets)
prom.MustRegister(downstreamCorrecteds)
prom.MustRegister(downstreamUncorrectables)
prom.MustRegister(upstreamFreq)
prom.MustRegister(upstreamPower)
}
func main() {
flag.Parse()
// Background worker
go func() {
for i := 1; ; i++ {
select {
case <-time.After(delay):
log.Printf("%v: #%d\n", time.Now(), i)
if err := pull(sourceURL, downstreamTable, upstreamTable); err != nil {
log.Printf("%v: %v\n", time.Now(), err)
}
}
}
}()
// Static files
staticFS, err := statik.New()
if err != nil {
log.Panicf("arris_mon: %s", err)
}
fs := fallbackFS{}
if resDir != "" {
fs = append(fs, http.Dir(resDir))
}
fs = append(fs, staticFS)
// App template
file, err := fs.Open("/templates/arris_mon.html")
if err != nil {
log.Panicf("arris_mon: %s", err)
}
bytes, err := ioutil.ReadAll(file)
if err != nil {
log.Panicf("arris_mon: %s", err)
}
tmpl, err := template.New("index.html").Parse(string(bytes))
if err != nil {
log.Panicf("arris_mon: %s", err)
}
// App handler
app := func(w http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/" {
http.Redirect(w, req, "/", http.StatusTemporaryRedirect)
return
}
if req.Method != http.MethodGet {
w.WriteHeader(http.StatusBadRequest)
return
}
tmpl.Execute(w, nil)
}
http.Handle("/", http.HandlerFunc(app))
http.Handle("/metrics", promhttp.Handler())
http.Handle("/static/", http.FileServer(fs))
log.Fatal(http.ListenAndServe(addr, nil))
}
func pull(url, downstreamTable, upstreamTable string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
doc, err := html.Parse(resp.Body)
if err != nil {
return err
}
downstream := parseTable(doc, downstreamTable)
logVerbose("Downstream: %v\n", downstream)
upstream := parseTable(doc, upstreamTable)
logVerbose("Upstream: %v\n", upstream)
setGauge := func(g *prom.GaugeVec, id string, name string, value string, format string) {
var val float64
fmt.Sscanf(value, format, &val)
g.WithLabelValues(id, name).Set(val)
}
it := downstream.iterator()
for dc := it(); dc != nil; dc = it() {
id, name := dc["DCID"], dc[""]
setGauge(downstreamFreq, id, name, dc["Freq"], "%f MHz")
setGauge(downstreamPower, id, name, dc["Power"], "%f dBmV")
setGauge(downstreamSnr, id, name, dc["SNR"], "%f dB")
setGauge(downstreamOctets, id, name, dc["Octets"], "%f")
setGauge(downstreamCorrecteds, id, name, dc["Correcteds"], "%f")
setGauge(downstreamUncorrectables, id, name, dc["Uncorrectables"], "%f")
}
it = upstream.iterator()
for uc := it(); uc != nil; uc = it() {
id, name := uc["UCID"], uc[""]
setGauge(upstreamFreq, id, name, uc["Freq"], "%f MHz")
setGauge(upstreamPower, id, name, uc["Power"], "%f dBmV")
}
return nil
}
type table [][]string
type record map[string]string
func parseTable(doc *html.Node, expr string) table {
tr := htmlquery.Find(htmlquery.FindOne(doc, expr), "/tr[td]")
t := make(table, len(tr))
for i, r := range tr {
td := htmlquery.Find(r, "/td")
t[i] = make([]string, len(td))
for j, d := range td {
t[i][j] = htmlquery.InnerText(d)
}
}
return t
}
func (t table) iterator() func() record {
i, n := 0, len(t)
return func() record {
if i++; i >= n {
return nil
}
keys, vals := t[0], t[i]
if len(keys) != len(vals) {
log.Panic("arris_mon: lengthes of keys and values are mismatched")
}
res := make(record, len(keys))
for j, val := range vals {
res[keys[j]] = val
}
return res
}
}