-
Notifications
You must be signed in to change notification settings - Fork 36
/
main.go
97 lines (78 loc) · 2.6 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
package main
import (
"fmt"
"net/http"
"os"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
"github.com/xperimental/nextcloud-exporter/internal/client"
"github.com/xperimental/nextcloud-exporter/internal/config"
"github.com/xperimental/nextcloud-exporter/internal/login"
"github.com/xperimental/nextcloud-exporter/internal/metrics"
"github.com/xperimental/nextcloud-exporter/serverinfo"
)
var (
// Version contains the version as set during the build.
Version = ""
// GitCommit contains the git commit hash set during the build.
GitCommit = ""
log = &logrus.Logger{
Out: os.Stderr,
Formatter: &logrus.TextFormatter{
DisableTimestamp: true,
},
Hooks: make(logrus.LevelHooks),
Level: logrus.InfoLevel,
ExitFunc: os.Exit,
ReportCaller: false,
}
)
func main() {
cfg, err := config.Get()
if err != nil {
log.Fatalf("Error loading configuration: %s", err)
}
if cfg.RunMode == config.RunModeHelp {
return
}
if cfg.RunMode == config.RunModeVersion {
fmt.Println(Version)
return
}
log.Infof("nextcloud-exporter %s", Version)
userAgent := fmt.Sprintf("nextcloud-exporter/%s", Version)
if cfg.RunMode == config.RunModeLogin {
if cfg.ServerURL == "" {
log.Fatalf("Need to specify --server for login.")
}
loginClient := login.Init(log, userAgent, cfg.ServerURL, cfg.TLSSkipVerify)
log.Infof("Starting interactive login on: %s", cfg.ServerURL)
if err := loginClient.StartInteractive(); err != nil {
log.Fatalf("Error during login: %s", err)
}
return
}
if err := cfg.Validate(); err != nil {
log.Fatalf("Invalid configuration: %s", err)
}
if cfg.AuthToken == "" {
log.Infof("Nextcloud server: %s User: %s", cfg.ServerURL, cfg.Username)
} else {
log.Infof("Nextcloud server: %s Authentication using token.", cfg.ServerURL)
}
infoURL := serverinfo.InfoURL(cfg.ServerURL, !cfg.Info.Apps, !cfg.Info.Update)
if cfg.TLSSkipVerify {
log.Warn("HTTPS certificate verification is disabled.")
}
infoClient := client.New(infoURL, cfg.Username, cfg.Password, cfg.AuthToken, cfg.Timeout, userAgent, cfg.TLSSkipVerify)
if err := metrics.RegisterCollector(log, infoClient, cfg.Info.Apps, cfg.Info.Update); err != nil {
log.Fatalf("Failed to register collector: %s", err)
}
if err := metrics.RegisterInfoMetric(Version, GitCommit); err != nil {
log.Fatalf("Failed to register info metric: %s", err)
}
http.Handle("/metrics", promhttp.Handler())
http.Handle("/", http.RedirectHandler("/metrics", http.StatusFound))
log.Infof("Listen on %s...", cfg.ListenAddr)
log.Fatal(http.ListenAndServe(cfg.ListenAddr, nil))
}