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 #
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #
22 #
24 use Net::LDAP;
25 use Getopt::Std;
26 use Net::LDAP::Schema;
27 use Net::LDAP::LDIF;
28 use Data::Dumper;
29 use MIME::Lite;
30 use Sys::Syslog;
31 use Switch;
32 use strict;
34 # Variables a config
36 my $admindef="admin";
38 my $cgi_file="cgi.cfg";
39 my $contacts_file="contacts.cfg";
40 my $contacts_groups_file="contactgroups.cfg";
42 my $TS_FILE='/tmp/gosa_timestamp';
43 my %Options;
44 my $nb_user=0;
45 my $nb_groupe=0;
47 my ($i,$file,$ldap,@nagiosmail,
48 $line,$text,$mesg,$entry,$userlist1,$userlist2,$userlist3,$userlist4,
49 $userlist5,$userlist6,$userlist7,$msg,@groupname,@groupmembers,@contactlias,
50 @groupdescription,@servicenotificationoptions,@servicenotificationperiod,
51 @hostnotificationoptions,@hostnotificationperiod,$stdout,
52 $usercontact,$members,@contactname,@nagiosalias,$j,@entries
53 );
55 # Les parametres de connexion proviennent du fichier smbldap-bind.conf
56 my $gosa_bind_conf="/etc/gosa_bind.conf";
57 my $gosa_ldap_conf="/etc/gosaldap.conf";
58 my %config_bind = &read_conf($gosa_bind_conf);
59 my %config = &read_conf($gosa_ldap_conf);
61 my $peopleou=$config{peopleou};
62 my $groupeou=$config{groupeou};
63 my $base=$config{base};
64 my $scope=$config{scope};# par defaut
65 my $server=$config{server};
67 my $admin=$config_bind{masterDN};
68 my $password=$config_bind{masterPw};
71 $stdout.="\n\nSearch new Nagios attribute in user list\n";
72 $stdout.="-"x55;$stdout.="\n";
73 #my $ts = getTS;
75 # $ldap = &anonBind;
76 # $mesg = $ldap->search(
77 # base => $LDAP_BASE,
78 # filter => "(&(modifyTimestamp>=$ts)(!(objectClass=gosaUserTemplate)))"
79 # );
81 # Put timestamp to file
82 #putTS;
84 # Work if changes is present
85 #if($mesg->count > 0)
86 #{
87 #$stdout.="Processing records modified after $ts\n\n";
88 $ldap = Net::LDAP->new($server);
89 $mesg = $ldap->bind($admin,password=>$password) or syslog('error',$mesg->err) && print $mesg->code && die $mesg->error;
93 #Partie pour l'objectClass NAgios Contact
94 $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosContact))", base=>$peopleou,scope=>$scope);
95 @entries = $mesg->entries;
96 $i=0;
97 foreach $entry (@entries) {
98 $stdout.="\nContact $i : \nName\t\t\t";$contactname[$i]=$entry->get_value('uid');$stdout.=$contactname[$i];
99 $stdout.="\n\n\tmail:\t\t\t\t";$nagiosmail[$i]=$entry->get_value('NagiosMail');$stdout.=$nagiosmail[$i];
100 $stdout.="\n\talias:\t\t\t\t";$nagiosalias[$i]=$entry->get_value('NagiosAlias');$stdout.=$nagiosalias[$i];
101 $stdout.="\n\tHostNotificationPeriod:\t\t";$hostnotificationperiod[$i]=$entry->get_value('HostNotificationPeriod');$stdout.=$hostnotificationperiod[$i];
102 $stdout.="\n\tServiceNotificationPeriod:\t";$servicenotificationperiod[$i]=$entry->get_value('ServiceNotificationPeriod');$stdout.=$servicenotificationperiod[$i];
103 $stdout.="\n\tHostNotificationOptions:\t";$hostnotificationoptions[$i]=$entry->get_value('HostNotificationOptions');$stdout.=$hostnotificationoptions[$i];
104 $stdout.="\n\tServiceNotificationOptions:\t";$servicenotificationoptions[$i]=$entry->get_value('ServiceNotificationOptions');$stdout.=$servicenotificationoptions[$i];
105 $stdout.="\n"." "x15;$stdout.="-"x20;$stdout.=" "x 15;
106 $usercontact.=$entry->get_value('uid')." ,";
107 $i++;
108 }
109 $nb_user=$i;
112 #Partie pour l'objectClass NAgios Group
113 $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosContactGroup))", base=>$groupeou,scope=>$scope);
114 @entries = $mesg->entries;
115 $i=0;
116 foreach $entry (@entries) {
117 $stdout.="\nGroupe $i : \nName\t\t";$groupname[$i]=$entry->get_value('cn');$stdout.=$groupname[$i];
118 $stdout.="\n\n\talias:\t\t";$groupdescription[$i]=$entry->get_value('description');$stdout.=$groupdescription[$i];
119 $stdout.="\n\tmembers:\t";
120 $j=0;
121 foreach $members($entry->get_value('memberUid'))
122 {
123 $stdout.=$members." ";
124 $groupmembers[$i][$j]=$members;
125 $j++;
126 }
127 $stdout.="\n"." "x15;$stdout.="-"x20;$stdout.=" "x 15;
128 $i++;
129 }
130 $nb_groupe=$i;
132 $userlist1.=$admindef;
134 #Partie pour l'objectClass NagiosAuth
135 $stdout.="\n\n\n\n\nAuthorization for the different Information in Nagios\n"."-" x 53;$stdout.="\n";
136 $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosAuth)(AuthorizedSystemInformation~=checked))", base=>$peopleou,scope=>$scope);
137 @entries = $mesg->entries;
138 $stdout.="\nSystem infos :\t\t";
139 foreach $entry (@entries) {
140 $stdout.= $entry->get_value('uid')."\t";
141 $userlist1.=$entry->get_value('uid')." ,";
142 }
143 $userlist1.=$admindef;
145 $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosAuth)(AuthorizedConfigurationInformation~=checked))", base=>$peopleou,scope=>$scope);
146 @entries = $mesg->entries;
147 $stdout.="\nConfiguration infos :\t";
148 foreach $entry (@entries) {
149 $stdout.= $entry->get_value('uid')."\t";
150 $userlist2.=$entry->get_value('uid')." , ";
151 }
152 $userlist2.=$admindef;
154 $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosAuth)(AuthorizedSystemCommands~=checked))", base=>$peopleou,scope=>$scope);
155 @entries = $mesg->entries;
156 $stdout.="\nSystem commands : \t";
157 foreach $entry (@entries) {
158 $stdout.= $entry->get_value('uid')."\t";
159 $userlist3.=$entry->get_value('uid')." , ";
160 }
161 $userlist3.=$admindef;
163 $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosAuth)(AuthorizedAllServices~=checked))", base=>$peopleou,scope=>$scope);
164 @entries = $mesg->entries;
165 $stdout.="\nAll services :\t\t";
166 foreach $entry (@entries) {
167 $stdout.= $entry->get_value('uid')."\t";
168 $userlist4.=$entry->get_value('uid')." ,";
169 }
170 $userlist4.=$admindef;
172 $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosAuth)(AuthorizedAllHosts~=checked))", base=>$peopleou,scope=>$scope);
173 @entries = $mesg->entries;
174 $stdout.="\nAll hosts :\t\t";
175 foreach $entry (@entries) {
176 $stdout.= $entry->get_value('uid')."\t";
177 $userlist5.=$entry->get_value('uid').",";
178 }
179 $userlist5.=$admindef;
182 $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosAuth)(AuthorizedAllServiceCommands~=checked))", base=>$peopleou,scope=>$scope);
183 @entries = $mesg->entries;
184 $stdout.="\nAll services commands :\t";
185 foreach $entry (@entries) {
186 $stdout.= $entry->get_value('uid')."\t";
187 $userlist6.=$entry->get_value('uid').",";
188 }
189 $userlist6.=$admindef;
191 $mesg = $ldap->search(filter=>"(&(objectClass~=nagiosAuth)(AuthorizedAllHostCommands~=checked))",base=>$peopleou,scope=>$scope);
192 @entries = $mesg->entries;
193 $stdout.="\nAll host commands :\t";
194 foreach $entry (@entries) {
195 $stdout.= $entry->get_value('uid')."\t";
196 $userlist7.=$entry->get_value('uid').",";
197 }
198 $userlist7.=$admindef;
201 &modiffile_cgi($cgi_file);
202 &modiffile_contact($contacts_file);
203 &modiffile_group($contacts_groups_file);
205 $ldap->unbind;
206 $stdout.="\n";
207 switch($config{stdout})
208 {
209 case "mail" {&mail()}
210 case "log" {&writelog()}
211 case "normal" {print $stdout}
212 }
213 exit(0);
215 sub modiffile_contact()
216 {
217 $file=$_[0];
218 my $text="";
219 open(FH,"$file") || die "Probleme d'ouverture du fichier $file";
220 $stdout.="\n\n"; $stdout.=" "x10;$stdout.="-"x25;$stdout.=" "x10;
221 $stdout.="\n\n$nb_user utilisateur(s) ajouté(s) dans le fichier $file\n";
222 for($i=0;$i<$nb_user;$i++)
223 {
224 $text.="\n\ndefine contact{\n";
225 $text.="\n\tcontact_name \t\t\t".$contactname[$i];
226 $text.="\n\talias \t\t\t\t".$nagiosalias[$i];
227 $text.="\n\thost_notification_period \t".$hostnotificationperiod[$i];
228 $text.="\n\thost_notification_options \t".$hostnotificationoptions[$i];
229 $text.="\n\tservice_notification_period \t".$servicenotificationperiod[$i];
230 $text.="\n\tservice_notification_options \t".$servicenotificationoptions[$i];
231 $text.="\n\temail \t\t\t\t".$nagiosmail[$i];
232 $text.="\n}\n\n";
233 }
234 close(FH);
235 open(FH,"> $file") || die "Probleme d'ouverture du fichier $file";
236 print FH "$text";
237 close(FH);
239 }
241 sub modiffile_group()
242 {
243 $file=$_[0];
244 $text="";
245 $j=0;
246 $i=0;
247 open(FH,"$file") || die "Probleme d'ouverture du fichier $file";
248 $stdout.="\n\n"; $stdout.=" "x10;$stdout.="-"x25;$stdout.=" "x10;
249 $stdout.="\n\n$nb_groupe groupe(s) ajouté(s) dans le fichier $file\n";
250 for($i=0;$i<$nb_groupe;$i++)
251 {
252 $text.="\n\ndefine contact{\n";
253 $text.="\n\tcontactgroup_name \t".$groupname[$i];
254 $text.="\n\talias \t\t\t".$groupdescription[$i];
255 $text.="\n\tmembers \t\t";
256 while(defined($groupmembers[$i][$j]))
257 {
258 $text.=$groupmembers[$i][$j]." ";
259 $j++;
260 }
261 $text.="\n}\n\n";
262 }
264 close(FH);
265 open(FH,"> $file") || die "Probleme d'ouverture du fichier $file";
266 print FH "$text";
267 close(FH);
269 }
271 sub modiffile_cgi()
272 {
273 $file=$_[0];
274 $text="";
275 open(FH,"$file") || die "Probleme d'ouverture du fichier $file";
276 while(<FH>)
277 {
278 $line=$_;
279 #$stdout.="$line";
280 if($line =~ /^authorized_for_system_information=*/i){$text.="authorized_for_system_information=".$userlist1}
281 elsif($line =~ /^authorized_for_configuration_information=*/i){$text.="authorized_for_configuration_information=".$userlist2}
282 elsif($line =~ /^authorized_for_system_commands=*/i){$text.="authorized_for_system_commands=".$userlist3}
283 elsif($line =~ /^authorized_for_all_services=*/i){$text.="authorized_for_all_services=".$userlist4."\n"}
284 elsif($line =~ /^authorized_for_all_hosts=*/i){$text.="authorized_for_all_hosts=".$userlist5}
285 elsif($line =~ /^authorized_for_all_service_commands=*/i){$text.="authorized_for_all_host_commands=".$userlist6."\n"}
286 elsif($line =~ /^authorized_for_all_host_commands=*/i){$text.="authorized_for_all_service_commands=".$userlist7}
287 else {$text.=$line};
288 }
289 close(FH);
290 open(FH,"> $file") || die "Probleme d'ouverture du fichier $file";
291 print FH "$text";
292 close(FH);
294 }
296 sub read_conf()
297 {
298 my %conf;
299 open (CONFIGFILE, "$_[0]") || die "Unable to open $_[0] for reading !\n";
300 while (<CONFIGFILE>) {
301 chomp($_);
302 ## throw away comments
303 next if ( /^\s*#/ || /^\s*$/ || /^\s*\;/);
304 ## check for a param = value
305 my ($parameter,$value)=read_parameter($_);
306 $value = &subst_configvar($value,\%conf);
307 $conf{$parameter}=$value;
308 }
309 close (CONFIGFILE);
310 return(%conf);
311 }
316 sub read_parameter
317 {
318 my $line=shift;
319 ## check for a param = value
320 if ($_=~/=/) {
321 my ($param,$val);
322 if ($_=~/"/) {
323 #my ($param,$val) = ($_=~/(.*)\s*=\s*"(.*)"/);
324 ($param,$val) = /\s*(.*?)\s*=\s*"(.*)"/;
325 } elsif ($_=~/'/) {
326 ($param,$val) = /\s*(.*?)\s*=\s*'(.*)'/;
327 } else {
328 ($param,$val) = /\s*(.*?)\s*=\s*(.*)/;
329 }
330 return ($param,$val);
331 }
332 }
334 sub subst_configvar
335 {
336 my $value = shift;
337 my $vars = shift;
339 $value =~ s/\$\{([^}]+)\}/$vars->{$1} ? $vars->{$1} : $1/eg;
340 return $value;
341 }
343 sub mail
344 {
346 if($config{email}eq ""){$config{email}="root"}
348 $msg = MIME::Lite->new(
349 From => 'monperl@opensides.be',
350 To => $config{email},
351 Subject => "Plugin Nagios Gosa",
352 Data => $stdout
353 );
356 $msg->send;
357 }
359 sub writelog
360 {
361 open(F, "> $config{logfile}");
362 print F $stdout;
363 close(F);
364 }
366 # Read timestamp
367 sub getTS
368 {
369 open(F, "< $TS_FILE");
370 my $ts = <F>;
371 chop $ts;
372 $ts ||= "19700101000000Z";
373 return $ts;
374 }
376 # save timestamp
377 sub putTS
378 {
379 my $ts = `date -u '+%Y%m%d%H%M%SZ'`;
380 open(F, "> $TS_FILE");
381 $stdout.= F $ts;
382 }
384 #connexion anonyme
385 sub anonBind
386 {
387 my $ldap = Net::LDAP->new( $server);
388 my $mesg = $ldap->bind();
389 $mesg->code && die $mesg->error;
390 return $ldap;
391 }