#!/usr/bin/perl # # Modified 2003-06-25 by Andrew J Cosgriff # to support an "snmpd" mode where it dumps everything at once in a # format readable by net-snmp's snmpd such that it can go into a table. # # Jehan Procaccia's original script available at # http://www.int-evry.fr/mci/user/procacci/Doc/monitor-snmp-ldap-rrd.html#htoc11# # for instructions on using this verison, see # http://polydistortion.net/monkey/archives/2003/06/27/001812.html # # Usage in english ! # no argument -> return result in stdout # 1st arg=0 -> stdout in csv format # 1st arg=1 -> same as 0 with header line describing the fields # arg1=mrtg + arg2=valXXXX where xxxx is in TotConnexions ... cf calls to # &RecupSimple below in the source ! # example ./monitor-jp.pl mrtg valTotConnexions # or for single value mrtg extract (no header): # ./monitor-jp.pl 0 mrtg valTotConnexions # below original french "usage" # monitor.pl # monitor le serveur LDAP local # retourne les infos en stdout # si pas de parametres, on affiche avec baratin # si parametre = 0, les infos sont sorties dans une ligne CSV # si parametre = 1, idem 0 avec une ligne en entete (donne signification des champs) # Pour mrtg -> donner le parametre mrtg suivit d'une valeur a monitorer # valuer dont le nom doit etre du type valTotConnexions ($arg =~ "^val") !. # cf section if($mrtg) 2eme argument de &RecupSimple(); -> aameliorer ... use strict; use Net::LDAP; use Switch; use vars qw( $connLDAP %Result $Discret $AffConn $Entete $mrtg $mrtgval $snmpd); { my ($DNConn, $PassConn, $BaseDN); my ($Serveur, $Port); my ($arg); $Serveur = "localhost"; $Port = 389; $DNConn = ""; $PassConn = ""; $BaseDN = "cn=monitor"; $Discret = 1 if (($ARGV[0] eq "0") ||($ARGV[0] eq "1")); $Entete = 1 if ($ARGV[0] eq "1"); foreach $arg (@ARGV){ if ($arg eq "mrtg"){$mrtg=1;} elsif ($arg eq "snmpd") {$snmpd=1;} elsif ($arg =~ "^val") {$mrtgval=$arg;} } #print $mrtgval; $connLDAP = new Net::LDAP($Serveur) || die "probleme $!\n"; $connLDAP->bind ( dn => $DNConn, password => $PassConn ) || die "probleme $! ",$@; if ($snmpd) { &RecupSimple("cn=Total,cn=connections,cn=monitor", "TotConnexions"); &RecupSimple("cn=Completed,cn=Operations,cn=monitor", "TotOperations"); &RecupSimple("cn=Bytes,cn=Statistics,cn=monitor", "TotBytes"); &RecupSimple("cn=PDU,cn=Statistics,cn=monitor", "TotPDU"); &RecupSimple("cn=Add,cn=Completed,cn=Operations,cn=monitor", "TotAdd"); &RecupSimple("cn=Modify,cn=Completed,cn=Operations,cn=monitor", "TotModify"); &RecupSimple("cn=Search,cn=Completed,cn=Operations,cn=monitor", "TotSearch"); &RecupSimple("cn=Compare,cn=Completed,cn=Operations,cn=monitor", "TotCompare"); &RecupSimple("cn=Delete,cn=Completed,cn=Operations,cn=monitor", "TotDelete"); &RecupSimple("cn=Bind,cn=Completed,cn=Operations,cn=monitor", "TotBind"); &RecupSimple("cn=Unbind,cn=Completed,cn=Operations,cn=monitor", "TotUnbind"); $connLDAP->unbind; exit(); } elsif ($mrtg) { switch ($mrtgval) { case "valTotConnexions" {&RecupSimple("cn=Total,cn=connections,cn=monitor", "TotConnexions")} case "valTotSearch" {&RecupSimple("cn=Search,cn=Completed,cn=Operations,cn=monitor", "TotSearch")} case "valTotAdd" {&RecupSimple("cn=Add,cn=Completed,cn=Operations,cn=monitor", "TotAdd")} case "valTotModify" {&RecupSimple("cn=Modify,cn=Completed,cn=Operations,cn=monitor", "TotModify")} case "valTotBind" {&RecupSimple("cn=Bind,cn=Completed,cn=Operations,cn=monitor", "TotModify")} case "valTotCompare" {&RecupSimple("cn=Compare,cn=Completed,cn=Operations,cn=monitor", "TotModify")} case "valTotUnbind" {&RecupSimple("cn=Unbind,cn=Completed,cn=Operations,cn=monitor", "TotModify")} case "valTotDelete" {&RecupSimple("cn=Unbind,cn=Completed,cn=Operations,cn=monitor", "TotModify")} case "valTotBytes" {&RecupSimple("cn=Bytes,cn=Statistics,cn=monitor", "TotBytes")} case "valTotPDU" {&RecupSimple("cn=PDU,cn=Statistics,cn=monitor", "TotPDU")} case "valTotOperations" {&RecupSimple("cn=Completed,cn=Operations,cn=monitor", "TotOperations")} else { print "entrez apres l'argument mrtg une valeur a monitorer: valTotConnexions, valTotSearch ...\nEnter after the mrtg argument, a value that you want to monitor, value starting with valXXXX, ei valTotConnexions, valTotSearch ...\n"} } $connLDAP->unbind; exit(); } &RecupSimple("cn=Total,cn=connections,cn=monitor", "TotConnexions"); &RecupSimple("cn=Current,cn=connections,cn=monitor", "CurrentConnexions"); &RecupSimple("cn=Threads,cn=Monitor", "NumThreads"); &RecupSimple("cn=Read Waiters,cn=monitor", "ReadWaiters", "="); &RecupSimple("cn=Write Waiters,cn=monitor", "WriteWaiters", "="); &RecupSimple("cn=Bytes,cn=Statistics,cn=monitor", "TotBytes"); &RecupSimple("cn=Entries,cn=Statistics,cn=monitor", "TotEntries"); &RecupSimple("cn=PDU,cn=Statistics,cn=monitor", "TotPDU"); &RecupSimple("cn=Completed,cn=Operations,cn=monitor", "TotOperations"); &RecupSimple("cn=Add,cn=Completed,cn=Operations,cn=monitor", "TotAdd"); &RecupSimple("cn=Bind,cn=Completed,cn=Operations,cn=monitor", "TotBind"); &RecupSimple("cn=Compare,cn=Completed,cn=Operations,cn=monitor", "TotCompare"); &RecupSimple("cn=Delete,cn=Completed,cn=Operations,cn=monitor", "TotDelete"); &RecupSimple("cn=Modify,cn=Completed,cn=Operations,cn=monitor", "TotModify"); &RecupSimple("cn=Search,cn=Completed,cn=Operations,cn=monitor", "TotSearch"); &RecupSimple("cn=Unbind,cn=Completed,cn=Operations,cn=monitor", "TotUnbind"); &RecupAllConn("cn=connections,cn=monitor") if (! $Discret); $connLDAP->unbind; &PrintEntete() if ($Entete); &PrintLigne() if ($Discret); } sub RecupSimple { my($mesg, $entry, $BaseDN, $NomInfo, $Info, $Filtre); $BaseDN = shift; $NomInfo = shift; $Filtre = shift; $mesg = $connLDAP->search ( base => $BaseDN, scope => "base", filter => "(objectclass=*)", attrs => "description" ) or die (" Echec de recherche sur search.$!"); $entry = $mesg->entry(0); $Info = $entry->get_value( 'description' ); if ($Filtre ne "") { $Info =~ /$Filtre(.*)/; $Info = $1; } print "$NomInfo : $Info\n" if (! $Discret); print "$Info\n" if ($mrtg || $snmpd); $Result{$NomInfo} = $Info; } sub RecupAllConn { my($mesg, $entry, $BaseDN, $Info, $LeDN, $LaConn); $BaseDN = shift; print "\n***************** Connexions courantes *************************\n"; $mesg = $connLDAP->search ( base => $BaseDN, scope => "one", filter => "(objectclass=*)", attrs => "description" ) or die (" Echec de recherche sur search.$!"); foreach $entry ($mesg->all_entries){ $LeDN = $entry->get_value( 'dn' ); if ($LeDN =~ /cn=connection (.*),cn=connec/i) { $LaConn = $1; print "$LeDN\n"; $Info = $entry->get_value( 'description'); print "** $Info\n"; } } } sub PrintLigne { my($Heure); $Heure = GetDateHeure(); print "$Heure;$Result{TotConnexions};$Result{CurrentConnexions}"; print ";$Result{TotEntries};$Result{TotBytes};$Result{TotOperations}"; print ";$Result{TotBind};$Result{TotSearch};$Result{TotAdd};$Result{TotDelete}"; print ";$Result{TotModify};$Result{TotCompare}\n"; } sub PrintEntete { print "Date;TotConn;CurrConn;TotEntries;TotBytes;TotOpe;TotBind;TotSearch;TotAdd"; print ";TotDel;TotMod;TotComp\n"; } # ---------------------------------------------------------------------------- # -- Fonction GetDateHeure -- # retourne la date du jour en format JJ/MM/AAAA:HHMM - # ---------------------------------------------------------------------------- sub GetDateHeure { my ($dateE, $an); my ($sec, $min, $heure, $mjour, $mois, $annee, $sjour, $ajour, $isdst) = localtime(time); $mois++; $an = 1900 + $annee; if (length($mois) == 1) {$mois = "0" . $mois; } if (length($mjour) == 1) {$mjour = "0" . $mjour; } if (length($heure) == 1) {$heure = "0" . $heure; } if (length($min) == 1) {$min = "0" . $min; } $dateE = "$mjour/$mois/$an:$heure$min"; return $dateE; }