forked from slashdoom/aruba_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
162 lines (134 loc) · 4.13 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 (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
// "os/signal"
"net/http"
// "strings"
// "time"
"github.com/slashdoom/aruba_exporter/config"
"github.com/slashdoom/aruba_exporter/connector"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
const version string = "0.0.1"
var (
showVersion = flag.Bool("version", false, "Print version information.")
listenAddress = flag.String("web.listen-address", ":9909", "Address on which to expose metrics and web interface.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
sshHosts = flag.String("ssh.targets", "", "Hosts to scrape")
sshUsername = flag.String("ssh.user", "aruba_exporter", "Username to use when connecting to devices using ssh")
sshKeyFile = flag.String("ssh.keyfile", "", "Public key file to use when connecting to devices using ssh")
sshPassword = flag.String("ssh.password", "", "Password to use when connecting to devices using ssh")
sshTimeout = flag.Int("ssh.timeout", 5, "Timeout to use for SSH connection")
sshBatchSize = flag.Int("ssh.batch-size", 10000, "The SSH response batch size")
level = flag.String("level", "info", "Set logging verbose level")
configFile = flag.String("config.file", "", "Path to config file")
devices []*connector.Device
cfg *config.Config
)
func init() {
log.SetOutput(os.Stdout)
flag.Usage = func() {
fmt.Println("Usage: aruba_exporter [ ... ]\n\nParameters:")
fmt.Println()
flag.PrintDefaults()
}
}
func main() {
flag.Parse()
if *showVersion {
printVersion()
os.Exit(0)
}
err := initialize()
if err != nil {
log.Fatalf("could not initialize exporter. %v", err)
}
startServer()
}
func initialize() error {
c, err := loadConfig()
if err != nil {
return err
}
l, err := log.ParseLevel(c.Level)
if err == nil {
log.SetLevel(l)
}
devices, err = devicesForConfig(c)
if err != nil {
return err
}
cfg = c
return nil
}
func printVersion() {
fmt.Println("aruba_exporter")
fmt.Printf("Version: %s\n", version)
fmt.Println("Author(s): slashdoom (Patrick Ryon)")
fmt.Println("Metric exporter for Aruba switches, controllers and instant APs")
}
func loadConfig() (*config.Config, error) {
l, err := log.ParseLevel(*level)
if err == nil {
log.SetLevel(l)
}
if len(*configFile) == 0 {
log.Infoln("Loading config flags")
return loadConfigFromFlags(), nil
}
log.Infoln("Loading config from", *configFile)
b, err := ioutil.ReadFile(*configFile)
if err != nil {
return nil, err
}
return config.Load(bytes.NewReader(b))
}
func loadConfigFromFlags() *config.Config {
c := config.New()
c.Level = *level
c.Timeout = *sshTimeout
c.BatchSize = *sshBatchSize
c.Username = *sshUsername
c.Password = *sshPassword
c.KeyFile = *sshKeyFile
c.DevicesFromTargets(*sshHosts)
log.Debugln(c)
f := c.Features
log.Debugln(f)
return c
}
func startServer() {
log.Infof("starting aruba_exporter (version: %s)\n", version)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<html>
<head>
<title>Aruba Exporter (Version ` + version + `)</title>
</head>
<body>
<h1>Aruba Exporter</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
<h2>More information:</h2>
<p><a href="https://github.com/slashdoom/aruba_exporter">github.com/slashdoom/aruba_exporter</a></p>
</body>
</html>
`))
})
http.HandleFunc(*metricsPath, handleMetricsRequest)
log.Infof("Listening for %s on %s\n", *metricsPath, *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}
func handleMetricsRequest(w http.ResponseWriter, r *http.Request) {
reg := prometheus.NewRegistry()
a := newArubaCollector(devices)
reg.MustRegister(a)
promhttp.HandlerFor(reg, promhttp.HandlerOpts{
ErrorLog: log.New(),
ErrorHandling: promhttp.ContinueOnError}).ServeHTTP(w, r)
}