use of com.novell.ldap.LDAPSearchConstraints in project opennms by OpenNMS.
the class LdapMonitor method poll.
/**
* {@inheritDoc}
*
* Poll the specified address for service availability.
*
* During the poll an attempt is made to connect the service.
*
* Provided that the interface's response is valid we set the service status
* to SERVICE_AVAILABLE and return.
*/
@Override
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
int serviceStatus = PollStatus.SERVICE_UNAVAILABLE;
String reason = null;
final TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT);
// get the parameters
//
final int ldapVersion = ParameterMap.getKeyedInteger(parameters, "version", LDAPConnection.LDAP_V3);
final int ldapPort = determinePort(parameters);
final String searchBase = ParameterMap.getKeyedString(parameters, "searchbase", DEFAULT_BASE);
final String searchFilter = ParameterMap.getKeyedString(parameters, "searchfilter", DEFAULT_FILTER);
final String password = (String) parameters.get("password");
final String ldapDn = (String) parameters.get("dn");
String address = InetAddrUtils.str(svc.getAddress());
// first just try a connection to the box via socket. Just in case there
// is
// a no way to route to the address, don't iterate through the retries,
// as a
// NoRouteToHost exception will only be thrown after about 5 minutes,
// thus tying
// up the thread
Double responseTime = null;
Socket socket = null;
try {
socket = new Socket();
socket.connect(new InetSocketAddress(svc.getAddress(), ldapPort), tracker.getConnectionTimeout());
socket.setSoTimeout(tracker.getSoTimeout());
LOG.debug("LdapMonitor: connected to host: {} on port: {}", address, ldapPort);
// We're connected, so upgrade status to unresponsive
serviceStatus = PollStatus.SERVICE_UNRESPONSIVE;
if (socket != null)
socket.close();
// lets detect the service
LDAPConnection lc = new LDAPConnection(new TimeoutLDAPSocket(tracker.getSoTimeout()));
for (tracker.reset(); tracker.shouldRetry() && !(serviceStatus == PollStatus.SERVICE_AVAILABLE); tracker.nextAttempt()) {
LOG.debug("polling LDAP on {}, {}", address, tracker);
// connect to the ldap server
tracker.startAttempt();
try {
lc.connect(address, ldapPort);
LOG.debug("connected to LDAP server {} on port {}", address, ldapPort);
} catch (LDAPException e) {
LOG.debug("could not connect to LDAP server {} on port {}", address, ldapPort);
reason = "could not connect to LDAP server " + address + " on port " + ldapPort;
continue;
}
// bind if possible
if (ldapDn != null && password != null) {
try {
lc.bind(ldapVersion, ldapDn, password.getBytes());
LOG.debug("bound to LDAP server version {} with distinguished name {}", ldapVersion, ldapDn);
LOG.debug("poll: responseTime= {}ms", tracker.elapsedTimeInMillis());
} catch (LDAPException e) {
try {
lc.disconnect();
} catch (LDAPException ex) {
LOG.debug(ex.getMessage());
}
LOG.debug("could not bind to LDAP server version {} with distinguished name {}", ldapVersion, ldapDn);
reason = "could not bind to LDAP server version " + ldapVersion + " with distinguished name " + ldapDn;
continue;
}
}
// do a quick search and see if any results come back
boolean attributeOnly = true;
String[] attrs = { LDAPConnection.NO_ATTRS };
int searchScope = LDAPConnection.SCOPE_ONE;
LOG.debug("running search {} from {}", searchFilter, searchBase);
LDAPSearchResults results = null;
int msLimit = (int) tracker.getTimeoutInMillis();
int serverLimit = (int) tracker.getTimeoutInSeconds() + 1;
LDAPSearchConstraints cons = new LDAPSearchConstraints(msLimit, serverLimit, // dereference: default = never
LDAPSearchConstraints.DEREF_NEVER, // maxResults: default = 1000
1000, // doReferrals: default = false
false, // batchSize: default = 1
1, // handler: default = null
null, // hop_limit: default = 10
10);
try {
results = lc.search(searchBase, searchScope, searchFilter, attrs, attributeOnly, cons);
if (results != null && results.hasMore()) {
responseTime = tracker.elapsedTimeInMillis();
LOG.debug("search yielded {} result(s)", results.getCount());
serviceStatus = PollStatus.SERVICE_AVAILABLE;
} else {
LOG.debug("no results found from search");
reason = "No results found from search";
serviceStatus = PollStatus.SERVICE_UNAVAILABLE;
}
} catch (LDAPException e) {
try {
lc.disconnect();
} catch (LDAPException ex) {
LOG.debug(ex.getMessage());
}
LOG.debug("could not perform search {} from {}", searchFilter, searchBase);
reason = "could not perform search " + searchFilter + " from " + searchBase;
continue;
}
try {
lc.disconnect();
LOG.debug("disconected from LDAP server {} on port {}", address, ldapPort);
} catch (LDAPException e) {
LOG.debug(e.getMessage());
}
}
} catch (ConnectException e) {
LOG.debug("connection refused to host {}", address, e);
reason = "connection refused to host " + address;
} catch (NoRouteToHostException e) {
LOG.debug("No route to host {}", address, e);
reason = "No route to host " + address;
} catch (InterruptedIOException e) {
LOG.debug("did not connect to host with {}", tracker);
reason = "did not connect to host with " + tracker;
} catch (Throwable t) {
LOG.debug("An undeclared throwable exception caught contacting host {}", address, t);
reason = "An undeclared throwable exception caught contacting host " + address;
}
return PollStatus.get(serviceStatus, reason, responseTime);
}
Aggregations