Search in sources :

Example 1 with IcingaItem

use of org.onebusaway.enterprise.webapp.actions.status.model.IcingaItem in project onebusaway-application-modules by camsys.

the class StatusProviderImpl method getIcingaStatus.

@Override
public StatusGroup getIcingaStatus() {
    StatusGroup group = new StatusGroup();
    group.setTitle("System Monitoring");
    group.setScope("Monitoring and Infrastructure notices");
    group.setSource("Monitoring subsystem -- automated notifications");
    IcingaResponse response = null;
    // fail if not configured!
    String baseUrl = _config.getConfigurationValueAsString("icinga.baseUrl", null);
    String command = _config.getConfigurationValueAsString("icinga.command", null);
    // be careful -- by default we aren't configured to do anything!
    if (baseUrl == null || command == null) {
        _log.info("missing required configuration for status group: baseUrl=" + baseUrl + ", command=" + command);
        return group;
    }
    try {
        HttpClient client = new HttpClient();
        String url = baseUrl + encode(command);
        HttpMethod method = new GetMethod(url);
        client.executeMethod(method);
        InputStream result = method.getResponseBodyAsStream();
        ObjectMapper mapper = new ObjectMapper();
        response = mapper.readValue(result, IcingaResponse.class);
    } catch (IOException e) {
        _log.error("Exception getting Icinga data " + e);
        group.addItem(exceptionStatus(e));
        return group;
    }
    if (response == null) {
        group.addItem(exceptionStatus());
        return group;
    }
    for (IcingaItem bean : response.getResult()) {
        StatusItem item = new StatusItem();
        item.setDescription(bean.getServiceName());
        item.setTitle(bean.getServiceDisplayName());
        int state = bean.getServiceCurrentState();
        StatusItem.Status status;
        if (state == 0) {
            status = StatusItem.Status.OK;
        } else if (state == 1) {
            status = StatusItem.Status.WARNING;
        } else {
            // 2 (or something even weirder!)
            status = StatusItem.Status.ERROR;
        }
        item.setStatus(status);
        group.addItem(item);
    }
    return group;
}
Also used : IcingaItem(org.onebusaway.enterprise.webapp.actions.status.model.IcingaItem) InputStream(java.io.InputStream) IOException(java.io.IOException) IcingaResponse(org.onebusaway.enterprise.webapp.actions.status.model.IcingaResponse) StatusItem(org.onebusaway.enterprise.webapp.actions.status.model.StatusItem) StatusGroup(org.onebusaway.enterprise.webapp.actions.status.model.StatusGroup) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpMethod(org.apache.commons.httpclient.HttpMethod) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HttpClient (org.apache.commons.httpclient.HttpClient)1 HttpMethod (org.apache.commons.httpclient.HttpMethod)1 GetMethod (org.apache.commons.httpclient.methods.GetMethod)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1 IcingaItem (org.onebusaway.enterprise.webapp.actions.status.model.IcingaItem)1 IcingaResponse (org.onebusaway.enterprise.webapp.actions.status.model.IcingaResponse)1 StatusGroup (org.onebusaway.enterprise.webapp.actions.status.model.StatusGroup)1 StatusItem (org.onebusaway.enterprise.webapp.actions.status.model.StatusItem)1