Java tutorial
/* Copyright (C) 2006 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.process.factory; import java.text.NumberFormat; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.Vector; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.clustercontrol.bean.HinemosModuleConstant; import com.clustercontrol.fault.FacilityNotFound; import com.clustercontrol.fault.HinemosUnknown; import com.clustercontrol.fault.InvalidRole; import com.clustercontrol.fault.MonitorNotFound; import com.clustercontrol.monitor.run.factory.RunMonitor; import com.clustercontrol.monitor.run.factory.RunMonitorNumericValueType; import com.clustercontrol.monitor.run.util.NodeToMonitorCache; import com.clustercontrol.poller.bean.PollerProtocolConstant; import com.clustercontrol.poller.impl.Snmp4jPollerImpl; import com.clustercontrol.poller.impl.WbemPollerImpl; import com.clustercontrol.poller.util.DataTable; import com.clustercontrol.poller.util.TableEntry; import com.clustercontrol.process.entity.MonitorProcessPollingMstData; import com.clustercontrol.process.entity.MonitorProcessPollingMstPK; import com.clustercontrol.process.model.ProcessCheckInfo; import com.clustercontrol.process.util.PollingDataManager; import com.clustercontrol.process.util.ProcessProperties; import com.clustercontrol.process.util.QueryUtil; import com.clustercontrol.repository.model.NodeInfo; import com.clustercontrol.repository.session.RepositoryControllerBean; import com.clustercontrol.util.MessageConstant; /** * ?<BR> * * @version 4.0.0 * @since 2.0.0 */ public class RunMonitorProcess extends RunMonitorNumericValueType { private static Log m_log = LogFactory.getLog(RunMonitorProcess.class); /** */ private ProcessCheckInfo m_process = null; /** */ private String m_command = ""; /** */ private String m_param = ""; /** */ private String m_message = null; /** */ private String m_messageOrg = null; /** * * @throws HinemosUnknown */ public RunMonitorProcess() throws HinemosUnknown { super(); } /** * ???CallableTask??????? * * @throws HinemosUnknown * * @see com.clustercontrol.monitor.run.factory.RunMonitor#runMonitorInfo() * @see com.clustercontrol.monitor.run.util.MonitorExecuteTask */ @Override protected RunMonitor createMonitorInstance() throws HinemosUnknown { // ???? RunMonitor monitor = new RunMonitorProcess(); monitor.setMessage(m_message); return monitor; } /** * @see runMonitorAggregateByNode */ protected boolean runMonitorInfo() throws FacilityNotFound, MonitorNotFound, InvalidRole, HinemosUnknown { // ??????????????????????? // runMonitorInfo ????? runMonitorInfoAggregatedByNode ???? throw new UnsupportedOperationException(); } /** * ?SNMP?????????? * ??RunMonitor??? * @throws HinemosUnknown */ @Override protected Object preCollect(Set<Integer> execMonitorIntervals) throws HinemosUnknown { m_log.debug("preCollect() : start preCollect"); if (!m_isMonitorJob) { // ???1????? if (NodeToMonitorCache.getInstance(HinemosModuleConstant.MONITOR_PROCESS) .getMonitorsWithCalendar(m_facilityId, execMonitorIntervals).size() == 0) { if (m_log.isDebugEnabled()) { m_log.debug("preCollect(); skip process polling because of no monitor setting. facilityId = " + m_facilityId); } return null; } } // ?,DB???(ID, ID, ???) PollingDataManager dataManager = new PollingDataManager(m_facilityId); String collectMethod = dataManager.getCollectMethod(); if (collectMethod == null) { throw new IllegalStateException( "preCollect() : collectMethod is not defined. facilityId = " + m_facilityId); } switch (collectMethod) { case PollerProtocolConstant.PROTOCOL_SNMP: { NodeInfo node = null; try { node = new RepositoryControllerBean().getNode(m_facilityId); } catch (FacilityNotFound | HinemosUnknown e) { throw new IllegalStateException("preCollect() : can't get NodeInfo. facilityId = " + m_facilityId); } // SNMP??????? DataTable snmpResponse = Snmp4jPollerImpl.getInstance().polling(node.getAvailableIpAddress(), node.getSnmpPort(), node.getSnmpVersion(), node.getSnmpCommunity(), node.getSnmpRetryCount(), node.getSnmpTimeout(), dataManager.getPollingTargets(collectMethod), node.getSnmpSecurityLevel(), node.getSnmpUser(), node.getSnmpAuthPassword(), node.getSnmpPrivPassword(), node.getSnmpAuthProtocol(), node.getSnmpPrivProtocol()); // SNMP???????????? List<ProcessInfo> procList = buildSnmpProcessList(snmpResponse, m_facilityId); if (m_log.isDebugEnabled()) { if (procList == null) { m_log.debug("preCollect() : facilityId = " + m_facilityId + ", procList = null"); } else { m_log.debug( "preCollect() : facilityId = " + m_facilityId + ", procList size = " + procList.size()); } } return procList; } case PollerProtocolConstant.PROTOCOL_WBEM: NodeInfo node = null; try { node = new RepositoryControllerBean().getNode(m_facilityId); } catch (FacilityNotFound | HinemosUnknown e) { throw new IllegalStateException("preCollect() : can't get NodeInfo. facilityId = " + m_facilityId); } // WBEM??????? WbemPollerImpl poller = new WbemPollerImpl(); DataTable wbemResponse = poller.polling(node.getAvailableIpAddress(), node.getWbemPort(), node.getWbemProtocol(), node.getWbemUser(), node.getWbemUserPassword(), node.getWbemNameSpace(), node.getWbemRetryCount(), node.getWbemTimeout(), dataManager.getPollingTargets(collectMethod)); // WBEM???????????? List<ProcessInfo> procList = buildWbemProcessList(wbemResponse, m_facilityId); if (m_log.isDebugEnabled()) { if (procList == null) { m_log.debug("preCollect() : facilityId = " + m_facilityId + ", procList = null"); } else { m_log.debug( "preCollect() : facilityId = " + m_facilityId + ", procList size = " + procList.size()); } } return procList; } // FIXME nagatsumas return null; } /** * ?????????<BR> * ?????? preCollect ????? * * @param facilityId ID * @return ???????true */ @Override public boolean collect(String facilityId) { if (m_log.isDebugEnabled()) m_log.debug("collect() : start." + " facilityId = " + m_facilityId + ", monitorId = " + m_monitorId + ", monitorType = " + m_monitorTypeId); // ?,??? int count = 0; // if (m_now != null) { m_nodeDate = m_now.getTime(); } m_value = 0; // m_messageOrg = MessageConstant.COMMAND.getMessage() + " : " + m_command + ", " + MessageConstant.PARAM.getMessage() + " : " + m_param; // ??? Pattern pCommand = null; Pattern pParam = null; try { // ????? if (m_process.getCaseSensitivityFlg()) { pCommand = Pattern.compile(m_command, Pattern.CASE_INSENSITIVE); pParam = Pattern.compile(m_param, Pattern.CASE_INSENSITIVE); } // ??? else { pCommand = Pattern.compile(m_command); pParam = Pattern.compile(m_param); } } catch (PatternSyntaxException e) { m_log.info("collect() command, parameter PatternSyntax error : " + e.getClass().getSimpleName() + ", " + e.getMessage()); m_message = MessageConstant.MESSAGE_PLEASE_SET_VALUE_WITH_REGEX.getMessage(); return false; } @SuppressWarnings("unchecked") List<ProcessInfo> procList = (List<ProcessInfo>) preCollectData; if (procList == null) { // TODO nagatsumas ???OK // ?????????????? return false; } else { // ?????????? for (ProcessInfo procInfo : procList) { if (pCommand.matcher(procInfo.command).matches()) { if (pParam.matcher(procInfo.param).matches()) { count++; // ?? m_nodeDate = procInfo.time; // ???? if (ProcessProperties.getProperties().isDetailedDisplay()) { m_messageOrg = m_messageOrg + "\n"; if (procInfo.pid != null) { // PID???????SNMP???? m_messageOrg = m_messageOrg + procInfo.pid + " : "; } m_messageOrg = m_messageOrg + procInfo.command + " " + procInfo.param; } } } } } // m_value = count; m_message = MessageConstant.PROCESS_NUMBER.getMessage() + " : " + NumberFormat.getNumberInstance().format(m_value); if (m_log.isDebugEnabled()) m_log.debug("collect() : end." + " facilityId = " + m_facilityId + ", monitorId = " + m_monitorId + ", monitorType = " + m_monitorTypeId + ", count = " + count); return true; // // ??????? // m_message = MessageConstant.MESSAGE_TIME_OUT.getMessage(); // return false; } /** * ????<BR> * @see com.clustercontrol.monitor.run.factory.OperationNumericValueInfo#setMonitorAdditionInfo() */ @Override protected void setCheckInfo() throws MonitorNotFound { // ? if (!m_isMonitorJob) { m_process = QueryUtil.getMonitorProcessInfoPK(m_monitorId); } else { m_process = QueryUtil.getMonitorProcessInfoPK(m_monitor.getMonitorId()); } // m_command = m_process.getCommand(); if (m_process.getParam() != null) { m_param = m_process.getParam(); } } /** * ????<BR> * @see com.clustercontrol.monitor.run.factory.OperationMonitor#getMessage(int) */ @Override public String getMessage(int id) { return m_message; } @Override public void setMessage(String message) { m_message = message; } /** * ????<BR> * @see com.clustercontrol.monitor.run.factory.OperationMonitor#getMessageOrg(int) */ @Override public String getMessageOrg(int id) { return m_messageOrg; } /** * SNMP?????????????PID???????? * @param snmpResponse * @param dataManager * @return List????1?1????????????????????null? */ private List<ProcessInfo> buildSnmpProcessList(DataTable snmpResponse, String facilityId) { PollingDataManager dataManager = new PollingDataManager(facilityId); String runName = ""; String runParam = ""; String runPath = ""; // SNMP??????OID?DB?? { // cc_monitor_process_polling_mst ? variable_id = "name" ? MonitorProcessPollingMstData pollingBean; pollingBean = ProcessMasterCache.getMonitorProcessPollingMst( (new MonitorProcessPollingMstPK(PollerProtocolConstant.PROTOCOL_SNMP, dataManager.getPlatformId(), dataManager.getSubPlatformId(), "name"))); if (pollingBean == null) { m_log.info("collect() pollingBean (name) is null"); return null; } runName = PollerProtocolConstant.getEntryKey(PollerProtocolConstant.PROTOCOL_SNMP, pollingBean.getPollingTarget()); m_log.debug("collect() runName : " + runName); // cc_monitor_process_polling_mst ? variable_id = "param" ? pollingBean = ProcessMasterCache.getMonitorProcessPollingMst( new MonitorProcessPollingMstPK(PollerProtocolConstant.PROTOCOL_SNMP, dataManager.getPlatformId(), dataManager.getSubPlatformId(), "param")); if (pollingBean == null) { m_log.info("collect() pollingBean (param) is null"); return null; } runParam = PollerProtocolConstant.getEntryKey(PollerProtocolConstant.PROTOCOL_SNMP, pollingBean.getPollingTarget()); m_log.debug("collect() runParam : " + runParam); // cc_monitor_process_polling_mst ? variable_id = "path" ? pollingBean = ProcessMasterCache.getMonitorProcessPollingMst( new MonitorProcessPollingMstPK(PollerProtocolConstant.PROTOCOL_SNMP, dataManager.getPlatformId(), dataManager.getSubPlatformId(), "path")); if (pollingBean == null) { m_log.info("collect() pollingBean (path) is null"); return null; } runPath = PollerProtocolConstant.getEntryKey(PollerProtocolConstant.PROTOCOL_SNMP, pollingBean.getPollingTarget()); m_log.debug("collect() runPath : " + runPath); } // SNMP?????????? Set<TableEntry> nameEntrySet = snmpResponse.getValueSetStartWith(runName); Set<TableEntry> paramEntrySet = snmpResponse.getValueSetStartWith(runParam); Set<TableEntry> commandEntrySet = snmpResponse.getValueSetStartWith(runPath); if (nameEntrySet == null || paramEntrySet == null || commandEntrySet == null) { // ??????????? // ????????????? m_log.info("collect() FacilityID : " + facilityId + ", " + "valueSetName(Set) or valueSetParam(Set) or valueSetPath(Set) is null , SNMP Polling failed"); return null; } if (m_log.isDebugEnabled()) { m_log.debug("process list (Name) size = " + nameEntrySet.size()); m_log.debug("process list (Param) size = " + paramEntrySet.size()); m_log.debug("process list (Command) size = " + commandEntrySet.size()); } List<ProcessInfo> processList = new ArrayList<>(); for (TableEntry nameEntry : nameEntrySet) { String pid; // ??????????? String command = null; long time = 0L; { String name = null; if (nameEntry != null) { if (!nameEntry.isValid()) { // polling????? m_message = nameEntry.getErrorDetail().getMessage(); return null; } name = (String) nameEntry.getValue(); if (name == null) { m_log.info("collect() FacilityID : " + facilityId + ", " + "NameEntry(value) is null. What snmp happened?"); m_message = MessageConstant.MESSAGE_COULD_NOT_GET_VALUE_PROCESS.getMessage(); return null; } } else { // ?????null??? m_log.debug("collect() FacilityID : " + facilityId + ", " + "NameEntry is null. What snmp happened?"); continue; } // ???OID?????? String key = nameEntry.getKey(); pid = key.substring(key.lastIndexOf(".")); // OID??? TableEntry commandEntry = snmpResponse.getValue(runPath + pid); if (commandEntry != null) { command = (String) commandEntry.getValue(); time = commandEntry.getDate(); if (command == null) { m_log.info("collect() FacilityID : " + facilityId + ", " + "commandEntry(value) is null. What snmp happened?"); m_message = MessageConstant.MESSAGE_COULD_NOT_GET_VALUE_PROCESS.getMessage(); break; // while? } } else { // ?????null??? m_log.debug("collect() FacilityID : " + facilityId + ", " + "commandEntry is null. What snmp happened?"); continue; } // Windows?????valueCommand?valueName?? if (command.length() == 0) { // ??????????? // ?null OR command = name; } else if (!command.startsWith("/") && command.endsWith("\\") && !command.equals(name)) { // ? // ?'/'??? // ?'\'?????? // ?????? command = command + name; } } // 2. ? String param = null; // OID??? TableEntry paramEntry = snmpResponse.getValue(runParam + pid); m_log.debug("collect() FacilityID : " + facilityId + ", " + " paramEntry: " + paramEntry); if (paramEntry != null) { param = (String) paramEntry.getValue(); if (param == null) { m_log.info("collect() FacilityID : " + facilityId + ", " + "paramEntry(value) is null. What snmp happened?"); m_message = MessageConstant.MESSAGE_COULD_NOT_GET_VALUE_PROCESS.getMessage(); return null; } } else { // ?????null??? m_log.debug( "collect() FacilityID : " + facilityId + ", " + "paramEntry is null. What snmp happened?"); continue; } processList.add(new ProcessInfo(pid, command, param, time)); } return processList; } /** * WBEM?????????????PID???????? * @param wbemResponse * @param dataManager * @return List????1?1????????????????????null? */ private List<ProcessInfo> buildWbemProcessList(DataTable wbemResponse, String facilityId) { PollingDataManager dataManager = new PollingDataManager(facilityId); String runParam = ""; // WBEM????param?? // WBEM??????class?DB?? // cc_monitor_process_polling_mst ? variable_id = "param" ? MonitorProcessPollingMstData pollingBean; pollingBean = ProcessMasterCache .getMonitorProcessPollingMst(new MonitorProcessPollingMstPK(PollerProtocolConstant.PROTOCOL_WBEM, dataManager.getPlatformId(), dataManager.getSubPlatformId(), "param")); if (pollingBean == null) { m_log.info("collect() pollingBean (param) is null"); return null; } runParam = PollerProtocolConstant.getEntryKey(PollerProtocolConstant.PROTOCOL_WBEM, pollingBean.getPollingTarget()); m_log.debug("collect() runParam : " + runParam); // ??????? Set<TableEntry> paramEntrySet = wbemResponse.getValueSetStartWith(runParam); if (paramEntrySet == null) { // ??????????? // ????????????? m_log.info("collect() FacilityID : " + facilityId + ", " + "paramEntrySet(Set) is null , WBEM Polling failed"); return null; } if (m_log.isDebugEnabled()) { m_log.debug("process list (Param) size = " + paramEntrySet.size()); } List<ProcessInfo> processList = new ArrayList<>(); for (TableEntry paramEntry : paramEntrySet) { String pid = null; String command = null; long time = 0L; String param = ""; if (paramEntry != null) { if (!paramEntry.isValid()) { m_message = paramEntry.getErrorDetail().getMessage(); return null; } @SuppressWarnings("unchecked") Vector<String> valueParam = (Vector<String>) paramEntry.getValue(); if (valueParam == null) { m_log.info("collect() FacilityID : " + facilityId + ", " + "ParamEntry(value) is null. What wbem happened?"); m_message = MessageConstant.MESSAGE_COULD_NOT_GET_VALUE_PROCESS.getMessage(); return null; } command = valueParam.get(0).toString(); m_log.debug("command : " + command); for (int i = 1; i < valueParam.size(); i++) { param = param + valueParam.get(i); if (i + 1 < valueParam.size()) { param = param + " "; } m_log.debug("param : " + param); } } else { // ?????null??? m_log.debug( "collect() FacilityID : " + facilityId + ", " + "ParamEntry is null. What wbem happened?"); continue; } time = paramEntry.getDate(); processList.add(new ProcessInfo(pid, command, param, time)); } return processList; } private static final class ProcessInfo { public final String command; public final String param; public final long time; public final String pid; public ProcessInfo(String pid, String command, String param, long time) { this.pid = pid; this.command = command; this.param = param; this.time = time; } } @Override protected String makeJobOrgMessage(String orgMsg, String msg) { if (m_monitor == null || m_monitor.getProcessCheckInfo() == null) { return ""; } String[] args = { m_monitor.getProcessCheckInfo().getCommand(), m_monitor.getProcessCheckInfo().getParam(), String.valueOf(m_monitor.getProcessCheckInfo().getCaseSensitivityFlg()) }; return MessageConstant.MESSAGE_JOB_MONITOR_ORGMSG_PROCESS.getMessage(args) + "\n" + orgMsg; } }