use of com.ibm.nagios.util.JDBCConnection in project nagios-for-i by IBM.
the class IASPUsage method execute.
@SuppressWarnings("unused")
public int execute(AS400 as400, Map<String, String> args, StringBuffer response) {
double percentUsed = 0.;
String description = null;
int aspNum;
String type = null;
String state = null;
long totalCapacity;
long capacityAvailable;
double maxIASPUsgVal = 0;
int IASPNum = 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 DEVICE_DESCRIPTION_NAME, ASP_NUMBER, ASP_STATE, ASP_TYPE, TOTAL_CAPACITY, TOTAL_CAPACITY_AVAILABLE FROM QSYS2.ASP_INFO WHERE ASP_NUMBER>32");
if (rs == null) {
response.append(Constants.retrieveDataError + " - " + "Cannot retrieve data from server");
return returnValue;
}
while (rs.next()) {
IASPNum++;
aspNum = rs.getInt("ASP_NUMBER");
description = rs.getString("DEVICE_DESCRIPTION_NAME");
state = rs.getString("ASP_STATE");
type = rs.getString("ASP_TYPE");
totalCapacity = rs.getLong("TOTAL_CAPACITY");
capacityAvailable = rs.getLong("TOTAL_CAPACITY_AVAILABLE");
percentUsed = (double) capacityAvailable / (double) totalCapacity;
maxIASPUsgVal = percentUsed > maxIASPUsgVal ? percentUsed : maxIASPUsgVal;
returnValue = CommonUtil.getStatus(percentUsed, doubleWarningCap, doubleCriticalCap, returnValue);
response.append("'IASP " + aspNum + "'= " + usageFormat.format(percentUsed / 100) + ";" + doubleWarningCap + ";" + doubleCriticalCap);
}
if (IASPNum == 0) {
response.insert(0, "IASP not found");
} else if (returnValue == Constants.UNKNOWN) {
response.setLength(0);
response.append("IASP state not right: " + state);
} else {
response.insert(0, "Highest IASP Usage: " + usageFormat.format(maxIASPUsgVal / 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;
}
use of com.ibm.nagios.util.JDBCConnection in project nagios-for-i by IBM.
the class LongRunSQL method execute.
public int execute(AS400 as400, Map<String, String> args, StringBuffer response) {
double duration = 0.;
int warnCount = 0;
int criticalCount = 0;
String stmtText = null;
Statement stmt = null;
ResultSet rs = null;
String warningCap = args.get("-W");
String criticalCap = args.get("-C");
// turn to millisecond
double doubleWarningCap = (warningCap == null) ? 10 : Double.parseDouble(warningCap);
// turn to millisecond
double doubleCriticalCap = (criticalCap == null) ? 6 : 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("WITH ACTIVE_USER_JOBS (Q_JOB_NAME, CPU_TIME, RUN_PRIORITY) AS (" + "SELECT JOB_NAME, CPU_TIME, RUN_PRIORITY FROM TABLE (QSYS2.ACTIVE_JOB_INFO('NO','','','')) x WHERE JOB_TYPE <> 'SYS'" + ") SELECT Q_JOB_NAME, CPU_TIME, RUN_PRIORITY, V_SQL_STATEMENT_TEXT, CURRENT TIMESTAMP - V_SQL_STMT_START_TIMESTAMP AS SQL_STMT_DURATION, B.* FROM ACTIVE_USER_JOBS, TABLE(QSYS2.GET_JOB_INFO(Q_JOB_NAME)) B " + "WHERE V_SQL_STMT_STATUS = 'ACTIVE' ORDER BY SQL_STMT_DURATION DESC");
if (rs == null) {
response.append(Constants.retrieveDataError + " - " + "Cannot retrieve data from server");
return returnValue;
}
while (rs.next()) {
duration = rs.getDouble("SQL_STMT_DURATION");
stmtText = rs.getString("V_SQL_STATEMENT_TEXT");
returnValue = CommonUtil.getStatus(duration, doubleWarningCap, doubleCriticalCap, returnValue);
if (returnValue == Constants.CRITICAL) {
criticalCount++;
response.append("CRITICAL: Statement Duration=" + df.format(duration) + "s\n Stmt Text: " + stmtText);
}
if (returnValue == Constants.WARN) {
warnCount++;
response.append("WARNING: Statement Duration=" + df.format(duration) + "s\n Stmt Text: " + stmtText);
}
}
if (returnValue == Constants.WARN) {
response.insert(0, "Long Run SQL Status: Warning Num: " + warnCount + " (Warning: " + doubleWarningCap + " Critical: " + doubleCriticalCap + ")\n");
} else if (returnValue == Constants.CRITICAL) {
response.insert(0, "Long Run SQL Status: Critical Num: " + criticalCount + " (Warning: " + doubleWarningCap + " Critical: " + doubleCriticalCap + ")\n");
} else if (returnValue == Constants.OK) {
response.insert(0, "Long Run SQL Status: OK\n");
}
} catch (Exception e) {
String errMsg = e.getMessage();
if (errMsg.contains("[SQL0443] NOT AUTHORIZED")) {
errMsg = "Service long run sql needs authority of *ALLOBJ";
}
response.append(Constants.retrieveDataException + " - " + errMsg == null ? e.toString() : errMsg);
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;
}
use of com.ibm.nagios.util.JDBCConnection in project nagios-for-i by IBM.
the class PageFaults method execute.
public int execute(AS400 as400, Map<String, String> args, StringBuffer response) {
String poolName = null;
double totalFaults = 0;
double maxFaults = 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 POOL_NAME, ELAPSED_TOTAL_FAULTS FROM QSYS2.POOL_INFO");
if (rs == null) {
response.append(Constants.retrieveDataError + " - " + "Cannot retrieve data from server");
return returnValue;
}
while (rs.next()) {
poolName = rs.getString("POOL_NAME");
totalFaults = rs.getDouble("ELAPSED_TOTAL_FAULTS");
maxFaults = totalFaults > maxFaults ? totalFaults : maxFaults;
returnValue = CommonUtil.getStatus(totalFaults, doubleWarningCap, doubleCriticalCap, returnValue);
response.append("'" + poolName + "'=" + totalFaults + ";" + doubleWarningCap + ";" + doubleCriticalCap);
}
response.insert(0, "Highest Page Faults: " + maxFaults + " | ");
} 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;
}
use of com.ibm.nagios.util.JDBCConnection in project nagios-for-i by IBM.
the class SpecificJobCPU method execute.
public int execute(AS400 as400, Map<String, String> args, StringBuffer response) {
int returnValue = Constants.UNKNOWN;
double CPUPercentage = 0.;
Statement stmt = null;
ResultSet rs = null;
String jobName = args.get("-J");
if (jobName == null) {
response.append("The argument -J [job name] is not set");
return returnValue;
}
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);
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();
// run the sql command first to set the base value
rs = stmt.executeQuery("SELECT ELAPSED_CPU_PERCENTAGE FROM TABLE(QSYS2.ACTIVE_JOB_INFO('NO', '', '" + jobName + "', '')) X");
// wait 5 seconds, run the SQL command second time to calculate the value of ELAPSED_CPU_PERCENTAG by interval
Thread.sleep(5000);
rs = stmt.executeQuery("SELECT ELAPSED_CPU_PERCENTAGE FROM TABLE(QSYS2.ACTIVE_JOB_INFO('NO', '', '" + jobName + "', '')) X");
if (rs == null) {
response.append(Constants.retrieveDataError + " - " + "Cannot retrieve data from server");
return returnValue;
}
response.append(jobName + " is not active");
while (rs.next()) {
CPUPercentage = rs.getDouble("ELAPSED_CPU_PERCENTAGE");
returnValue = CommonUtil.getStatus(CPUPercentage, doubleWarningCap, doubleCriticalCap, returnValue);
response.setLength(0);
response.append("Job: ").append(jobName).append(" CPU: ").append(CPUPercentage).append("% ").append(" | CPU = ").append(CPUPercentage).append("%;").append(doubleWarningCap).append(";").append(doubleCriticalCap).append("\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;
}
use of com.ibm.nagios.util.JDBCConnection 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;
}
Aggregations