-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
executable file
·88 lines (79 loc) · 1.66 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
package main
import (
"flag"
"log"
"net/http"
"os"
"strconv"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/maesoser/tplink_exporter/macdb"
"github.com/maesoser/tplink_exporter/tplink"
)
func GetEnvStr(name, value string) string {
if os.Getenv(name) != "" {
return os.Getenv(name)
}
return value
}
func main() {
Address := flag.String(
"a",
GetEnvStr("TPLINK_ROUTER_ADDR", "192.168.0.1"),
"Router's address",
)
Pass := flag.String(
"w",
GetEnvStr("TPLINK_ROUTER_PASSWD", "admin"),
"Router's password",
)
User := flag.String(
"u",
GetEnvStr("TPLINK_ROUTER_USER", "admin"),
"Router's username",
)
Port := flag.Int(
"p",
9300,
"Prometheus port",
)
Verbose := flag.Bool(
"v",
false,
"Verbose output",
)
Reboot := flag.Bool(
"reboot",
false,
"Reboots the router",
)
Filename := flag.String(
"f",
GetEnvStr("TPLINK_ROUTER_MACS", "/etc/known_macs"),
"MAC Database",
)
flag.Parse()
macs := macdb.MACDB{}
err := macs.Load(*Filename)
if err != nil {
log.Println("Unable to load MAC database:", err)
} else {
log.Printf("%d MACs loaded", macs.Size())
}
router := tplink.NewRouter(*Address, *User, *Pass)
router.Verbose = *Verbose
if (*Reboot){
if err := router.Login(); err != nil {
log.Fatal("Error logging: %v", err)
}
if err := router.Reboot(); err != nil {
log.Fatal(err)
}
log.Println("Reboot command sent")
os.Exit(1)
}
c := newRouterCollector(router, macs)
prometheus.MustRegister(c)
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(*Port), nil))
}