Listing 2 ldap-gather.pl
#!/usr/bin/perl -w
#
# Program: LDAP Statistics Gather <ldap-gather.pl>
#
# Author: Ryan Matteson <matty@daemons.net>
#
# Current Version: 1.0
#
# Last Updated: 11-20-2004
#
# Purpose:
# ldap-gather is a Perl script designed to extract various performance
# metrics from an OpenLDAP server
#
# License:
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Installation:
# Copy the Perl script to a suitable location
#
# Usage (script should be run as a non-privileged user):
# $ ./ldap-gather.pl
# Usage: ldap-gather.pl [ -s server ] [ -p port ] [ -d dir ] [ -h ]
#
# Examples:
# Grab the statistics from ldap.daemons.net, and store them in the
# directory /var/ldap_data
# $ ldap-gather.pl -s ldap.daemons.net -p 389 -d /var/ldap_data
use Net::LDAP;
use Getopt::Std;
use Time::Local;
#######################################
# Global variables get set to 0 here #
#######################################
my $timestamp = time();
my ($ss, $mm, $hr, $md, $mo, $yr, $wd, $yd, $isdst) = localtime(time());
my $year = 2000 + ($yr - 100);
my $filename = "ldapallator-$year-$mo-$md-000";
###################################
# Get the arguments from the user #
###################################
my $server = 0, $port = 0, $dir = 0;
%options=();
getopts("hp:s:d:",\%options);
if ( defined $options{s} ) {
$server = $options{s};
}
if ( defined $options{p} ) {
$port = $options{p};
}
if ( defined $options{d} ) {
$dir = $options{d};
}
if (defined $options{h} ) {
printf("Usage: ldap-gather.pl [ -s server ] [ -p port ] \
[ -d delay ] [ -h ]\n");
exit(1)
}
if ( ( ! $server ) || (! $port ) || (! $dir)) {
printf("Usage: ldap-gather.pl [ -s server ] [ -p port ] [ -d dir ] \
[ -h ]\n");
exit(1);
}
######################################
# Open the file if it doesn't exist #
######################################
my $ldap_file = "$dir/$filename";
if ( -e $ldap_file ) {
open (LDAPFILE, ">>$ldap_file") || \
die "ERROR: Couldn't open $ldap_file in append mode\n";
} else {
open(LDAPFILE, ">$ldap_file") || \
die "ERROR: Couldn't open $ldap_file in write mode\n";
print LDAPFILE ("TIMESTAMP TOTAL_CONNECTIONS BYTES_SENT \
INITIATED_OPERATIONS COMPLETED_OPERATIONS REFERRALS_SENT ENTRIES_SENT \
BIND_OPERATIONS UNBIND_OPERATIONS ADD_OPERATIONS DELETE_OPERATIONS \
MODIFY_OPERATIONS COMPARE_OPERATIONS SEARCH_OPERATIONS WRITE_WAITERS \
READ_WAITERS\n");
}
###################################################
# Create new connection and bind to the server #
###################################################
my $ldap = new Net::LDAP($server, port=> $port) or die($@);
$ldap->bind(
"base" => "",
"version" => 3
) or die($@);
###############################################
# Collect the statistics from the server #
###############################################
my $total_connections = \
getMonitorDesc("cn=Total,cn=Connections,cn=Monitor","monitorCounter");
my $bytes_sent = \
getMonitorDesc("cn=Bytes,cn=Statistics,cn=Monitor","monitorCounter");
my $completed_operations = \
getMonitorDesc("cn=Operations,cn=Monitor","monitorOpCompleted");
my $initiated_operations = \
getMonitorDesc("cn=Operations,cn=Monitor","monitorOpInitiated");
my $referrals_sent = \
getMonitorDesc("cn=Referrals,cn=Statistics,cn=Monitor","monitorCounter");
my $entries_sent = \
getMonitorDesc("cn=Entries,cn=Statistics,cn=Monitor","monitorCounter");
my $bind_operations = \
getMonitorDesc("cn=Bind,cn=Operations,cn=Monitor","monitorOpCompleted");
my $unbind_operations = \
getMonitorDesc("cn=Unbind,cn=Operations,cn=Monitor","monitorOpCompleted");
my $add_operations = \
getMonitorDesc("cn=Add,cn=Operations,cn=Monitor","monitorOpInitiated");
my $delete_operations = \
getMonitorDesc("cn=Delete,cn=Operations,cn=Monitor","monitorOpCompleted");
my $modify_operations = \
getMonitorDesc("cn=Modify,cn=Operations,cn=Monitor","monitorOpCompleted");
my $compare_operations = \
getMonitorDesc("cn=Compare,cn=Operations,cn=Monitor","monitorOpCompleted");
my $search_operations = \
getMonitorDesc("cn=Search,cn=Operations,cn=Monitor","monitorOpCompleted");
my $write_waiters = \
getMonitorDesc("cn=Write,cn=Waiters,cn=Monitor","monitorCounter");
my $read_waiters = \
getMonitorDesc("cn=Read,cn=Waiters,cn=Monitor","monitorCounter");
###############################################
# Print the values to $logfile
###############################################
print (LDAPFILE "$timestamp $total_connections $bytes_sent
$initiated_operations $completed_operations $referrals_sent $entries_sent
$bind_operations $unbind_operations $add_operations $delete_operations
$modify_operations $compare_operations $search_operations $write_waiters
$read_waiters \n");
close(LDAPFILE);
##########################################################################
# getMonitorDesc returns the current value of an attribute. getMonitorDesc
# was developed by Quanah Gibson-Mount
##########################################################################
sub getMonitorDesc {
my $dn = $_[0];
my $attr = $_[1];
if (!$attr) { $attr="description"};
my $searchResults = $ldap->search(base => "$dn",
scope => 'base',
filter => 'objectClass=*',
attrs => ["$attr"],);
my $entry = $searchResults->pop_entry() if $searchResults->count() == 1;
$entry->get_value("$attr");
}
|