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;
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;
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.=$groupename[$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);
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()
225 {
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);
250 }
252 sub modiffile_group()
253 {
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 $text.="\n}\n\n";
278 }
280 close(FH);
281 open(FH,"> $file") || die "Can't open file $file";
282 print FH "$text";
283 close(FH);
285 }
287 sub modiffile_cgi()
288 {
289 $file=$_[0];
290 $text="";
291 open(FH,"$file") || die "Can't open file $file";
292 while(<FH>)
293 {
294 $line=$_;
295 #$stdout.="$line";
296 if($line =~ s/^(authorized_for_system_information=).*$/$1$userlist1/){$text.=$line;}
297 elsif($line =~ s/^(authorized_for_configuration_information=).*$/$1$userlist2/){$text.=$line;}
298 elsif($line =~ s/^(authorized_for_system_commands=).*$/$1$userlist3/){$text.=$line;}
299 elsif($line =~ s/^(authorized_for_all_services=).*$/$1$userlist4/){$text.=$line;}
300 elsif($line =~ s/^(authorized_for_all_hosts=).*$/$1$userlist5/){$text.=$line;}
301 elsif($line =~ s/^(authorized_for_all_service_commands=).*$/$1$userlist6/){$text.=$line;}
302 elsif($line =~ s/^(authorized_for_all_host_commands=).*$/$1$userlist7/){$text.=$line;}
303 else {$text.=$line};
304 }
305 close(FH);
306 open(FH,"> $file") || die "Can't open file $file";
307 print FH "$text";
308 close(FH);
310 }
312 sub read_conf()
313 {
314 my %conf;
315 open (CONFIGFILE, "$_[0]") || die "Can't open $_[0] for reading !\n";
316 while (<CONFIGFILE>) {
317 chomp($_);
318 ## throw away comments
319 next if ( /^\s*#/ || /^\s*$/ || /^\s*\;/);
320 ## check for a param = value
321 my ($parameter,$value)=read_parameter($_);
322 $value = &subst_configvar($value,\%conf);
323 $conf{$parameter}=$value;
324 }
325 close (CONFIGFILE);
326 return(%conf);
327 }
332 sub read_parameter
333 {
334 my $line=shift;
335 ## check for a param = value
336 if ($_=~/=/) {
337 my ($param,$val);
338 if ($_=~/"/) {
339 #my ($param,$val) = ($_=~/(.*)\s*=\s*"(.*)"/);
340 ($param,$val) = /\s*(.*?)\s*=\s*"(.*)"/;
341 } elsif ($_=~/'/) {
342 ($param,$val) = /\s*(.*?)\s*=\s*'(.*)'/;
343 } else {
344 ($param,$val) = /\s*(.*?)\s*=\s*(.*)/;
345 }
346 return ($param,$val);
347 }
348 }
350 sub subst_configvar
351 {
352 my $value = shift;
353 my $vars = shift;
355 $value =~ s/\$\{([^}]+)\}/$vars->{$1} ? $vars->{$1} : $1/eg;
356 return $value;
357 }
359 sub mail
360 {
362 if($config{email}eq ""){$config{email}="root"}
364 $msg = MIME::Lite->new(
365 From => 'monperl@opensides.be',
366 To => $config{email},
367 Subject => "Plugin Nagios Gosa",
368 Data => $stdout
369 );
372 $msg->send;
373 }
375 sub writelog
376 {
377 open(F, "> $config{logfile}");
378 print F $stdout;
379 close(F);
380 }
382 # Read timestamp
383 sub getTS
384 {
385 open(F, "< $TS_FILE");
386 my $ts = <F>;
387 chop $ts;
388 $ts ||= "19700101000000Z";
389 return $ts;
390 }
392 # save timestamp
393 sub putTS
394 {
395 my $ts = `date -u '+%Y%m%d%H%M%SZ'`;
396 open(F, "> $TS_FILE");
397 $stdout.= F $ts;
398 }
400 #connexion anonyme
401 sub anonBind
402 {
403 my $ldap = Net::LDAP->new( $server);
404 my $mesg = $ldap->bind();
405 $mesg->code && die $mesg->error;
406 return $ldap;
407 }