Search in sources :

Example 1 with AS400

use of com.ibm.as400.access.AS400 in project nagios-for-i by IBM.

the class CheckIBMiStatus method main.

public static void main(String[] args) {
    int retValue = Constants.UNKNOWN;
    try {
        ParseArgs(args);
        socket = new Socket(Constants.SERVER, Constants.PORT);
        socket.setReuseAddress(true);
        socket.setSoLinger(true, 0);
        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        oos.writeObject(argsMap);
        socket.shutdownOutput();
        InputStream is = socket.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String result = null;
        if ((result = br.readLine()) != null) {
            String message;
            while ((message = br.readLine()) != null) {
                System.out.println(message);
            }
            retValue = Integer.parseInt(result);
        }
    } catch (IOException e) {
        try {
            String metric = argsMap.get("-M");
            String system = argsMap.get("-H");
            Action action = ActionFactory.get(metric);
            StringBuffer response = new StringBuffer();
            if (!HostConfigInfo.load()) {
                System.err.println("load file Nagios.host.java.config.ser error");
                System.exit(retValue);
            }
            if ("HMC".equalsIgnoreCase(metric)) {
                retValue = action.execute(null, argsMap, response);
            } else {
                String user = HostConfigInfo.getUserID(system);
                String pass = HostConfigInfo.getPassword(system);
                if (user == null || pass == null) {
                    System.err.println("Host user profile not set");
                    System.exit(retValue);
                }
                String password = Base64Coder.decodeString(pass);
                argsMap.put("-U", user);
                argsMap.put("-P", password);
                AS400 as400 = null;
                String ssl = argsMap.get("-SSL");
                if (ssl != null && ssl.equalsIgnoreCase("Y")) {
                    as400 = new SecureAS400(system, user, password);
                } else {
                    as400 = new AS400(system, user, password);
                }
                as400.setGuiAvailable(false);
                as400.validateSignon();
                retValue = action.execute(as400, argsMap, response);
            }
            System.out.println(response);
        } catch (Exception e1) {
            retValue = Constants.UNKNOWN;
            e1.printStackTrace();
        }
    } finally {
        try {
            if (socket != null) {
                socket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    System.exit(retValue);
}
Also used : Action(com.ibm.nagios.service.Action) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) IOException(java.io.IOException) SecureAS400(com.ibm.as400.access.SecureAS400) BufferedReader(java.io.BufferedReader) AS400(com.ibm.as400.access.AS400) SecureAS400(com.ibm.as400.access.SecureAS400) Socket(java.net.Socket)

Example 2 with AS400

use of com.ibm.as400.access.AS400 in project nagios-for-i by IBM.

the class DiskConfig method execute.

public int execute(AS400 as400, Map<String, String> args, StringBuffer response) {
    int returnValue = Constants.UNKNOWN;
    String system = args.get("-H");
    String userID = HostConfigInfo.getSSTUserID(system);
    String pass = HostConfigInfo.getSSTPassword(system);
    if (userID == null || pass == null) {
        response.append("SST user profile not set");
        return returnValue;
    }
    int userLen = userID.length();
    String password = Base64Coder.decodeString(pass);
    int passLen = password.length();
    final int ccsid = as400.getCcsid();
    QYHCHCOP pgmCall = null;
    AS400Text userText = new AS400Text(userLen, ccsid, as400);
    AS400Text passText = new AS400Text(passLen, ccsid, as400);
    try {
        pgmCall = new QYHCHCOP();
        pgmCall.setParameter(pgmCall.USER_ID, userText.toBytes(userID));
        pgmCall.setParameter(pgmCall.USER_ID_LEN, BinaryConverter.intToByteArray(userLen));
        pgmCall.setParameter(pgmCall.PASSWORD, passText.toBytes(password));
        pgmCall.setParameter(pgmCall.PASSWOD_LEN, BinaryConverter.intToByteArray(passLen));
        String receiver = pgmCall.run(as400, response);
        if (receiver == null) {
            return returnValue;
        }
        ArrayList<Disk> disks = DiskXMLReader.getDiskConfigInfo(receiver.trim());
        int ASPNum;
        int unitNum;
        String resourceName = null;
        int status;
        int warningStatus = 0;
        returnValue = Constants.OK;
        for (int i = 0; i < disks.size(); i++) {
            Disk disk = disks.get(i);
            ASPNum = disk.getASPNum();
            unitNum = disk.getDiskNum();
            resourceName = disk.getResourceName();
            status = disk.getStatus();
            if (status != Hardware_OK && returnValue != Constants.WARN) {
                returnValue = Constants.WARN;
                warningStatus = status;
            }
            response.append("Unit " + ASPNum + "-" + unitNum + ": " + resourceName + " Status: " + STATUS[status] + "\n");
        }
        if (returnValue == Constants.OK) {
            response.insert(0, "Disk Status: OK\n");
        } else if (returnValue == Constants.WARN) {
            response.insert(0, "Disk Status: " + STATUS[warningStatus] + "\n");
        }
    } catch (Exception e) {
        response.setLength(0);
        response.append(Constants.retrieveDataException + " - " + e.toString());
        CommonUtil.printStack(e.getStackTrace(), response);
        e.printStackTrace();
    } finally {
        userText = null;
        passText = null;
        pgmCall = null;
    }
    return returnValue;
}
Also used : AS400Text(com.ibm.as400.access.AS400Text) QYHCHCOP(com.ibm.nagios.util.QYHCHCOP) Disk(com.ibm.nagios.model.Disk)

Example 3 with AS400

use of com.ibm.as400.access.AS400 in project nagios-for-i by IBM.

the class SpecificMessage method execute.

public int execute(AS400 as400, Map<String, String> args, StringBuffer response) {
    int version = 0;
    int release = 0;
    try {
        version = as400.getVersion();
        release = as400.getRelease();
    } catch (Exception eVR) {
        response.append(Constants.retrieveDataException + " - " + eVR.getMessage());
        return Constants.UNKNOWN;
    }
    Integer returnValue = Constants.UNKNOWN;
    Date date = null;
    Time time = null;
    String msgText = null;
    int severity = 0;
    String user = null;
    Set<String> msgSet = new HashSet<String>();
    Statement stmt = null;
    ResultSet rs = null;
    String sqlType = null;
    String regEx = null;
    int count = 0;
    String messageID = args.get("-I");
    if (messageID == null) {
        response.append("The argument -I [message ID] is not set");
        return returnValue;
    }
    if (messageID.indexOf("*") != -1) {
        sqlType = "like";
        regEx = messageID;
        messageID = messageID.replace("*", "%");
    } else {
        sqlType = "in";
        String[] splitTemp = messageID.split(",");
        int msgNum = splitTemp.length;
        messageID = "";
        for (int i = 0; i < msgNum; i++) {
            messageID += "'" + splitTemp[i] + "'";
            if (i != msgNum - 1) {
                messageID += ",";
            }
        }
    }
    if (version > 7 || (version == 7 && release > 1)) {
        // system version is priority to v7r1, run the plugin by SQL service
        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();
            if (sqlType.equalsIgnoreCase("like")) {
                rs = stmt.executeQuery("SELECT MESSAGE_ID, FROM_USER, MESSAGE_TEXT, SEVERITY, MESSAGE_TIMESTAMP FROM QSYS2.MESSAGE_QUEUE_INFO WHERE MESSAGE_ID LIKE '" + messageID.toUpperCase() + "'");
            } else if (sqlType.equalsIgnoreCase("in")) {
                rs = stmt.executeQuery("SELECT MESSAGE_ID, FROM_USER, MESSAGE_TEXT, SEVERITY, MESSAGE_TIMESTAMP FROM QSYS2.MESSAGE_QUEUE_INFO WHERE MESSAGE_ID IN (" + messageID.toUpperCase() + ")");
            }
            if (rs == null) {
                response.append(Constants.retrieveDataError + " - " + "Cannot retrieve data from server");
                return returnValue;
            }
            while (rs.next()) {
                user = rs.getString("FROM_USER");
                severity = rs.getInt("SEVERITY");
                date = rs.getDate("MESSAGE_TIMESTAMP");
                time = rs.getTime("MESSAGE_TIMESTAMP");
                messageID = rs.getString("MESSAGE_ID");
                msgText = rs.getString("MESSAGE_TEXT");
                msgSet.add(messageID);
                count++;
                returnValue = Constants.WARN;
                response.append("Message ID: " + messageID + " User: " + user + " Severity: " + severity + " Date: " + date + " Time: " + time + " Text: " + msgText + "\n");
            }
            if (count == 0) {
                returnValue = Constants.OK;
                response.append("Status: OK");
            } else {
                response.insert(0, "Message counts: " + count + "\n");
            }
        } 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 {
            date = null;
            time = 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();
            }
        }
    } else {
        // system version is v7r1 or below, run the plugin by API
        ProgramCallDocument pcml = null;
        boolean rc = false;
        String msgID = null;
        String strTime = null;
        String strDate = null;
        String m_parameter;
        try {
            pcml = new ProgramCallDocument(as400, "com.ibm.nagios.util.qgyolmsg");
            int[] fieldIndex = new int[1];
            // Set selection criteria value
            m_parameter = "qgyolmsg.msgSelInfo.selectionCriteria";
            fieldIndex[0] = 0;
            pcml.setValue(m_parameter, fieldIndex, "*ALL      ");
            // Set message queue information for the list
            m_parameter = "qgyolmsg.userOrQueueInfo.userOrQueueIndicator";
            pcml.setValue(m_parameter, "1");
            m_parameter = "qgyolmsg.userOrQueueInfo.userOrQueueName";
            pcml.setValue(m_parameter, "QSYSOPR   QSYS      ");
            // Set starting message key
            m_parameter = "qgyolmsg.msgSelInfo.messageKeys";
            fieldIndex[0] = 0;
            pcml.setValue(m_parameter, fieldIndex, 0);
            fieldIndex[0] = 1;
            pcml.setValue(m_parameter, fieldIndex, 65535);
            // Set fields to return
            int[] keyIndex;
            int keyValue;
            keyIndex = new int[1];
            m_parameter = "qgyolmsg.msgSelInfo.fldsToReturn";
            keyIndex[0] = 0;
            // Message
            keyValue = 301;
            pcml.setIntValue(m_parameter, keyIndex, keyValue);
            keyIndex[0] = 1;
            // User profile
            keyValue = 607;
            pcml.setIntValue(m_parameter, keyIndex, keyValue);
            keyIndex[0] = 2;
            // Reply status
            keyValue = 1001;
            pcml.setIntValue(m_parameter, keyIndex, keyValue);
            rc = pcml.callProgram("qgyolmsg");
            if (rc == false) {
                // Print out all of the AS/400 messages
                AS400Message[] messageList = pcml.getMessageList("qgyolmsg");
                for (int i = 0; i < messageList.length; i++) {
                    response.append(Constants.retrieveDataException + " - " + messageList[i].getID() + "  " + messageList[i].getText());
                }
                return Constants.UNKNOWN;
            }
            // Get number messages returned
            m_parameter = "qgyolmsg.listInfo.rcdsReturned";
            int nbrEntries = pcml.getIntValue(m_parameter);
            int[] index = new int[1];
            fieldIndex = new int[2];
            for (int i = 0; i < nbrEntries; i++) {
                index[0] = i;
                // Retrieve message ID
                msgID = pcml.getStringValue("qgyolmsg.lstm0100.msgEntry.msgID", index).trim();
                // Retrieve message type
                // type = pcml.getStringValue("qgyolmsg.lstm0100.msgEntry.msgType", index).trim();
                // Retrieve sent time
                strTime = pcml.getStringValue("qgyolmsg.lstm0100.msgEntry.timeSent", index);
                // Retrieve sent date
                strDate = pcml.getStringValue("qgyolmsg.lstm0100.msgEntry.dateSent", index).substring(1);
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");
                Date dateTime = dateFormat.parse(strDate + strTime);
                // Retrieve filed number
                int nbrKeyFields = pcml.getIntValue("qgyolmsg.lstm0100.msgEntry.nbrOfFields", index);
                for (int j = 0; j < nbrKeyFields; j++) {
                    fieldIndex[0] = i;
                    fieldIndex[1] = j;
                    // Retrieve key field data
                    byte[] keyData = (byte[]) pcml.getValue("qgyolmsg.lstm0100.msgEntry.fieldInfo.keyData", fieldIndex);
                    // Retrieve field key
                    int dataKey = pcml.getIntValue("qgyolmsg.lstm0100.msgEntry.fieldInfo.keyField", fieldIndex);
                    // Retrieve data length
                    int dataLength = pcml.getIntValue("qgyolmsg.lstm0100.msgEntry.fieldInfo.lengthOfData", fieldIndex);
                    switch(dataKey) {
                        case 301:
                            // Set the message: Char(*)
                            if (dataLength > 0) {
                                AS400Text text = new AS400Text(dataLength, as400.getCcsid(), as400);
                                msgText = (String) text.toObject(keyData, 0);
                                text = null;
                            }
                            break;
                        case 607:
                            // Set the user profile: Char(*)
                            if (dataLength > 0) {
                                AS400Text text = new AS400Text(dataLength, as400.getCcsid(), as400);
                                user = (String) text.toObject(keyData, 0);
                                text = null;
                            }
                            break;
                    }
                }
                if (sqlType.equalsIgnoreCase("in") && messageID.contains(msgID)) {
                    response.append("Message ID: " + msgID + " User: " + user + " Severity: " + severity + " Time: " + dateTime + " Text: " + msgText + "\n");
                    msgSet.add(msgID);
                    count++;
                    returnValue = Constants.WARN;
                } else if (sqlType.equalsIgnoreCase("like")) {
                    if (Pattern.compile(regEx).matcher(msgID).find()) {
                        response.append("Message ID: " + msgID + " User: " + user + " Severity: " + severity + " Time: " + dateTime + " Text: " + msgText + "\n");
                        msgSet.add(msgID);
                        count++;
                        returnValue = Constants.WARN;
                    }
                }
            }
            if (count == 0) {
                returnValue = Constants.OK;
                response.append("Status: OK");
            } else {
                response.insert(0, "Message counts: " + count + "\n");
            }
            m_parameter = "qgyolmsg.listInfo.rqsHandle";
            Object handle = pcml.getValue(m_parameter);
            m_parameter = "qgyclst.closeHand";
            pcml.setValue(m_parameter, handle);
            m_parameter = "qgyclst";
            pcml.callProgram("qgyclst");
        } catch (Exception e) {
            response.setLength(0);
            response.append(Constants.retrieveDataException + " - " + e.toString());
        }
    }
    return returnValue;
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) JDBCConnection(com.ibm.nagios.util.JDBCConnection) Time(java.sql.Time) SQLException(java.sql.SQLException) Date(java.util.Date) ProgramCallDocument(com.ibm.as400.data.ProgramCallDocument) AS400Text(com.ibm.as400.access.AS400Text) ResultSet(java.sql.ResultSet) AS400Message(com.ibm.as400.access.AS400Message) SimpleDateFormat(java.text.SimpleDateFormat) JDBCConnection(com.ibm.nagios.util.JDBCConnection) HashSet(java.util.HashSet)

Example 4 with AS400

use of com.ibm.as400.access.AS400 in project nagios-for-i by IBM.

the class CurSignOnUsers method execute.

public int execute(AS400 as400, Map<String, String> args, StringBuffer response) {
    int ccsid = as400.getCcsid();
    AS400Text text4 = new AS400Text(4, ccsid, as400);
    byte[] data = null;
    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);
    QWCRSSTS pgmCall = new QWCRSSTS();
    try {
        data = pgmCall.run(as400, response);
        if (data == null) {
            response.append(Constants.retrieveDataError);
            return returnValue;
        }
        int signOnUserNum = BinaryConverter.byteArrayToInt(text4.toBytes(text4.toObject(data, 24)), 0);
        returnValue = CommonUtil.getStatus(signOnUserNum, intWarningCap, intCriticalCap, returnValue);
        response.insert(0, "Currently Sign On Users Num: " + signOnUserNum + " | 'Currently Sign On Users Num' = " + signOnUserNum + ";" + warningCap + ";" + criticalCap);
    } catch (Exception e) {
        response.append(Constants.retrieveDataException + " - " + e.toString());
        CommonUtil.printStack(e.getStackTrace(), response);
        e.printStackTrace();
    } finally {
        data = null;
    }
    return returnValue;
}
Also used : AS400Text(com.ibm.as400.access.AS400Text) QWCRSSTS(com.ibm.nagios.util.QWCRSSTS)

Example 5 with AS400

use of com.ibm.as400.access.AS400 in project nagios-for-i by IBM.

the class CustomSQL method processClCommand.

private boolean processClCommand(AS400 as400, String command, StringBuffer response) throws Exception {
    if (cmd == null) {
        cmd = new CommandCall(as400);
    }
    cmd.setCommand(command);
    boolean rc = cmd.run();
    if (!rc) {
        AS400Message[] msgList = cmd.getMessageList();
        response.append(Constants.retrieveDataError).append(" - ").append("Failed to call the command:").append(command).append("\n");
        for (AS400Message message : msgList) {
            response.append(message.getID()).append(":").append(message.getText()).append("\n");
        }
    }
    return rc;
}
Also used : CommandCall(com.ibm.as400.access.CommandCall) AS400Message(com.ibm.as400.access.AS400Message)

Aggregations

IFSFile (com.ibm.as400.access.IFSFile)16 AS400Text (com.ibm.as400.access.AS400Text)14 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)14 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)14 AS400Message (com.ibm.as400.access.AS400Message)12 AS400 (com.ibm.as400.access.AS400)11 CommandCall (com.ibm.as400.access.CommandCall)9 IOException (java.io.IOException)8 AS400FileRecordDescription (com.ibm.as400.access.AS400FileRecordDescription)5 Record (com.ibm.as400.access.Record)5 RecordFormat (com.ibm.as400.access.RecordFormat)5 SequentialFile (com.ibm.as400.access.SequentialFile)5 BadLocationException (javax.swing.text.BadLocationException)5 ProgramCall (com.ibm.as400.access.ProgramCall)4 Point (java.awt.Point)4 Path (java.nio.file.Path)4 AS400SecurityException (com.ibm.as400.access.AS400SecurityException)3 RequestNotSupportedException (com.ibm.as400.access.RequestNotSupportedException)3 IntrospectionException (java.beans.IntrospectionException)3 ConnectorException (org.identityconnectors.framework.common.exceptions.ConnectorException)3