summaryrefslogtreecommitdiffstats
path: root/roles/common/files/usr/local/share/munin/plugins/slapd2_
blob: 56774ac9d6f0293d635fd1c3f0cef9adda96c6a3 (plain)
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
#!/usr/bin/perl -w
# -*- perl -*-
# vim: ft=perl

# Copyright Bjorn Ruberg <bjorn@ruberg.no>
# Licenced under GPL v2
#
# TODO:
# - Check for OpenLDAP version

# We use one script for all monitoring.
# This script may be symlinked with several names, all
# performing different functions:
# slapd_statistics_bytes
# slapd_statistics_pdu
# slapd_statistics_referrals
# slapd_statistics_entries
# slapd_connections
# slapd_waiters
# slapd_operations
# slapd_operations_diff

# Magic markers
#%# family=auto
#%# capabilities=autoconf suggest

use strict;

my $ret = '';

if (! eval "require Net::LDAP;") {
   $ret = "Net::LDAP not found";
}

use vars qw ( $config $param $act $scope $descr $cn $vlabel
	      $info $title $label);

# Change these to reflect your LDAP ACL. The given DN must have
# read access to the Monitor branch.
my $basedn = "cn=Monitor";
my $server = ($ENV{'server'} || 'localhost');
my $userdn = ($ENV{'binddn'} || '');
my $userpw = ($ENV{'bindpw'} || '');

# Remember: connections, bytes, pdu needs scope=base

# The possible measurements
my %ops =
    ('statistics_bytes'
     => {
         'search' => "cn=Bytes,cn=Statistics",
         'desc'   => "The number of bytes sent by the LDAP server.",
         'vlabel' => 'Bytes per ${graph_period}',
         'label'  => 'Bytes',
         'title'  => "Number of bytes sent",
         'info'   => "The graph shows the number of bytes sent",
	 'scope'  => "base"
         },
     'statistics_pdu'
     => {
         'search' => "cn=PDU,cn=Statistics",
         'desc'   => "The number of PDUs sent by the LDAP server.",
         'vlabel' => 'PDUs per ${graph_period}',
         'label'  => 'PDUs',
         'title'  => "Number of PDUs sent",
         'info'   => "The graph shows the number of PDUs sent",
	 'scope'  => "base"
         },
     # Referrals
     'statistics_referrals' 
     => {
         'search' => "cn=Referrals,cn=Statistics",
         'desc'   => "The number of Referrals sent by the LDAP server.",
         'vlabel' => 'Referrals per ${graph_period}',
         'label'  => 'Referrals',
         'title'  => "Number of LDAP Referrals",
         'info'   => "The graph shows the number of referrals sent",
	 'scope'  => "base"
         },
     # Entries
     'statistics_entries'
     => {
         'search' => "cn=Entries,cn=Statistics",
         'desc'   => "The number of Entries sent by the LDAP server.",
         'vlabel' => 'Entries per ${graph_period}',
         'label'  => 'Entries',
         'title'  => "Number of LDAP Entries",
         'info'   => "The graph shows the number of entries sent",
	 'scope'  => "base"
         },
     # Only read Total
     'connections' 
     => {
         'search' => 'cn=Total,cn=Connections',
         'desc'   => 'The number of connections',
         'label'  => 'Connections',
         'vlabel' => 'Connections per ${graph_period}',
         'title'  => 'Number of Connections',
         'info'   => 'Number of connections to the LDAP server',
         'scope'  => "base"
         },
     # dn: cn=Write,cn=Waiters,cn=Monitor
     # dn: cn=Read,cn=Waiters,cn=Monitor
     'waiters' 
     => {
         'search' => 'cn=Waiters',
         'filter' => '(|(cn=Write)(cn=Read))',
         'desc'   => "The current number of Waiters",
         'label2' => {'write' => 'Write',
                      'read'  => 'Read'},
         'vlabel' => "Waiters",
         'title'  => "Number of Waiters",
         'info'   => "The graph shows the number of Waiters"
         },
     'operations'
     => {
         'search' => "cn=Operations",
         'desc'   => "Operations",
         'vlabel' => 'Operations per ${graph_period}',
         'label'  => 'Operations',
         'title'  => "Operations",
         'info'   => "Number of completed LDAP operations"
         },
     'operations_diff'
     => {
         'search' => "cn=Operations",
         'desc'   => "Operations deviance",
         'vlabel' => 'Deviance',
         'label'  => 'Deviance',
         'title'  => "Operations deviance",
         'info'   => "Deviance between Initiated and Completed ops"
       }
     );

# Config subroutine
sub config {
    my $action = shift;
    print <<EOF;
graph_args --base 1000 -l 0
graph_scale no
graph_vlabel $ops{$action}->{'vlabel'}
graph_title $ops{$action}->{'title'}
graph_category OpenLDAP
graph_info $ops{$action}->{'info'}
EOF
    
    if ($ops{$action}->{'label2'}) {
        while (my ($key, $val) = each (%{$ops{$action}->{'label2'}})) {
          my $name = $action . "_" . $key;
          print "$name.label $val\n";
          print "$name.type GAUGE\n";
        }
    } elsif ($action =~ /^operations(?:_diff)?$/) {
        my $ldap = Net::LDAP->new ($server)
            or die "Failed to connect to server $server: $@";
        my $mesg;
        if ($userdn ne '') {
          $mesg = $ldap->bind ($userdn, password => $userpw)
              or die "Failed to bind with $userdn: $@";
        } else {
          require Authen::SASL;
          my $sasl = Authen::SASL::->new( mechanism => 'EXTERNAL' );
          $mesg = $ldap->bind( undef, sasl => $sasl )
              or die "Failed to SASL bind: $@";
        }
        if ($mesg->code) {
          die "Failed to bind: " . $mesg->error;
        }
        my $searchdn = $ops{$action}->{'search'} . "," . $basedn;
        $mesg =
            $ldap->search (
                           base   => $searchdn,
                           scope  => 'one',
                           filter => '(objectclass=*)',
                           attrs  => ['monitorOpInitiated',
                                      'monitorOpCompleted',
                                      'cn'],
                           );
        $mesg->code && die $mesg->error;
        
        my $max = $mesg->count;
        for (my $i = 0 ; $i < $max ; $i++) {
            my $entry = $mesg->entry ($i);
            my $cn = $entry->get_value ('cn');
            my $name = $action . "_" . lc ($cn);
            print "$name.label $cn\n";
            print "$name.type DERIVE\n";
            print "$name.min 0\n";
            if ($action eq "operations") {
                print "$name.info The number of $cn operations\n";
            } else {
                print "$name.info The difference between Initiated ";
                print "and Completed operations (should be 0)\n";
                print "$name.warning 1\n";
            }            
        }

        $ldap->unbind;
    } else {
        print "$action.label $ops{$action}->{'label'}\n";
        print "$action.type DERIVE\n";
        print "$action.min 0\n";
    }
}

# Determine action based on filename first
(my $action = $0) =~ s/^.*slapd2_([\w\d_]+)$/$1/;

if ($ARGV[0]) {
    if ($ARGV[0] eq 'autoconf') {
        # Check for Net::LDAP
	if ($ret) {
	    print "no ($ret)\n";
	    exit 0;
	}

        # Check for LDAP version 3
        my $ldap = Net::LDAP->new ($server, version => 3)
            or do { print "no ($@)\n"; exit 0; };

        my $mesg;
        if ($userdn ne '') {
          $mesg = $ldap->bind ($userdn, password => $userpw)
            or do { print "no ($@)\n"; exit 0; };
        } else {
          require Authen::SASL;
          my $sasl = Authen::SASL::->new( mechanism => 'EXTERNAL' );
          $mesg = $ldap->bind( undef, sasl => $sasl )
            or do { print "no ($@)\n"; exit 0; };
        }
        if ($mesg->code) {
          print "no (" . $mesg->error . ")\n";
          exit 0;
        }

        $mesg =
            $ldap->search (
                           base   => $basedn,
                           scope  => 'one',
                           filter => '(objectClass=monitorServer)',
                           attrs  => [
                                       'cn',
                                     ],
                           );
        if ($mesg->code) {
          print "no (" . $mesg->error . ")\n";
          exit 0;
        }
        print "yes\n";
        exit 0;
    } elsif ($ARGV[0] eq "config") {
        if(!exists $ops{$action}) {
            die "Unknown action specified: $action";
        }
        &config ($action);
    } elsif ($ARGV[0] eq "suggest") {
        print join ("\n", keys (%ops)), "\n";
    }
    exit 0;
}

# If $action isn't in %ops, we quit
if(!exists $ops{$action}) {
    die "Unknown action specified: $action";
}

# Default scope for LDAP searches. We'll change to other scopes if
# necessary.
$scope = "one";

# Net::LDAP variant
my $ldap = Net::LDAP->new ($server, version => 3)
    or die "Failed to connect to server $server: $@";
my $mesg;
if ($userdn ne '') {
  $mesg = $ldap->bind ($userdn, password => $userpw)
      or die "Failed to bind with $userdn: $@";
} else {
  require Authen::SASL;
  my $sasl = Authen::SASL::->new( mechanism => 'EXTERNAL' );
  $mesg = $ldap->bind( undef, sasl => $sasl )
      or die "Failed to bind anonymously: $@";
}
if ($mesg->code) {
  die "Failed to bind: " . $mesg->error;
}

my $searchdn = $ops{$action}->{'search'} . "," . $basedn;
my $searchattrs;

if ($action =~ /^operations(_diff)?$/) {
    # We look for different parameters in Operations branch
    $searchattrs = ['monitorOpInitiated', 'monitorOpCompleted', 'cn'];
} else {
    $searchattrs = ['monitorCounter', 'cn'];
}

my $filter;
if ($ops{$action}->{'filter'}) {
  $filter = "(&(objectclass=*)" . $ops{$action}->{'filter'} . ")";
} else {
  $filter = "(objectClass=*)";
}

if ($ops{$action}->{'scope'}) {
  $scope = $ops{$action}->{'scope'};
}

$mesg =
    $ldap->search (
                   base   => $searchdn,
                   scope  => $scope,
                   filter => $filter,
                   attrs  => $searchattrs,
                   );

$mesg->code && die $mesg->error;

my $max = $mesg->count;

for (my $i = 0 ; $i < $max ; $i++) {
    my $entry = $mesg->entry ($i);
    my $cn = $entry->get_value('cn');
    if ($action =~ /operations(_diff)?$/) {
	if ($1) {
	    my $opsInit =
		$entry->get_value('monitorOpInitiated'); 
	    my $opsComp =
		$entry->get_value('monitorOpCompleted');
	    print lc ("operations_diff_${cn}.value ");
	    print ($opsInit - $opsComp);
	    print "\n";
	} else {
	    print lc ("operations_${cn}.value ");
	    print $entry->get_value('monitorOpCompleted'),
	    "\n";
	}
    } else {
	# Hotfix, must do for now
	if ($action =~ /_/ || $action eq 'connections') {
	    print lc ("${action}.value ");
	} else {
	    print lc ("${action}_${cn}.value ");
	}
	print $entry->get_value('monitorCounter'), "\n";
    }
}
$ldap->unbind;