-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldapgraph.pl
453 lines (372 loc) · 14.6 KB
/
ldapgraph.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#!/usr/bin/perl -aw
#### This Program retrives information from the OpenLDAP Monitor backend and places them in rrd files.
#### The tool ldapgraph.cgi can be used to present graphs in a webpage - however it is only provided as an example and not that much maintained.
# Parts of this code is based on the mailgraph rrd frontend by David Schweikert <dws@ee.ethz.ch>
# and on the ldapmon code posted to the openldap mailinglist by Unknown <UNKNOWN>
#
# Note: I can't figure out who posted the original code, if you know please let me know so i can give credits where they are due.
# Version 0.2a - Changed to daemon mode and integrated the ldapmon tool into the main daemon
# Version 0.1 - Initial Ldapgraph implementation - should be run from cron
use strict;
use Net::LDAP;
use Net::LDAP::Util qw(ldap_error_name ldap_error_text);
use POSIX;
use Time::Local;
use RRDs;
use Getopt::Long;
use Config::General;
# Version info
my $version= '0.2a';
# Production or Development?
my $production = 1;
#######################################
# MONITOR VARIABLES #
#######################################
# Lets get the program name
my @prog = split(/\//,$0);
my $prog = pop @prog;
# Variable for the LDAP Connection handle
my $ldap;
# Monitor dn - replace with config reading sometime
my $baseDN = "cn=Monitor";
# Basic search spaces - replace with configuration reading at some point
my $statsDN = "cn=Statistics,$baseDN";
my $operationsDN = "cn=Operations,$baseDN";
my $connectionsDN = "cn=Connections,$baseDN";
# Search spaces for interessting counters - replace with config reading at some point
my $bytesDN = "cn=Bytes,$statsDN";
my $pduDN = "cn=PDU,$statsDN";
my $entriesDN = "cn=Entries,$statsDN";
my $completedBindsDN = "cn=Bind,$operationsDN";
my $completedUnBindsDN = "cn=Unbind,$operationsDN";
my $completedAddsDN = "cn=Add,cn=Operations,cn=Monitor";
my $completedDelsDN = "cn=Delete,$operationsDN";
my $completedModrdnsDN = "cn=Modrdn,$operationsDN";
my $completedModsDN = "cn=Modify,$operationsDN";
my $completedSearchsDN = "cn=Search,$operationsDN";
my $totalConnectionsDN = "cn=Total,$connectionsDN";
my $currentConnectionsDN = "cn=Current,$connectionsDN";
my $readWaitersDN = "cn=Read,cn=Waiters,cn=Monitor";
##########################################
# RRDRELATED VARIABLES #
##########################################
# rrd File name
my $rrdfile;
# rrd stepping
my $rrdstep = 300;
my $xpoints = 540;
my $points_per_sample = 3;
# Simple time variables
my $year;
my $minute;
#######################################
# GENERAL VARIABLES #
#######################################
# Locations
my $daemon_logfile = '';
my $daemon_rrd_dir = '';
my $config_file = '';
if($production)
{
$daemon_logfile = '/var/log/ldapgraph.log';
$daemon_rrd_dir = '/var/lib/ldapgraph/';
$config_file = '/var/lib/ldapgraph/ldapgraph.conf'
}
# Hash map for commandline options
my %opt = ();
# Signals to Trap and Handle
$SIG{'INT' } = 'interrupt';
$SIG{'HUP' } = 'interrupt_hup';
$SIG{'ABRT'} = 'interrupt';
$SIG{'QUIT'} = 'interrupt';
$SIG{'TRAP'} = 'interrupt';
$SIG{'STOP'} = 'interrupt';
$SIG{'TERM'} = 'interrupt';
#####################################
# READ CONFIGURATIONS #
#####################################
my %config;
sub readconfig
{
my $conf = new Config::General($config_file);
%config = $conf->getall;
}
# Interrupt: Extremely simple interrupt handler
sub interrupt()
{
print "caught @_ exiting\n";
exit;
}
# Handler for the HUP signal
sub interrupt_hup()
{
print "Caught @_ signal reloading config\n";
readconfig;
}
# Help: Information about how to call this daemon
sub help(){
print "Help not written, but options are version, help, debug, daemonized and verbose \n";
exit;
}
# OptionHandling: Function for handling command line options
sub optionHandling()
{
Getopt::Long::Configure('no_ignore_case');
GetOptions(\%opt, 'help|h', 'version|V', 'debug', 'daemonized|d', 'verbose|v') or exit(1);
# Display help if option line help is used
help() if $opt{help};
# Display version if option version is used
if ($opt{version}) {
print "ldapgraph $version by esben\@ofn.dk";
exit;
}
}
# Daemonize: Daemonizes the application
sub daemonize {
chdir($daemon_rrd_dir) or die "Can't chdir to $daemon_rrd_dir: $!";
open STDIN, '>>/dev/null' or die "Can't read /dev/null: $!";
open STDOUT, '>>' .$daemon_logfile or die "Can't write to /dev/null: $!";
open STDERR, '>>' .$daemon_logfile or die "Can't write to /dev/null: $!";
defined(my $pid = fork) or die "Can't fork: $!";
exit if $pid;
setsid or die "Can't start a new session: $!";
umask 0;
}
# getMonitorDesc: Retrives the information from ldap datatree
sub getMonitorDesc
{
my $dn = $_[0];
my $attr = $_[1];
if (!$attr) { $attr = "description"};
print "$dn\n" if $opt{debug};
print "$attr\n" if $opt{debug};
my $searchResults = $ldap->search(base => "$dn",
scope => 'base',
filter => 'objectClass=*',
attrs => ["$attr"],);
my $entry = $searchResults->pop_entry() if $searchResults->count() == 1;
return $entry->get_value("$attr");
}
# ldapmonitoring: Retrives values from the ldap monitor
sub ldapmonitoring
{
my $serverNumber = shift;
my $host = $config{'server'}[$serverNumber];
my $port = $config{'port'}[$serverNumber];
my $bindPW = $config{'bindpw'}[$serverNumber];
my $bindDN = $config{'binddn'}[$serverNumber];
print $host . "\n" if $opt{debug};
print $bindPW . "\n" if $opt{debug};
print $bindDN . "\n" if $opt{debug};
if ($ldap = Net::LDAP->new("$host", port=> "$port", version =>3, timeout => 10)){
print "Succesfully created socket to host: $host\n" if $opt{debug};
}
else
{
print "Cannot create socket to $host\n";
return 0;
}
# Attempt to connect to the ldap host.
my $bindResult;
if (defined $bindDN and defined $bindPW)
{
$bindResult = $ldap->bind($bindDN, password => $bindPW);
print "Succefully bound using $bindDN and $bindPW\n" if $opt{debug};
}
else
{
$bindResult = $ldap->bind();
}
if ($bindResult->is_error()) {
print
"Authentication failed.\n",
return 0;
}
# Hash map for the values read from the monitor backend.
my %monValues = (
bytes => '',
read => '',
operations => '',
entries => '',
totalcon => '',
curcon => '',
pdu => '',
unbind => '',
bind => '',
adds => '',
deletes => '',
modrdns => '',
mods => '',
searches => '',
bytes => ''
);
if ($opt{debug}) {
print "Binds: " . getMonitorDesc("$completedBindsDN", "monitorOpCompleted"), "\n";
print "UnBinds: " . getMonitorDesc("$completedUnBindsDN", "monitorOpCompleted"), "\n";
print "Completed Operations: " . getMonitorDesc("$operationsDN", "monitorOpCompleted"), "\n";
print "Searches " . getMonitorDesc("$completedSearchsDN", "monitorOpCompleted"), "\n";
print "Total connections: " . getMonitorDesc("$totalConnectionsDN", "monitorCounter"), "\n";
print "Outgoing PDUs: " . getMonitorDesc("$pduDN", "monitorCounter"), "\n";
print "Entries: " . getMonitorDesc("$entriesDN", "monitorCounter"), "\n";
print "Adds: " . getMonitorDesc("$completedAddsDN", "monitorOpCompleted"), "\n";
print "Deletes: " . getMonitorDesc("$completedDelsDN", "monitorOpCompleted"), "\n";
print "Mods: " . getMonitorDesc("$completedModsDN", "monitorOpCompleted"), "\n";
print "Modrdns: " . getMonitorDesc("$completedModrdnsDN", "monitorOpCompleted"), "\n";
print "Current connections: " . getMonitorDesc("$currentConnectionsDN", "monitorCounter"), "\n";
print "Bytes handled: " . getMonitorDesc("$bytesDN", "monitorCounter")."\n";
print "Read-Wait: " . getMonitorDesc("$readWaitersDN", "monitorCounter")."\n";
}
$monValues{'bind'} = getMonitorDesc("$completedBindsDN", "monitorOpCompleted");
$monValues{'unbind'} = getMonitorDesc("$completedUnBindsDN", "monitorOpCompleted");
$monValues{'operations'} = getMonitorDesc("$operationsDN", "monitorOpCompleted");
$monValues{'searches'} = getMonitorDesc("$completedSearchsDN", "monitorOpCompleted");
$monValues{'totalcon'} = getMonitorDesc("$totalConnectionsDN", "monitorCounter");
$monValues{'pdu'} = getMonitorDesc("$pduDN", "monitorCounter");
$monValues{'bytes'} = getMonitorDesc("$bytesDN", "monitorCounter");
$monValues{'entries'} = getMonitorDesc("$entriesDN", "monitorCounter");
$monValues{'read'} = getMonitorDesc("$readWaitersDN", "monitorCounter");
$monValues{'adds'} = getMonitorDesc("$completedAddsDN", "monitorOpCompleted");
$monValues{'mods'} = getMonitorDesc("$completedModsDN", "monitorOpCompleted");
$monValues{'modrdns'} = getMonitorDesc("$completedModrdnsDN", "monitorOpCompleted");
$monValues{'curcon'} = getMonitorDesc("$currentConnectionsDN", "monitorCounter");
$monValues{'deletes'} = getMonitorDesc("$completedDelsDN", "monitorOpCompleted");
$bindResult = $ldap->unbind;
if ($bindResult->is_error()){
print "An error occured while unbinding: $_\n";
}
$ldap->disconnect();
return %monValues;
}
sub setupRRD($)
{
my $start_time = shift;
my $rows = $xpoints/$points_per_sample;
my $realrows = int($rows*1.1); # ensure that the full range is covered
my $day_steps = int(3600*24 / ($rrdstep*$rows));
# use multiples, otherwise rrdtool could choose the wrong RRA
my $week_steps = $day_steps*7;
my $month_steps = $week_steps*5; #Not strictly correct - 5 week summizes, will be broken after many years, not really a problem if we only want one years history
my $year_steps = $month_steps*12;
my $heartbeat = $rrdstep*2;
# rrd definition
if(! -f $rrdfile) {
RRDs::create($rrdfile, '--start', $start_time, '--step', $rrdstep,
'DS:curcon:GAUGE:'.($heartbeat).':0:U',
'DS:totalcon:DERIVE:'.($heartbeat).':0:U',
'DS:pdus:DERIVE:'.($heartbeat).':0:U',
'DS:binds:DERIVE:'.($heartbeat).':0:U',
'DS:unbinds:DERIVE:'.($heartbeat).':0:U',
'DS:adds:DERIVE:'.($heartbeat).':0:U',
'DS:deletes:DERIVE:'.($heartbeat).':0:U',
'DS:modrdns:DERIVE:'.($heartbeat).':0:U',
'DS:mods:DERIVE:'.($heartbeat).':0:U',
'DS:searches:DERIVE:'.($heartbeat).':0:U',
'DS:bytes:DERIVE:'.($heartbeat).':0:U',
'DS:entries:DERIVE:'.($heartbeat).':0:U',
'DS:operations:DERIVE:'.($heartbeat).':0:U',
'DS:read:GAUGE:'.($heartbeat).':0:U',
"RRA:AVERAGE:0.5:$day_steps:$realrows", # day
"RRA:AVERAGE:0.5:$week_steps:$realrows", # week
"RRA:AVERAGE:0.5:$month_steps:$realrows", # month
"RRA:AVERAGE:0.5:$year_steps:$realrows", # year
"RRA:MAX:0.5:$day_steps:$realrows", # day
"RRA:MAX:0.5:$week_steps:$realrows", # week
"RRA:MAX:0.5:$month_steps:$realrows", # month
"RRA:MAX:0.5:$year_steps:$realrows", # year
);
$minute = $start_time;
my $rrd_error = RRDs::error;
die "Error while creating $rrdfile: $rrd_error\n" if $rrd_error;
}
elsif(-f $rrdfile) {
$minute = RRDs::last($rrdfile) + $rrdstep;
my $rrd_error=RRDs::error;
die "ERROR while updating $rrdfile : $rrd_error\n" if $rrd_error;
}
}
sub update
{
my ($t,$rrdfile,%monValues) = @_;
my $m = $t - $t%$rrdstep;
if (defined $monValues{'bind'} )
{
print "update $minute:$monValues{curcon}:$monValues{totalcon}:$monValues{pdu}:$monValues{bind}:$monValues{unbind}:$monValues{adds}:$monValues{deletes}:$monValues{modrdns}:$monValues{mods}:$monValues{searches}:$monValues{bytes}:$monValues{entries}:$monValues{operations}:$monValues{read}\n" if $opt{verbose};
RRDs::update $rrdfile, "$minute:$monValues{curcon}:$monValues{totalcon}:$monValues{pdu}:$monValues{bind}:$monValues{unbind}:$monValues{adds}:$monValues{deletes}:$monValues{modrdns}:$monValues{mods}:$monValues{searches}:$monValues{bytes}:$monValues{entries}:$monValues{operations}:$monValues{read}";
my $rrd_error=RRDs::error;
print "ERROR while updating $rrdfile: $rrd_error\n" if $rrd_error;
}
if($m > $minute + $rrdstep or not defined $monValues{'bind'}) {
for(my $sm = $minute + $rrdstep;$sm < $m;$sm += $rrdstep) {
print "update $sm:0:0:0:0:0:0:0:0:0:0:0:0:0:0 (SKIP)\n" if $opt{verbose};
RRDs::update $rrdfile, "$sm:0:0:0:0:0:0:0:0:0:0:0:0:0:0";
my $rrd_error=RRDs::error;
die "ERROR while updating $rrdfile: $rrd_error\n" if $rrd_error;
}
}
$minute = $m;
return 1;
}
sub main
{
# Start by handling options and figure out if we need to quit or not, daemonize or whatever.
optionHandling();
chdir $daemon_rrd_dir or die "mailgraph: can't chdir to $daemon_rrd_dir: $!";
-w $daemon_rrd_dir or die "mailgraph: can't write to $daemon_rrd_dir\n";
#Should the program be daemonized or should we just report to STDOUT?
if( $opt{daemonized} )
{
&daemonize;
}
# Read the configuration file for host options
readconfig;
# Get the number of defined servers
# Ugly i know, but i don't know a better way!
my $counter = 0;
my $tmp = $config{'server'};
my @tmparray = @$tmp;
for ($counter = 0; $counter <= scalar(@tmparray)-1; $counter++) {
#Make sure rrd file exist otherwise create it
$rrdfile = $config{'server'}[$counter];
if(! -f "$daemon_rrd_dir/$rrdfile") {
setupRRD(time());
}
else {
$minute = RRDs::last($rrdfile) + $rrdstep;
my $rrd_error=RRDs::error;
die "ERROR while updating $daemon_rrd_dir/$rrdfile: $rrd_error\n" if $rrd_error;
}
}
my %monValues;
# All parts of the monitor logic is done inside this while loop
while (1) {
for ($counter = 0; $counter <= scalar(@tmparray)-1; $counter++) {
print "Counter: " . $counter . "\n";
# Actual monitoring
## If we have values from ldap for the particular server then store them in the hash, and write them to the rrd file
## using output of time() as timestamp
if (%monValues = &ldapmonitoring($counter)) {
if ( $opt{debug} ) {
while ( my ($key, $value) = each(%monValues) ) {
print "$key => $value\n";
}
}
update(time(),$config{'server'}[$counter],%monValues);
}
}
# Sleep for 300 Seconds - Not a good schedule, but works perfectly for a small number of servers. And it is needed as we would otherwise use a lot of cpu cycles and filling the rrd log
sleep(300);
}
}
&main;
__END__
=head1 NAME
ldapgraph version 0.2a
=head1 DESCRIPTION
This script retrives values from the ldap monitor backend, and places them in an RRD file.
This rrd file can be used to graph performance for the ldap daemon.
=head1 COPYRIGHT
Copyright (c) 2007 by Esben Bach. All rights reserved.
=head1 LICENSE
=head1 AUTHOR
S<Esben Bach E<lt>esben@ofn.dkE<gt>>
=cut