-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (98 loc) · 2.75 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
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/miton18/go-warp10/base"
"github.com/miton18/go-warp10/instrumentation"
"github.com/miton18/unifi-exporter/client"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// RootCmd launch the nothing.
var RootCmd = &cobra.Command{
Use: "unifi_exporter",
Run: rootFn,
}
func init() {
RootCmd.PersistentFlags().String("config", "", "config file to use")
RootCmd.PersistentFlags().BoolP("verbose", "v", false, "verbose output")
cobra.OnInitialize(initCobra)
log.SetLevel(log.InfoLevel)
if viper.GetBool("verbose") {
log.SetLevel(log.DebugLevel)
}
}
func main() {
err := RootCmd.Execute()
if err != nil {
log.Fatalf("%v", err)
}
}
func rootFn(cmd *cobra.Command, args []string) {
host := viper.GetString("controller.host")
port := viper.GetInt("controller.port")
user := viper.GetString("controller.user")
pass := viper.GetString("controller.password")
refresh := viper.GetDuration("refresh.period")
whitelist := viper.GetStringSlice("site.whitelist")
warpHost := viper.GetString("warp10.host")
warpToken := viper.GetString("warp10.token")
listen := viper.GetString("listen")
if user == "" {
log.Fatal("cannot connect to Unifi controller without user name")
}
if pass == "" {
log.Fatal("cannot connect to Unifi controller without user password")
}
gracefulStop := make(chan os.Signal, 2)
signal.Notify(gracefulStop, syscall.SIGTERM)
signal.Notify(gracefulStop, syscall.SIGINT)
data := make(chan instrumentation.Metrics)
log.Infof("Starting HTTP server on: %s", listen)
startServer(listen, data)
log.Info(warpHost)
wClient := base.NewClient(warpHost)
wClient.WriteToken = warpToken
log.Infof("Start scrapping Unifi controller every %s", refresh)
t := time.NewTicker(refresh)
go func() {
client := client.New(user, pass, host, port, nil, true)
//StartTasks(client, wClient)
err := client.Connect()
if err != nil {
log.WithError(err).Fatal("cannot join Unifi controller")
}
for {
<-t.C
metrics, err := scrappe(client, whitelist)
if err != nil {
log.WithError(err).Fatal("cannot scrape unifi controller metrics")
}
fmt.Println(metrics.Get().Sensision())
flush(wClient, metrics)
}
}()
<-gracefulStop
// Stop metrics aggregator
t.Stop()
log.Info("Stopping...")
err := stopServer()
if err != nil {
log.WithError(err).Error("cannot close HTTP server")
}
}
func flush(wClient *base.Client, metrics instrumentation.Metrics) {
err := wClient.Update(metrics.Get())
if err != nil {
log.WithError(err).Fatal("cannot send metrics to Warp10")
}
res, err := wClient.Meta(metrics.Get())
if err != nil {
log.WithError(err).Error("cannot meta metrics to Warp10")
}
log.Infof("META: %s", string(res))
}