Search in sources :

Example 1 with NagiosStatus

use of org.vcell.rest.health.HealthService.NagiosStatus in project vcell by virtualcell.

the class HealthRestlet method handle.

@Override
public void handle(Request req, Response response) {
    if (req.getMethod().equals(Method.GET)) {
        try {
            VCellApiApplication application = ((VCellApiApplication) getApplication());
            HttpRequest request = (HttpRequest) req;
            Form form = request.getResourceRef().getQueryAsForm();
            String requestTypeString = form.getFirstValue(VCellApiApplication.HEALTH_CHECK, true);
            RequestType requestType = null;
            if (requestTypeString != null) {
                if (requestTypeString.equalsIgnoreCase(VCellApiApplication.HEALTH_CHECK_LOGIN)) {
                    requestType = RequestType.login;
                } else if (requestTypeString.equalsIgnoreCase(VCellApiApplication.HEALTH_CHECK_SIM)) {
                    requestType = RequestType.sim;
                } else if (requestTypeString.equalsIgnoreCase(VCellApiApplication.HEALTH_CHECK_ALL)) {
                    requestType = RequestType.all;
                }
            }
            if (requestType == null) {
                throw new RuntimeException("expecting /" + VCellApiApplication.HEALTH + "?" + VCellApiApplication.HEALTH_CHECK + "=" + "(" + VCellApiApplication.HEALTH_CHECK_LOGIN + "|" + VCellApiApplication.HEALTH_CHECK_SIM + "|" + VCellApiApplication.HEALTH_CHECK_ALL + ")" + "[&" + VCellApiApplication.HEALTH_CHECK_ALL_START_TIMESTAMP + "=<start_ms>]" + "[&" + VCellApiApplication.HEALTH_CHECK_ALL_END_TIMESTAMP + "=<end_ms>]" + "[&" + VCellApiApplication.HEALTH_CHECK_STATUS_TIMESTAMP + "=<status_ms>]");
            }
            // defaults to current status
            long status_timestamp = System.currentTimeMillis();
            String statusTimestampString = form.getFirstValue(VCellApiApplication.HEALTH_CHECK_STATUS_TIMESTAMP, true);
            if (statusTimestampString != null) {
                status_timestamp = Long.parseLong(statusTimestampString);
            }
            // defaults to returning logs (all option) starting two hours ago
            long start_timestamp = System.currentTimeMillis() - (2 * 60 * 60 * 1000);
            String startTimestampString = form.getFirstValue(VCellApiApplication.HEALTH_CHECK_ALL_START_TIMESTAMP, true);
            if (startTimestampString != null) {
                start_timestamp = Long.parseLong(startTimestampString);
            }
            // defaults to returning logs (all option) up to present time
            long end_timestamp = System.currentTimeMillis();
            String endTimestampString = form.getFirstValue(VCellApiApplication.HEALTH_CHECK_ALL_END_TIMESTAMP, true);
            if (endTimestampString != null) {
                end_timestamp = Long.parseLong(endTimestampString);
            }
            HealthService healthService = application.getHealthService();
            switch(requestType) {
                case all:
                    {
                        HealthEvent[] events = healthService.query(start_timestamp, end_timestamp);
                        ArrayUtils.reverse(events);
                        Gson gson = new Gson();
                        String healthEventsJSON = gson.toJson(events);
                        response.setStatus(Status.SUCCESS_OK);
                        response.setEntity(new JsonRepresentation(healthEventsJSON));
                        break;
                    }
                case login:
                    {
                        NagiosStatus nagiosStatus = healthService.getLoginStatus(status_timestamp);
                        Gson gson = new Gson();
                        String statusJSON = gson.toJson(nagiosStatus);
                        response.setStatus(Status.SUCCESS_OK);
                        response.setEntity(new JsonRepresentation(statusJSON));
                        break;
                    }
                case sim:
                    {
                        NagiosStatus nagiosStatus = healthService.getRunsimStatus(status_timestamp);
                        Gson gson = new Gson();
                        String statusJSON = gson.toJson(nagiosStatus);
                        response.setStatus(Status.SUCCESS_OK);
                        response.setEntity(new JsonRepresentation(statusJSON));
                        break;
                    }
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.setStatus(Status.SERVER_ERROR_INTERNAL);
            response.setEntity("failed to retrieve system health: " + e.getMessage(), MediaType.TEXT_PLAIN);
        }
    }
}
Also used : HttpRequest(org.restlet.engine.adapter.HttpRequest) Form(org.restlet.data.Form) VCellApiApplication(org.vcell.rest.VCellApiApplication) Gson(com.google.gson.Gson) NagiosStatus(org.vcell.rest.health.HealthService.NagiosStatus) JsonRepresentation(org.restlet.ext.json.JsonRepresentation)

Aggregations

Gson (com.google.gson.Gson)1 Form (org.restlet.data.Form)1 HttpRequest (org.restlet.engine.adapter.HttpRequest)1 JsonRepresentation (org.restlet.ext.json.JsonRepresentation)1 VCellApiApplication (org.vcell.rest.VCellApiApplication)1 NagiosStatus (org.vcell.rest.health.HealthService.NagiosStatus)1