/
common.go
150 lines (131 loc) · 3.81 KB
/
common.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
package sender
import (
"fmt"
"time"
"github.com/rechecked/rcagent/internal/config"
"github.com/rechecked/rcagent/internal/manager"
"github.com/rechecked/rcagent/internal/server"
"github.com/rechecked/rcagent/internal/status"
)
// Set up passive related loop
func Run() {
manager.Init()
// Verify we have checks to run...
if len(config.CfgData.Checks) == 0 {
config.LogDebug("SENDER: No checks configured")
} else {
config.LogDebugf("SENDER: %d checks configured", len(config.CfgData.Checks))
}
// Verify we have senders to send to...
if len(config.CfgData.Senders) == 0 {
config.LogDebug("SENDER: No senders configured")
} else {
config.LogDebugf("SENDER: %d senders configured", len(config.CfgData.Senders))
}
// Tick every second and check for any passive checks we need
t := time.NewTicker(1 * time.Second)
defer t.Stop()
for range t.C {
config.CfgData.RLock()
runChecks()
config.CfgData.RUnlock()
}
}
func runChecks() {
now := time.Now()
for i, check := range config.CfgData.Checks {
if check.Disabled || check.NextRun.After(now) {
continue
}
// Parse check interval duration and error if it is bad and disable,
// then set next run time if it isn't disabled
dur, err := time.ParseDuration(check.Interval)
if err != nil {
config.LogDebugf("Interval Error: %s\n", err)
config.Log.Infof("The interval for '%s' is invalid, disabling", check.Name())
config.CfgData.Checks[i].Disabled = true
}
config.CfgData.Checks[i].NextRun = now.Add(dur)
// Run the check and get the value data back, try to send it off if we can
data, err := server.GetDataFromEndpoint(check.Endpoint, check.Options)
if err != nil {
config.Log.Infof("Error: runChecks: %s\n", err)
}
// If we are using a plugin, we need to convert the plugin result
// to a check result since it will need to be to send to NRDP
if p, ok := data.(status.PluginResults); ok {
data = status.CheckResult{
Output: p.Output,
Exitcode: p.ExitCode,
}
}
// For normal check results
chk, ok := data.(status.CheckResult)
if ok {
go sendToSenders(chk, check)
config.LogDebugf("%s\n", chk.String())
continue
}
// If the check is not CheckResult or PluginResult, then it's invalid so stop trying
config.LogDebug(data)
config.Log.Infof("The check for '%s' is invalid, check endpoints and options, disabling",
check.Name())
config.CfgData.Checks[i].Disabled = true
}
}
func sendToSenders(chk status.CheckResult, cfg config.CheckCfg) {
config.LogDebugf("Sending check: %s\n", chk.String())
// Get all senders
config.CfgData.RLock()
senders := config.CfgData.Senders
config.CfgData.RUnlock()
for _, sender := range senders {
// We only have NRDP for now but more later?
if sender.Type == "nrdp" {
s := new(NRDPServer)
err := s.SetConn(sender.Url, sender.Token)
if err != nil {
config.Log.Errorf("Error: sendToSenders: %s", err)
}
// Set output
output := chk.Output
if chk.LongOutput != "" {
output = fmt.Sprintf("%s\n%s", output, chk.LongOutput)
}
if chk.Perfdata != "" {
output = fmt.Sprintf("%s | %s", output, chk.Perfdata)
}
// Create the nrdp result
var checks []NRDPCheckResult
if cfg.Servicename != "" {
checks = []NRDPCheckResult{
{
Checkresult: NRDPObjectType{
Type: "service",
},
Hostname: cfg.Hostname,
Servicename: cfg.Servicename,
State: chk.Exitcode,
Output: output,
},
}
} else {
checks = []NRDPCheckResult{
{
Checkresult: NRDPObjectType{
Type: "host",
},
Hostname: cfg.Hostname,
State: chk.Exitcode,
Output: output,
},
}
}
resp, err := s.Send(checks)
if err != nil {
config.Log.Errorf("Error: sendToSenders: %s", err)
}
config.LogDebugf("Sender NRDP repsonse: %s", resp.String())
}
}
}