-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
451 lines (404 loc) · 11.3 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package main
import (
"context"
"errors"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/cenkalti/backoff"
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/reiver/go-telnet"
)
var (
hostPort = flag.String("host_port", ":9489", "Port to serve metrics on.")
modemIP = flag.String("modem_ip", "", "Internal IP of the modem.")
modemPort = flag.Int("modem_port", 23, "Port to telnet to.")
modemPass = flag.String("modem_pass", "", "Admin password for the modem.")
modemName = flag.String("modem_hostname", "tc", "Hostname for modem.")
diagFile = flag.String("diag_file", "", "Dump diag to this file.")
scrapeDeadline = flag.Duration("scrape_deadline", 23*time.Second, "Cancel scrapes that take longer than this.")
// An invalid password is misconfiguration that should prevent the
// reconnection loop from running.
errInvalidPassword = errors.New("invalid password")
// Writes will fail with this error when we're not connected.
errNotConnected = errors.New("not connected")
)
type Conn struct {
Prompt, Pass, Addr string
Server *http.Server
// mu is used to prevent concurrent calls to writeLine.
// Because writeLine needs to read back the data it's written,
// concurrent calls can end up racing between the write and
// its subsequent read. It also prevents concurrent access
// to state.
mu sync.Mutex
w *telnet.Conn
r *Reader
state ConnState
// sigch is the channel we pass to signal.Notify
sigch chan os.Signal
// up is used to gate writes until we're connected
up chan bool
// sdMu protects shutdown which is set by Exit.
sdMu sync.Mutex
shutdown bool
}
func NewConn(server *http.Server, addr, pass, prompt string) *Conn {
c := &Conn{
Addr: addr,
Pass: pass,
Prompt: prompt,
Server: server,
sigch: make(chan os.Signal, 1),
up: make(chan bool),
r: NewReader(),
}
signal.Notify(c.sigch, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGHUP)
c.state = CONNECTING
return c
}
// Method states are really nice implementation-wise until
// you want to compare them, then it all goes to shit.
// I'll just use a state map like a pleb, then.
type ConnState int
const (
// SHUTDOWN is first so that reads from
// a closed chan ConnState cause shutdown.
SHUTDOWN ConnState = iota
CONNECTING
CONNECTED
DISCONNECTING
)
func (c *Conn) State() ConnState {
c.mu.Lock()
defer c.mu.Unlock()
return c.state
}
func (c *Conn) Transition(state ConnState) {
c.mu.Lock()
defer c.mu.Unlock()
c.state = state
}
func (c *Conn) ConnectLoop(ctx context.Context, cancel context.CancelFunc) {
glog.Infof("ConnectLoop starting.")
transitions := map[ConnState]func(context.Context) ConnState{
CONNECTING: c.Connecting,
CONNECTED: c.Connected,
DISCONNECTING: c.Disconnecting,
}
for {
to, ok := transitions[c.State()]
if !ok {
// Handles SHUTDOWN as well as any unknown states.
break
}
c.Transition(to(ctx))
}
glog.Infoln("Shutting down.")
c.Server.Shutdown(ctx)
signal.Stop(c.sigch)
cancel()
glog.Infof("ConnectLoop ending.")
}
func (c *Conn) Connecting(ctx context.Context) ConnState {
glog.Infof("Connecting to %s.", c.Addr)
ctx, cancel := context.WithCancel(ctx)
ch := make(chan ConnState)
defer close(ch)
go func() {
b := backoff.WithContext(&backoff.ExponentialBackOff{
InitialInterval: time.Second,
RandomizationFactor: 0.1,
Multiplier: 2,
MaxInterval: 5 * time.Minute,
MaxElapsedTime: 0,
Clock: backoff.SystemClock,
}, ctx)
b.Reset()
if err := backoff.Retry(c.dial, b); err != nil {
glog.Errorf("Permanent connection error: %v", err)
ch <- SHUTDOWN
return
}
ch <- CONNECTED
}()
for {
select {
case state := <-ch:
return state
case sig := <-c.sigch:
// This should result in the backoff failing with a
// permanent connection error, so loop back and cleanup.
glog.Errorf("Received %s while connecting, canceling.", sig)
cancel()
}
}
}
func (c *Conn) dial() error {
conn, err := telnet.DialTo(c.Addr)
if err != nil {
return err
}
c.w = conn
return nil
}
func (c *Conn) Connected(ctx context.Context) ConnState {
glog.Infof("Connected to %s.", c.Addr)
ctx, cancel := context.WithCancel(ctx)
c.r.Use(c.w, cancel)
// We defer closing the underlying telnet connection here, which will
// implicitly cause cancel to be called in Reader.fill, so we don't
// need to also explicitly defer cancel here.
defer c.w.Close()
ch := make(chan ConnState)
defer close(ch)
go func() {
err := c.Login()
switch err {
case nil:
// Connected and logged in happily!
glog.Infof("Successfully logged into %s.", c.Addr)
c.up <- true
case errInvalidPassword:
// Invalid password is not retryable.
c.sdMu.Lock()
c.shutdown = true
c.sdMu.Unlock()
fallthrough
default:
// Tear it all down and (maybe) try again.
glog.Errorf("Error logging in: %v", err)
ch <- DISCONNECTING
}
close(c.up)
}()
// Calling Exit should cause the reader to get an EOF so we loop here to
// clean up. But we might have got to a bad state where we're still
// connected but at the password prompt, where sending "exit" won't work.
// If we receive > 1 signal while connected, we assume "exit" hasn't
// caused the reader to receive EOF, and forcibly disconnect instead.
for i := 0; i < 2; i++ {
select {
case state := <-ch:
// Something bad happened during login /o\
return state
case <-ctx.Done():
// Reader has called cancel, so it's hit EOF.
return DISCONNECTING
case sig := <-c.sigch:
glog.Errorf("Received %s while connected, exiting.", sig)
// SIGHUP forces a disconnect/reconnect without shutting down.
c.Exit(sig != syscall.SIGHUP)
}
}
glog.Error("Received too many signals, forcibly disconnecting.")
// c.shutdown will have been set by Exit() to the correct value already.
return DISCONNECTING
}
func (c *Conn) AwaitConnection() bool {
// Reads of a closed bool channel return false, conveniently.
return <-c.up
}
func (c *Conn) Disconnecting(ctx context.Context) ConnState {
// NOTE: no signal handling in this function!
glog.Infof("Disconnecting from %s.", c.Addr)
// Drain anything from c.up before resetting the channel, just
// in case the goroutine from Connected is live and sat waiting to send.
// We need it to have closed c.up so that any awaitors get notified.
for <-c.up {
}
// Recreate up channel so new awaitors will wait for a reconnection.
c.up = make(chan bool)
// Clean out connection state.
c.r.Reset()
c.w = nil
c.sdMu.Lock()
defer c.sdMu.Unlock()
if c.shutdown {
return SHUTDOWN
}
return CONNECTING
}
func (c *Conn) writeLine(s string, star ...byte) error {
if c.State() != CONNECTED {
// Drop writes on the floor unless we're connected.
return errNotConnected
}
// writeLine can't be called concurrently because that causes
// races between the write and subsequent read.
c.mu.Lock()
defer c.mu.Unlock()
p := []byte(s + "\r\n")
_, err := c.w.Write(p)
if err != nil {
return err
}
if len(star) > 0 {
for i := range s {
p[i] = star[0]
}
}
glog.V(2).Infof("write: %q", p)
// Read the bytes we wrote echoed back to us.
return c.r.ExpectBytes(p)
}
func (c *Conn) WriteLine(s string) error {
return c.writeLine(s)
}
func (c *Conn) WritePass() error {
return c.writeLine(c.Pass, '*')
}
func (c *Conn) SeekPrompt() error {
return c.r.SeekPast(c.Prompt)
}
func (c *Conn) Expect(s string) error {
return c.r.ExpectBytes([]byte(s))
}
func (c *Conn) Login() error {
if err := c.Expect("\r\nPassword: "); err != nil {
return fmt.Errorf("expect password: %v", err)
}
if err := c.WritePass(); err != nil {
return fmt.Errorf("write password: %v", err)
}
// A second password prompt indicates login failure.
// Expect will rewind the input if it doesn't read these bytes.
// If the prompt is set to a string beginning with a capital P
// this will not work, but it's an easy, lazy check.
// Attempting to read a whole password prompt will cause the
// reader to block forever if the modem's command prompt is
// shorter, which it is by default.
if err := c.Expect("\r\nP"); err == nil {
return errInvalidPassword
}
if err := c.SeekPrompt(); err != nil {
return fmt.Errorf("expect prompt: %v", err)
}
glog.Infoln("logged in")
return nil
}
func (c *Conn) Exit(shutdown bool) {
glog.Infoln("Sending exit command.")
c.sdMu.Lock()
defer c.sdMu.Unlock()
if c.shutdown {
return
}
c.shutdown = shutdown
switch c.State() {
case CONNECTED:
// The state machine code is written such that an EOF from the reader
// triggers everything to shut down nicely.
if err := c.WriteLine("exit"); err != nil {
glog.Errorf("exit: %v", err)
}
case CONNECTING:
// Pretend we received a sigint. This will safely cancel any ongoing
// connection attempts withoout needing to store the CancelFunc.
c.sigch <- os.Interrupt
}
}
func (c *Conn) DumpDiags(diagFile string) error {
w, err := os.OpenFile(diagFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
glog.Exitf("Couldn't open diag file: %v", err)
}
defer w.Close()
if err := c.WriteLine("wan adsl diag"); err != nil {
return err
}
out, err := c.r.ReadPast("504~511\r\r\n")
if err != nil {
return err
}
if _, err = w.Write(out); err != nil {
return err
}
if err = c.WriteLine("sys diag"); err != nil {
return err
}
out, err = c.r.ReadPast(c.Prompt)
if err != nil {
return err
}
if _, err = w.Write(out); err != nil {
return err
}
return nil
}
type logAdapter struct{}
func (_ logAdapter) Println(v ...interface{}) {
glog.Infoln(append([]interface{}{"prometheus:"}, v...))
}
func main() {
flag.Parse()
if *modemIP == "" {
glog.Exit("--modem_ip is a required flag.")
}
pass := *modemPass
if pass == "" {
pass = os.Getenv("MODEM_PASS")
if pass == "" {
glog.Exit("You must provide the admin password via --modem_pass or MODEM_PASS env var.")
}
}
ctx, cancel := context.WithCancel(context.Background())
conn := NewConn(
&http.Server{Addr: *hostPort},
fmt.Sprintf("%s:%d", *modemIP, *modemPort),
pass,
fmt.Sprintf("\r\n%s> ", *modemName))
go conn.ConnectLoop(ctx, cancel)
if *diagFile != "" {
if ok := conn.AwaitConnection(); !ok {
glog.Exit("Something went wrong with initial connection.")
}
err := conn.DumpDiags(*diagFile)
if err != nil {
glog.Exitf("Dumping diagnostics failed: %v", err)
}
conn.Exit(true)
<-ctx.Done()
glog.Exitf("Dumped diagnostics to %q", *diagFile)
}
agg := NewAggregator(conn, *scrapeDeadline,
SysUptime(conn),
CPUStats(conn),
HeapStats(conn),
MBufStats(conn),
ADSLStatus(conn),
ADSLMode(conn),
ADSLErrors(conn),
NoiseMargin(conn, false),
NoiseMargin(conn, true),
SyncRate(conn),
ATMCells(conn),
SARCounters(conn),
EthCounters(conn),
)
prometheus.MustRegister(agg)
opts := promhttp.HandlerOpts{
ErrorLog: logAdapter{},
MaxRequestsInFlight: 1,
}
h := promhttp.InstrumentMetricHandler(prometheus.DefaultRegisterer,
promhttp.HandlerFor(prometheus.DefaultGatherer, opts))
http.Handle("/metrics", h)
http.HandleFunc("/quitquitquit", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Shut down!")
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
conn.Exit(true)
})
go conn.Server.ListenAndServe()
<-ctx.Done()
glog.Exitln("Shut down, bye!")
}