-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldapgraphd
executable file
·569 lines (464 loc) · 15.5 KB
/
ldapgraphd
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#!/usr/bin/perl
# ldapgraph -- An rrdtool-based graphing tool for LDAP server statistics
# copyright (c) 2006-2011 Chris St. Pierre <[email protected]>
# based on mailgraph copyright (c) 2000-2005 David Schweikert <[email protected]>
# released under the GNU General Public License
use strict;
use warnings;
use RRDs;
use File::Tail;
use Getopt::Long;
use POSIX 'setsid';
use Parse::389Log;
use Parse::OpenLDAPLog;
use Pod::Usage;
my $VERSION = "1.1.3";
# config
my $rrdstep = 60;
my $xpoints = 540;
my $points_per_sample = 3;
# global variables
my $this_minute;
my %sum = ( add => 0, srch => 0, bind => 0, mod => 0, del => 0, ext => 0,
cmp => 0, modrdn => 0,
connxn => 0, ssl => 0, tls => 0, sasl => 0, unsasl => 0);
my $rrd_inited=0;
my %opt = ();
Getopt::Long::Configure('no_ignore_case');
GetOptions(\%opt,
'help|h', 'logfile|l=s', 'year|y=i', 'daemon|d!', 'pidfile=s',
'rrddir=s', 'instance|I=s', '389') or exit(1);
pod2usage(0) if $opt{'help'};
my $pidfile = $opt{'pidfile'} || '/var/run/ldapgraph.pid';
my $rrd_dir = $opt{'rrddir'} || '/var/lib/ldapgraph';
my $ops_rrd = "$rrd_dir/fds_ops.rrd";
my $connxn_rrd = "$rrd_dir/fds_connxn.rrd";
die "ldapgraphd: can't write to $rrd_dir\n" unless -w $rrd_dir;
daemonize() if $opt{'daemon'};
my ($logfile, $logtype);
if ($opt{'logfile'}) {
$logfile = $opt{'logfile'};
if ($opt{'389'}) {
$logtype = '389';
} else {
$logtype = 'openldap';
}
} else { # no logfile specified -- use the instance to figure it out
$logtype = '389';
my ($ds_version, $instance);
if ($opt{'instance'}) {
$instance = $opt{'instance'};
if (-d "/var/log/dirsrv/slapd-$instance" &&
-r "/var/log/dirsrv/slapd-$instance") {
$ds_version = "1.1";
} elsif (-d "/opt/fedora-ds/slapd-$instance" &&
-r "/opt/fedora-ds/slapd-$instance") {
$ds_version = "1.0";
} else {
die "Could not read or locate logs in either " .
"/var/log/dirsrv/slapd-$instance or " .
"/opt/fedora-ds/slapd-$instance\n";
}
} else { # no instance specified on the command line
# try to determine the instance
if (-d "/opt/fedora-ds" && -r "/opt/fedora-ds") {
$ds_version = "1.0";
} elsif (-d "/var/log/dirsrv" && -r "/var/log/dirsrv") {
$ds_version = "1.1";
} else {
die "No installation of 389 DS found\n";
}
my $dsroot;
if ($ds_version eq '1.0') {
$dsroot = "/opt/fedora-ds";
} elsif ($ds_version eq '1.1') {
$dsroot = "/var/log/dirsrv";
}
opendir(DIR, $dsroot); # made sure it was readable earlier
my $file;
my @instances;
while (defined($file = readdir(DIR))) {
push(@instances, $file) if $file =~ /^slapd-/;
}
closedir(DIR);
if (scalar(@instances) == 1) {
$instance = shift(@instances);
$instance =~ s/^slapd-//;
} elsif (scalar(@instances) == 0) {
die "No instance ($dsroot/slapd-*) found in $dsroot\n";
} else { # scalar(@instances) > 1
warn "More than one instance of 389 DS found in $dsroot\n";
die "You must specify an instance on the command line with -I\n";
}
}
if ($ds_version eq '1.0') {
$logfile = "/opt/fedora-ds/slapd-$instance/logs/access";
} elsif ($ds_version eq '1.1') {
$logfile = "/var/log/dirsrv/slapd-$instance/access";
}
}
my $file = File::Tail->new(name => $logfile, tail => -1);
my $parser;
if ($logtype eq '389') {
$parser = new Parse::389Log($file,
year => $opt{'year'},
arrayref => 1);
} else {
$parser = new Parse::OpenLDAPLog($file,
year => $opt{'year'},
arrayref => 1);
}
while(my $line = $parser->nextline()) {
process_line($line);
}
##################################################
sub daemonize {
open STDIN, '<', '/dev/null'
or die "ldapgraphd: can't read /dev/null: $!";
open STDOUT, '>', '/dev/null'
or die "ldapgraphd: can't write to /dev/null: $!";
defined(my $pid = fork) or die "ldapgraphd: can't fork: $!";
if ($pid) {
# parent
open my $PIDFILE, ">", $pidfile
or die "ldapgraphd: can't write to $pidfile: $!\n";
print $PIDFILE "$pid\n";
close($PIDFILE);
exit;
}
# child
setsid or die "ldapgraphd: can't start a new session: $!";
open STDERR, '>', '&STDOUT' or die "ldapgraphd: can't dup stdout: $!";
return;
}
sub init_rrd {
my $m = 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;
my $year_steps = $month_steps * 12;
if(! -f $ops_rrd) {
RRDs::create($ops_rrd, '--start', $m, '--step', $rrdstep,
'DS:add:GAUGE:' . ($rrdstep * 2) . ':0:U',
'DS:srch:GAUGE:' . ($rrdstep * 2) . ':0:U',
'DS:bind:GAUGE:' . ($rrdstep * 2) . ':0:U',
'DS:mod:GAUGE:' . ($rrdstep * 2) . ':0:U',
'DS:del:GAUGE:' . ($rrdstep * 2) . ':0:U',
'DS:ext:GAUGE:' . ($rrdstep * 2) . ':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
);
my $err = RRDs::error;
warn "create of $ops_rrd failed: $err\n" if $err;
$this_minute = $m;
} elsif (-f $ops_rrd) {
# CMP and MODRDN op tracking added in v1.0.3;
# upgrade old DBs if necessary
foreach my $op (qw(cmp modrdn)) {
chomp(my $has_op = `rrdtool info $ops_rrd | grep -F 'ds[$op]' | wc -l`);
if (!$has_op) {
add_ds($ops_rrd, $op, 'GAUGE', $rrdstep * 2, 0, 'U');
}
}
$this_minute = RRDs::last($ops_rrd) + $rrdstep;
}
if (!-f $connxn_rrd) {
RRDs::create($connxn_rrd, '--start', $m, '--step', $rrdstep,
'DS:connxn:GAUGE:' . ($rrdstep * 2) . ':0:U',
'DS:ssl:GAUGE:' . ($rrdstep * 2) . ':0:U',
'DS:tls:GAUGE:' . ($rrdstep * 2) . ':0:U',
'DS:sasl:GAUGE:' . ($rrdstep * 2) . ':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
);
my $err = RRDs::error;
warn "create of $connxn_rrd failed: $err\n" if $err;
$this_minute = $m;
} elsif (-f $connxn_rrd) {
# TLS tracking added in v0.3, SASL bind tracking added in v1.0.2
# upgrade old DBs if necessary
foreach my $conn (qw(tls sasl)) {
chomp(my $has_conn = `rrdtool info $connxn_rrd | grep -F 'ds[$conn]' | wc -l`);
if (!$has_conn) {
add_ds($connxn_rrd, $conn, 'GAUGE', $rrdstep * 2, 0, 'U');
}
}
$this_minute = RRDs::last($connxn_rrd) + $rrdstep;
}
$rrd_inited = 1;
return;
}
# much code for add_ds taken from add_ds.pl,
# Copyright (C) 2000 Selena M. Brewington, covered under the GPL
sub add_ds {
my ($rrd, $ds, $type, $heartbeat, $llim, $ulim) = @_;
if ($llim eq 'U') {
$llim = 'NaN';
}
if ($ulim eq 'U') {
$ulim = 'NaN';
}
# we export the RRD to XML, munge the XML, then re-import it
# first, BACK UP THE RRD!
system("cp $rrd $rrd.bak");
my $cdp_prep_end = '</cdp_prep>';
my $row_end = '</row>';
my $name = '<name>';
my $name_end = '</name>';
my $field = '<v> NaN </v>';
my $found_ds = 0;
my $num_sources = 0;
my $last;
my $fields = " ";
my $datasource;
# export to XML
system("rrdtool dump $rrd $rrd.xml");
# input XML
open(my $IN, '<', "$rrd.xml") or do {
warn "Couldn't open $rrd.xml for read: $!\n";
warn "Conversion of RRD was not successful\n";
return;
};
# open file to write munged XML to
open(my $OUT, '>', "$rrd-fixed.xml") or do {
warn "Couldn't open $rrd-fixed.xml for write: $!\n";
warn "Conversion of RRD was not successful\n";
return;
};
while (<$IN>) {
if (($_ =~ s/$row_end$/$fields$row_end/) && $found_ds) {
# need to hit <ds> types first, if we don't, we're screwed
print $OUT;
} elsif (/$cdp_prep_end/) {
print $OUT "\t\t\t<ds>\n";
print $OUT "\t\t\t<primary_value> NaN </primary_value>\n";
print $OUT "\t\t\t<secondary_value> NaN </secondary_value>\n";
print $OUT "\t\t\t<value> 0.0000000000e+00 </value>\n";
print $OUT "\t\t\t<unknown_datapoints> 0 </unknown_datapoints>\n";
print $OUT "\t\t\t</ds>\n";
print $OUT;
} elsif (/$name_end$/) {
($datasource) = /$name (\w+)/;
$found_ds++;
print $OUT;
} elsif (/Round Robin Archives/) {
# print out additional datasource definitions
($num_sources) = ($datasource =~ /(\d+)/);
for (my $x = $num_sources; $x < $num_sources + 1; $x++) {
$fields .= $field;
print $OUT "\n\t<ds>\n";
print $OUT "\t\t<name> $ds <\/name>\n";
print $OUT "\t\t<type> $type <\/type>\n";
print $OUT "\t\t<minimal_heartbeat> $heartbeat <\/minimal_heartbeat>\n";
print $OUT "\t\t<min> $llim <\/min>\n";
print $OUT "\t\t<max> $ulim <\/max>\n\n";
print $OUT "\t\t<!-- PDP Status-->\n";
print $OUT "\t\t<last_ds> 0 <\/last_ds>\n";
print $OUT "\t\t<value> 0.0000000000e+00 <\/value>\n";
print $OUT "\t\t<unknown_sec> 0 <\/unknown_sec>\n";
print $OUT "\t<\/ds>\n\n";
}
print $OUT;
} else {
print $OUT;
}
$last = $_;
}
close($OUT);
close($IN);
# import from fixed XML
unlink($rrd);
system("rrdtool restore $rrd-fixed.xml $rrd");
return;
}
sub process_line {
my $sl = shift;
my $time = $sl->[0];
my $op = $sl->[1];
my $text = $sl->[2];
if ($op) {
if ($op =~ /^ADD|SRCH|MOD|DEL|CMP|MODRDN$/) {
event($time, lc($op));
} elsif ($op eq 'BIND') {
event($time, 'bind');
event($time, 'sasl') if $text =~ /^method=sasl/;
} elsif ($op eq 'RESULT' && $text =~ /^err=14 tag=97/) {
# SASL bind in progress
event($time, 'unsasl');
} elsif ($op eq 'EXT') {
event($time, 'ext');
event($time, 'tls')
if $text =~ /oid="1\.3\.6\.1\.4\.1\.1466\.20037"/;
} elsif ($op eq 'SSL' && $text =~ /^connection from/) {
event($time, 'ssl');
} elsif ($op eq 'ACCEPT') {
if ($text =~ /:636\)/) {
# OpenLDAP doesn't clearly flag SSL connections, so we
# have to guess based on port number
event($time, 'ssl');
} else {
event($time, 'connxn');
}
}
} elsif ($text =~ /^connection from/) {
# 389 doesn't flag connections with an operation
event($time, 'connxn');
}
return;
}
sub event {
my ($t, $type) = @_;
update($t) and $sum{$type}++;
return;
}
# returns 1 if $sum should be updated
sub update {
my $t = shift;
my $m = $t - $t % $rrdstep;
init_rrd($m) unless $rrd_inited;
return 1 if $m == $this_minute;
return 0 if $m < $this_minute;
RRDs::update($ops_rrd,
$this_minute . ':' .
$sum{'add'} . ':' .
$sum{'srch'} . ':' .
$sum{'bind'} . ':' .
$sum{'mod'} . ':' .
$sum{'del'} . ':' .
$sum{'ext'} . ':' .
$sum{'cmp'} . ':' .
$sum{'modrdn'});
my $err = RRDs::error;
warn "update of $ops_rrd failed: $err\n" if $err;
# since TLS connections are initialized with the StartTLS EXT operation,
# and SASL connections are initialized at BIND time, they get counted
# as _both_ plaintext _and_ TLS or SASL. (E.g., one count for the initial
# connection, which is plaintext at that time, and one count later when
# the connection is secured.) Although the code here counts TLS and
# SASL connections twice -- thus inflating the 'connxn' counter -- that
# inflation is undone when the graph is drawn.
#
# this should be corrected, but tuning the data in an RRD like that would
# be difficult. As it stands, existing 389 DS Graph users already have
# lots of data in this format, so we're stuck with this for a while at
# least.
RRDs::update($connxn_rrd,
$this_minute . ':' .
$sum{'connxn'} . ':' .
$sum{'ssl'} . ':' .
$sum{'tls'} . ':' .
($sum{'sasl'} - $sum{'unsasl'}));
$err = RRDs::error;
warn "update of $connxn_rrd failed: $err\n" if $err;
if ($m > $this_minute + $rrdstep) {
for (my $sm = $this_minute + $rrdstep; $sm < $m; $sm += $rrdstep) {
RRDs::update($ops_rrd, "$sm:0:0:0:0:0:0:0:0");
RRDs::update($connxn_rrd, "$sm:0:0:0:0");
}
}
$this_minute = $m;
$sum{'add'} = 0;
$sum{'srch'} = 0;
$sum{'bind'} = 0;
$sum{'mod'} = 0;
$sum{'del'} = 0;
$sum{'ext'} = 0;
$sum{'cmp'} = 0;
$sum{'modrdn'} = 0;
$sum{'connxn'} = 0;
$sum{'sasl'} = 0;
$sum{'unsasl'} = 0;
$sum{'ssl'} = 0;
$sum{'tls'} = 0;
return 1;
}
__END__
=head1 NAME
ldapgraphd - Graphing and statistics for 389 Directory Server
=head1 SYNOPSIS
B<ldapgraphd> [--logfile=</path/to/logfile>] [--year=<year>] [--daemon]
[--pidfile=<pidfile>] [--rrddir=<rrddir>] [--instance=<instance_name>]
B<ldapgraphd> -h
=head1 DESCRIPTION
B<ldapgraphd> is a Perl daemon that reads access logs from 389
Directory Server and creates graphs of the data with RRDtool. It
works with 389 DS, RHDS, Sun DS, and earlier Fedora DS versions,
including Fedora DS 1.0.x. OpenLDAP support is forthcoming.
=head1 LOG FILE SELECTION
B<ldapgraphd> provides several options for selecting the log file to
read from:
=over
=item For 389 DS or Fedora DS logs, you can supply the B<< -I
<instance> >> flag to specify the instance whose log you wish to
follow.
=item For OpenLDAP logs, you must supply the B<< -l <logfile> >> flag
to specify the full path to the log to follow.
=item For 389 DS, Fedora DS, RHDS, or Sun DS logs, you may supply both
the B<< -l <logfile> >> flag to specify the full path to the log, and
the B<< --389 >> flag to inform ldapgraphd that the log is a 389-style
log. By default, a log file specified with B<< -l <logfile> >> is
assumed to be an OpenLDAP log.
=item If you have only a single 389 DS or Fedora DS instance, you can
specify no log selection options at all to use the access log for the
single instance.
=back
=head1 OPTIONS
=over
=item B<< --logfile=<logfile> >>, B<< -l <logfile> >>
=over
Full path to the OpenLDAP log file. To use B<-l> with 389 DS, Fedora
DS, RHDS, or Sun DS, you must also pass the B<--389> flag.
=back
=item B<--389>
=over
Use the 389 DS-style parser for a log file specified with the B<-l>
option.
=back
=item B<< instance=<instance_name> >>, B<< -I <instance_name> >>
=over
Track data for the specified 389 DS or Fedora DS instance. If you
specify an instance name with this option, do not include C<slapd->.
=back
=item B<< --year=<year> >>, B<< -y <year> >>
=over
The log files parsed by ldapgraphd are from the specified year.
Default is the current year.
=back
=item B<--daemon>, B<-d>
=over
Daemonize ldapgraphd
=back
=item B<< --pidfile=<pidfile> >>
=over
Write ldapgraphd's PID to the specified file instead of
C</var/run/ldapgraph.pid>
=back
=item B<< --rrddir=<rrddir> >>
=over
Write the RRDs to the specified directory instead of
C</var/lib/ldapgraph>.
=back
=item B<-h>
=over
Display usage information
=back
=back
=head1 SEE ALSO
L<rrdtool(1)>, L<RRDs(3)>
=head1 AUTHOR
Chris St. Pierre <[email protected]>