com.clustercontrol.snmp.factory.RunMonitorSnmp.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.snmp.factory.RunMonitorSnmp.java

Source

/*
    
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.snmp.factory;

import java.text.DateFormat;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.snmp4j.smi.SMIConstants;

import com.clustercontrol.fault.FacilityNotFound;
import com.clustercontrol.fault.HinemosUnknown;
import com.clustercontrol.fault.MonitorNotFound;
import com.clustercontrol.monitor.run.factory.RunMonitor;
import com.clustercontrol.monitor.run.factory.RunMonitorNumericValueType;
import com.clustercontrol.repository.model.NodeInfo;
import com.clustercontrol.repository.session.RepositoryControllerBean;
import com.clustercontrol.monitor.bean.ConvertValueConstant;
import com.clustercontrol.snmp.factory.MonitorSnmpCache.MonitorSnmpValue;
import com.clustercontrol.snmp.factory.MonitorSnmpCache.MonitorSnmpValuePK;
import com.clustercontrol.snmp.model.SnmpCheckInfo;
import com.clustercontrol.snmp.util.QueryUtil;
import com.clustercontrol.snmp.util.RequestSnmp4j;
import com.clustercontrol.snmp.util.SnmpProperties;
import com.clustercontrol.util.HinemosTime;
import com.clustercontrol.util.MessageConstant;

/**
 * SNMP ?<BR>
 *
 * @version 4.0.0
 * @since 2.0.0
 */
public class RunMonitorSnmp extends RunMonitorNumericValueType {

    private static Log m_log = LogFactory.getLog(RunMonitorSnmp.class);

    /** SNMP */
    private SnmpCheckInfo m_snmp = null;

    /** OID */
    private String m_snmpOid = null;

    /** ?? */
    private int m_convertFlg = 0;

    /**  */
    private String m_messageOrg = null;

    /**  */
    private String m_message = "";

    /**
     * 
     * 
     */
    public RunMonitorSnmp() {
        super();
    }

    /**
     * ???CallableTask???????
     * 
     * @see com.clustercontrol.monitor.run.factory.RunMonitor#runMonitorInfo()
     * @see com.clustercontrol.monitor.run.util.MonitorExecuteTask
     */
    @Override
    protected RunMonitor createMonitorInstance() {
        return new RunMonitorSnmp();
    }

    /**
     * OID?
     * 
     * @param facilityId ID
     * @return ???????true
     * @throws HinemosUnknown
     */
    @Override
    public boolean collect(String facilityId) throws HinemosUnknown {

        if (m_now != null) {
            m_nodeDate = m_now.getTime();
        }
        m_value = 0;

        // 
        m_message = "";
        m_messageOrg = MessageConstant.OID.getMessage() + " : " + m_snmpOid;

        NodeInfo info = null;
        try {
            // ??
            info = new RepositoryControllerBean().getNode(facilityId);
        } catch (FacilityNotFound e) {
            m_message = MessageConstant.MESSAGE_COULD_NOT_GET_NODE_ATTRIBUTES.getMessage();
            m_messageOrg = m_messageOrg + " (" + e.getMessage() + ")";
            resetCache(m_monitorId, facilityId);
            return false;
        }

        // SNMP?
        RequestSnmp4j m_request = new RequestSnmp4j();

        m_log.debug("version=" + info.getSnmpVersion());
        boolean result = false;
        try {
            result = m_request.polling(info.getAvailableIpAddress(), info.getSnmpCommunity(), info.getSnmpPort(),
                    m_snmpOid, info.getSnmpVersion(), info.getSnmpTimeout(), info.getSnmpRetryCount(),
                    info.getSnmpSecurityLevel(), info.getSnmpUser(), info.getSnmpAuthPassword(),
                    info.getSnmpPrivPassword(), info.getSnmpAuthProtocol(), info.getSnmpPrivProtocol());
        } catch (Exception e) {
            m_message = MessageConstant.MESSAGE_COULD_NOT_GET_NODE_ATTRIBUTES.getMessage();
            m_messageOrg = m_message + ", " + e.getMessage() + " (" + e.getClass().getName() + ")";
            if (e instanceof NumberFormatException) {
                m_log.warn(m_messageOrg);
            } else {
                m_log.warn(m_messageOrg, e);
            }
            resetCache(m_monitorId, facilityId);
            return false;
        }

        if (result) {

            // ??
            double value = -1;
            try {
                if (m_request.getValue() == null) {
                    m_log.debug("collect() : m_request.getValue() is null");
                    return false;
                }
                value = Double.parseDouble(m_request.getValue());
            } catch (NumberFormatException e) {
                m_log.info("collect() : " + e.getClass().getSimpleName() + ", " + e.getMessage());
                String[] args = { m_request.getValue() };
                m_message = MessageConstant.MESSAGE_COULD_NOT_GET_NUMERIC_VALUE.getMessage(args);
                resetCache(m_monitorId, facilityId);
                return false;
            } catch (Exception e) {
                m_log.warn("collect() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e);
                String[] args = { m_request.getValue() };
                m_message = MessageConstant.MESSAGE_COULD_NOT_GET_NUMERIC_VALUE.getMessage(args);
                resetCache(m_monitorId, facilityId);
                return false;
            }
            long date = m_request.getDate();

            // ????
            if (m_convertFlg == ConvertValueConstant.TYPE_NO) {
                m_value = value;
                m_nodeDate = date;
            }
            // ??
            else if (m_convertFlg == ConvertValueConstant.TYPE_DELTA) {
                // ??
                MonitorSnmpValue valueEntity = null;
                Double prevValue = 0d;
                Long prevDate = 0l;
                if (!m_isMonitorJob) {
                    // ??
                    // cache??
                    valueEntity = MonitorSnmpCache.getMonitorSnmpValue(m_monitorId, facilityId);

                    // ???
                    prevValue = valueEntity.getValue();
                    if (valueEntity.getGetDate() != null) {
                        prevDate = valueEntity.getGetDate();
                    }
                } else {
                    // ??
                    valueEntity = new MonitorSnmpValue(new MonitorSnmpValuePK(m_monitorId, facilityId));
                    if (m_prvData instanceof MonitorSnmpValue) {
                        // ????
                        prevValue = ((MonitorSnmpValue) m_prvData).getValue();
                        prevDate = ((MonitorSnmpValue) m_prvData).getGetDate();
                    }
                }

                if (prevValue != null) {
                    if (prevValue > value) {
                        if (m_request.getType() == SMIConstants.SYNTAX_COUNTER32) {
                            value += ((double) Integer.MAX_VALUE + 1) * 2;
                        } else if (m_request.getType() == SMIConstants.SYNTAX_COUNTER64) {
                            value += ((double) Long.MAX_VALUE + 1) * 2;
                        }
                    }
                }

                // SNMP????
                valueEntity.setValue(Double.valueOf(value));
                valueEntity.setGetDate(date);

                if (!m_isMonitorJob) {
                    // ???ID?????
                    if (m_monitor.getMonitorFlg())
                        MonitorSnmpCache.update(m_monitorId, facilityId, valueEntity);

                    // ???SNMP????????
                    int tolerance = (m_runInterval + SnmpProperties.getProperties().getValidSecond()) * 1000;

                    if (prevDate > date - tolerance) {

                        // ??null???
                        if (prevValue == null) {
                            m_log.debug("collect() : prevValue is null");
                            return false;
                        }

                        m_value = value - prevValue;
                        m_nodeDate = m_request.getDate();
                    } else {
                        if (prevDate != 0l) {
                            DateFormat df = DateFormat.getDateTimeInstance();
                            df.setTimeZone(HinemosTime.getTimeZone());
                            String[] args = { df.format(new Date(prevDate)) };
                            m_message = MessageConstant.MESSAGE_TOO_OLD_TO_CALCULATE.getMessage(args);
                            return false;
                        } else {
                            // ???0??
                            m_nodeDate = 0l;
                        }
                    }
                } else {
                    m_value = value - prevValue;
                    m_nodeDate = m_request.getDate();
                    m_curData = valueEntity;
                }

            }
            m_message = MessageConstant.SELECT_VALUE.getMessage() + " : " + m_value;
        } else {
            m_message = m_request.getMessage();
            resetCache(m_monitorId, facilityId);
        }
        return result;
    }

    /* (? Javadoc)
     * SNMP
     * @see com.clustercontrol.monitor.run.factory.OperationNumericValueInfo#setMonitorAdditionInfo()
     */
    @Override
    protected void setCheckInfo() throws MonitorNotFound {

        // SNMP?
        m_snmp = QueryUtil.getMonitorSnmpInfoPK(m_monitorId);

        // SNMP
        m_snmpOid = m_snmp.getSnmpOid().trim();
        m_convertFlg = m_snmp.getConvertFlg().intValue();
    }

    /* (? Javadoc)
     * ?
     * @see com.clustercontrol.monitor.run.factory.OperationMonitor#getMessage(int)
     */
    @Override
    public String getMessage(int id) {
        return m_message;
    }

    /* (? Javadoc)
     * ?
     * @see com.clustercontrol.monitor.run.factory.OperationMonitor#getMessageOrg(int)
     */
    @Override
    public String getMessageOrg(int id) {
        return m_messageOrg;
    }

    /**
     * SNMP???????????null?
     * @param m_monitorId ID
     * @param facilityId ID
     */
    private void resetCache(String m_monitorId, String facilityId) {

        MonitorSnmpValue valueEntity = null;

        // key??????????????
        if (MonitorSnmpCache.getMonitorSnmpValue(m_monitorId, facilityId) == null)
            return;
        valueEntity = new MonitorSnmpValue(new MonitorSnmpValuePK(m_monitorId, facilityId));
        MonitorSnmpCache.update(m_monitorId, facilityId, valueEntity);
    }

    @Override
    protected String makeJobOrgMessage(String orgMsg, String msg) {
        String[] args = { "" };
        if (m_convertFlg == ConvertValueConstant.TYPE_NO) {
            // ???
            args[0] = MessageConstant.CONVERT_NO.getMessage();
        } else if (m_convertFlg == ConvertValueConstant.TYPE_DELTA) {
            // ?
            args[0] = MessageConstant.DELTA.getMessage();
        }
        return MessageConstant.MESSAGE_JOB_MONITOR_ORGMSG_SNMP_N.getMessage(args) + "\n" + orgMsg + "\n" + msg;
    }
}