Java tutorial
/* 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.custom.factory; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.clustercontrol.bean.HinemosModuleConstant; import com.clustercontrol.bean.PriorityConstant; import com.clustercontrol.custom.bean.CommandResultDTO; import com.clustercontrol.custom.bean.CustomConstant.CommandExecType; import com.clustercontrol.fault.CustomInvalid; import com.clustercontrol.fault.HinemosUnknown; import com.clustercontrol.fault.MonitorNotFound; import com.clustercontrol.hub.bean.StringSample; import com.clustercontrol.hub.util.CollectStringDataUtil; import com.clustercontrol.jobmanagement.bean.MonitorJobEndNode; import com.clustercontrol.jobmanagement.bean.RunStatusConstant; import com.clustercontrol.jobmanagement.util.MonitorJobWorker; import com.clustercontrol.maintenance.util.HinemosPropertyUtil; import com.clustercontrol.monitor.run.model.MonitorInfo; import com.clustercontrol.monitor.run.model.MonitorStringValueInfo; import com.clustercontrol.monitor.session.MonitorSettingControllerBean; import com.clustercontrol.repository.session.RepositoryControllerBean; import com.clustercontrol.util.HinemosTime; import com.clustercontrol.util.MessageConstant; /** * (String)????<br/> * * @version 6.0.0 * @since 6.0.0 */ public class RunCustomString extends RunCustomBase { private static Log m_log = LogFactory.getLog(RunCustomString.class); private static volatile Integer maxStringLen; /** * <br/> * * @param result * ?????? */ public RunCustomString(CommandResultDTO result) { this.result = result; } /** * ????<br/> * * @throws HinemosUnknown * ?????? * @throws MonitorNotFound * ?????? * @throws CustomInvalid * ?????? */ @Override public void monitor() throws HinemosUnknown, MonitorNotFound, CustomInvalid { // Local Variables MonitorInfo monitor = null; int priority = PriorityConstant.TYPE_UNKNOWN; String facilityPath = ""; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); dateFormat.setTimeZone(HinemosTime.getTimeZone()); String msg = ""; String msgOrig = ""; boolean isMonitorJob = result.getRunInstructionInfo() != null; // MAIN try { monitor = new MonitorSettingControllerBean().getMonitor(result.getMonitorId()); String executeDate = dateFormat.format(result.getExecuteDate()); String exitDate = dateFormat.format(result.getExitDate()); String collectDate = dateFormat.format(result.getCollectDate()); facilityPath = new RepositoryControllerBean().getFacilityPath(result.getFacilityId(), null); if (result.getTimeout() || result.getStdout() == null || result.getStdout().isEmpty() || result.getResults() == null) { if (m_log.isDebugEnabled()) { m_log.debug("command monitoring : timeout or no stdout [" + result + "]"); } // if command execution failed (timeout or no stdout) if (isMonitorJob || monitor.getMonitorFlg()) { msg = "FAILURE : command execution failed (timeout, no stdout or not unexecutable command)..."; msgOrig = "FAILURE : command execution failed (timeout, no stdout or unexecutable command)...\n\n" + "COMMAND : " + result.getCommand() + "\n" + "COLLECTION DATE : " + collectDate + "\n" + "executed at " + executeDate + "\n" + "exited (or timeout) at " + exitDate + "\n" + "EXIT CODE : " + (result.getExitCode() != null ? result.getExitCode() : "timeout") + "\n\n" + "[STDOUT]\n" + result.getStdout() + "\n" + "[STDERR]\n" + result.getStderr() + "\n"; // if (!isMonitorJob) { notify(PriorityConstant.TYPE_UNKNOWN, monitor, result.getFacilityId(), facilityPath, null, msg, msgOrig, HinemosModuleConstant.MONITOR_CUSTOM_S); } else { // this.monitorJobEndNodeList.add(new MonitorJobEndNode(result.getRunInstructionInfo(), HinemosModuleConstant.MONITOR_CUSTOM_S, makeJobOrgMessage(monitor, msgOrig), "", RunStatusConstant.END, MonitorJobWorker.getReturnValue( result.getRunInstructionInfo(), PriorityConstant.TYPE_UNKNOWN))); } } } else { // if command stdout was returned Monitor msg = result.getStdout(); // Stdout??? Pattern patternMsg = Pattern.compile("\r\n$|\n$"); Matcher matcherMsg = patternMsg.matcher(msg); msg = matcherMsg.replaceAll(""); if (m_log.isDebugEnabled()) { m_log.debug("command monitoring : values [" + msg + "]"); } // ? if (monitor.getCollectorFlg()) { // collector each value List<StringSample> sampleList = new ArrayList<StringSample>(); StringSample sample = new StringSample(new Date(HinemosTime.currentTimeMillis()), monitor.getMonitorId()); // sample.set(result.getFacilityId(), "custom", msg); sampleList.add(sample); if (!sampleList.isEmpty()) { CollectStringDataUtil.store(sampleList); } } // int orderNo = 0; if (isMonitorJob || monitor.getMonitorFlg()) { // monitor each value for (MonitorStringValueInfo stringValueInfo : monitor.getStringValueInfo()) { ++orderNo; if (m_log.isDebugEnabled()) { m_log.info(String.format( "monitoring (monitorId = %s, orderNo = %d, patten = %s, enabled = %s, casesensitive = %s)", monitor.getMonitorId(), orderNo, stringValueInfo.getPattern(), stringValueInfo.getValidFlg(), stringValueInfo.getCaseSensitivityFlg())); } if (!stringValueInfo.getValidFlg()) { // ????? continue; } // ? if (m_log.isDebugEnabled()) { m_log.debug(String.format("filtering customtrap (regex = %s, Msg = %s", stringValueInfo.getPattern(), msg)); } try { Pattern pattern = null; if (stringValueInfo.getCaseSensitivityFlg()) { // ????? pattern = Pattern.compile(stringValueInfo.getPattern(), Pattern.DOTALL | Pattern.CASE_INSENSITIVE); } else { // ??? pattern = Pattern.compile(stringValueInfo.getPattern(), Pattern.DOTALL); } Matcher matcher = pattern.matcher(msg); if (matcher.matches()) { if (stringValueInfo.getProcessType()) { msgOrig = MessageConstant.LOGFILE_PATTERN.getMessage() + "=" + stringValueInfo.getPattern() + "\n" + MessageConstant.LOGFILE_LINE.getMessage() + "=" + msg + "\n\n" + "COMMAND : " + result.getCommand() + "\n" + "COLLECTION DATE : " + collectDate + "\n" + "executed at " + executeDate + "\n" + "exited (or timeout) at " + exitDate + "\n" + "EXIT CODE : " + (result.getExitCode() != null ? result.getExitCode() : "timeout") + "\n\n" + "[STDOUT]\n" + result.getStdout() + "\n\n" + "[STDERR]\n" + result.getStderr() + "\n"; msg = makeMsg(stringValueInfo, msg); priority = stringValueInfo.getPriority(); if (!isMonitorJob) { // notify(priority, monitor, result.getFacilityId(), facilityPath, stringValueInfo.getPattern(), msg, msgOrig, HinemosModuleConstant.MONITOR_CUSTOM_S); } else { // this.monitorJobEndNodeList .add(new MonitorJobEndNode(result.getRunInstructionInfo(), HinemosModuleConstant.MONITOR_CUSTOM_S, makeJobOrgMessage(monitor, msgOrig), "", RunStatusConstant.END, MonitorJobWorker.getReturnValue( result.getRunInstructionInfo(), priority))); } } else { m_log.debug(String.format("not ProcessType (regex = %s, Msg = %s", stringValueInfo.getPattern(), result.getStdout())); } break;// Syslog?????? } } catch (RuntimeException e) { m_log.warn("filtering failure. (regex = " + stringValueInfo.getPattern() + ") . " + e.getMessage(), e); } } } } } catch (MonitorNotFound | CustomInvalid | HinemosUnknown e) { m_log.warn("unexpected internal failure occurred. [" + result + "]"); throw e; } catch (Exception e) { m_log.warn("unexpected internal failure occurred. [" + result + "]", e); throw new HinemosUnknown("unexpected internal failure occurred. [" + result + "]", e); } } /** * ???? * * @param stringValueInfoMonitorStringValueInfo * @param msg * @return */ private String makeMsg(MonitorStringValueInfo stringValueInfo, String msg) { String updateMsg = ""; if (null != msg) { updateMsg = stringValueInfo.getMessage().replace("#[LOG_LINE]", msg); // DB? int maxLen = HinemosPropertyUtil.getHinemosPropertyNum("monitor.log.line.max.length", Long.valueOf(256)) .intValue(); if (maxStringLen == null || maxStringLen != maxLen) { synchronized (RunCustomString.class) { if (maxStringLen == null || maxStringLen != maxLen) { m_log.info("monitor.log.line.max.length = " + maxLen); maxStringLen = maxLen; } } } if (updateMsg.length() > maxLen) { updateMsg = updateMsg.substring(0, maxLen); } } return updateMsg; } private String makeJobOrgMessage(MonitorInfo monitorInfo, String orgMsg) { if (monitorInfo == null || monitorInfo.getCustomCheckInfo() == null) { return ""; } String[] args = { monitorInfo.getCustomCheckInfo().getCommandExecType() == CommandExecType.SELECTED ? monitorInfo.getCustomCheckInfo().getSelectedFacilityId() : "", monitorInfo.getCustomCheckInfo().getSpecifyUser() ? monitorInfo.getCustomCheckInfo().getEffectiveUser() : "", monitorInfo.getCustomCheckInfo().getCommand(), monitorInfo.getCustomCheckInfo().getTimeout().toString() }; return MessageConstant.MESSAGE_JOB_MONITOR_ORGMSG_CUSTOM_S.getMessage(args) + "\n" + orgMsg; } }