Java tutorial
/* Copyright (C) 2010 NTT DATA Corporation This program is free software; you can redistribute it and/or Modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */ package com.clustercontrol.selfcheck.monitor; import java.util.ArrayList; import java.util.Date; import java.util.HashSet; import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.clustercontrol.bean.PriorityConstant; import com.clustercontrol.bean.SnmpVersionConstant; import com.clustercontrol.maintenance.util.HinemosPropertyUtil; import com.clustercontrol.poller.bean.PollerProtocolConstant; import com.clustercontrol.poller.impl.Snmp4jPollerImpl; import com.clustercontrol.poller.util.DataTable; import com.clustercontrol.poller.util.TableEntry; import com.clustercontrol.util.HinemosTime; import com.clustercontrol.util.MessageConstant; import com.clustercontrol.util.apllog.AplLogger; /** * ????? */ public class RAMSwapOutMonitor extends SelfCheckMonitorBase { private static Log m_log = LogFactory.getLog(RAMSwapOutMonitor.class); public final String monitorId = "SYS_SWAPOUT"; public final String subKey = ""; public final String application = "SELFCHECK (swap-out)"; public long intervalMSec; public int snmpPort; public int snmpVersion; public String snmpCommunity; public int snmpRetries; public int snmpTimeout; public static final String SNMP_POLLING_IPADDRESS = "127.0.0.1"; public static final String POLLING_TARGET_OID = ".1.3.6.1.4.1.2021.11.63"; public static final String RAW_SWAP_OUT_OUT_OID = ".1.3.6.1.4.1.2021.11.63.0"; private static volatile TableEntry previousMibValue = null; /** * * @param snmpPort SNMP?? * @param snmpVersion SNMP? * @param snmpCommunity SNMP?? * @param snmpRetries SNMP * @param snmpTimeout SNMP[msec] */ public RAMSwapOutMonitor() { } /** * ???? */ @Override public String toString() { return "monitoring ram's swap-out."; } /** * ID */ @Override public String getMonitorId() { return monitorId; } /** * ???? * @return ???? */ @Override public void execute() { if (!HinemosPropertyUtil.getHinemosPropertyBool("selfcheck.monitoring.swapout", false)) { m_log.debug("skip"); return; } /** */ long swapOutSize = 0; long lastUpdateTime = 0; boolean warn = true; this.intervalMSec = HinemosPropertyUtil.getHinemosPropertyNum("selfcheck.interval", Long.valueOf(150)) * 1000; this.snmpPort = HinemosPropertyUtil.getHinemosPropertyNum("selfcheck.snmp.port", Long.valueOf(161)) .intValue(); String snmpVersionStr = HinemosPropertyUtil.getHinemosPropertyStr("selfcheck.snmp.version", SnmpVersionConstant.STRING_V2); this.snmpVersion = SnmpVersionConstant.stringToType(snmpVersionStr); this.snmpCommunity = HinemosPropertyUtil.getHinemosPropertyStr("selfcheck.snmp.community", "public"); this.snmpRetries = HinemosPropertyUtil.getHinemosPropertyNum("selfcheck.snmp.retries", Long.valueOf(3)) .intValue(); this.snmpTimeout = HinemosPropertyUtil.getHinemosPropertyNum("selfcheck.snmp.timeout", Long.valueOf(3000)) .intValue(); /** ? */ if (m_log.isDebugEnabled()) m_log.debug("monitoring swap-out."); // ??? if (previousMibValue != null) { lastUpdateTime = previousMibValue.getDate(); } // ???MByte????? swapOutSize = getSwapOut(); if (swapOutSize < 0) { m_log.info("skipped monitoring swap-out."); return; } else if (swapOutSize == 0) { m_log.debug("swap-out does not occurred."); warn = false; } if (warn) { m_log.info("swap-out occurred. (swapOutSize=" + swapOutSize + ")"); } if (!isNotify(subKey, warn)) { return; } String[] msgAttr1 = { Long.toString(swapOutSize) }; AplLogger.put(PriorityConstant.TYPE_WARNING, PLUGIN_ID, MessageConstant.MESSAGE_SYS_005_SYS_SFC, msgAttr1, "ram swap-out(" + swapOutSize + " [blocks]) occurred since " + String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", new Date(lastUpdateTime)) + "."); return; } /** * Swap Out ????????? * ???????????2???????? * -1??? */ private long getSwapOut() { Snmp4jPollerImpl poller = null; DataTable dataTable = null; TableEntry entry = null; long now = 0; long lastUpdateTime = 0; long previousSwapOut = 0; long currentSwapOut = 0; long swapOut = -1; /** ? */ try { // ??OID Set<String> oidSet = new HashSet<String>(); oidSet.add(POLLING_TARGET_OID); // ????? poller = Snmp4jPollerImpl.getInstance(); dataTable = poller.polling(SNMP_POLLING_IPADDRESS, snmpPort, snmpVersion, snmpCommunity, snmpRetries, snmpTimeout, oidSet, null, null, null, null, null, null); entry = dataTable.getValue(getEntryKey(RAW_SWAP_OUT_OUT_OID)); // ???? if (previousMibValue != null) { now = HinemosTime.currentTimeMillis(); // ????? lastUpdateTime = previousMibValue.getDate(); // ??2???????? if ((now - lastUpdateTime) < intervalMSec * 2) { previousSwapOut = (Long) previousMibValue.getValue(); currentSwapOut = (Long) entry.getValue(); // ????????? swapOut = currentSwapOut - previousSwapOut; } } // ????????????? previousMibValue = entry; } catch (Exception e) { m_log.warn("failed to snmp polling.", e); } // ?????????????????????? return swapOut; } /** * OIDTableEntry???? */ private String getEntryKey(String oidString) { return PollerProtocolConstant.PROTOCOL_SNMP + "." + oidString; } public static ArrayList<String> getOidList() { ArrayList<String> oidList = new ArrayList<String>(); oidList.add(POLLING_TARGET_OID); oidList.add(RAW_SWAP_OUT_OUT_OID); return oidList; } /** * ? */ public static void main(String[] args) { long swapOut = new RAMSwapOutMonitor().getSwapOut(); try { Thread.sleep(1500); } catch (InterruptedException e) { e.printStackTrace(); } swapOut = new RAMSwapOutMonitor().getSwapOut(); System.out.println("Usage : " + swapOut); } }