Code

- Updated fro new Debian upload
[gosa.git] / contrib / scripts / goNagiosUser.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) 2007 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 MIME::Lite;
27 use Sys::Syslog;
28 use Switch;
29 use strict;
31 # Default config variables
33 my $nb_user=0;
34 my $nb_group=0;
36 my ($i,$file,$ldap,     $line,$text,$mesg,$entry,$msg,$stdout,$members,$j,@entries,
37         @groupname,@groupmembers,@contactlias,@nagiosmail,
38         @groupdescription,@servicenotificationoptions,@servicenotificationperiod,
39         @hostnotificationoptions,@hostnotificationperiod,@contactname,@nagiosalias,
40         @authorizedsysteminformation,@authorizedconfigurationinformation,
41         @authorizedsystemcommands,@authorizedallservices,
42         @authorizedallhosts,@authorizedallservicecommands,
43         @authorizedallhostcommands
44 );
46 my $error_open = "Problem opening file ";
48 # Where are the config stored
49 my $gosa_bind_conf="ldap2nagios_bind.conf";
50 my $gosa_ldap_conf="ldap2nagios.conf";
52 # Read the config files
53 my %config_bind = &read_conf($gosa_bind_conf);
54 my %config = &read_conf($gosa_ldap_conf);
56 # Parameters in ldap2nagios.conf
57 my $peopleou=$config{peopleou};
58 my $groupeou=$config{groupeou};
59 my $base=$config{base};
60 my $scope=$config{scope};# par defaut
61 my $server=$config{server};
62 my $logs=$config{logs};
63 my $logfile=$config{logfile};
64 my $email=$config{email};
65 my $email_from=$config{email_from};
66 my $nagiosadmin=$config{nagiosadmin};
67 my $cgi_file=$config{cgi_file};
68 my $contacts_file=$config{contacts_file};
69 my $contacts_groups_file=$config{contacts_groups_file};
71 # Parameters in ldap2nagios_bind.conf
72 my $admin=$config_bind{masterDN};
73 my $password=$config_bind{masterPw};
75 my $usersysteminformation = $nagiosadmin.",";
76 my $userconfigurationinformation = $nagiosadmin.",";
77 my $usersystemcommands = $nagiosadmin.",";
78 my $userallservices = $nagiosadmin.",";
79 my $userallhosts = $nagiosadmin.",";
80 my $userallhostscommands = $nagiosadmin.",";
81 my $userallservicescommands = $nagiosadmin.",";
83         $ldap = Net::LDAP->new($server);
84         $mesg = $ldap->bind($admin,password=>$password) or syslog('error',$mesg->err) && print $mesg->code && die $mesg->error;
86         #Searching the objectClass nagiosContact
87         $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosContact))", base=>$peopleou,scope=>$scope);
88         @entries = $mesg->entries;
89         $i=0;
90         foreach $entry (@entries) {
91                 $contactname[$i]=$entry->get_value('uid');
92                 $nagiosmail[$i]=$entry->get_value('NagiosMail');
93                 $nagiosalias[$i]=$entry->get_value('NagiosAlias');
94                 $hostnotificationperiod[$i]=$entry->get_value('HostNotificationPeriod');
95                 $servicenotificationperiod[$i]=$entry->get_value('ServiceNotificationPeriod');
96                 $hostnotificationoptions[$i]=$entry->get_value('HostNotificationOptions');
97                 $servicenotificationoptions[$i]=$entry->get_value('ServiceNotificationOptions');
98                 $authorizedsysteminformation[$i]=$entry->get_value('AuthorizedSystemInformation');
99                 $authorizedconfigurationinformation[$i]=$entry->get_value('AuthorizedConfigurationInformation');
100                 $authorizedsystemcommands[$i]=$entry->get_value('AuthorizedSystemCommands');
101                 $authorizedallservices[$i]=$entry->get_value('AuthorizedAllServices');
102                 $authorizedallhosts[$i]=$entry->get_value('AuthorizedAllHosts');
103                 $authorizedallservicecommands[$i]=$entry->get_value('AuthorizedAllServiceCommands');
104                 $authorizedallhostcommands[$i]=$entry->get_value('AuthorizedAllHostCommands');
105                 
106                 if($authorizedsysteminformation[$i] eq "checked")
107                         {$usersysteminformation.= $contactname[$i]};
108                 
109                 if($authorizedconfigurationinformation[$i] eq "checked")
110                         {$userconfigurationinformation.= $contactname[$i]};
111                 
112                 if($authorizedsystemcommands[$i] eq "checked")
113                         {$usersystemcommands.= $contactname[$i]};
114                                         
115                 if($authorizedallservices[$i] eq "checked")
116                         {$userallservices.= $contactname[$i]};
117                         
118                 if($authorizedallhosts[$i] eq "checked")
119                         {$userallhosts.= $contactname[$i]};
120                         
121                 if($authorizedallservicecommands[$i] eq "checked")
122                         {$userallservicescommands.= $contactname[$i]};
123                         
124                 if($authorizedallhostcommands[$i] eq "checked")
125                         {$userallhostscommands.= $contactname[$i]};
126                         
127                 $i++;
128         }
129         $nb_user=$i;
130                 
131         #Searching the objectClass nagiosContactGroup
132         $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosContactGroup))", base=>$groupeou,scope=>$scope);
133         @entries = $mesg->entries;
134         $i=0;
135         foreach $entry (@entries) {
136                 $groupname[$i]=$entry->get_value('cn');
138                 $groupdescription[$i]=$entry->get_value('description');
140                 if(!$groupdescription[$i]){$groupdescription[$i]=$groupname[$i]};
142                 $j=0;
143                 
144                         foreach $members($entry->get_value('memberUid')) {
145                                 $groupmembers[$i][$j]=$members;
146                                 $j++;
147                         }
148                 $i++;
149         }
150         $nb_group=$i;
152         &modiffile_cgi($cgi_file);
153         &modiffile_contact($contacts_file);
154         &modiffile_group($contacts_groups_file);
155         
156         $ldap->unbind;
157         switch($logs)
158         {
159         case "mail"     {&mail()}
160         case "log"      {&writelog()}
161         case "terminal" {&write_stdout()}
163         }
164         exit(0);
166 sub modiffile_contact()
168         $file=$_[0];
169         my $text="";
170         open(FH,"$file") || die "$error_open $file";
172         for($i=0;$i<$nb_user;$i++)
173         {
174                 $text.="\n\ndefine contact{\n";
175                 $text.="\n\tcontact_name \t\t\t".$contactname[$i];
176                 $text.="\n\talias \t\t\t\t".$nagiosalias[$i];
177                 $text.="\n\thost_notification_period \t".$hostnotificationperiod[$i];
178                 $text.="\n\thost_notification_options \t".$hostnotificationoptions[$i];
179                 $text.="\n\tservice_notification_period \t".$servicenotificationperiod[$i];
180                 $text.="\n\tservice_notification_options \t".$servicenotificationoptions[$i];
181                 $text.="\n\temail \t\t\t\t".$nagiosmail[$i];
182                 $text.="\n}\n\n";
183         }
184         close(FH);
185         open(FH,"> $file") || die "$error_open $file";
186         print  FH "$text";
187         close(FH);
188         
191 sub modiffile_group()
193         $file=$_[0];
194         $text="";
195         $j=0;
196         $i=0;
197         open(FH,"$file") || die "$error_open $file";
199         for($i=0;$i<$nb_group;$i++)
200         {
201                 $text.="\n\ndefine contactgroup{\n";
202                 $text.="\n\tcontactgroup_name \t".$groupname[$i];
203                 $text.="\n\talias \t\t\t".$groupdescription[$i];
204                 $text.="\n\tmembers \t\t";
205                 while(defined($groupmembers[$i][$j]))
206                 {
207                         $text.=$groupmembers[$i][$j]." ";
208                         $j++;
209                 }
210                 $text.="\n}\n\n";
211         }
212         
213         close(FH);
214         open(FH,"> $file") || die "$error_open $file";
215         print FH "$text";
216         close(FH);
217         
220 sub modiffile_cgi()
222         $file=$_[0];
223         $text="";
224         open(FH,"$file") || die "$error_open $file";
225         while(<FH>)
226         {       
227                 $line=$_;
228                 if($line =~ /^authorized_for_system_information=*/i){$text.="authorized_for_system_information=".$usersysteminformation}
229                 elsif($line =~ /^authorized_for_configuration_information=*/i){$text.="authorized_for_configuration_information=".$userconfigurationinformation}
230                 elsif($line =~ /^authorized_for_system_commands=*/i){$text.="authorized_for_system_commands=".$usersystemcommands}
231                 elsif($line =~ /^authorized_for_all_services=*/i){$text.="authorized_for_all_services=".$userallservices."\n"}
232                 elsif($line =~ /^authorized_for_all_hosts=*/i){$text.="authorized_for_all_hosts=".$userallhosts."\n"}
233                 elsif($line =~ /^authorized_for_all_service_commands=*/i){$text.="authorized_for_all_host_commands=".$userallhostscommands."\n"}
234                 elsif($line =~ /^authorized_for_all_host_commands=*/i){$text.="authorized_for_all_service_commands=".$userallservicescommands."\n"}
235                 else {$text.=$line};
236         }
237         close(FH);
238         open(FH,"> $file") || die "$error_open $file";
239         print FH "$text";
240         close(FH);
241         
244 sub read_conf()
246         my %conf;
247         open (CONFIGFILE, "$_[0]") || die "Unable to open $_[0] for reading !\n";
248         while (<CONFIGFILE>) {
249                 chomp($_);
250                 ## throw away comments
251                 next if ( /^\s*#/ || /^\s*$/ || /^\s*\;/);
252                 ## check for a param = value
253                 my ($parameter,$value)=read_parameter($_);
254                 $value = &subst_configvar($value,\%conf);
255                 $conf{$parameter}=$value;
256           }
257         close (CONFIGFILE);
258         return(%conf);
262 sub read_parameter
264         my $line=shift;
265         ## check for a param = value
266         if ($_=~/=/) {
267           my ($param,$val);
268           if ($_=~/"/) {
269                 #my ($param,$val) = ($_=~/(.*)\s*=\s*"(.*)"/);
270                 ($param,$val) = /\s*(.*?)\s*=\s*"(.*)"/;
271           } elsif ($_=~/'/) {
272                 ($param,$val) = /\s*(.*?)\s*=\s*'(.*)'/;
273           } else {
274                 ($param,$val) = /\s*(.*?)\s*=\s*(.*)/;
275           }
276           return ($param,$val);
277         }
280  
281 sub subst_configvar
283         my $value = shift;
284         my $vars = shift;
286         $value =~ s/\$\{([^}]+)\}/$vars->{$1} ? $vars->{$1} : $1/eg;
287         return $value;
291 sub mail
294 if($email eq ""){$email="root"}
296 $msg = MIME::Lite->new(
297              From     => $config{email_from},
298              To       => $config{email},
299              Subject  => "Ldap2nagios Gosa plugin",
300              Data     => $stdout
301              );
304 $msg->send;
308 sub writelog
310         open(F, "> $logfile");
311         print F $stdout;
312         close(F);
315 sub write_stdout
317         $j=0;
319         $stdout.="\n\nSearch for Nagios attribute in user and group list\n";
320         $stdout.="-"x55;$stdout.="\n";
322         for($i=0;$i<$nb_user;$i++) {
323                 $stdout.="\nContact $i : \n\tName\t\t\t $contactname[$i]";
324                 $stdout.="\n\n\tmail:\t\t\t\t $nagiosmail[$i]";
325                 $stdout.="\n\talias:\t\t\t\t $nagiosalias[$i]";
326                 $stdout.="\n\tHostNotificationPeriod:\t\t $hostnotificationperiod[$i]";
327                 $stdout.="\n\tServiceNotificationPeriod:\t $servicenotificationperiod[$i]";
328                 $stdout.="\n\tHostNotificationOptions:\t $hostnotificationoptions[$i]";
329                 $stdout.="\n\tServiceNotificationOptions:\t $servicenotificationoptions[$i]";
330                 $stdout.="\n"." "x15;$stdout.="-"x20;$stdout.=" "x 15;
331         }
332         
333         for($i=0;$i<$nb_group;$i++) {
334                 $stdout.="\nGroupe $i : \n\tName\t\t $groupname[$i]";
335                 $stdout.="\n\talias \t\t $groupdescription[$i]";
336                 $stdout.="\n\tmembers \t";
337                 while(defined($groupmembers[$i][$j]))
338                 {
339                         $stdout.=$groupmembers[$i][$j]. " ";
340                         $j++;
341                 }
342         }
343         
344         $stdout.="\n\n\n\n\nAuthorization for the various Informations in Nagios\n"."-" x 53;$stdout.="\n";
345         
346         $stdout.="\nSystem infos :\t\t $usersysteminformation";
347         $stdout.="\nConfiguration infos :\t $userconfigurationinformation";
348         $stdout.="\nSystem commands : \t $usersystemcommands";
349         $stdout.="\nAll services :\t\t $userallservices";
350         $stdout.="\nAll hosts :\t\t $userallhosts";
351         $stdout.="\nAll services commands :\t $userallservices";
352         $stdout.="\nAll host commands :\t $userallservicescommands";
353         
354         $stdout.="\n\n"; $stdout.=" "x10;$stdout.="-"x25;$stdout.=" "x10;
355         $stdout.="\n$nb_user user(s) added in the file $file\n";
356         $stdout.="\n$nb_group group(s) added in the file $file\n";
357         
358         print $stdout;