Code

- added config files for the goNagios.pl script
[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];
119         $stdout.="\n\n\talias:\t\t";$groupdescription[$i]=$entry->get_value('description');$stdout.=$groupdescription[$i];
120         $stdout.="\n\tmembers:\t";
121         $j=0;
122         foreach $members($entry->get_value('memberUid'))
123         {
124         $stdout.=$members." ";
125         $groupmembers[$i][$j]=$members;
126         $j++;
127         }
128         $stdout.="\n"." "x15;$stdout.="-"x20;$stdout.=" "x 15;                  
129         $i++;
130         }
131         $nb_groupe=$i;
133         #Part of the ObjectClass NagiosAuth
134         $stdout.="\n\n\n\n\nAuthorization for the different Information in Nagios\n"."-" x 53;$stdout.="\n";
135         $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosAuth)(AuthorizedSystemInformation~=checked))", base=>$peopleou,scope=>$scope);
136         @entries = $mesg->entries;
137         $stdout.="\nSystem infos :\t\t";
138         foreach $entry (@entries) {
139         $stdout.= $entry->get_value('uid')."\t";
140         $userlist1.=$entry->get_value('uid').",";
141         }
142         $userlist1.=$admindef;
144         $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosAuth)(AuthorizedConfigurationInformation~=checked))", base=>$peopleou,scope=>$scope);
145         @entries = $mesg->entries;
146         $stdout.="\nConfiguration infos :\t";
147         foreach $entry (@entries) {
148         $stdout.= $entry->get_value('uid')."\t";
149         $userlist2.=$entry->get_value('uid').",";
150         }
151         $userlist2.=$admindef;
153         $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosAuth)(AuthorizedSystemCommands~=checked))", base=>$peopleou,scope=>$scope);
154         @entries = $mesg->entries;
155         $stdout.="\nSystem commands : \t";
156         foreach $entry (@entries) {
157         $stdout.= $entry->get_value('uid')."\t";
158         $userlist3.=$entry->get_value('uid').",";
159         }
160         $userlist3.=$admindef;
162         $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosAuth)(AuthorizedAllServices~=checked))", base=>$peopleou,scope=>$scope);
163         @entries = $mesg->entries;
164         $stdout.="\nAll services :\t\t";
165         foreach $entry (@entries) {
166         $stdout.= $entry->get_value('uid')."\t";
167         $userlist4.=$entry->get_value('uid').",";
168         }
169         $userlist4.=$admindef;
171         $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosAuth)(AuthorizedAllHosts~=checked))", base=>$peopleou,scope=>$scope);
172         @entries = $mesg->entries;
173         $stdout.="\nAll hosts :\t\t";
174         foreach $entry (@entries) {
175         $stdout.= $entry->get_value('uid')."\t";
176         $userlist5.=$entry->get_value('uid').",";
177         }
178         $userlist5.=$admindef;
181         $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosAuth)(AuthorizedAllServiceCommands~=checked))", base=>$peopleou,scope=>$scope);
182         @entries = $mesg->entries;
183         $stdout.="\nAll services commands :\t";
184         foreach $entry (@entries) {
185         $stdout.= $entry->get_value('uid')."\t";
186         $userlist6.=$entry->get_value('uid').",";
187         }
188         $userlist6.=$admindef;
190         $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosAuth)(AuthorizedAllHostCommands~=checked))",base=>$peopleou,scope=>$scope);
191         @entries = $mesg->entries;
192         $stdout.="\nAll host commands :\t";
193         foreach $entry (@entries) {
194         $stdout.= $entry->get_value('uid')."\t";
195         $userlist7.=$entry->get_value('uid').",";
196         }
197         $userlist7.=$admindef;
200         &modiffile_cgi($cgi_file);
201         &modiffile_contact($contacts_file);
202         &modiffile_group($contacts_groups_file);
203         
204         $ldap->unbind;
205         $stdout.="\n";
206         switch($config{stdout})
207         {
208         case "mail"     {&mail()}
209         case "log"      {&writelog()}
210         case "normal"   {print $stdout}
211         }
212         exit(0);
214 sub modiffile_contact()
216         $file=$_[0];
217         my $text="";
218         open(FH,"$file") || die "Can't open file $file";
219         $stdout.="\n\n"; $stdout.=" "x10;$stdout.="-"x25;$stdout.=" "x10;
220         $stdout.="\n\n$nb_user user(s) added in file $file\n";
221         for($i=0;$i<$nb_user;$i++)
222         {
223                 $text.="\n\ndefine contact{\n";
224                 $text.="\n\tcontact_name \t\t\t".$contactname[$i];
225                 $text.="\n\talias \t\t\t\t".$nagiosalias[$i];
226                 $text.="\n\thost_notification_period \t".$hostnotificationperiod[$i];
227                 $text.="\n\thost_notification_options \t".$hostnotificationoptions[$i];
228                 $text.="\n\tservice_notification_period \t".$servicenotificationperiod[$i];
229                 $text.="\n\tservice_notification_options \t".$servicenotificationoptions[$i];
230                 $text.="\n\tservice_notification_commands \t".$config{service_notification_commands};
231                 $text.="\n\thost_notification_commands \t".$config{host_notification_commands};
232                 $text.="\n\temail \t\t\t\t".$nagiosmail[$i];
233                 $text.="\n}\n\n";
234         }
235         close(FH);
236         open(FH,"> $file") || die "Can't open file $file";
237         print  FH "$text";
238         close(FH);
239         
242 sub modiffile_group()
244         $file=$_[0];
245         $text="";
246         $j=0;
247         $i=0;
248         open(FH,"$file") || die "Can't open $file";
249         $stdout.="\n\n"; $stdout.=" "x10;$stdout.="-"x25;$stdout.=" "x10;
250         $stdout.="\n\n$nb_groupe group(s) added in file $file\n";
251         for($i=0;$i<$nb_groupe;$i++)
252         {
253                 $text.="\n\ndefine contact{\n";
254                 $text.="\n\tcontactgroup_name \t".$groupname[$i];
255                 $text.="\n\talias \t\t\t".$groupdescription[$i];
256                 $text.="\n\tmembers \t\t";
257                 while(defined($groupmembers[$i][$j]))
258                 {
259                         $text.=$groupmembers[$i][$j]." ";
260                         $j++;
261                 }
262                 $text.="\n}\n\n";
263         }
264         
265         close(FH);
266         open(FH,"> $file") || die "Can't open file $file";
267         print FH "$text";
268         close(FH);
269         
272 sub modiffile_cgi()
274         $file=$_[0];
275         $text="";
276         open(FH,"$file") || die "Can't open file $file";
277         while(<FH>)
278         {       
279                 $line=$_;
280                 #$stdout.="$line";
281                 if($line =~ s/^(authorized_for_system_information=).*$/$1$userlist1/){$text.=$line;}
282                 elsif($line =~ s/^(authorized_for_configuration_information=).*$/$1$userlist2/){$text.=$line;}
283                 elsif($line =~ s/^(authorized_for_system_commands=).*$/$1$userlist3/){$text.=$line;}
284                 elsif($line =~ s/^(authorized_for_all_services=).*$/$1$userlist4/){$text.=$line;}
285                 elsif($line =~ s/^(authorized_for_all_hosts=).*$/$1$userlist5/){$text.=$line;}
286                 elsif($line =~ s/^(authorized_for_all_service_commands=).*$/$1$userlist6/){$text.=$line;}
287                 elsif($line =~ s/^(authorized_for_all_host_commands=).*$/$1$userlist7/){$text.=$line;}
288                 else {$text.=$line};
289         }
290         close(FH);
291         open(FH,"> $file") || die "Can't open file $file";
292         print FH "$text";
293         close(FH);
294         
297 sub read_conf()
299         my %conf;
300         open (CONFIGFILE, "$_[0]") || die "Can't open $_[0] for reading !\n";
301         while (<CONFIGFILE>) {
302                 chomp($_);
303                 ## throw away comments
304                 next if ( /^\s*#/ || /^\s*$/ || /^\s*\;/);
305                 ## check for a param = value
306                 my ($parameter,$value)=read_parameter($_);
307                 $value = &subst_configvar($value,\%conf);
308                 $conf{$parameter}=$value;
309           }
310         close (CONFIGFILE);
311         return(%conf);
317 sub read_parameter
319         my $line=shift;
320         ## check for a param = value
321         if ($_=~/=/) {
322           my ($param,$val);
323           if ($_=~/"/) {
324                 #my ($param,$val) = ($_=~/(.*)\s*=\s*"(.*)"/);
325                 ($param,$val) = /\s*(.*?)\s*=\s*"(.*)"/;
326           } elsif ($_=~/'/) {
327                 ($param,$val) = /\s*(.*?)\s*=\s*'(.*)'/;
328           } else {
329                 ($param,$val) = /\s*(.*?)\s*=\s*(.*)/;
330           }
331           return ($param,$val);
332         }
335 sub subst_configvar
337         my $value = shift;
338         my $vars = shift;
340         $value =~ s/\$\{([^}]+)\}/$vars->{$1} ? $vars->{$1} : $1/eg;
341         return $value;
344 sub mail
347 if($config{email}eq ""){$config{email}="root"}
349 $msg = MIME::Lite->new(
350              From     => 'monperl@opensides.be',
351              To       => $config{email},
352              Subject  => "Plugin Nagios Gosa",
353              Data     => $stdout
354              );
357 $msg->send;
360 sub writelog
362         open(F, "> $config{logfile}");
363         print F $stdout;
364         close(F);
367 # Read timestamp
368 sub getTS
370         open(F, "< $TS_FILE");
371         my $ts = <F>;
372         chop $ts;
373         $ts ||= "19700101000000Z";
374         return $ts;
377 # save timestamp
378 sub putTS
380         my $ts = `date -u '+%Y%m%d%H%M%SZ'`;
381         open(F, "> $TS_FILE");
382         $stdout.= F $ts;
385 #connexion anonyme
386 sub anonBind
388         my $ldap = Net::LDAP->new( $server);
389         my $mesg = $ldap->bind();
390         $mesg->code && die $mesg->error;
391         return $ldap;