File: //lib/python2.7/site-packages/lap/ldapattr.py
# author: Jean Michel Feltrin (jean.feltrin@locaweb.com.br)
import subprocess
import os
import re
def __run__(params):
try:
import ldap
import ldap.modlist as modlist
except ImportError:
return [2, "CRITICAL - Error: Please install python-ldap"]
class ldapConn(object):
serverUri = None
bindDn = None
passwd = None
conn = None
baseDn = None
searchFilter = None
searchScope = ldap.SCOPE_SUBTREE
retrieveAttr = None
def __init__(self,uri,bindDN,passwd):
self.serverUri = uri
self.bindDn = bindDN
self.passwd = passwd
self.conn = self.connect()
def connect(self):
for server in self.serverUri.split(','):
try:
conn = ldap.initialize(server)
conn.set_option(ldap.OPT_NETWORK_TIMEOUT, 10.0)
conn.simple_bind_s(self.bindDn, self.passwd)
return conn
except ldap.LDAPError, e:
return [2, "CRIT - Could not connect to LDAP server [%s]" % server]
def search(self, searchFilter):
self.searchFilter = searchFilter
try:
ldap_result_id = self.conn.search(self.baseDn, self.searchScope, self.searchFilter, self.retrieveAttr)
result_set = []
while 1:
result_type, result_data = self.conn.result(ldap_result_id, 0)
if (result_data == []):
break
else:
if result_type == ldap.RES_SEARCH_ENTRY:
result_set.append(result_data)
return result_set
except ldap.LDAPError, e:
return [2, "CRIT - LDAP search failed: [filter=%s, baseDn=%s] - %s" % (self.searchFilter, self.baseDn, e)]
def close(self):
self.conn.unbind_s()
del self.conn
try:
warn = int(params.get("warn",0))
crit = int(params.get("crit",0))
bindDN = params.get("bindDN","")
baseDN = params.get("baseDN","")
passwd = params.get("passwd","")
uri = params.get("uri")
attr = params.get("attr")
except Exception, e:
return [2, "CRITICAL - Error getting params: %s" % repr(e)]
try:
lconn = ldapConn(uri,bindDN,passwd)
lconn.baseDn = baseDN
lconn.retrieveAttr = [attr]
result = lconn.search("objectClass=*")[0][0][1].get(attr)
if isinstance(result,list):
result = int(result[0])
print crit
if result < crit:
return [0, "OK - Ldap attribute(%s) monitoring: %s" % (attr,result)]
else:
return [2, "CRIT - Ldap attribute(%s) monitoring: %s" % (attr,result)]
except Exception, e:
return [2, "CRITICAL - Error: %s" % repr(e)]