-
Notifications
You must be signed in to change notification settings - Fork 0
/
nagios-watch-webcal.pl
executable file
·159 lines (138 loc) · 4.96 KB
/
nagios-watch-webcal.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
#!/usr/bin/perl -w
# Author: Maxim Janssens
# Email: [email protected]
use strict;
use LWP::Simple;
# Global Variables
my $calendar = get "https://link.to/ical/calendar" or die "Cannot get calendar: $!\n";
my @lines = split "\n", $calendar;
chomp (my $today = `date '+%Y%m%d'`);
my @employees = ("employee1", "employee2", "employee3", "employee4");
my $directory = "/etc/nagios";
my $confdir = "$directory/conf.d";
my $watchdir = "$directory/nagioswatchers";
my $watchfile = "$confdir/nagioswatch.cfg";
my $nagioscfg = "$directory/nagios.cfg";
my $g = my $i = my $k = my $m = my $w = 0;
my ($date, $summary, $key, $value, $watcher, $lurker, $recipient, $nametmp);
my (@daywatch, @sendlist, @pagers, @previous);
my (%events, %previous, %current);
# main()
print "====GET_WATCHERS====\n";
&get_watchers;
print "====UPDATE_WATCHFILE====\n";
&update_watchfile($daywatch[0],$daywatch[1]);
print "====CHECK_EXISTING====\n";
&check_existing($daywatch[0],$daywatch[1]);
print "====NOTIFY_WATCHERS====\n";
¬ify_watchers;
print "===NOTIFY_NAGIOS====\n";
¬ify_nagios;
# Subroutines
sub get_watchers {
foreach my $line (@lines){
if($line =~ /^\s/){ $line =~ s/^\s//; }
if($line =~ s/^SUMMARY://){ chomp($summary = $line); $i++; }
elsif($line =~ s/^DTSTART;VALUE=DATE:// || $line =~ s/^DTSTART;TZID="\w+\/\w+"://){ $line =~ s/^(\d{8})\w+/$1/; chomp($date = $line); $events{$date} = (); $i++; }
if($i == 2){ $events{$date} = $summary; $i = 0; }
else { next; }
}
while(($key, $value) = each(%events)){
if($key =~ /$today/){ $watcher = $value; }
}
if($watcher eq ''){
print "No watcher found, same watcher today...\n";
exit 0;
} else {
@daywatch = split ' ', $watcher;
my $x = my $i = 0;
foreach my $entry (@daywatch){
my $tmp = $employees[$i];
if($entry !~ /$tmp/i && $x >= 3) { die "Calendar entry does not match any employee: $!\n"; }
else { print $entry . "\n"; next; }
}
return 0;
}
}
sub update_watchfile {
$watcher = $_[0];
$lurker = $_[1];
$w = 0;
open(NAGIOSWATCH, '<', $watchfile) or die "$watchfile can't be opened: $!\n";
while (<NAGIOSWATCH>){
if(/alias\s+(\w+)/){ $nametmp = lc($1); $previous{$nametmp} = (); }
if(/pager\s+(\d+)/){ $previous{$nametmp} = $1; $previous[$w] = $1; $w++; }
}
close NAGIOSWATCH;
open(NAGIOSWATCH, '>', $watchfile) or die "$watchfile can't be opened: $!\n";
print NAGIOSWATCH "";
close NAGIOSWATCH;
open (NAGIOSWATCH, ">>", $watchfile) or die "$watchfile can't be opened: $!\n";
open (WATCHER, "$watchdir/$watcher.cfg") or die "$watcher.cfg can't be opened: $!\n";
open (LURKER, "$watchdir/$lurker.cfg") or die "$lurker.cfg can't be opened: $!\n";
while (<WATCHER>){
if(/contact_name\s+(\w+)/){ $nametmp = $1; $current{$nametmp} = (); print NAGIOSWATCH "\tcontact_name SynqWatch\n";next; }
if(/pager\s+(\d+)/){ $current{$nametmp} = $1; print NAGIOSWATCH "$_"; }
else { print NAGIOSWATCH "$_"; }
}
print NAGIOSWATCH "\n";
while (<LURKER>){
if(/contact_name\s+(\w+)/) { $nametmp = $1; $current{$nametmp} = (); print NAGIOSWATCH "\tcontact_name SynqLurk\n";next; }
if(/pager\s+(\d+)/){ $current{$nametmp} = $1; print NAGIOSWATCH "$_"; }
else { print NAGIOSWATCH "$_"; }
}
close WATCHER;
close LURKER;
close NAGIOSWATCH;
open(WATCH, '<', $watchfile) or die "$watchfile cannot be opened: $!\n";
while(<WATCH>){ print; }
close WATCH;
}
sub check_existing {
$watcher = $_[0];
$lurker = $_[1];
my $z = $m = 0;
open(NAGIOSWATCH, "<", $watchfile) or die "Can't open $watchfile: $!\n";
while (<NAGIOSWATCH>){
if(/pager\s+(\d+)/){ $pagers[$m] = $1; $m++; }
}
close NAGIOSWATCH;
foreach my $key1 (keys %previous){
$z = 0;
foreach my $key2 (keys %current){
if($key1 ne $key2){ $z++; }
if($key1 ne $key2 && $z > 1){ &off_duty_watcher($key1, $previous{$key1}) }
}
}
for(my $i = 0; $i <= $#pagers; $i++){
if($i == 0 && $pagers[$i] == $previous[$i]){ print "$watcher remains the watcher\n"; }
elsif($i >= 1 && $pagers[$i] == $previous[$i]){ print "$lurker remains the lurker\n"; }
else { $sendlist[$i] = $pagers[$i]; }
}
foreach(@sendlist){ print "New recipient: $_\n"; }
}
sub notify_watchers {
my $z = 1;
foreach(@sendlist){
$recipient = $_;
if($z == 1){
# `/usr/nagios/libexec/send_sms.pl "$watcher, je hebt vanavond 1e NagiosWatch, $lurker heeft 2e Watch." $recipient`;
print "$watcher is watcher met $recipient\n";
$z++;
} elsif($z != 1){
# `/usr/nagios/libexec/send_sms.pl "$lurker, je hebt vanavond 2e NagiosWatch, $watcher heeft 1e Watch." $recipient`;
print "$lurker is lurker met $recipient\n";
}
}
}
sub off_duty_watcher {
my $no_watcher = $_[0];
my $no_recipient = $_[1];
if($no_recipient == 0 || $no_watcher eq ''){ die "Too few arguments in subroutine: off_duty_watcher:$!\n"; }
print "no_watcher is $no_watcher en no_recipient is $no_recipient\n";
# `/usr/nagios/libexec/send_sms.pl "$no_watcher, je hebt vanaf vandaag geen NagiosWatch meer!" $no_recipient`;
}
sub notify_nagios {
print "Reloading Nagios ...\n";
system "/etc/init.d/nagios3 reload";
}