-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path389_monitorstats.py
124 lines (103 loc) · 2.79 KB
/
389_monitorstats.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import collectd
import ldap
instances = {}
config = None
monitorattrs = [
'anonymousbinds',
'unauthbinds',
'simpleauthbinds',
'strongauthbinds',
'bindsecurityerrors',
'inops',
'readops',
'compareops',
'addentryops',
'removeentryops',
'modifyentryops',
'modifyrdnops',
'listops',
'searchops',
'onelevelsearchops',
'wholesubtreesearchops',
'referrals',
'chainings',
'securityerrors',
'errors',
'connections',
'connectionseq',
'bytesrecv',
'bytessent',
'entriesreturned',
'referralsreturned',
'masterentries',
'copyentries',
'cacheentries',
'cachehits',
'slavehits'
]
class LDAPStats(object):
def __init__(
self,
hostname=None,
port=389,
binddn=None,
bindpw=None,
):
self.hostname = hostname
self.binddn = binddn
self.bindpw = bindpw
self.port = int(port)
if self.hostname != None:
self.__get_stats__()
def __get_stats__(self):
self.srv = ldap.open(self.hostname, self.port)
if self.binddn != None:
self.srv.simple_bind_s(self.binddn, self.bindpw)
(dn, attrs) = self.srv.search_s('cn=snmp,cn=monitor',ldap.SCOPE_BASE, attrlist=monitorattrs)[0]
for a in monitorattrs:
if int(attrs[a][0]) < 0:
attrs[a][0] = int(attrs[a][0]) * -1
setattr(self, a, int(attrs[a][0]))
del self.srv
def get_stats(self, hr=False):
stats = []
for a in monitorattrs:
stats.append(int(getattr(self, a)))
if hr == False:
return stats
else:
return dict(zip(monitorattrs, stats))
def configer(config):
global instances
collectd.debug('Configuring Stuff')
# children', 'key', 'parent', 'values'
for c in config.children:
if c.key == 'server':
for srv in c.children:
if srv.key == 'hostname':
hostname = '.'.join(srv.values)
elif srv.key == 'port':
port = int(srv.values[0])
instances[hostname] = port
def initer():
collectd.debug('initing stuff')
def reader(input_data=None):
global instances
for h in instances:
srv = LDAPStats(hostname=h, port=instances[h])
data = srv.get_stats(hr=True)
for key, value in data.iteritems():
dispatch_value(key,value)
def dispatch_value(value_type, value):
if not value:
return
metric = collectd.Values()
metric.plugin = '389_monitorstats'
metric.type = value_type
metric.values = [value]
metric.dispatch()
collectd.register_config(configer)
collectd.register_init(initer)
collectd.register_read(reader)