-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnagctl.go
246 lines (211 loc) · 7.95 KB
/
nagctl.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"fmt"
"time"
"strings"
"regexp"
"os/exec"
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
"github.com/martini-contrib/binding"
"github.com/martini-contrib/auth"
"github.com/vaughan0/go-ini"
"github.com/cxfcxf/nagtomaps"
)
type DataPost struct {
Exec string `form:"exec"`
Hosts string `form:"hosts"`
Services string `form:"services"`
}
type AckPost struct {
Hosts string `form:"hosts"`
Services string `form:"services"`
Ackall string `form:"ackall"`
}
type Dstatus struct {
Servers_have_problem []string
Services_have_problem map[string][]string
Servers_enabled []string
Services_enabled map[string][]string
Servers_ok_disabled []string
Services_ok_disabled map[string][]string
}
func iniParser(inifile string) (string, string, string, string, string) {
f, err := ini.LoadFile(inifile)
if err != nil {panic(err)}
authname, _ := f.Get("authlogin", "username")
authpass, _ := f.Get("authlogin", "password")
port, _ := f.Get("listen", "port")
sfile, _ := f.Get("files", "sfile")
efile, _ := f.Get("files", "efile")
return authname, authpass, port, sfile, efile
}
func getStatus(sfile string) Dstatus{
sdata := nagtomaps.ParseStatus(sfile)
var dstatus Dstatus
//hosts have problem
for _, server := range sdata.Hoststatuslist {
if server["current_state"] != "0" && server["notifications_enabled"] == "1" && server["acknowledgement_type"] == "0" && server["current_attempt"] > "1" {
dstatus.Servers_have_problem = append(dstatus.Servers_have_problem, server["host_name"])
}
}
//hosts enabled
for _, server := range sdata.Hoststatuslist {
if (server["acknowledgement_type"] != "0" && server["current_state"] != "0") || (server["notifications_enabled"] == "0" && server["current_state"] != "0") {
} else {
dstatus.Servers_enabled = append(dstatus.Servers_enabled, server["host_name"])
}
}
//host ok disabled
for _, server := range sdata.Hoststatuslist {
if server["current_state"] == "0" && server["notifications_enabled"] == "0" {
dstatus.Servers_ok_disabled = append(dstatus.Servers_ok_disabled, server["host_name"])
}
}
//services have problem
dstatus.Services_have_problem = make(map[string][]string)
for _, serverserv := range sdata.Servicestatuslist {
for _, service := range serverserv {
if service["current_state"] != "0" && service["notifications_enabled"] == "1" && service["acknowledgement_type"] == "0" && service["current_attempt"] > "1" {
dstatus.Services_have_problem[service["host_name"]] = append(dstatus.Services_have_problem[service["host_name"]], service["service_description"])
}
}
}
//services enabled
dstatus.Services_enabled = make(map[string][]string)
for _, serverserv := range sdata.Servicestatuslist {
for _, service := range serverserv {
if (service["acknowledgement_type"] != "0" && service["current_state"] != "0") || (service["notifications_enabled"] == "0" && service["current_state"] != "0") {
} else {
dstatus.Services_enabled[service["host_name"]] = append(dstatus.Services_enabled[service["host_name"]], service["service_description"])
}
}
}
//services ok disabled
dstatus.Services_ok_disabled = make(map[string][]string)
for _, serverserv := range sdata.Servicestatuslist {
for _, service := range serverserv {
if service["current_state"] == "0" && service["notifications_enabled"] == "0" {
dstatus.Services_ok_disabled[service["host_name"]] = append(dstatus.Services_ok_disabled[service["host_name"]], service["service_description"])
}
}
}
return dstatus
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func nagiosExec(host string, service string, efile string) {
if host != "" && service == "" {
command := fmt.Sprintf("/bin/echo \"[%d] ACKNOWLEDGE_HOST_PROBLEM;%s;2;1;0;admin;acknowledged by nagctl webui\n\" > %s", time.Now().Unix(), host, efile)
cmd := exec.Command("sh", "-c", command)
if err := cmd.Run() ; err != nil {panic(err)}
} else {
command := fmt.Sprintf("/bin/echo \"[%d] ACKNOWLEDGE_SVC_PROBLEM;%s;%s;2;1;0;admin;acknowledged by nagctl webui\n\" > %s", time.Now().Unix(), host, service, efile)
cmd := exec.Command("sh", "-c", command)
if err := cmd.Run() ; err != nil {panic(err)}
}
}
func nagiosExecCtl(execute string, hosts string, services string, ds Dstatus, efile string) {
exe := strings.ToUpper(execute)
if hosts != "" && services != "" {
h, _ := regexp.Compile(hosts)
s, _ := regexp.Compile(services)
for host, secs := range ds.Services_enabled {
for _, service := range secs {
if h.MatchString(host) && s.MatchString(service) {
command := fmt.Sprintf("/bin/echo \"[%d] %s_SVC_NOTIFICATIONS;%s;%s\n\" > %s", time.Now().Unix(), exe, host, service, efile)
cmd := exec.Command("sh", "-c", command)
if err := cmd.Run() ; err != nil {panic(err)}
}
}
}
} else if hosts != "" && services == "" {
h, _ := regexp.Compile(hosts)
for _, host := range ds.Servers_enabled {
if h.MatchString(host) {
command := fmt.Sprintf("/bin/echo \"[%d] %s_HOST_NOTIFICATIONS;%s\n\" > %s", time.Now().Unix(), exe, host, efile)
cmd := exec.Command("sh", "-c", command)
if err := cmd.Run() ; err != nil {panic(err)}
command = fmt.Sprintf("/bin/echo \"[%d] %s_HOST_SVC_NOTIFICATIONS;%s\n\" > %s", time.Now().Unix(), exe, host, efile)
cmd = exec.Command("sh", "-c", command)
if err := cmd.Run() ; err != nil {panic(err)}
}
}
} else if hosts == "" && services != "" {
s, _ := regexp.Compile(services)
for host, secs := range ds.Services_enabled {
for _, service := range secs {
if s.MatchString(service) {
command := fmt.Sprintf("/bin/echo \"[%d] %s_SVC_NOTIFICATIONS;%s;%s\n\" > %s", time.Now().Unix(), exe, host, service, efile)
cmd := exec.Command("sh", "-c", command)
if err := cmd.Run() ; err != nil {panic(err)}
}
}
}
} else if hosts == "" && services == "" {
for _, host := range ds.Servers_ok_disabled {
command := fmt.Sprintf("/bin/echo \"[%d] %s_HOST_NOTIFICATIONS;%s\n\" > %s", time.Now().Unix(), exe, host, efile)
cmd := exec.Command("sh", "-c", command)
if err := cmd.Run() ; err != nil {panic(err)}
}
for host, secs := range ds.Services_ok_disabled {
for _, service := range secs {
command := fmt.Sprintf("/bin/echo \"[%d] %s_SVC_NOTIFICATIONS;%s;%s\n\" > %s", time.Now().Unix(), exe, host, service, efile)
cmd := exec.Command("sh", "-c", command)
if err := cmd.Run() ; err != nil {panic(err)}
}
}
}
}
func main() {
authname, authpass, port, sfile, efile := iniParser("nagctl.ini")
m := martini.Classic()
m.Use(render.Renderer())
m.Use(auth.Basic(authname, authpass))
m.Get("/", func() string {
return "you are hiting the webroot directory! please use /status or /nagctl"
})
m.Get("/status", func(r render.Render) {
ds := getStatus(sfile)
r.HTML(200, "status", ds)
})
m.Post("/status", binding.Bind(AckPost{}), func(ap AckPost, r render.Render) {
if ap.Ackall == "notempty" {
ds := getStatus(sfile)
for _, s := range ds.Servers_have_problem {
go nagiosExec(s, "", efile)
}
for sr, svs := range ds.Services_have_problem {
if !stringInSlice(sr, ds.Servers_have_problem) {
for _, sev := range svs {
go nagiosExec(sr, sev, efile)
}
}
}
ap.Ackall = fmt.Sprintf("Servers: ==> %s\n Services: ==> %s\n", ds.Servers_have_problem, ds.Services_have_problem)
} else if ap.Hosts != "" {
go nagiosExec(ap.Hosts, "", efile)
} else {
hslist := strings.Split(ap.Services, " ")
go nagiosExec(hslist[0], hslist[1], efile)
}
r.HTML(200, "finish", ap)
})
m.Get("/nagctl", func(r render.Render) {
r.HTML(200, "nagctl", nil)
})
m.Post("/nagctl", binding.Bind(DataPost{}), func(dp DataPost, r render.Render) {
ds := getStatus(sfile)
nagiosExecCtl(dp.Exec, dp.Hosts, dp.Services, ds, efile)
command := fmt.Sprintf("Executed --> Exec: %s Hosts: %s services: %s \n",
dp.Exec, dp.Hosts, dp.Services)
r.HTML(200, "nagfin", command)
})
m.RunOnAddr(port)
}