-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
91 lines (82 loc) · 2.71 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
package main
import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
)
func probeHandler(w http.ResponseWriter, r *http.Request, configs LoginConfigs) {
// Logs all the connections
logger.WithFields(
log.Fields{
"subsytem": "probe_handler",
"part": "connection_info",
"user_address": r.RemoteAddr,
"server_host": r.Host,
"user_agent": r.UserAgent(),
}).Info("This connection was established")
var loginType = ""
// Extract the target from the url
target := r.URL.Query().Get("target")
if target == "" {
logger.WithFields(
log.Fields{
"subsystem": "probe_handler",
"part": "target_check",
}).Error("The target is not given")
}
// Find the target in the configuration
targetConfig, err := findTargetInConfig(configs, target)
if err != nil {
logger.WithFields(
log.Fields{
"subsystem": "probe_handler",
"part": "target_config_check",
}).Error("The given target does not have configuration")
} else {
loginType = targetConfig.LoginType
}
// Get the status and elapsed time for the tests
status, elapsed := getStatus(targetConfig)
statusValue := 0
if status {
statusValue = 1
}
var statusMetric = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "login_status",
Help: "Shows the status of the given target 0 for failure 1 for success"},
[]string{"target", "login_type"})
var elapsedMetric = prometheus.NewGauge(
prometheus.GaugeOpts{Name: "login_elapsed_seconds", Help: "Shows how long it took the get the data in seconds"})
registry := prometheus.NewRegistry()
registry.MustRegister(statusMetric)
registry.MustRegister(elapsedMetric)
statusMetric.WithLabelValues(target, loginType).Set(float64(statusValue))
elapsedMetric.Set(elapsed)
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
/// findTargetInConfig Finds the given target in login configs
func findTargetInConfig(configs LoginConfigs, target string) (SingleLoginConfig, error) {
for _, config := range configs.Configs {
if config.Target == target {
return config, nil
}
}
config := SingleLoginConfig{}
return config, fmt.Errorf("can not find target: %s", target)
}
func main() {
loginConfig := readConfig(configFilePath)
http.HandleFunc("/probe", func(w http.ResponseWriter, r *http.Request) {
probeHandler(w, r, loginConfig)
})
logger.WithFields(
log.Fields{
"subsystem": "main",
"part": "port_setting",
}).Info("Started Listening on " + fmt.Sprintf("%s", listenIp) + ":" + fmt.Sprintf("%v", listenPort))
logger.Fatal(http.ListenAndServe(fmt.Sprintf("%s", listenIp)+":"+fmt.Sprintf("%v", listenPort), nil))
}