Having the need to tally daily counts of user services kept in the directory, I whipped up a small script which is run daily by cron. The program runs through all user accounts and extracts the service attribute type from the LDAP directory and writes totals into a MySQL database table.

#!/usr/bin/perl

use strict;
use Net::LDAP;
use DBI;

my $DSN = "DBI:mysql:database=db;host=localhost";

my $dbh = DBI->connect($DSN, 'ldap', 'secret',
                {'RaiseError' => 1, PrintError => 1});

my $BASE = 'ou=People,dc=fupps,dc=com';
my $ld = Net::LDAP->new('ldap.fupps.com', port => 389);
my $rc = $ld->bind('cn=manager,dc=fupps,dc=com', password => 'secret');
$rc->code && die "$0: Can't bind to directory: ", $rc->error ;


(my $sql = <<TOKEN) =~ s/^\s*%%\s?//gm;
        %% INSERT INTO servicecount (service, nusers)
        %%      VALUES (?, ?)
TOKEN

my $sth = $dbh->prepare($sql);

my ($entry, @services, $s);
my ($msg, $e, @entries);

$msg = $ld->search(
        base     => $BASE,
        scope    => 'sub',
        filter   => "(&(objectclass=fuppsPerson)(service=*))",
        attrs    => [ 'service' ],
);
die ("search failed with ",$msg->code(),"\n") if $msg->code();

my %allserv;

@entries = $msg->entries;
foreach $e (@entries) {
        @services = $e->get_value('service');

        map { $allserv{lc $_}++; } @services;

}

foreach $_ (sort keys(%allserv)) {
        $sth->execute($_, $allserv{$_});
}

$sth->finish();
$dbh->disconnect;
exit 0;

This will enable me to later graph the growth of required hardware for given services rendered.

Flattr this
LDAP, MySQL, and Database :: 05 Sep 2006 :: e-mail

Comments

blog comments powered by Disqus