-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopenldap_operations.pl
135 lines (117 loc) · 4.31 KB
/
openldap_operations.pl
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#! /usr/bin/perl -w
#====================================================================
# What's this ?
#====================================================================
# Script designed for cacti [ http://www.cacti.net ]
# Gets operations from the OpenLDAP (2.2.x and further) monitor branch
# You have to configure OpenLDAP with --enable-monitor and
# set database monitor in slapd.conf in order to use this script
#
# Copyright (C) 2009 Clement OUDOT
# Copyright (C) 2009 LTB-project.org
#
# 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
# of the License, 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, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#====================================================================
#====================================================================
# Modules
#====================================================================
use strict;
use Net::LDAP;
use Getopt::Std;
#====================================================================
# Configuration
#====================================================================
# Command line parameters
my ( $host, $port, $binddn, $bindpw, $timeout, $ldap_version ) = &options;
# Name of the Operations branch in monitor
my $branch = "cn=Operations,cn=Monitor";
#====================================================================
# options() subroutine
#====================================================================
sub options {
# Init Options Hash Table
my %opts;
getopt( 'hpDWtv', \%opts );
&usage unless exists $opts{"h"};
$opts{"p"} = 389 unless exists $opts{"p"};
$opts{"t"} = 5 unless exists $opts{"t"};
$opts{"v"} = 3 unless exists $opts{"v"};
return ( $opts{"h"}, $opts{"p"}, $opts{"D"}, $opts{"W"}, $opts{"t"},
$opts{"v"} );
}
#====================================================================
# usage() subroutine
#====================================================================
sub usage {
print STDERR
"Usage: $0 -h host [-p port] [-D binddn -W bindpw] [-t timeout] [-v ldap_version]\n";
print STDERR "Default values are :\n";
print STDERR
"port: 389\nbinddn/bindpw: without (anonymous connection)\ntimeout: 5\nldap_version: 3\n";
exit 1;
}
#====================================================================
# Connection to OpenLDAP monitor
#====================================================================
# Create LDAP connection
my $ldap = Net::LDAP->new(
$host,
port => $port,
version => $ldap_version,
timeout => $timeout
) or die "Unable to connect to $host on port $port\n";
# Bind (anonymous or no)
my $bind;
if ( $binddn && $bindpw ) {
$bind = $ldap->bind( $binddn, password => $bindpw );
}
else {
$bind = $ldap->bind;
}
if ( $bind->code ) {
print STDERR "Bind : " . $bind->error . "\n";
exit 1;
}
# Search
my $search = $ldap->search(
base => $branch,
scope => 'one',
filter => 'objectClass=*',
attrs => [ 'monitorOpInitiated', 'monitorOpCompleted', 'cn' ]
);
if ( $search->code ) {
print STDERR "Search : " . $search->error . "\n";
$ldap->unbind;
exit 1;
}
# Unbind
$ldap->unbind;
#====================================================================
# Parse results
#====================================================================
foreach ( $search->entries ) {
my $cn = lc( $_->get_value('cn') );
my $initiated = $_->get_value('monitorOpInitiated');
my $completed = $_->get_value('monitorOpCompleted');
$initiated = "U" unless defined $initiated;
$completed = "U" unless defined $completed;
print "$cn-initiated:$initiated ";
print "$cn-completed:$completed ";
}
print "\n";
#====================================================================
# Exit
#====================================================================
exit 0;