diaries, triumphs, failures, and rants

pageicon Thursday May 01, 2008

ds_monitor.pl

Here's ds_monitor.pl. This little Perl script prints out some interesting Directory Server operational statistics. I'll try to keep an updated version at my tools.

#! /usr/bin/perl -w

#
# terry.gardner@sun.com
#
# 20-JUL-2005
# 20-SEP-2005
# 21-SEP-2005
# 08-FEB-2008
#

# Parameter and variables

my ( $lines, $linesPerPage, $loadPerConnection );

#
# set up some default values
#
$port      = 10389;
$root      = "/ldap";
$instance  = "instance-name";
$delay     = 1;
$hostname  = "localhost";
$pidFile   = $root . "/logs/pid";

#
# parameters that cannot be modified by
# command line options
#
$searchBase= "cn=monitor";

sub displayUsageHints() {
  print "ds_monitor.pl prints a tabular display of some directory server\n";
  print "        operational information.\n";
  print "\n";
  print "Usage:\n";
  print "ds_monitor.pl [OPTIONS]\n";
  print "\n";
  print "[--hostname hostname]            LDAP server hostname, default: $hostname\n";
  print "[--port port]                    LDAP server port, default: $port\n";
  print "[--instance instanceName]        LDAP server instance, default: $instance\n";
  print "[--delay delayBetweenIterations] delay between samples, default: $delay\n";
  print "[--pidFile file ]                file containing the PID of the running directory server, default: $pidFile\n";
  print "[--help]                         display ds_monitor.pl command invocation hints\n";
}

#
# process command line options
#
$sn = 0;
for (@ARGV) {
  if (/--port/) {
    $port = $ARGV[++$sn];
    ++$sn;
  } elsif (/--hostname/) {
    $hostname = $ARGV[++$sn];
    ++$sn;
  } elsif (/--root/) {
    $root = $ARGV[++$sn];    
    ++$sn;
  } elsif (/--instance/) {
    $instance = $ARGV[++$sn];
    ++$sn;
  } elsif (/--delay/) {
    $delay = $ARGV[++$sn];
    ++$sn;
  } elsif (/--pidFile/) {
    $pidFile = $ARGV[++$sn];
    ++$sn;
  } elsif (/--help/) {
    &displayUsageHints;
    exit;
  }
}

#
# check to see if the $pidFile can be opened
#
open(pidFile,$pidFile) or die "cannot open $pidFile: $!";
close(pidFile);

#
# read directory server pid, used
# later for process size
#
$pid = `cat $pidFile`; chomp $pid;

$scratchFile = "/tmp/" . "ds_monitor" . `date +%H%m%S`;

$lines = 0;
$linesPerPage = 30;

$lastCurrentConnections = 0;
$currentConnections = 0;

for (;;) {

  `ldapsearch -s base -h $hostname -p $port -b $searchBase '(objectClass=*)' > $scratchFile`;
  $size = `ps -p $pid -o vsz | tail -1`; chomp $size;

  $uptime = `uptime`;
  ($junk,
   $junk,
   $junk,
   $junk,
   $junk,
   $junk,
   $junk,
   $junk,
   $junk,
   $cpu) = split(" ",$uptime,9999);

  $cpu =~ s/,//g;

  open(scratchFile,$scratchFile) || die "Failed to open $scratchFile: $!";

  while ()
  {
    chomp();

    s/://;

    ($attribute,$value) = split(" ",$_,2);

    if (/currentconnections/) {
      $lastCurrentConnections = $currentConnections;
      $currentConnections = $value;
    } elsif (/totalconnections/) {
      $totalConnections = $value;
    } elsif (/readwaiters/) {
      $readWaiters = $value;
    } elsif (/request-que-backlog/) {
      $requestQueueBacklog = $value;
    } elsif (/connectionpeak/) {
      $connectionPeak = $value;
    }

    $attribute = "";
  }


  close(scratchFile);
  
  # Get the current time
  ($sec,$min,$hour,$junk,$junk,$junk,$junk,$junk,$junk) = localtime(time);

  $diffConnections = $currentConnections - $lastCurrentConnections;

  printf "%9.9s %2.2d:%2.2d:%2.2d size:%d la:%.2f tc:%d peak:%d cc:%d (%d) rq:%d rw:%d\n",
       $hostname,
       $hour,
       $min,
       $sec,
       $size,
       $cpu,
       $totalConnections,
       $connectionPeak,
       $currentConnections,
       $diffConnections,
       $requestQueueBacklog,
       $readWaiters;

  sleep $delay;

}


Technorati Tags: , , , , ,

Comments:

very interesting, thanks for sharing.
I'll take it for a spin on 6 and 7 sometime this week.

Posted by arnaud on October 12, 2009 at 01:35 PM EDT #

Post a Comment:
  • HTML Syntax: NOT allowed

« December 2009
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
       
Today

Feeds

Search this blog

Links

Weblog menu

Today's referrers

Today's Page Hits: 269