-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
106 lines (86 loc) · 3.12 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
package main
import (
"fmt"
"net"
"github.com/ConSol/go-neb-wrapper/neb"
"github.com/griesbacher/Iapetos/callbacks"
"github.com/griesbacher/Iapetos/config"
"github.com/griesbacher/Iapetos/cyclic"
"github.com/griesbacher/Iapetos/logging"
"github.com/griesbacher/Iapetos/prom"
"github.com/griesbacher/nagflux/helper"
"github.com/prometheus/common/version"
)
const configFileKey = "config_file"
// Build contains the current git commit id
// compile passing -ldflags "-X main.Build <build sha1>" to set the id.
var Build string
var prometheusListener net.Listener
var stoppables = []cyclic.Stoppable{}
var iapetosVersion = "1.0"
//This is an example main file, which should demonstrate how to use the library.
func init() {
//Start selfobserving
stoppables = append(stoppables, cyclic.StartSelfObserver())
//Start Host and Service stats
stoppables = append(stoppables, cyclic.StartHostStatistics())
stoppables = append(stoppables, cyclic.StartServiceStatistics())
// just some information about your plugin
neb.Title = "Iapetos"
neb.Name = neb.Title
neb.Desc = "Offers a Prometheus interface for Nagios"
neb.License = "GPL v3"
neb.Version = fmt.Sprintf("%s - Build(%s)", iapetosVersion, Build)
neb.Author = "Philip Griesbacher"
//Set callbacks
neb.AddCallback(neb.HostCheckData, callbacks.HostCheckData)
neb.AddCallback(neb.ServiceCheckData, callbacks.ServiceCheckData)
neb.AddCallback(neb.NotificationData, callbacks.NotificationCheckData)
neb.AddCallback(neb.ContactNotificationData, callbacks.ContactNotificationCheckData)
//Init Hook
neb.NebModuleInitHook = func(flags int, args string) int {
neb.CoreFLog("Init - %s - by %s\n", neb.Version, neb.Author)
neb.CoreFLog("Init flags: %d\n", flags)
neb.CoreFLog("Init args: %s\n", args)
argsMap := helper.StringToMap(args, ",", "=")
if configFile, ok := argsMap[configFileKey]; ok {
err := config.InitConfig(configFile)
if err == nil {
neb.CoreFLog("Loading Configfile: %s\n", args)
if logging.InitLogDestination() != nil {
neb.CoreFLog(err.Error())
return neb.Error
}
logging.Flog("Build Info %s\n", version.Info())
logging.Flog("Build context %s\n", version.BuildContext())
} else {
neb.CoreFLog("Could not loaded Configfile: %s, Error: %s\n", args, err.Error())
return neb.Error
}
} else {
neb.CoreFLog("Could not file Configfile entry(%s) in the given config: %s\n", configFileKey, args)
return neb.Error
}
var err error
prometheusListener, err = prom.InitPrometheus(config.GetConfig().Prometheus.Address)
if err == nil {
logging.Flog("Starting Prometheus at %s\n", config.GetConfig().Prometheus.Address)
return neb.Ok
}
logging.Flog("Could not starting Prometheus at %s. Error: %s\n", config.GetConfig().Prometheus.Address, err)
return neb.Error
}
//Deinit Hook
neb.NebModuleDeinitHook = func(flags, reason int) int {
logging.Flog("Deinit\n")
logging.Flog("Deinit flags: %d\n", flags)
logging.Flog("Deinit reason: %d\n", reason)
for _, s := range stoppables {
s.Stop()
}
prometheusListener.Close()
return neb.Ok
}
}
//DON'T USE MAIN, IT WILL NEVER BE CALLED! USE CALLBACKS.
func main() {}