-
Notifications
You must be signed in to change notification settings - Fork 0
/
old-rebuild_nagios_configs.pl
268 lines (226 loc) · 5.44 KB
/
old-rebuild_nagios_configs.pl
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env perl
use strict;
use File::Path;
use Net::LDAP;
use Data::Dumper;
# Config stuff
my $base_dir = "/usr/local/nagios/etc/cluenet/";
# Main code
my $ldap = Net::LDAP->new("ldap.cluenet.org", timeout => 30);
if(!$ldap) {
print "Could not connect to ldap\n";
}
print "Clearing current configs\n";
rmtree($base_dir);
mkdir($base_dir);
my @admins = ();
my @owners = ();
my $mesg = $ldap->search(
filter => "(&(objectClass=server)(isActive=TRUE))",
base => "ou=servers,dc=cluenet,dc=org"
);
my @entries = $mesg->entries;
print "Starting server configs\n";
foreach (@entries) {
my $entry = $_;
print "Starting " . $entry->get_value('cn') . "\n";
my $hostname = $entry->get_value('cn');
if(!$hostname) {
print "Skipping - missing hostname\n";
next;
}
my $servername = $hostname;
$servername =~ s/\.cluenet\.org$//;
my $owner = $entry->get_value('owner');
$owner =~ s/uid=(.*),ou=people,dc=cluenet,dc=org/$1/;
if(!$owner) {
print "Skipping - missing owner\n";
next;
}
my $ip_address = $entry->get_value('ipAddress');
if(!$ip_address) {
print "Skipping - missing ip address\n";
next;
}
my $ssh_port = $entry->get_value('sshPort');
my $description = $entry->get_value('description');
if(!$description) {
$description = "";
}
my $os = $entry->get_value('operatingSystem');
if(!$os) {
$os = 'linux'; # windows sux
}
open(FH, ">", "$base_dir/$hostname.cfg");
# Write the host out
print FH <<EOS;
define host {
host_name $hostname
alias $servername
address $ip_address
hostgroups $owner\_servers
max_check_attempts 5
notification_interval 0
check_command check-host-alive
check_period 24x7
contacts $owner
contact_groups $servername\_admins
notification_period 24x7
}
EOS
;
# Write out the services
# - I'd like these to be ldap attributes but I'm not sure how we can do shit for now so hardcoding
print "Adding ping check\n";
print FH <<EOS;
define service {
host_name $hostname
service_description PING check
notification_period 24x7
check_period 24x7
max_check_attempts 3
normal_check_interval 5
retry_check_interval 1
notification_interval 30
notification_options w,u,c,r,f,s
check_command check_ping!100.0,20%!500.00,60%
}
define serviceextinfo {
host_name $hostname
service_description PING check
notes_url /nagiosgraph/cgi-bin/show.cgi?host=\$HOSTNAME\$&service=\$SERVICEDESC\$
}
EOS
;
if(grep(/windows/i, $os)) {
} else {
if($ssh_port) {
print "Adding SSH check on port $ssh_port\n";
print FH <<EOS;
define service {
host_name $hostname
service_description SSH running on port $ssh_port
notification_period 24x7
check_period 24x7
max_check_attempts 3
normal_check_interval 5
retry_check_interval 1
notification_interval 30
notification_options w,u,c,r,f,s
check_command check_ssh!$ssh_port
}
#define serviceextinfo {
# host_name $hostname
# service_description SSH running on port $ssh_port
# notes_url /nagiosgraph/cgi-bin/show.cgi?host=\$HOSTNAME\$&service=\$SERVICEDESC\$
#}
EOS
;
} else {
print "Skipping ssh check - no port specified\n";
}
}
# Write out the contact group
print "Writing out the contact group\n";
my $members = "";
foreach($entry->get_value('authorizedAdministrator')) {
my $user = $_;
$user =~ s/uid=(.*),ou=people,dc=cluenet,dc=org/$1/;
$members = $members . ", " . $user;
if(!grep(/$user/, @admins)) {
push(@admins, $user);
}
}
print FH <<EOS;
define contactgroup {
contactgroup_name $servername\_admins
alias $hostname admins
members __nagiosbot_twitter__,__nagiosbot_irc__$members
}
EOS
;
# Push admins and owner into admins array
if(!grep(/$owner/, @admins)) {
push(@admins, $owner);
}
if(!grep(/$owner/, @owners)) {
push(@owners, $owner);
}
close(FH);
print "Done " . $entry->get_value('cn') . "\n";
}
print "Finished server configs\n";
print "Starting user config\n";
open(FH, ">", "$base_dir/users.cfg");
print "Adding __nagiosbot_twitter__\n";
print FH <<EOS;
define contact {
contact_name __nagiosbot_twitter__
alias Nagios twitter relay bot
host_notification_period 24x7
service_notification_period 24x7
host_notification_commands notify-host-by-twitter
service_notification_commands notify-service-by-twitter
service_notification_options w,u,c,r,f,s
host_notification_options d,u,r,f,s
}
EOS
;
print "Adding __nagiosbot_irc__\n";
print FH <<EOS;
define contact {
contact_name __nagiosbot_irc__
alias Nagios IRC relay bot
host_notification_period 24x7
service_notification_period 24x7
host_notification_commands notify-host-by-irc
service_notification_commands notify-service-by-irc
service_notification_options w,u,c,r,f,s
host_notification_options d,u,r,f,s
}
EOS
;
foreach(@admins) {
print "Adding user $_ \n";
print FH <<EOS;
define contact {
contact_name $_
host_notification_period 24x7
service_notification_period 24x7
host_notification_commands notify-host-by-email
service_notification_commands notify-service-by-email
EOS
;
my $mesg = $ldap->search(
filter => "(&(objectClass=person)(uid=" . $_ . "))",
base => "ou=people,dc=cluenet,dc=org"
);
foreach($mesg->entries) {
my $entry = $_;
my $gecos = $entry->get_value('gecos');
if($gecos) {
print FH "\talias $gecos\n";
}
my $mail = $entry->get_value('mail');
if($mail) {
print FH "#\temail $mail\n";
}
}
print FH <<EOS;
}
EOS
;
}
foreach(@owners) {
print FH <<EOS;
define hostgroup {
hostgroup_name $_\_servers
alias $_\'s servers
}
EOS
;
}
close(FH);
print "Finished user config\n";
print "Reloading\n";
`/etc/init.d/nagios reload`