Kaarsemaker.net


nagios_uptime_report.pl
2.12 KB

69 lines

Download

jabber_notify
check_nick

Preview

 1 #!/usr/bin/perl
 2 # nagios_uptime_report.pl (aka nagios2mgmt.pl)
 3 #
 4 # Mail uptime graphs for a host to a set of addresses. Useful to
 5 # keep curious managers at a distance.
 6 #
 7 # 1) Run this script on 01:00 on the first of every month.
 8 #    0 1 1 * * /home/dennis/bin/nagios_uptime_report.pl
 9 # 2) Make management receive this mail
10 # 3) ???
11 # 4) Profit
12 
13 use strict;
14 use warnings;
15 use POSIX qw(mktime);
 1 #!/usr/bin/perl
 2 # nagios_uptime_report.pl (aka nagios2mgmt.pl)
 3 #
 4 # Mail uptime graphs for a host to a set of addresses. Useful to
 5 # keep curious managers at a distance.
 6 #
 7 # 1) Run this script on 01:00 on the first of every month.
 8 #    0 1 1 * * /home/dennis/bin/nagios_uptime_report.pl
 9 # 2) Make management receive this mail
10 # 3) ???
11 # 4) Profit
12 
13 use strict;
14 use warnings;
15 use POSIX qw(mktime);
16 use MIME::Lite;
17 use LWP::Simple;
18 
19 # "config"
20 my $from = '"Nagios uptime graphs" <noreply@example.com>';
21 my $subject = 'Nagios uptime graphs for %d/%d',
22 my @recipients = ('me@example.com',
23                   'you@example.com');
24 
25 my $nagios_host = 'http://user:pass@nagios.example.com';
26 my @hosts = ('www.example.com','test.example.com','mail.example.com');
27 
28 # Here's where to get the graphs
29 my $baseurl = "https://$nagios_host/nagios/cgi-bin/trends.cgi?createimage" .
30               '&assumeinitialstates=yes&assumestatesduringnotrunning=yes&initialassumedhoststate=0&' .
31               'initialassumedservicestate=0&assumestateretention=yes&includesoftstates=no' .
32               '&host=%s&backtrack=4&zoom=4&t1=%d&t2=%d';
33 
34 # Which month to graph
35 # - Previous month if no month given
36 # - Allow overrides in argv, ./$0 year month
37 my $year = $ARGV[0];
38 my $month = $ARGV[1];
39 my ($cursec, $curmin, $curhour, $curmday, $curmon, $curyear, $curwday, $curyday, $curisdst) = gmtime(time);
40 $year = $curyear + 1900 if not defined $year;
41 $month = $curmon if not defined $month;
42 my $t1 = mktime(0, 0, 0, 1, $month-1, $year - 1900, 0, 0, 0);
43 my $t2 = mktime(0, 0, 0, 1, $month, $year - 1900, 0, 0, 0);
44 
45 # Construct mail
46 my $msg = MIME::Lite->new(
47     From => $from,
48     To => join(',', @recipients),
49     Subject => sprintf($subject, $year, $month),
50     Type => 'multipart/related',
51 );
52 $msg->attach(
53     Type => 'text/html',
54     Data => '<body>' . join('<br />', map(sprintf('<img src="cid:%s.png" />', $_), @hosts)) . '</body>'
55 );
56 
57 # Fetch images and attach
58 foreach my $host (@hosts) {
59     my $url = sprintf $baseurl, $host, $t1, $t2;
60     my $img = get($url);
61     $msg->attach(
62         Type => 'image/png',
63         Data => $img,
64         Id  => "$host.png",
65     );
66 }
67 
68 # Send!
69 $msg->send();

Show all