Search in sources :

Example 41 with AS400

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

the class QYHCHCOP method run.

public final String run(AS400 system, StringBuffer response) throws Exception {
    final ProgramCall pgm = new ProgramCall(system, programName_, parameters_);
    init(system);
    if (!pgm.run()) {
        AS400Message[] msgList = pgm.getMessageList();
        StackTraceElement[] stackTrace = new Throwable().getStackTrace();
        response.append(Constants.retrieveDataError + " - ");
        for (int i = 0; i < msgList.length; i++) {
            String msgID = msgList[i].getID();
            String errMsg = msgList[i].getText();
            if (msgID.equalsIgnoreCase("MCH1001") && errMsg.equalsIgnoreCase("Attempt to use permanent system object QYHCHCOP without authority.")) {
                errMsg = "Service Disk configuration needs authorities of *ALLOBJ, *SERVICE and *IOSYSCFG";
            }
            response.append(msgID + ": " + errMsg + "\n");
        }
        CommonUtil.printStack(stackTrace, response);
        return null;
    }
    String receiver = new String(parameters_[3].getOutputData(), EBCDIC_CODE_PAGE);
    // byte[] EBCDIC_string = reponse.getBytes(EBCDIC_CODE_PAGE);
    // response = new String(EBCDIC_string,EBCDIC_CODE_PAGE);
    int returnData = BinaryConverter.byteArrayToInt(parameters_[5].getOutputData(), 0);
    if (returnData > OUTPUT_LENGTH) {
        // call again
        parameters_[RECEIVER].setOutputDataLength(returnData);
        parameters_[RECEIVER_LEN].setInputData(BinaryConverter.intToByteArray(returnData));
        if (!pgm.run()) {
            AS400Message[] msgList = pgm.getMessageList();
            StackTraceElement[] stackTrace = new Throwable().getStackTrace();
            response.append(Constants.retrieveDataError + " - " + stackTrace[0].toString());
            for (int i = 0; i < msgList.length; i++) {
                response.append(msgList[i].getID() + ": " + msgList[i].getText() + "\n");
            }
            CommonUtil.printStack(stackTrace, response);
            return null;
        } else {
            receiver = new String(parameters_[3].getOutputData(), EBCDIC_CODE_PAGE);
        }
    }
    errorCode_ = null;
    parameters_ = null;
    return receiver;
}
Also used : AS400Message(com.ibm.as400.access.AS400Message) ProgramCall(com.ibm.as400.access.ProgramCall)

Example 42 with AS400

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

the class Message 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;
    }
    int messageNum = 0;
    int severity = 0;
    String type = null;
    String msgText = null;
    String msgID = null;
    String user = null;
    Date date = null;
    Time time = null;
    Statement stmt = null;
    ResultSet rs = null;
    String msgQueLib = args.get("-LIB");
    msgQueLib = msgQueLib == null ? "QSYS" : msgQueLib.trim().toUpperCase();
    String msgQueName = args.get("-NAME");
    msgQueName = msgQueName == null ? "QSYSOPR" : msgQueName.trim().toUpperCase();
    String msgType = args.get("-TY");
    msgType = msgType == null ? "INQUIRY, ESCAPE, REPLY" : msgType.trim().toUpperCase();
    String[] types = msgType.split(",");
    String requireType = "";
    for (int i = 0; i < types.length; i++) {
        requireType = requireType + "'" + types[i].trim() + "'";
        if (i != types.length - 1)
            requireType += ",";
    }
    Integer 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);
    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();
            rs = stmt.executeQuery("SELECT MESSAGE_ID, FROM_USER, MESSAGE_TYPE, MESSAGE_TEXT, SEVERITY, MESSAGE_TIMESTAMP FROM QSYS2.MESSAGE_QUEUE_INFO WHERE MESSAGE_QUEUE_LIBRARY = '" + msgQueLib.trim() + "' AND MESSAGE_QUEUE_NAME = '" + msgQueName.trim() + "' AND MESSAGE_TYPE IN (" + requireType + ")");
            if (rs == null) {
                response.append(Constants.retrieveDataError + " - " + "Cannot retrieve data from server");
                return returnValue;
            }
            while (rs.next()) {
                msgID = rs.getString("MESSAGE_ID");
                user = rs.getString("FROM_USER");
                type = rs.getString("MESSAGE_TYPE");
                msgText = rs.getString("MESSAGE_TEXT");
                date = rs.getDate("MESSAGE_TIMESTAMP");
                time = rs.getTime("MESSAGE_TIMESTAMP");
                severity = rs.getInt("SEVERITY");
                response.append("ID: " + msgID + " Text: " + msgText + "\n");
                response.append("User: " + user + " Type: " + type + " Severity: " + severity + " Date Sent: " + date + " Time Sent: " + time + "\n");
                messageNum++;
            }
            returnValue = CommonUtil.getStatus(messageNum, intWarningCap, intCriticalCap, returnValue);
            response.insert(0, "Message Num in type " + requireType + ": " + messageNum + "\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;
            types = 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());
            }
        }
    } else {
        // system version is v7r1 or below, run the plugin by API
        ProgramCallDocument pcml = null;
        boolean rc = false;
        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";
            msgQueName += BLANK10.substring(msgQueName.length());
            msgQueLib += BLANK10.substring(msgQueLib.length());
            pcml.setValue(m_parameter, msgQueName + msgQueLib);
            // 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;
                    }
                }
                type = MESSAGE_TYPE[Integer.parseInt(type)];
                if (msgType.contains(type)) {
                    response.append("ID: " + msgID + " Text: " + msgText + "\n");
                    response.append("User: " + user + " Type: " + type + " Severity: " + severity + " Time Sent: " + dateTime + "\n");
                    messageNum++;
                }
            }
            returnValue = CommonUtil.getStatus(messageNum, intWarningCap, intCriticalCap, returnValue);
            response.insert(0, "Message Num in type " + requireType + ": " + messageNum + "\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());
            e.printStackTrace();
        }
    }
    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)

Example 43 with AS400

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

the class HostConfig method Insert.

private static boolean Insert(String hostAddr, String userID, String password, String type) {
    try {
        if (type.equalsIgnoreCase("host")) {
            AS400 as400 = new AS400(hostAddr, userID, password);
            SocketProperties socketProps = new SocketProperties();
            // set timeout to 15 seconds
            socketProps.setLoginTimeout(15000);
            as400.setSocketProperties(socketProps);
            as400.setGuiAvailable(false);
            try {
                as400.validateSignon();
            } catch (Exception e) {
                System.out.println(e.toString());
                return false;
            }
        }
        if (load()) {
            if (type.equalsIgnoreCase("host")) {
                hosts.put(hostAddr, new UserInfo(userID, Base64Coder.encodeString(password)));
            } else if (type.equalsIgnoreCase("sst")) {
                sst.put(hostAddr, new UserInfo(userID, Base64Coder.encodeString(password)));
            } else if (type.equalsIgnoreCase("hmc")) {
                hmc.put(hostAddr, new UserInfo(userID, Base64Coder.encodeString(password)));
            }
            if (save()) {
                refreshProfile();
                return true;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println(e.toString());
        for (StackTraceElement ele : e.getStackTrace()) {
            System.out.println(ele);
        }
    }
    return false;
}
Also used : AS400(com.ibm.as400.access.AS400) UserInfo(com.ibm.nagios.config.util.UserInfo) SocketProperties(com.ibm.as400.access.SocketProperties) IOException(java.io.IOException)

Example 44 with AS400

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

the class ConnectToSystem method run.

@SuppressWarnings("unchecked")
public void run() {
    StringBuffer response = new StringBuffer();
    int retval = Constants.UNKNOWN;
    HashMap<String, String> args = new HashMap<String, String>();
    try {
        InputStream is = socket.getInputStream();
        ObjectInputStream ois = new ObjectInputStream(is);
        args = (HashMap<String, String>) ois.readObject();
        String systemName = args.get("-H");
        String metric = args.get("-M");
        if (systemName == null) {
            if (metric.equalsIgnoreCase("DaemonServer")) {
                // check daemon server status
                CheckIBMiStatus check = new CheckIBMiStatus(null, args);
                retval = check.run(response);
            } else if (metric.equalsIgnoreCase("RefreshProfile")) {
                // reload HostConfigInfo
                HostConfigInfo.load();
            }
        } else if (metric.equalsIgnoreCase("HMC")) {
            CheckIBMiStatus check = new CheckIBMiStatus(null, args);
            retval = check.run(response);
        } else {
            // check IBM i status
            // load user profile and password from Nagios.host.java.config.ser
            String user = HostConfigInfo.getUserID(systemName);
            String pass = HostConfigInfo.getPassword(systemName);
            if (user == null || pass == null) {
                response.append("Host user profile not set");
                return;
            }
            String password = Base64Coder.decodeString(pass);
            args.put("-U", user);
            args.put("-P", password);
            AS400Connection as400Conn = new AS400Connection();
            AS400 as400 = as400Conn.getAS400Object(systemName, user, password, response, args.get("-SSL"));
            if (as400 == null) {
                response.append("\nConnectToSystem - run(): as400 is null");
            } else {
                as400.setGuiAvailable(false);
                // Server.metricPool.execute(new CheckIBMiStatus(as400, socket, args));
                CheckIBMiStatus check = new CheckIBMiStatus(as400, args);
                retval = check.run(response);
            }
        }
    } 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 {
            PrintResponse(retval, response);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        response = null;
    }
}
Also used : AS400Connection(com.ibm.nagios.util.AS400Connection) HashMap(java.util.HashMap) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) AS400(com.ibm.as400.access.AS400) IOException(java.io.IOException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

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