Search in sources :

Example 6 with JDBCConnection

use of com.ibm.nagios.util.JDBCConnection in project nagios-for-i by IBM.

the class SubsystemJobs method execute.

public int execute(AS400 as400, Map<String, String> args, StringBuffer response) {
    int returnValue = Constants.UNKNOWN;
    String warningCap = args.get("-W");
    String criticalCap = args.get("-C");
    int intWarningCap = (warningCap == null) ? 100 : Integer.parseInt(warningCap);
    int intCriticalCap = (criticalCap == null) ? 100 : Integer.parseInt(criticalCap);
    String subsystem = args.get("-S");
    if (subsystem == null) {
        response.append("The argument -S [subsystem] is not set");
        return returnValue;
    }
    String jobName = null;
    String curentUser = null;
    String CPUPercent = null;
    String jobStatus = null;
    String functionType = null;
    String function = null;
    String jobType = null;
    int jobCount = 0;
    Statement stmt = null;
    ResultSet rs = null;
    Connection connection = null;
    try {
        JDBCConnection JDBCConn = new JDBCConnection();
        connection = JDBCConn.getJDBCConnection(as400.getSystemName(), args.get("-U"), args.get("-P"), args.get("-SSL"));
        if (connection == null) {
            response.append(Constants.retrieveDataError + " - " + "Cannot get the JDBC connection");
            return returnValue;
        }
        stmt = connection.createStatement();
        rs = stmt.executeQuery("SELECT SUBSTR(JOB_NAME,8,POSSTR(SUBSTR(JOB_NAME,8),'/')-1) AS JOB_USER, SUBSTR(SUBSTR(JOB_NAME,8),POSSTR(SUBSTR(JOB_NAME,8),'/')+1)  AS JOB_NAME, AUTHORIZATION_NAME, ELAPSED_CPU_PERCENTAGE, FUNCTION_TYPE, FUNCTION, JOB_STATUS, JOB_TYPE FROM TABLE (QSYS2.ACTIVE_JOB_INFO('NO', '', '', '')) AS X WHERE SUBSYSTEM = '" + subsystem.toUpperCase() + "'");
        if (rs == null) {
            response.append(Constants.retrieveDataError + " - " + "Cannot retrieve data from server");
            return returnValue;
        }
        while (rs.next()) {
            function = rs.getString("FUNCTION");
            // if(function == null)
            // continue;
            jobName = String.format("%-10s", rs.getString("JOB_NAME"));
            curentUser = String.format("%-10s", rs.getString("JOB_USER"));
            CPUPercent = String.format("%-5s", rs.getString("ELAPSED_CPU_PERCENTAGE"));
            jobStatus = String.format("%-4s", rs.getString("JOB_STATUS"));
            functionType = rs.getString("FUNCTION_TYPE");
            jobType = rs.getString("JOB_TYPE");
            function = functionType + "-" + function;
            jobCount++;
            if (!jobType.equalsIgnoreCase("SBS")) {
                response.append("Job: " + jobName + " CPU: " + CPUPercent + " User: " + curentUser + " Status: " + jobStatus + " Function: " + function + "\n");
            }
        }
        if (jobCount == 0) {
            response.insert(0, "subsystem " + subsystem + " is not active\n");
            returnValue = Constants.CRITICAL;
        } else {
            response.insert(0, jobCount - 1 + " jobs in subsystem " + subsystem + "\n");
            returnValue = CommonUtil.getStatus(jobCount, intWarningCap, intCriticalCap, returnValue);
        }
    } catch (Exception e) {
        response.setLength(0);
        response.append(Constants.retrieveDataException + " - " + e.toString());
        CommonUtil.printStack(e.getStackTrace(), response);
        CommonUtil.logError(args.get("-H"), this.getClass().getName(), e.getMessage());
        e.printStackTrace();
    } finally {
        try {
            if (rs != null)
                rs.close();
            if (stmt != null)
                stmt.close();
            if (connection != null)
                connection.close();
        } catch (SQLException e) {
            response.append(Constants.retrieveDataException + " - " + e.toString());
        }
    }
    return returnValue;
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) JDBCConnection(com.ibm.nagios.util.JDBCConnection) JDBCConnection(com.ibm.nagios.util.JDBCConnection) SQLException(java.sql.SQLException)

Example 7 with JDBCConnection

use of com.ibm.nagios.util.JDBCConnection in project nagios-for-i by IBM.

the class ASPUsage method execute.

@SuppressWarnings("unused")
public int execute(AS400 as400, Map<String, String> args, StringBuffer response) {
    double percentUsed = 0.;
    long ASPStorage;
    long curTempStorage;
    double maxASPUsgVal = 0;
    Statement stmt = null;
    ResultSet rs = null;
    String warningCap = args.get("-W");
    String criticalCap = args.get("-C");
    double doubleWarningCap = (warningCap == null) ? 100 : Double.parseDouble(warningCap);
    double doubleCriticalCap = (criticalCap == null) ? 100 : Double.parseDouble(criticalCap);
    int returnValue = Constants.UNKNOWN;
    Connection connection = null;
    try {
        JDBCConnection JDBCConn = new JDBCConnection();
        connection = JDBCConn.getJDBCConnection(as400.getSystemName(), args.get("-U"), args.get("-P"), args.get("-SSL"));
        if (connection == null) {
            response.append(Constants.retrieveDataError + " - " + "Cannot get the JDBC connection");
            return returnValue;
        }
        stmt = connection.createStatement();
        rs = stmt.executeQuery("SELECT SYSTEM_ASP_USED, SYSTEM_ASP_STORAGE, CURRENT_TEMPORARY_STORAGE FROM QSYS2.SYSTEM_STATUS_INFO");
        if (rs == null) {
            response.append(Constants.retrieveDataError + " - " + "Cannot retrieve data from server");
            return returnValue;
        }
        while (rs.next()) {
            percentUsed = rs.getDouble("SYSTEM_ASP_USED");
            ASPStorage = rs.getLong("SYSTEM_ASP_STORAGE");
            curTempStorage = rs.getLong("CURRENT_TEMPORARY_STORAGE");
            maxASPUsgVal = percentUsed > maxASPUsgVal ? percentUsed : maxASPUsgVal;
            returnValue = CommonUtil.getStatus(percentUsed, doubleWarningCap, doubleCriticalCap, returnValue);
            response.append("'ASP Usage' = " + usageFormat.format(percentUsed / 100) + ";" + doubleWarningCap + ";" + doubleCriticalCap);
        }
        response.insert(0, "Highest ASP Usage: " + usageFormat.format(maxASPUsgVal / 100) + " | ");
        return returnValue;
    } catch (Exception e) {
        response.setLength(0);
        response.append(Constants.retrieveDataException + " - " + e.toString());
        CommonUtil.printStack(e.getStackTrace(), response);
        CommonUtil.logError(args.get("-H"), this.getClass().getName(), e.getMessage());
        e.printStackTrace();
    } finally {
        try {
            if (rs != null)
                rs.close();
            if (stmt != null)
                stmt.close();
            if (connection != null)
                connection.close();
        } catch (SQLException e) {
            response.append(Constants.retrieveDataException + " - " + e.toString());
            e.printStackTrace();
        }
    }
    return returnValue;
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) JDBCConnection(com.ibm.nagios.util.JDBCConnection) JDBCConnection(com.ibm.nagios.util.JDBCConnection) SQLException(java.sql.SQLException)

Example 8 with JDBCConnection

use of com.ibm.nagios.util.JDBCConnection in project nagios-for-i by IBM.

the class CPUOverloadJobs method execute.

public int execute(AS400 as400, Map<String, String> args, StringBuffer response) {
    double CPUPercentage = 0.;
    int jobCount = 0;
    String user = null;
    String jobName = null;
    String subsystem = null;
    Statement stmt = null;
    ResultSet rs = null;
    String warningCap = args.get("-W");
    String criticalCap = args.get("-C");
    double doubleWarningCap = (warningCap == null) ? 100 : Double.parseDouble(warningCap);
    double doubleCriticalCap = (criticalCap == null) ? 100 : Double.parseDouble(criticalCap);
    int returnValue = Constants.UNKNOWN;
    Connection connection = null;
    try {
        JDBCConnection JDBCConn = new JDBCConnection();
        connection = JDBCConn.getJDBCConnection(as400.getSystemName(), args.get("-U"), args.get("-P"), args.get("-SSL"));
        if (connection == null) {
            response.append(Constants.retrieveDataError + " - " + "Cannot get the JDBC connection");
            return returnValue;
        }
        stmt = connection.createStatement();
        rs = stmt.executeQuery("SELECT SUBSTR(JOB_NAME,8,POSSTR(SUBSTR(JOB_NAME,8),'/')-1) AS JOB_USER, SUBSTR(SUBSTR(JOB_NAME,8),POSSTR(SUBSTR(JOB_NAME,8),'/')+1) AS JOB_NAME, ELAPSED_CPU_PERCENTAGE, SUBSYSTEM FROM TABLE(QSYS2.ACTIVE_JOB_INFO('NO', '', '', '')) AS X");
        if (rs == null) {
            response.append(Constants.retrieveDataError + " - " + "Cannot retrieve data from server");
            return returnValue;
        }
        while (rs.next()) {
            CPUPercentage = rs.getDouble("ELAPSED_CPU_PERCENTAGE");
            returnValue = CommonUtil.getStatus(CPUPercentage, doubleWarningCap, doubleCriticalCap, returnValue);
            if (returnValue != Constants.OK) {
                user = rs.getString("JOB_USER");
                jobName = String.format("%-10s", rs.getString("JOB_NAME"));
                subsystem = rs.getString("SUBSYSTEM");
                subsystem = subsystem != null ? subsystem : "-   ";
                response.append("Job: " + jobName + " USER: " + user + " SUBSYSTEM: " + subsystem + "CPU: " + df.format(CPUPercentage) + "%\n");
                jobCount++;
            }
        }
        response.insert(0, "CPU overload job num: " + jobCount + " | 'CPU overload job num' = " + jobCount + "\n");
        return returnValue;
    } catch (Exception e) {
        response.append(Constants.retrieveDataException + " - " + e.toString());
        CommonUtil.printStack(e.getStackTrace(), response);
        CommonUtil.logError(args.get("-H"), this.getClass().getName(), e.getMessage());
        e.printStackTrace();
    } finally {
        try {
            if (rs != null)
                rs.close();
            if (stmt != null)
                stmt.close();
            if (connection != null)
                connection.close();
        } catch (SQLException e) {
            response.append(Constants.retrieveDataException + " - " + e.toString());
            e.printStackTrace();
        }
    }
    return returnValue;
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) JDBCConnection(com.ibm.nagios.util.JDBCConnection) JDBCConnection(com.ibm.nagios.util.JDBCConnection) SQLException(java.sql.SQLException)

Example 9 with JDBCConnection

use of com.ibm.nagios.util.JDBCConnection in project nagios-for-i by IBM.

the class CustomSQL method execute.

public int execute(AS400 as400, Map<String, String> args, StringBuffer response) {
    Statement stmt = null;
    ResultSet rs = null;
    // for multi value's id field
    int index = 1;
    // for record result number
    int count = 0;
    String outputValue = "";
    ArrayList<String[]> columns = new ArrayList<String[]>();
    String func = args.get("-F");
    String warningCap = args.get("-W");
    String criticalCap = args.get("-C");
    int returnValue = Constants.UNKNOWN;
    if (func == null) {
        response.append("The argument -F [function name] is not set");
        return returnValue;
    }
    Connection connection = null;
    try {
        JDBCConnection JDBCConn = new JDBCConnection();
        connection = JDBCConn.getJDBCConnection(as400.getSystemName(), args.get("-U"), args.get("-P"), args.get("-SSL"));
        if (connection == null) {
            response.append(Constants.retrieveDataError + " - " + "Cannot get the JDBC connection");
            return returnValue;
        }
        CustomBean custBean = CustomPluginFactory.get(func);
        if (custBean == null) {
            response.append(Constants.retrieveDataError + " - " + "function definition not found: " + func);
            return returnValue;
        }
        // if threshold not set by service, load the configuration from CustomSQL.xml
        if (warningCap == null) {
            warningCap = custBean.warning;
        }
        if (criticalCap == null) {
            criticalCap = custBean.critical;
        }
        stmt = connection.createStatement();
        // call the pre commands
        if (!custBean.preCmd.isEmpty()) {
            for (CustomCommand command : custBean.preCmd) {
                if (command.getType().equalsIgnoreCase("CL")) {
                    if (!processClCommand(as400, command.getCommand(), response)) {
                        return returnValue;
                    }
                } else if (command.getType().equalsIgnoreCase("SQL")) {
                    stmt.executeQuery(custBean.sqlCmd);
                }
            }
        }
        rs = stmt.executeQuery(custBean.sqlCmd);
        if (rs == null) {
            response.append(Constants.retrieveDataError + " - " + "Cannot retrieve data from server");
            return returnValue;
        }
        ResultSetMetaData rsmtadta = rs.getMetaData();
        int colCount = rsmtadta.getColumnCount();
        for (int i = 1; i <= colCount; i++) {
            String colName = rsmtadta.getColumnName(i);
            String colType = rsmtadta.getColumnTypeName(i);
            columns.add(new String[] { colName, colType });
        }
        if (custBean.type.equalsIgnoreCase("single-value")) {
            if (rs.next()) {
                if (columns.size() == 1) {
                    String colName = columns.get(0)[0];
                    String colType = columns.get(0)[1];
                    if (colType.equalsIgnoreCase("INTEGER")) {
                        int intWarningCap = (warningCap == null) ? 100 : Integer.parseInt(warningCap);
                        int intCriticalCap = (criticalCap == null) ? 100 : Integer.parseInt(criticalCap);
                        int value = rs.getInt(colName);
                        returnValue = CommonUtil.getStatus(value, intWarningCap, intCriticalCap, returnValue);
                        response.append(custBean.commonName + ": " + value + " | '" + custBean.commonName + "' = " + value + ";" + intWarningCap + ";" + intCriticalCap);
                    } else if (colType.equalsIgnoreCase("DECIMAL")) {
                        double doubleWarningCap = (warningCap == null) ? 100 : Double.parseDouble(warningCap);
                        double doubleCriticalCap = (criticalCap == null) ? 100 : Double.parseDouble(criticalCap);
                        double value = rs.getDouble(colName);
                        returnValue = CommonUtil.getStatus(value, doubleWarningCap, doubleCriticalCap, returnValue);
                        response.append(custBean.commonName + ": " + value + " | '" + custBean.commonName + "' = " + value + ";" + doubleWarningCap + ";" + doubleCriticalCap);
                    }
                } else {
                    response.append("Error: The type of single value only support to select 1 column\n");
                    response.append("SQL: ").append(custBean.sqlCmd);
                    return Constants.UNKNOWN;
                }
            }
        } else if (custBean.type.equalsIgnoreCase("muti-value") || custBean.type.equalsIgnoreCase("multi-value")) {
            String idName = null;
            String colName = null;
            String colType = null;
            if (columns.size() == 1) {
                colName = columns.get(0)[0];
                colType = columns.get(0)[1];
            } else if (columns.size() == 2) {
                idName = columns.get(0)[0];
                colName = columns.get(1)[0];
                colType = columns.get(1)[1];
            } else {
                response.append("Error: The type of multiple value only support to select 1 or 2 columns\n");
                response.append("SQL: ").append(custBean.sqlCmd);
                return Constants.UNKNOWN;
            }
            while (rs.next()) {
                String id = "";
                if (idName != null) {
                    id = rs.getString(idName);
                } else {
                    id = String.valueOf(index++);
                }
                if (colType.equalsIgnoreCase("INTEGER")) {
                    int maxVal = 0;
                    int intWarningCap = (warningCap == null) ? 100 : Integer.parseInt(warningCap);
                    int intCriticalCap = (criticalCap == null) ? 100 : Integer.parseInt(criticalCap);
                    int value = rs.getInt(colName);
                    if (value > maxVal) {
                        maxVal = value;
                        outputValue = String.valueOf(maxVal);
                    }
                    returnValue = CommonUtil.getStatus(value, intWarningCap, intCriticalCap, returnValue);
                    response.append("'" + id + "' = " + value + ";" + intWarningCap + ";" + intCriticalCap);
                } else if (colType.equalsIgnoreCase("DECIMAL")) {
                    double maxVal = 0;
                    double doubleWarningCap = (warningCap == null) ? 100 : Double.parseDouble(warningCap);
                    double doubleCriticalCap = (criticalCap == null) ? 100 : Double.parseDouble(criticalCap);
                    double value = rs.getDouble(colName);
                    if (value > maxVal) {
                        maxVal = value;
                        outputValue = String.valueOf(maxVal);
                    }
                    returnValue = CommonUtil.getStatus(value, doubleWarningCap, doubleCriticalCap, returnValue);
                    response.append("'" + id + "' = " + value + ";" + doubleWarningCap + ";" + doubleCriticalCap);
                }
            }
            response.insert(0, custBean.commonName + ": " + outputValue + " | ");
        } else if (custBean.type.equalsIgnoreCase("list")) {
            double intWarningCap = (warningCap == null) ? 1 : Double.parseDouble(warningCap);
            double intCriticalCap = (criticalCap == null) ? 1 : Double.parseDouble(criticalCap);
            while (rs.next()) {
                for (int i = 0; i < columns.size(); i++) {
                    String colName = columns.get(i)[0];
                    String value = rs.getString(colName);
                    response.append(colName + ": " + value + " ");
                }
                response.append("\n");
                count++;
            }
            returnValue = CommonUtil.getStatus(count, intWarningCap, intCriticalCap, returnValue);
            response.insert(0, custBean.commonName + " count of record: " + count + "\n");
        }
        // call the post commands
        if (!custBean.postCmd.isEmpty()) {
            for (CustomCommand command : custBean.postCmd) {
                if (command.getType().equalsIgnoreCase("CL")) {
                    if (!processClCommand(as400, command.getCommand(), response)) {
                        return Constants.UNKNOWN;
                    }
                } else if (command.getType().equalsIgnoreCase("SQL")) {
                    stmt.executeQuery(custBean.sqlCmd);
                }
            }
        }
    } catch (Exception e) {
        response.append(Constants.retrieveDataException + " -  " + e.toString());
        CommonUtil.printStack(e.getStackTrace(), response);
        CommonUtil.logError(args.get("-H"), this.getClass().getName(), e.getMessage());
        e.printStackTrace();
    } finally {
        try {
            if (rs != null)
                rs.close();
            if (stmt != null)
                stmt.close();
            if (connection != null)
                connection.close();
        } catch (SQLException e) {
            response.append(Constants.retrieveDataException + " -  " + e.toString());
            e.printStackTrace();
        }
    }
    return returnValue;
}
Also used : CustomBean(com.ibm.nagios.util.CustomBean) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) JDBCConnection(com.ibm.nagios.util.JDBCConnection) SQLException(java.sql.SQLException) ResultSetMetaData(java.sql.ResultSetMetaData) CustomCommand(com.ibm.nagios.util.CustomCommand) ResultSet(java.sql.ResultSet) JDBCConnection(com.ibm.nagios.util.JDBCConnection)

Example 10 with JDBCConnection

use of com.ibm.nagios.util.JDBCConnection in project nagios-for-i by IBM.

the class DiskUsage method execute.

@SuppressWarnings("unused")
public int execute(AS400 as400, Map<String, String> args, StringBuffer response) {
    double percentUsed = 0.;
    String aspNumber = null;
    String unitNumber = null;
    String unitType = null;
    long capacity;
    long available;
    double maxDskUsgVal = 0;
    Statement stmt = null;
    ResultSet rs = null;
    String warningCap = args.get("-W");
    String criticalCap = args.get("-C");
    double doubleWarningCap = (warningCap == null) ? 100 : Double.parseDouble(warningCap);
    double doubleCriticalCap = (criticalCap == null) ? 100 : Double.parseDouble(criticalCap);
    int returnValue = Constants.UNKNOWN;
    Connection connection = null;
    try {
        JDBCConnection JDBCConn = new JDBCConnection();
        connection = JDBCConn.getJDBCConnection(as400.getSystemName(), args.get("-U"), args.get("-P"), args.get("-SSL"));
        if (connection == null) {
            response.append(Constants.retrieveDataError + " - " + "Cannot get the JDBC connection");
            return returnValue;
        }
        stmt = connection.createStatement();
        rs = stmt.executeQuery("SELECT ASP_NUMBER, UNIT_NUMBER, UNIT_TYPE, UNIT_STORAGE_CAPACITY, UNIT_SPACE_AVAILABLE, PERCENT_USED FROM QSYS2.SYSDISKSTAT");
        if (rs == null) {
            response.append(Constants.retrieveDataError + " - " + "Cannot retrieve data from server");
            return returnValue;
        }
        while (rs.next()) {
            percentUsed = rs.getDouble("PERCENT_USED");
            aspNumber = rs.getString("ASP_NUMBER");
            unitNumber = rs.getString("UNIT_NUMBER");
            unitType = rs.getString("UNIT_TYPE");
            capacity = rs.getLong("UNIT_STORAGE_CAPACITY") / 1000000;
            available = rs.getLong("UNIT_SPACE_AVAILABLE") / 1000000;
            maxDskUsgVal = percentUsed > maxDskUsgVal ? percentUsed : maxDskUsgVal;
            returnValue = CommonUtil.getStatus(percentUsed, doubleWarningCap, doubleCriticalCap, returnValue);
            response.append("'Unit " + aspNumber + "-" + unitNumber + "'= " + usageFormat.format(percentUsed / 100) + ";" + doubleWarningCap + ";" + doubleCriticalCap);
        }
        response.insert(0, "Highest Disk Usage:" + usageFormat.format(maxDskUsgVal / 100) + " | ");
        return returnValue;
    } catch (Exception e) {
        response.setLength(0);
        response.append(Constants.retrieveDataException + " - " + e.toString());
        CommonUtil.printStack(e.getStackTrace(), response);
        CommonUtil.logError(args.get("-H"), this.getClass().getName(), e.getMessage());
        e.printStackTrace();
    } finally {
        usageFormat = null;
        try {
            if (rs != null)
                rs.close();
            if (stmt != null)
                stmt.close();
            if (connection != null)
                connection.close();
        } catch (SQLException e) {
            response.append(Constants.retrieveDataException + " - " + e.toString());
            e.printStackTrace();
        }
    }
    return returnValue;
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) JDBCConnection(com.ibm.nagios.util.JDBCConnection) JDBCConnection(com.ibm.nagios.util.JDBCConnection) SQLException(java.sql.SQLException)

Aggregations

JDBCConnection (com.ibm.nagios.util.JDBCConnection)14 Connection (java.sql.Connection)14 ResultSet (java.sql.ResultSet)14 SQLException (java.sql.SQLException)14 Statement (java.sql.Statement)14 AS400Message (com.ibm.as400.access.AS400Message)2 AS400Text (com.ibm.as400.access.AS400Text)2 ProgramCallDocument (com.ibm.as400.data.ProgramCallDocument)2 Time (java.sql.Time)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 CustomBean (com.ibm.nagios.util.CustomBean)1 CustomCommand (com.ibm.nagios.util.CustomCommand)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1