-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
122 lines (111 loc) · 3.47 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
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"time"
flags "github.com/jessevdk/go-flags"
"github.com/msh100/modem-stats/modems/comhemc2"
"github.com/msh100/modem-stats/modems/superhub3"
"github.com/msh100/modem-stats/modems/superhub4"
"github.com/msh100/modem-stats/modems/superhub5"
"github.com/msh100/modem-stats/modems/tc4400"
"github.com/msh100/modem-stats/modems/ubee"
"github.com/msh100/modem-stats/outputs"
"github.com/msh100/modem-stats/utils"
)
var commandLineOpts struct {
Daemon bool `short:"d" long:"daemon" description:"Gather statistics on new line to STDIN"`
PrometheusPort int `short:"p" long:"port" description:"Prometheus exporter port (disabled if not defined)"`
Modem string `short:"m" long:"modem" description:"Which modem to use" default:"superhub3"`
ModemIP string `long:"ip" description:"The modem's IP address"`
Username string `long:"username" description:"The modem's username (if applicable)"`
Password string `long:"password" description:"The modem's password (if applicable)"`
}
func main() {
_, err := flags.ParseArgs(&commandLineOpts, os.Args)
if err != nil {
log.Fatal("error parsing command line arguments")
os.Exit(1)
}
var body []byte
var fetchTime int64
if localFile := utils.Getenv("LOCAL_FILE", ""); localFile != "" {
timeStart := time.Now().UnixNano() / int64(time.Millisecond)
file, _ := os.Open(localFile)
body, _ = ioutil.ReadAll(file)
fetchTime = (time.Now().UnixNano() / int64(time.Millisecond)) - timeStart
}
var routerType string
superhubType := utils.Getenv("SH_VERSION", "")
if superhubType != "" {
routerType = fmt.Sprintf("superhub%s", superhubType)
} else {
routerType = utils.Getenv("ROUTER_TYPE", commandLineOpts.Modem)
}
var modem utils.DocsisModem
switch routerType {
case "superhub4":
modem = &superhub4.Modem{
IPAddress: utils.Getenv("ROUTER_IP", commandLineOpts.ModemIP),
Stats: body,
FetchTime: fetchTime,
}
case "comhemc2":
modem = &comhemc2.Modem{
IPAddress: utils.Getenv("ROUTER_IP", commandLineOpts.ModemIP),
Stats: body,
FetchTime: fetchTime,
Username: utils.Getenv("ROUTER_USER", commandLineOpts.Username),
Password: utils.Getenv("ROUTER_PASS", commandLineOpts.Password),
}
case "superhub3":
modem = &superhub3.Modem{
IPAddress: utils.Getenv("ROUTER_IP", commandLineOpts.ModemIP),
Stats: body,
FetchTime: fetchTime,
}
case "ubee":
modem = &ubee.Modem{
IPAddress: utils.Getenv("ROUTER_IP", commandLineOpts.ModemIP),
Stats: body,
FetchTime: fetchTime,
}
case "superhub5":
modem = &superhub5.Modem{
IPAddress: utils.Getenv("ROUTER_IP", commandLineOpts.ModemIP),
Stats: body,
FetchTime: fetchTime,
}
case "tc4400":
modem = &tc4400.Modem{
IPAddress: utils.Getenv("ROUTER_IP", commandLineOpts.ModemIP),
Stats: body,
FetchTime: fetchTime,
Username: utils.Getenv("ROUTER_USER", commandLineOpts.Username),
Password: utils.Getenv("ROUTER_PASS", commandLineOpts.Password),
}
default:
log.Fatalf("unknown modem: %s", routerType)
}
if commandLineOpts.PrometheusPort > 0 {
outputs.Prometheus(modem, commandLineOpts.PrometheusPort)
} else {
for {
modemStats, err := utils.FetchStats(modem)
if err != nil {
log.Printf("Error returned by parser: %v", err)
} else {
outputs.PrintForInflux(modemStats)
}
if commandLineOpts.Daemon {
bufio.NewScanner(os.Stdin).Scan()
utils.ResetStats(modem)
} else {
break
}
}
}
}