com.clustercontrol.systemlog.service.SystemLogNotifier.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.systemlog.service.SystemLogNotifier.java

Source

/*
    
 Copyright (C) 2011 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.systemlog.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.clustercontrol.bean.HinemosModuleConstant;
import com.clustercontrol.commons.util.NotifyGroupIdGenerator;
import com.clustercontrol.jobmanagement.bean.RunInstructionInfo;
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.notify.bean.OutputBasicInfo;
import com.clustercontrol.notify.session.NotifyControllerBean;
import com.clustercontrol.repository.bean.FacilityTreeAttributeConstant;
import com.clustercontrol.repository.session.RepositoryControllerBean;
import com.clustercontrol.systemlog.bean.SyslogMessage;
import com.clustercontrol.util.MessageConstant;

public class SystemLogNotifier {
    /** ?  */
    private final static Log m_log = LogFactory.getLog(SystemLogNotifier.class);

    public void put(String receiverId, List<SyslogMessage> syslogList, MonitorInfo monitorInfo,
            List<MonitorStringValueInfo> ruleList, List<String> facilityIdList,
            RunInstructionInfo runInstructionInfo) {

        ArrayList<OutputBasicInfo> logOutputList = new ArrayList<OutputBasicInfo>();
        for (int i = 0; i < syslogList.size(); i++) {
            SyslogMessage syslog = syslogList.get(i);
            MonitorStringValueInfo rule = ruleList.get(i);
            String logFacilityId = facilityIdList.get(i);

            String message = "";
            if (HinemosModuleConstant.MONITOR_LOGFILE.equals(monitorInfo.getMonitorTypeId())) {
                message += MessageConstant.LOGFILE_FILENAME.getMessage() + "="
                        + monitorInfo.getLogfileCheckInfo().getLogfile() + "\n";
            }

            message += MessageConstant.LOGFILE_PATTERN.getMessage() + "=" + rule.getPattern() + "\n"
                    + MessageConstant.LOGFILE_LINE.getMessage() + "=" + syslog.rawSyslog;

            //????
            // ???????????
            m_log.debug("call makeMessage.");
            OutputBasicInfo logOutput = makeMessage(receiverId, message, syslog, monitorInfo, rule, logFacilityId,
                    syslog.hostname);
            logOutputList.add(logOutput);
            m_log.debug("called makeMessage.");
        }

        //??
        if (runInstructionInfo == null) {
            // 
            new NotifyControllerBean().notify(logOutputList, NotifyGroupIdGenerator.generate(monitorInfo));
        } else {
            if (!logOutputList.isEmpty()) {
                // 
                MonitorJobWorker.endMonitorJob(runInstructionInfo, HinemosModuleConstant.MONITOR_SYSTEMLOG,
                        logOutputList.get(0).getMessageOrg(), "", RunStatusConstant.END,
                        MonitorJobWorker.getReturnValue(runInstructionInfo, logOutputList.get(0).getPriority()));
            }
        }
    }

    /**
     * ?????????
     *
     * @param msg 
     * @param logmsg syslog
     * @param moninfo 
     * @param filterInfo 
     * @param facilityID ID
     * @param nodeName ??
     * @return 
     *
     * @since 1.0.0
     */
    private static OutputBasicInfo makeMessage(String receiverId, String msg, SyslogMessage syslog,
            MonitorInfo monInfo, MonitorStringValueInfo filterInfo, String facilityId, String nodeName) {

        m_log.debug("Make ObjectMsg");

        OutputBasicInfo output = new OutputBasicInfo();

        output.setMonitorId(filterInfo.getMonitorId());
        output.setFacilityId(facilityId);

        output.setPluginId(HinemosModuleConstant.MONITOR_SYSTEMLOG);

        // ????????????
        output.setSubKey(filterInfo.getPattern());

        if (FacilityTreeAttributeConstant.UNREGISTERED_SCOPE.equals(facilityId)) {
            //?????????
            output.setScopeText(nodeName);
        } else {
            // ??]
            try {
                m_log.debug("call getFacilityPath.");
                String facilityPath = new RepositoryControllerBean().getFacilityPath(facilityId, null);
                m_log.debug("called getFacilityPath.");

                output.setScopeText(facilityPath);
            } catch (Exception e) {
                m_log.warn("makeMessage() cannot get facility path.(facilityId = " + facilityId + ") : "
                        + e.getClass().getSimpleName() + ", " + e.getMessage(), e);
            }
        }

        output.setApplication(monInfo.getApplication());

        // ?#[LOG_LINE]???????
        // ???
        if (filterInfo.getMessage() != null) {
            String str = filterInfo.getMessage().replace("#[LOG_LINE]", syslog.message);
            //DB?
            int maxLen = HinemosPropertyUtil.getHinemosPropertyNum("monitor.log.line.max.length", Long.valueOf(256))
                    .intValue();
            if (str.length() > maxLen) {
                str = str.substring(0, maxLen);
            }
            output.setMessage(str);
        }

        output.setMessageOrg(msg);
        output.setPriority(filterInfo.getPriority());
        output.setGenerationDate(syslog.date);

        if (receiverId != null && !"".equals(receiverId)) {
            output.setMultiId(receiverId);
        }

        return output;
    }
}