#!/usr/local/bin/perl use strict; use Net::LDAP; use Net::LDAP::Util qw(ldap_error_name ldap_error_text); use Getopt::Std; use POSIX qw/strftime/; use Time::Local; my $ldaphost = ''; my $port = ''; my $bindDN = ''; my $password = ''; my @prog = split(/\//,$0); my $prog = pop @prog; my $debug = 0; my ($Adds,$baseDN,$binds,$mods,$searches,$connects,$bytes,$pdus,$entries,$ops,$version); $baseDN="cn=monitor"; our ($opt_A,$opt_b,$opt_B,$opt_C,$opt_d,$opt_D,$opt_E, $opt_h,$opt_M,$opt_O,$opt_P,$opt_p,$opt_S,$opt_Y); if (not getopts('dBb:CD:Eh:OPp:AmMSY')) { usage() } if ($opt_A) { $Adds = "$opt_A" } if ($opt_b) { $baseDN = "$opt_b" } if ($opt_B) { $binds = "$opt_B" } if ($opt_C) { $connects = "$opt_C" } if ($opt_d) { $debug = 1 } if ($opt_D) { $bindDN = "$opt_D" } if ($opt_E) { $entries = "$opt_E" } if ($opt_h) { $ldaphost = "$opt_h" } if ($opt_M) { $mods = "$opt_M" } if ($opt_O) { $ops = "$opt_O" } if ($opt_P) { $pdus = "$opt_P" } if ($opt_p) { $port = "$opt_p" } if ($opt_S) { $searches = "$opt_S" } if ($opt_Y) { $bytes = "$opt_Y" } my $ldap; unless ($ldap = Net::LDAP->new("$ldaphost", port => "$port", version => 3)) { #print "Exit: can't connect to $ldaphost:$port.\n"; # Print out 0 instead of an error message. This allows cricket to correctly # gather statistics when you take a host down for maintenance. print "0\n"; exit 1; } my $bindResult = $ldap->bind(); if ($bindResult->is_error()) { print "I'm sorry, your attempt to authenticate failed.\n", exit 1; } my $startTimeDN = 'cn=Start,cn=Time,cn=Monitor'; # get start time, convert to local time my $searchResults = $ldap->search(base => "$startTimeDN", scope => 'base', filter => 'objectClass=*', attrs => ['+'],); my $entry = $searchResults->pop_entry() if $searchResults->count() == 1; my $startTimeZ = $entry->get_value('createTimestamp'); my ($year,$month,$date,$hour,$min,$sec) = unpack "A4A2A2A2A2A2", $startTimeZ; $year -= 1900; $date =~ s/^0(\d)/$1/; $month =~ s/^0(\d)/$1/; --$month; my $tmU = timegm($sec,$min,$hour,$date,$month,$year); my $startTime = strftime("%c",localtime($tmU)); $version = getServerVersion($baseDN); if ($version == 21) { my $bytesDN = 'cn=Bytes,cn=Statistics,cn=Monitor'; my $pduDN = 'cn=PDU,cn=Statistics,cn=Monitor'; my $entriesDN = 'cn=Entries,cn=Statistics,cn=Monitor'; my $completedOpsDN = 'cn=Completed,cn=Operations,cn=Monitor'; my $completedBindsDN = 'cn=Bind,cn=Completed,cn=Operations,cn=Monitor'; my $completedUnBindsDN = 'cn=Unbind,cn=Completed,cn=Operations,cn=Monitor'; my $completedAddsDN = 'cn=Add,cn=Completed,cn=Operations,cn=Monitor'; my $completedDelsDN = 'cn=Delete,cn=Completed,cn=Operations,cn=Monitor'; my $completedModrdnsDN = 'cn=Modrdn,cn=Completed,cn=Operations,cn=Monitor'; my $completedModsDN = 'cn=Modify,cn=Completed,cn=Operations,cn=Monitor'; my $completedSearchsDN = 'cn=Search,cn=Completed,cn=Operations,cn=Monitor'; my $totalConnectionsDN = 'cn=Total,cn=Connections,cn=Monitor'; my $totalCurrentsDN = 'cn=Current,cn=Connections,cn=Monitor'; print getMonitorDesc("$completedBindsDN"), "\n" if $binds; print getMonitorDesc("$completedSearchsDN"), "\n" if $searches; print getMonitorDesc("$totalConnectionsDN"), "\n" if $connects; print getMonitorDesc("$pduDN"), "\n" if $pdus; print getMonitorDesc("$bytesDN"), "\n" if $bytes; print getMonitorDesc("$entriesDN"), "\n" if $entries; print getMonitorDesc("$completedOpsDN"), "\n" if $ops; print getMonitorDesc("$completedAddsDN"), "\n", if $Adds; print getMonitorDesc("$completedModsDN"), "\n", if $mods; if ($debug == 1) { print "start time: $startTime\n"; print "current connections: ", getMonitorDesc("$totalCurrentsDN"), "\n"; print "total PDUs served: ", getMonitorDesc("$pduDN"), "\n"; print "total completed operations: ", getMonitorDesc("$completedOpsDN"), "\n"; print "total completed unbinds: ", getMonitorDesc("$completedUnBindsDN"), "\n"; print "total completed adds: ", getMonitorDesc("$completedAddsDN"), "\n"; print "total completed deletes: ", getMonitorDesc("$completedDelsDN"), "\n"; print "total completed modrdns: ", getMonitorDesc("$completedModrdnsDN"), "\n"; print "total completed mods: ", getMonitorDesc("$completedModsDN"), "\n"; print "total bytes sent: ", getMonitorDesc("$bytesDN"), "\n"; } } if ($version == 22) { my $bytesDN = 'cn=Bytes,cn=Statistics,cn=Monitor'; my $pduDN = 'cn=PDU,cn=Statistics,cn=Monitor'; my $entriesDN = 'cn=Entries,cn=Statistics,cn=Monitor'; my $completedOpsDN = 'cn=Operations,cn=Monitor'; my $completedBindsDN = 'cn=Bind,cn=Operations,cn=Monitor'; my $completedUnBindsDN = 'cn=Unbind,cn=Operations,cn=Monitor'; my $completedAddsDN = 'cn=Add,cn=Operations,cn=Monitor'; my $completedDelsDN = 'cn=Delete,cn=Operations,cn=Monitor'; my $completedModrdnsDN = 'cn=Modrdn,cn=Operations,cn=Monitor'; my $completedModsDN = 'cn=Modify,cn=Operations,cn=Monitor'; my $completedSearchsDN = 'cn=Search,cn=Operations,cn=Monitor'; my $totalConnectionsDN = 'cn=Total,cn=Connections,cn=Monitor'; my $totalCurrentsDN = 'cn=Current,cn=Connections,cn=Monitor'; print getMonitorDesc("$completedBindsDN", "monitorOpCompleted"), "\n" if $binds; print getMonitorDesc("$completedOpsDN", "monitorOpCompleted"), "\n" if $ops; print getMonitorDesc("$completedSearchsDN", "monitorOpCompleted"), "\n" if $searches; print getMonitorDesc("$totalConnectionsDN", "monitorCounter"), "\n" if $connects; print getMonitorDesc("$pduDN", "monitorCounter"), "\n" if $pdus; print getMonitorDesc("$bytesDN", "monitorCounter"), "\n" if $bytes; print getMonitorDesc("$entriesDN", "monitorCounter"), "\n" if $entries; print getMonitorDesc("$completedAddsDN", "monitorOpCompleted"), "\n", if $Adds; print getMonitorDesc("$completedModsDN", "monitorOpCompleted"), "\n", if $mods; if ($debug == 1) { print "start time: $startTime\n"; print "current connections: ", getMonitorDesc("$totalCurrentsDN", "monitorCounter"), "\n"; print "total PDUs served: ", getMonitorDesc("$pduDN", "monitorCounter"), "\n"; print "total completed unbinds: ", getMonitorDesc("$completedUnBindsDN", "monitorOpCompleted"), "\n"; print "total completed adds: ", getMonitorDesc("$completedAddsDN", "monitorOpCompleted"), "\n"; print "total completed deletes: ", getMonitorDesc("$completedDelsDN", "monitorOpCompleted"), "\n"; print "total completed modrdns: ", getMonitorDesc("$completedModrdnsDN", "monitorOpCompleted"), "\n"; print "total completed mods: ", getMonitorDesc("$completedModsDN", "monitorOpCompleted"), "\n"; print "total bytes sent: ", getMonitorDesc("$bytesDN", "monitorCounter"), "\n"; } } my $mesg = $ldap->unbind(); 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"); } sub getServerVersion() { my $dn = $_[0]; my $ret; my $searchResults = $ldap->search(base => "$dn", scope => 'base', filter => 'cn=monitor', attrs => ['description'],); my $entry = $searchResults->pop_entry() if $searchResults->count() == 1; $ret = $entry->get_value("description"); if ($ret =~ /slapd 2\.1/) { return 21; } else { return 22; } } sub usage { print STDERR "$prog [-b baseDN | -D bindDN | -h host | -p port ]\n", "\t-A: report the number of adds\n", "\t-b: use baseDN for search\n", "\t-B: report the number of binds\n", "\t-C: report the number of connects\n", "\t-d: turn on debugging output\n", "\t-D: bind as bindDN\n", "\t-E: report the number of entries served\n", "\t-h: contact host (default: $ldaphost)\n", "\t-M: report the number of Mods\n", "\t-O: report the number of completed operations\n", "\t-p: use port (default: $port)\n", "\t-P: report the number of outgoing pdus\n", "\t-S: report the number of searches\n", "\t-Y: report the number of bytes\n"; exit 1; }