Nagios Writing Plugin

nagios

// In this example, this code render the string "ALL CHECKS SUCCESSFUL." if 
// everything is ok.  Otherwise, it displays the errors.  I think on the back-end 
// I use the http_check plugin.  We can also write plugin for Nagios.  Those 
// plugins uses certain exit code.  I wrote a plugin like this once, but I do not 
// remember why I needed a plugin like that.

package com.quantros.qsupport;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.infoobjectsinc.util.DBMSConnection;
import com.infoobjectsinc.util.QGlobals;
import com.infoobjectsinc.util.QuantrosException;
import com.quantros.AppModules.IOFunctions;
import com.quantros.AppModules.ProcessDataHandler;
import com.quantros.platform.db.QJdbcTemplate;

public class NagiosChecks implements ProcessDataHandler {
    private Vector errors;
    private QJdbcTemplate dbtemplate;

    /**
     * This is a plugin for Nagios, a network / service monitoring tool
     * @param req request object
     * @param res  response object
     * @param servctxt ServletContext object
     * @throws ServletException and IOException
     */
    public void processData(HttpServletRequest req, HttpServletResponse res,
            ServletContext servctxt) throws ServletException, QuantrosException,
            Exception {
        try{
            HttpSession session = req.getSession(true);
            IOFunctions iof = new IOFunctions(QGlobals.debugger);
            this.errors = new Vector();

            String type = iof.getParameter(req, "type", "");

            this.dbtemplate = new QJdbcTemplate(
                    DBMSConnection.getDataSource("SupportDataSource"), 
                    QGlobals.debugger,
                    "NagiosChecks: processData");

            if (! type.equals("")) {
                this.check_is_file_uploaded();
            } else {
                this.check_copy_data_from_production_to_training();
                this.check_product_activation();
                this.check_provider_upload();
                this.check_generic_batch_upload();
                this.check_save_or_clone_requests();
            }

            String errstr = this.join(this.errors, "\n");
            res.setContentType("text/html");
            String message = "";
            if (errstr.equalsIgnoreCase("")) {
                message = "<p>ALL CHECKS SUCCESSFUL.</p>";
            } else {
                message = "<pre>" + errstr + "</pre>";
            }            
            res.getWriter().print("<html><head></head><body>" + message + "<iframe style='width: 100%; border:0px;' src='/MicroStrategy/plugins/CustomESM/jsp/AuditLogCheck.jsp'></iframe></body></html>");
        } catch(Exception e) {
            QGlobals.debugger.error("NavigationTree", "processData",    e);
            throw new QuantrosException(e);
        }

    }

    // Join a list of strings
    private String join(Vector v, String separator) {
        StringBuffer buffer = new StringBuffer();
        Iterator iter = v.iterator();
        while (iter.hasNext()) {
            buffer.append(iter.next());
            if (iter.hasNext()) {
                buffer.append(separator);
            }
        }
        return buffer.toString();
    }

    private void check_copy_data_from_production_to_training() throws Exception {
        String sql = "SELECT * FROM SUPPORT.LAST_PRODUCTION_TO_TRAINING WHERE LAST_COPIED_TIME < SYSDATE - INTERVAL '30' MINUTE";
        List list = this.dbtemplate.QueryForList(sql);
        boolean lagging = false;
        if (list.size() > 0) {
            lagging = true;
        }        
        sql = "SELECT * FROM SUPPORT.SYNC_DATA_SQL WHERE ADDDATE < SYSDATE - INTERVAL '30' MINUTE";
        list = this.dbtemplate.QueryForList(sql);
        if (list.size() > 0) {
            lagging = true;
        }
        if (lagging) {
            this.errors.add("The background process for copying data from production to training and Production(PSOM) is lagging behind or not running at all.");
        }
    }

    private void check_product_activation() throws Exception {
        String sql = "SELECT * FROM SUPPORT.ACTIVATIONSCHEDULE WHERE STATUS != 'C' AND RELEASE_DATE < SYSDATE - INTERVAL '8' HOUR AND ADDDATE < SYSDATE - INTERVAL '1' HOUR";
        List list = this.dbtemplate.QueryForList(sql);
        if (list.size() > 0) {
            this.errors.add("The background process for activating products is not working appropriately.");
        }        
    }

    private void check_provider_upload() throws Exception {
        /*
         * In the SUPPORT.QSUP_PROVIDER_INFO_UPLOAD table, we only have one date column (the UPLOAD_DATE column).
         * For the provider upload tool, we do not have ability to schedule when the file should be process.  We always process
         * the file that same night it was uploaded.  The UPLOAD_DATE column holds the time that the file
         * was uploaded.  This is different from product activation.  Therefore, here we have to remove the time portion
         * and add one day to our query
         */
        String sql = "SELECT * FROM SUPPORT.QSUP_PROVIDER_INFO_UPLOAD WHERE (STATUS='P') AND (TO_DATE(TRUNC(UPLOAD_DATE)) + 1 < SYSDATE - INTERVAL '8' HOUR)";
        List list = this.dbtemplate.QueryForList(sql);
        if (list.size() > 0) {
            this.errors.add("The background process for provider upload is not working appropriately.");
        }
    }

    private void check_homepage_publish() throws Exception {
        String sql = "SELECT * FROM SUPPORT.HOMEPAGE_PUBLISH_SCHEDULE WHERE STATUS != 'C' AND RELEASEDATE < SYSDATE - INTERVAL '8' HOUR";
        List list = this.dbtemplate.QueryForList(sql);
        if (list.size() > 0) {
            this.errors.add("The background process for publishing homepage is not working appropriately.");
        }        
    }

    private void check_is_file_uploaded() throws Exception {
        String sql = "SELECT * FROM SUPPORT.GENERIC_FILE_UPLOAD WHERE STATUS IN ('UPLOADED')";        
        List list = this.dbtemplate.QueryForList(sql);
        if (list.size() > 0) {
            this.errors.add("A QSupport user has just uploaded an Excel file.  Look at the SUPPORT.GENERIC_FILE_UPLOAD for details, and do appropriate thing.");
            return;
        }
    }

    private void check_generic_batch_upload() throws Exception {
        String sql = "SELECT * FROM SUPPORT.GENERIC_FILE_UPLOAD WHERE STATUS IN ('UPLOADED','PARSING STARTED') AND MODDATE < SYSDATE - INTERVAL '1' HOUR";
        List list = this.dbtemplate.QueryForList(sql);
        if (list.size() > 0) {
            this.errors.add("One of the background processes that process the SUPPORT.GENERIC_FILE_UPLOAD table is not working appropriately.  There are upload jobs that have had the status of UPLOADED or 'PARSING STARTED' for more than 2 hours.");
            return;
        }
        sql = "SELECT * FROM SUPPORT.GENERIC_FILE_UPLOAD WHERE STATUS IN ('PARSING COMPLETED') AND MODDATE < SYSDATE - INTERVAL '24' HOUR";
        if (list.size() > 0) {
            this.errors.add("One of the background processes that process the SUPPORT.GENERIC_FILE_UPLOAD table is not working appropriately.  There are jobs that have had the status of 'PARSING COMPLETED' for more than 24 hours.  Is this a large facilities upload job such that it is causing the product activation to take more than 24 hours to activate all the included products for all the included facilities?");
            return;
        }
    }

    private void check_save_or_clone_requests() throws Exception {
        String sql = "SELECT * FROM SUPPORT.CLONE_REQUESTS WHERE STATUS = 'REQUESTED' AND ADDDATE < SYSDATE - INTERVAL '2' HOUR";
        List list = this.dbtemplate.QueryForList(sql);
        if (list.size() > 0) {
            this.errors.add("The background process SaveOrClone is not working appropriately or is lagging behind.");
        }
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License