Nagios LDAP Contact Creator Script
By Jesse Morgan
The Following is an ugly little perl script I’ve whipped up for generating contact entries for Nagios; each file is read via a cfg_dir command in the nagios.cfg. This script is set to run nightly, capturing any new users and updating users and groups automagically.
Let me know if you find it useful.
<pre lang="perl">
#!/usr/bin/perl
# read your ldap repo and generation nagios config files for contacts
# and contact_groups
use strict;
use Net::LDAP;
use Data::Dumper;
# Set all your custom configs here:
my $ldaphostname='ldap.example.int';
my $groupdn='ou=People,ou=Groups,dc=example,dc=int';
my $peopledn='ou=People,ou=active,ou=Users,dc=example,dc=int';
my $contactdir='/etc/nagios/contacts';
my $ldap = new Net::LDAP ($ldaphostname);
$ldap->bind() || die "failed to bind";
#search for groups
my $mesg = $ldap->search(
base => $groupdn,
filter => "(objectclass=posixgroup)",
attrs => ['cn','memberuid'],
);
#loop through each entry
while (my $group= $mesg->pop_entry() ){
#set the name of the group
my $cn=$group->get_value('cn');
#create a comma-separated list of group members
my $members=$group->get_value('memberuid', asref => 1);
$members= join( ',', @$members);
# create our configuration block
my $contactinfo="
# We only have one contact in this simple configuration file,
# so there is no need to create more than one contact group.
define contactgroup{
contactgroup_name $cn
alias $cn
members $members
}\n";
# write this block to a file named after the group cn
print `echo "$contactinfo" > $contactdir/group_$cn.cfg`;
}
#search for users
my $mesg = $ldap->search(
base => $peopledn,
filter => "(objectclass=person)",
attrs => ['uid','cn','mail','description'],
);
#loop through each entry
while (my $user= $mesg->pop_entry() ){
my ($pager,$addr);
# grab the four fields we're interested in
my $uid=$user->get_value('uid');
my $cn=$user->get_value('cn');
my $mail=$user->get_value('mail');
my $desc=$user->get_value('description');
# Note that I store the user's SMS address and Instant Messenger
# addresses in the description field in the following format:
# SMS:1234445555@some.url;
# IM:morgajel@yahoo,morgajel@gtalk;
# for those interested, you can send email to an sms address
# with help from <a href="http://sms411.net/how-to-send-email-to-a-phone">http://sms411.net/how-to-send-email-to-a-phone</a>
#strip pager info out of desc and format it
#(note it's in sms format):
if ($desc =~/SMS:([^;]+);?/){
$pager= " pager $1\n";
}
# strip addresses out of desc and format them:
if ($desc =~/IM:([^;]+);?/){
my @multiple_addr=split(/,/,$1);
my $count=1;
# break each address out into an addressX line
# (for informational purposes);
foreach my $addr (@multiple_addr){
$addr=$addr." address$count $addr\n";
$count=$count+1 ;
}
}
#generate the first half of the contact info
my $contactinfo="
define contact{
contact_name $uid ; Short name of user
use generic-contact
alias $cn ; Full name of user
email $mail
$pager
$addr
}";
# write this block to a file named after the user uid
`echo "$contactinfo" > $contactdir/$uid.cfg`;
}