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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
#!/usr/bin/perl
#
# Generate list of nodes to load from /etc/munin/munin.conf to check
# all the machines reporting to sitesummary.
use strict;
use warnings;
use SiteSummary;
use Getopt::Std;
sub usage {
print <<EOF;
Usage: $0 [-hmnw]
-h Show usage information
-m Generate munin configuration for all munin clients
-n Generate nagios configuration for all nagios clients
-r Do not generate nagios checks requiring nrpe configuration on remote hosts
-w List all client DNS/IP-addresses and MAC addresses
EOF
}
my %opts;
getopts("hmnwr", \%opts) || (usage(), exit(1));
my %hostnames;
for_all_hosts(\&handle_host);
if ($opts{'h'}) {
usage();
exit 0;
} elsif ($opts{'m'}) {
print_munin_list();
} elsif ($opts{'w'}) {
print_ip_hw_list();
} elsif ($opts{'n'}) {
generate_nagios_config();
} else {
print_list();
}
exit 0;
sub handle_host {
my $hostid = shift;
my $address = get_dns_address($hostid);
$hostnames{$address} = $hostid;
}
sub print_list {
for my $hostname (sort keys %hostnames) {
print "$hostname\n";
}
}
sub is_pkg_installed {
my ($hostid, $pkgname) = @_;
# Check debian/dpkg-l for 'ii *pkgname '
my $path = get_filepath_current($hostid, "/debian/dpkg-l");
if (open (my $fh, $path)) {
while(<$fh>) {
if (m/^ii *$pkgname /) {
close($fh);
return 1
}
}
close($fh);
}
return undef;
}
sub is_munin_client {
my $hostid = shift;
return is_pkg_installed($hostid, "munin-node");
}
sub is_nagios_client {
my $hostid = shift;
return is_pkg_installed($hostid, "nagios-nrpe-server") ||
is_pkg_installed($hostid, "nagios-text") ||
is_pkg_installed($hostid, "nagios2") ||
is_pkg_installed($hostid, "nagios3");
}
sub print_munin_list {
for my $hostname (sort keys %hostnames) {
next unless (is_munin_client($hostnames{$hostname}));
# Using hostname as address, to avoid hardcoding IP addresses in
# the file. Might be an idea to fetch the IP address from
# system/ifconfig-a
print <<EOF;
[$hostname]
address $hostname
use_node_name yes
EOF
}
}
sub print_ip_hw_list {
for my $hostname (sort keys %hostnames) {
my $macaddress = get_primary_macaddress($hostnames{$hostname});
print "$hostname $macaddress\n";
}
}
sub is_remote_nagios_client {
my $hostid = shift;
return is_pkg_installed($hostid, "nagios-nrpe-server") && !
(is_pkg_installed($hostid, "nagios-text") ||
is_pkg_installed($hostid, "nagios2") ||
is_pkg_installed($hostid, "nagios3"));
}
sub print_nagios_service_check {
my ($remote, $hostname, $description, $check, $check_args) = @_;
my $template = "server-service";
my $cmd;
return if $opts{'r'} && $remote;
if ($remote) {
$cmd = "check_nrpe!$check";
if (defined $check_args) {
$check_args =~ s/!/ /;
$cmd .= " -a $check_args";
}
} else {
$cmd = "$check";
$cmd .= "!$check_args" if defined $check_args;
}
print <<EOF;
define service {
use $template;
host_name $hostname
service_description $description
check_command $cmd
}
EOF
}
sub print_nagios_host_check {
my ($hostname, $address) = @_;
my $template = "server-host";
print <<EOF;
define host {
use $template
host_name $hostname
address $address
}
EOF
}
sub generate_nagios_config {
for my $hostname (sort keys %hostnames) {
my $hostid = $hostnames{$hostname};
my $address = get_dns_address($hostid);
my $redirect = "";
my $remote = is_remote_nagios_client($hostid);
print <<EOF;
##################### $hostname #######################
EOF
print_nagios_host_check($hostname, $address);
# first, check ping to see if the other checks should be performed
print_nagios_service_check(0, $hostname, "ping",
"check_ping", "100.0,20%!500.0,60%");
my %tcpservices =
(
139 => { name => 'samba', package => 'samba' },
389 => { name => 'ldap', package => 'slapd' },
631 => { name => 'CUPS', package => 'cupsys' },
4949 => { name => 'munin', package => 'munin-node' },
7100 => { name => 'xfs', package => 'xfs' }, # check X font server
);
for my $port (sort { $a <=> $b } keys %tcpservices) {
next if (exists $tcpservices{$port}->{package} && !
is_pkg_installed($hostid,
$tcpservices{$port}->{package}));
my $servicename = $tcpservices{$port}->{name};
print_nagios_service_check(0, $hostname, $servicename,
"check_tcp", $port);
}
# The rest of the checks only work if NRPE is installed and configured
next unless (is_nagios_client($hostid));
print_nagios_service_check($remote, $hostname, "swap",
"check_swap", "10%!5%");
print_nagios_service_check($remote, $hostname, "current users",
"check_users", "20!50");
print_nagios_service_check($remote, $hostname, "processes total",
"check_procs", "500!1000");
print_nagios_service_check($remote, $hostname, "processes zombie",
"check_procs_zombie", "20!100");
print_nagios_service_check($remote, $hostname, "apt-updates",
"check_apt");
# Check unix load
print_nagios_service_check($remote, $hostname, "load as in top",
"check_load", "75,75,75!90,90,90");
# check disk free space
my $path = get_filepath_current($hostid, "/system/procmounts");
if ( -e $path ) {
open (F, "<", $path) || die "unable to read from $path";
my %checked;
while (<F>) {
chomp;
my ($device, $partition, $fs, $opts) = split;
next if (exists $checked{$device});
next if ($fs eq "devpts" ||
$fs eq "autofs" ||
$fs eq "binfmt_misc" ||
$fs eq "fusectl" ||
$fs eq "iso9660" ||
$fs eq "nfs" ||
$fs eq "nfsd" ||
$fs eq "proc" ||
$fs eq "rootfs" ||
$fs eq "rpc_pipefs" ||
$fs eq "sysfs" ||
$fs eq "tmpfs" ||
$fs eq "usbfs");
$checked{$device} = 1;
my $warn = 10;
my $crit = 5;
print_nagios_service_check($remote, $hostname,
"disk $partition",
"check_disk",
"$warn%!$crit%!$partition");
}
}
# check software raid status
if ( -e get_filepath_current($hostid, "/system/mdstat") ) {
print_nagios_service_check($remote, $hostname, "sw-raid",
"check_linux_raid");
}
# check munin if munin-node is installed
# check hw raid status
# check hardware status
# check LDAP and LDAPS using the protocol
# Check DNS server
print_nagios_service_check($remote, $hostname, "dns",
"check_dns")
if is_pkg_installed($hostid, "pdns-server");
# Check IMAPS server
print_nagios_service_check($remote, $hostname, "imaps",
"check_imaps")
if is_pkg_installed($hostid, "courier-imap-ssl");
# Check NFS server
print_nagios_service_check($remote, $hostname, "nfs",
"check_nfs")
if is_pkg_installed($hostid, "nfs-kernel-server");
# Check Squid web proxy
print_nagios_service_check($remote, $hostname, "squid",
"check_squid", "3128!http://www")
if is_pkg_installed($hostid, "squid");
# Check SSH server
print_nagios_service_check($remote, $hostname, "ssh",
"check_ssh")
if is_pkg_installed($hostid, "openssh-server");
print_nagios_service_check($remote, $hostname, "http",
"check_http")
if (is_pkg_installed($hostid, "apache") ||
is_pkg_installed($hostid, "apache2"));
print_nagios_service_check($remote, $hostname, "ntp time server",
"check_ntp", "-H!localhost")
if (is_pkg_installed($hostid, "ntp") ||
is_pkg_installed($hostid, "ntp-server"));
# Detect if cron no longer is running
print_nagios_service_check($remote, $hostname, "process - cron",
"check_procs_command", "1:15!1:25!cron")
if (is_pkg_installed($hostid, "cron"));
print_nagios_service_check($remote, $hostname, "dhcp",
"check_dhcp")
if is_pkg_installed($hostid, "dhcp3-server");
}
}
|