Code

Updated to 2.6.10
[gosa.git] / gosa-plugins / nagios / contrib / goNagios.pl
1 #!/usr/bin/perl -w
4 # Copyright (C) 2005 Guillaume Delecourt <guillaume.delecourt@opensides.be>
5 # Copyright (C) 2005 Vincent Senave <vincent.senave@opensides.be>
6 # Copyright (C) 2005-2009 Benoit Mortier <benoit.mortier@opensides.be>
7 #
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #
23 #
25 use Net::LDAP;
26 use Getopt::Std;
27 use Net::LDAP::Schema;
28 use Net::LDAP::LDIF;
29 use Data::Dumper;
30 use MIME::Lite;
31 use Sys::Syslog;
32 use Switch; 
33 use strict;
35 # Variables a config
37 my $admindef="admin";
39 my $cgi_file="cgi.cfg";
40 my $contacts_file="contacts.cfg";
41 my $contacts_groups_file="contactgroups.cfg";
43 my $TS_FILE='/tmp/gosa_timestamp';
44 my %Options;
45 my $nb_user=0;
46 my $nb_groupe=0;
48 my ($i,$file,$ldap,@nagiosmail,
49         $line,$text,$mesg,$entry,$userlist1,$userlist2,$userlist3,$userlist4,
50         $userlist5,$userlist6,$userlist7,$msg,@groupname,@groupmembers,@contactlias,
51         @groupdescription,@servicenotificationoptions,@servicenotificationperiod,
52         @hostnotificationoptions,@hostnotificationperiod,$stdout,
53         $usercontact,$members,@contactname,@nagiosalias,$j,@entries
54 );
56 # The connexion parameters are in gosa_bind.conf
57 my $gosa_bind_conf="/etc/gosa/gosa_bind.conf";
58 my $gosa_ldap_conf="/etc/gosa/nagios_ldap.conf";
59 my %config_bind = &read_conf($gosa_bind_conf);
60 my %config = &read_conf($gosa_ldap_conf);
62 my $peopleou=$config{peopleou};
63 my $groupeou=$config{groupeou};
64 my $base=$config{base};
65 my $scope=$config{scope};# par defaut
66 my $server=$config{server};
68 my $admin=$config_bind{masterDN};
69 my $password=$config_bind{masterPw};
72         $stdout.="\n\nSearch new Nagios attribute in user list\n";
73         $stdout.="-"x55;$stdout.="\n";
74         #my $ts = getTS;
76 #       $ldap = &anonBind;
77 #       $mesg = $ldap->search(
78 #       base => $LDAP_BASE,
79 #       filter => "(&(modifyTimestamp>=$ts)(!(objectClass=gosaUserTemplate)))"
80 #       );
82         # Put timestamp to file
83         #putTS;
85         # Work if changes is present
86         #if($mesg->count > 0)
87         #{
88         #$stdout.="Processing records modified after $ts\n\n";
89         $ldap = Net::LDAP->new($server);
90         $mesg = $ldap->bind($admin,password=>$password) or syslog('error',$mesg->err) && print $mesg->code && die $mesg->error;
92         
94         #Part of the ObjectClass NAgios Contact
95         $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosContact))", base=>$peopleou,scope=>$scope);
96         @entries = $mesg->entries;
97         $i=0;
98         foreach $entry (@entries) {
99         $stdout.="\nContact $i : \nName\t\t\t";$contactname[$i]=$entry->get_value('uid');$stdout.=$contactname[$i];
100         $stdout.="\n\n\tmail:\t\t\t\t";$nagiosmail[$i]=$entry->get_value('NagiosMail');$stdout.=$nagiosmail[$i];
101         $stdout.="\n\talias:\t\t\t\t";$nagiosalias[$i]=$entry->get_value('NagiosAlias');$stdout.=$nagiosalias[$i];
102         $stdout.="\n\tHostNotificationPeriod:\t\t";$hostnotificationperiod[$i]=$entry->get_value('HostNotificationPeriod');$stdout.=$hostnotificationperiod[$i];
103         $stdout.="\n\tServiceNotificationPeriod:\t";$servicenotificationperiod[$i]=$entry->get_value('ServiceNotificationPeriod');$stdout.=$servicenotificationperiod[$i];
104         $stdout.="\n\tHostNotificationOptions:\t";$hostnotificationoptions[$i]=$entry->get_value('HostNotificationOptions');$stdout.=$hostnotificationoptions[$i];
105         $stdout.="\n\tServiceNotificationOptions:\t";$servicenotificationoptions[$i]=$entry->get_value('ServiceNotificationOptions');$stdout.=$servicenotificationoptions[$i];
106         $stdout.="\n"." "x15;$stdout.="-"x20;$stdout.=" "x 15;                          
107         $usercontact.=$entry->get_value('uid')."  ,";
108         $i++;
109         }
110         $nb_user=$i;
111                 
112                 
113         #Part of the ObjectClass NAgios Group
114         $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosContactGroup))", base=>$groupeou,scope=>$scope);
115         @entries = $mesg->entries;
116         $i=0;
117         foreach $entry (@entries) {
118         $stdout.="\nGroupe $i : \nName\t\t";$groupname[$i]=$entry->get_value('cn');$stdout.=$groupname[$i];
120         $stdout.="\n\n\talias:\t\t";
121         $groupdescription[$i]=$entry->get_value('description');
123         if(defined($groupdescription[$i])) { 
124                 $stdout.=$groupdescription[$i];
125         } else { 
126                 # We need a valid description entry, so we'll just use the groupname
127                 $stdout.=$groupname[$i];
128         }
130         $stdout.="\n\tmembers:\t";
131         $j=0;
132         foreach $members($entry->get_value('memberUid'))
133         {
134         $stdout.=$members." ";
135         $groupmembers[$i][$j]=$members;
136         $j++;
137         }
138         $stdout.="\n"." "x15;$stdout.="-"x20;$stdout.=" "x 15;                  
139         $i++;
140         }
141         $nb_groupe=$i;
143         #Part of the ObjectClass NagiosAuth
144         $stdout.="\n\n\n\n\nAuthorization for the different Information in Nagios\n"."-" x 53;$stdout.="\n";
145         $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosAuth)(AuthorizedSystemInformation~=checked))", base=>$peopleou,scope=>$scope);
146         @entries = $mesg->entries;
147         $stdout.="\nSystem infos :\t\t";
148         foreach $entry (@entries) {
149         $stdout.= $entry->get_value('uid')."\t";
150         $userlist1.=$entry->get_value('uid').",";
151         }
152         $userlist1.=$admindef;
154         $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosAuth)(AuthorizedConfigurationInformation~=checked))", base=>$peopleou,scope=>$scope);
155         @entries = $mesg->entries;
156         $stdout.="\nConfiguration infos :\t";
157         foreach $entry (@entries) {
158         $stdout.= $entry->get_value('uid')."\t";
159         $userlist2.=$entry->get_value('uid').",";
160         }
161         $userlist2.=$admindef;
163         $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosAuth)(AuthorizedSystemCommands~=checked))", base=>$peopleou,scope=>$scope);
164         @entries = $mesg->entries;
165         $stdout.="\nSystem commands : \t";
166         foreach $entry (@entries) {
167         $stdout.= $entry->get_value('uid')."\t";
168         $userlist3.=$entry->get_value('uid').",";
169         }
170         $userlist3.=$admindef;
172         $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosAuth)(AuthorizedAllServices~=checked))", base=>$peopleou,scope=>$scope);
173         @entries = $mesg->entries;
174         $stdout.="\nAll services :\t\t";
175         foreach $entry (@entries) {
176         $stdout.= $entry->get_value('uid')."\t";
177         $userlist4.=$entry->get_value('uid').",";
178         }
179         $userlist4.=$admindef;
181         $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosAuth)(AuthorizedAllHosts~=checked))", base=>$peopleou,scope=>$scope);
182         @entries = $mesg->entries;
183         $stdout.="\nAll hosts :\t\t";
184         foreach $entry (@entries) {
185         $stdout.= $entry->get_value('uid')."\t";
186         $userlist5.=$entry->get_value('uid').",";
187         }
188         $userlist5.=$admindef;
191         $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosAuth)(AuthorizedAllServiceCommands~=checked))", base=>$peopleou,scope=>$scope);
192         @entries = $mesg->entries;
193         $stdout.="\nAll services commands :\t";
194         foreach $entry (@entries) {
195         $stdout.= $entry->get_value('uid')."\t";
196         $userlist6.=$entry->get_value('uid').",";
197         }
198         $userlist6.=$admindef;
200         $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosAuth)(AuthorizedAllHostCommands~=checked))",base=>$peopleou,scope=>$scope);
201         @entries = $mesg->entries;
202         $stdout.="\nAll host commands :\t";
203         foreach $entry (@entries) {
204         $stdout.= $entry->get_value('uid')."\t";
205         $userlist7.=$entry->get_value('uid').",";
206         }
207         $userlist7.=$admindef;
210         &modiffile_cgi($cgi_file);
211         &modiffile_contact($contacts_file);
212         &modiffile_group($contacts_groups_file);
213         
214         $ldap->unbind;
215         $stdout.="\n";
216         switch($config{stdout})
217         {
218         case "mail"     {&mail()}
219         case "log"      {&writelog()}
220         case "normal"   {print $stdout}
221         }
222         exit(0);
224 sub modiffile_contact()
226         $file=$_[0];
227         my $text="";
228         open(FH,"$file") || die "Can't open file $file";
229         $stdout.="\n\n"; $stdout.=" "x10;$stdout.="-"x25;$stdout.=" "x10;
230         $stdout.="\n\n$nb_user user(s) added in file $file\n";
231         for($i=0;$i<$nb_user;$i++)
232         {
233                 $text.="\n\ndefine contact{\n";
234                 $text.="\n\tcontact_name \t\t\t".$contactname[$i];
235                 $text.="\n\talias \t\t\t\t".$nagiosalias[$i];
236                 $text.="\n\thost_notification_period \t".$hostnotificationperiod[$i];
237                 $text.="\n\thost_notification_options \t".$hostnotificationoptions[$i];
238                 $text.="\n\tservice_notification_period \t".$servicenotificationperiod[$i];
239                 $text.="\n\tservice_notification_options \t".$servicenotificationoptions[$i];
240                 $text.="\n\tservice_notification_commands \t".$config{service_notification_commands};
241                 $text.="\n\thost_notification_commands \t".$config{host_notification_commands};
242                 $text.="\n\temail \t\t\t\t".$nagiosmail[$i];
243                 $text.="\n}\n\n";
244         }
245         close(FH);
246         open(FH,"> $file") || die "Can't open file $file";
247         print  FH "$text";
248         close(FH);
249         
252 sub modiffile_group()
254         $file=$_[0];
255         $text="";
256         $j=0;
257         $i=0;
258         open(FH,"$file") || die "Can't open $file";
259         $stdout.="\n\n"; $stdout.=" "x10;$stdout.="-"x25;$stdout.=" "x10;
260         $stdout.="\n\n$nb_groupe group(s) added in file $file\n";
261         for($i=0;$i<$nb_groupe;$i++)
262         {
263                 $text.="\n\ndefine contactgroup{\n";
264                 $text.="\n\tcontactgroup_name \t".$groupname[$i];
265                 if(defined($groupdescription[$i])) {
266                         $text.="\n\talias \t\t\t".$groupdescription[$i];
267                 } else { 
268                         # We need a valid alias entry, so we'll just use the groupname
269                         $text.="\n\talias \t\t\t".$groupname[$i]; 
270                 }
271                 $text.="\n\tmembers \t\t";
272                 while(defined($groupmembers[$i][$j]))
273                 {
274                         $text.=$groupmembers[$i][$j];
275                         $j++;
276                         
277                         if(defined($groupmembers[$i][$j])) { 
278                                 $text.=","; 
279                         }
280                 }
281                 $text.="\n}\n\n";
282         }
283         
284         close(FH);
285         open(FH,"> $file") || die "Can't open file $file";
286         print FH "$text";
287         close(FH);
288         
291 sub modiffile_cgi()
293         $file=$_[0];
294         $text="";
295         open(FH,"$file") || die "Can't open file $file";
296         while(<FH>)
297         {       
298                 $line=$_;
299                 #$stdout.="$line";
300                 if($line =~ s/^(authorized_for_system_information=).*$/$1$userlist1/){$text.=$line;}
301                 elsif($line =~ s/^(authorized_for_configuration_information=).*$/$1$userlist2/){$text.=$line;}
302                 elsif($line =~ s/^(authorized_for_system_commands=).*$/$1$userlist3/){$text.=$line;}
303                 elsif($line =~ s/^(authorized_for_all_services=).*$/$1$userlist4/){$text.=$line;}
304                 elsif($line =~ s/^(authorized_for_all_hosts=).*$/$1$userlist5/){$text.=$line;}
305                 elsif($line =~ s/^(authorized_for_all_service_commands=).*$/$1$userlist6/){$text.=$line;}
306                 elsif($line =~ s/^(authorized_for_all_host_commands=).*$/$1$userlist7/){$text.=$line;}
307                 else {$text.=$line};
308         }
309         close(FH);
310         open(FH,"> $file") || die "Can't open file $file";
311         print FH "$text";
312         close(FH);
313         
316 sub read_conf()
318         my %conf;
319         open (CONFIGFILE, "$_[0]") || die "Can't open $_[0] for reading !\n";
320         while (<CONFIGFILE>) {
321                 chomp($_);
322                 ## throw away comments
323                 next if ( /^\s*#/ || /^\s*$/ || /^\s*\;/);
324                 ## check for a param = value
325                 my ($parameter,$value)=read_parameter($_);
326                 $value = &subst_configvar($value,\%conf);
327                 $conf{$parameter}=$value;
328           }
329         close (CONFIGFILE);
330         return(%conf);
336 sub read_parameter
338         my $line=shift;
339         ## check for a param = value
340         if ($_=~/=/) {
341           my ($param,$val);
342           if ($_=~/"/) {
343                 #my ($param,$val) = ($_=~/(.*)\s*=\s*"(.*)"/);
344                 ($param,$val) = /\s*(.*?)\s*=\s*"(.*)"/;
345           } elsif ($_=~/'/) {
346                 ($param,$val) = /\s*(.*?)\s*=\s*'(.*)'/;
347           } else {
348                 ($param,$val) = /\s*(.*?)\s*=\s*(.*)/;
349           }
350           return ($param,$val);
351         }
354 sub subst_configvar
356         my $value = shift;
357         my $vars = shift;
359         $value =~ s/\$\{([^}]+)\}/$vars->{$1} ? $vars->{$1} : $1/eg;
360         return $value;
363 sub mail
366 if($config{email}eq ""){$config{email}="root"}
368 $msg = MIME::Lite->new(
369              From     => 'monperl@opensides.be',
370              To       => $config{email},
371              Subject  => "Plugin Nagios Gosa",
372              Data     => $stdout
373              );
376 $msg->send;
379 sub writelog
381         open(F, "> $config{logfile}");
382         print F $stdout;
383         close(F);
386 # Read timestamp
387 sub getTS
389         open(F, "< $TS_FILE");
390         my $ts = <F>;
391         chop $ts;
392         $ts ||= "19700101000000Z";
393         return $ts;
396 # save timestamp
397 sub putTS
399         my $ts = `date -u '+%Y%m%d%H%M%SZ'`;
400         open(F, "> $TS_FILE");
401         $stdout.= F $ts;
404 #connexion anonyme
405 sub anonBind
407         my $ldap = Net::LDAP->new( $server);
408         my $mesg = $ldap->bind();
409         $mesg->code && die $mesg->error;
410         return $ldap;