Java tutorial
/* Copyright (C) 2008 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.performance.operator; import java.math.BigInteger; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.clustercontrol.fault.CollectorNotFound; import com.clustercontrol.performance.monitor.entity.CollectorPollingMstData; import com.clustercontrol.performance.monitor.model.CollectorPollingMstEntity; import com.clustercontrol.performance.monitor.util.QueryUtil; import com.clustercontrol.poller.bean.PollerProtocolConstant; import com.clustercontrol.poller.util.DataTable; import com.clustercontrol.poller.util.TableEntry; abstract public class Operator { private static Log m_log = LogFactory.getLog(Operator.class); // ?? private String expression; // ???? private String collectMethod; private String platformId; private String subPlatformId; private String itemCode; private List<String> variables = new ArrayList<String>(); // SNMP?????COUNTER32?????? private final static long COUNTER32_MAX_VALUE = ((long) (Integer.MAX_VALUE)) * 2 + 1; private final static BigInteger COUTNER64_MAX_VALUE = BigInteger.valueOf(Long.MAX_VALUE).shiftLeft(1) .add(BigInteger.valueOf(1)); private Map<String, CollectorPollingMstData> m_oidMap = new ConcurrentHashMap<String, CollectorPollingMstData>(); public abstract double calc(DataTable currentTable, DataTable previousTable, String deviceName) throws CollectedDataNotFoundException, InvalidValueException; /** * ??????? * @param variable ?? * @param currentTable * @param deviceName * @return ? * @throws CollectedDataNotFoundException * @throws InvalidValueException */ protected double getCurrentMibValue(String variable, DataTable currentTable, String deviceName) throws CollectedDataNotFoundException, InvalidValueException { return getValue(variable, currentTable, deviceName); } /** * ??????? * @param variable ?? * @param previousTable * @param deviceName * @return ? * @throws CollectedDataNotFoundException * @throws InvalidValueException */ protected double getPreviousMibValue(String variable, DataTable previousTable, String deviceName) throws CollectedDataNotFoundException, InvalidValueException { return getValue(variable, previousTable, deviceName); } /** * ???????? * entryKey???"*"??????????????? * * @param variable ?? * @param currentTable * @param previousTable * @param deviceName * @return * @throws CollectedDataNotFoundException * @throws InvalidValueException */ protected double getDifferenceValue(String variable, DataTable currentTable, DataTable previousTable, String deviceName) throws CollectedDataNotFoundException, InvalidValueException { if (m_log.isDebugEnabled()) { m_log.debug("getDifferenceValue() variable : " + variable); } CollectorPollingMstData data = getCollectorPollingMstData(variable); // ??? String entryKey = data.getEntryKey(); String targetIndex = entryKey.substring(entryKey.lastIndexOf(".") + 1); if (m_log.isDebugEnabled()) { m_log.debug("getDifferenceValue() targetIndex : " + targetIndex); } // entryKey?????? if ("?".equals(targetIndex)) { // SNMPWBEM????? if (getCollectMethod().equals(PollerProtocolConstant.PROTOCOL_SNMP) || getCollectMethod().equals(PollerProtocolConstant.PROTOCOL_WBEM)) { CollectorPollingMstData mstData = getCollectorPollingMstData("key"); String pollingTarget = mstData.getPollingTarget(); if (m_log.isDebugEnabled()) { m_log.debug("getDifferenceValue() targetKey : " + pollingTarget); } // ?????????????? String currentIndex = null; String previousIndex = null; try { Set<TableEntry> currentEntrys = currentTable.getValueSetStartWith( PollerProtocolConstant.getEntryKey(getCollectMethod(), pollingTarget)); Set<TableEntry> previousEntrys = previousTable.getValueSetStartWith( PollerProtocolConstant.getEntryKey(getCollectMethod(), pollingTarget)); if (currentEntrys == null || previousEntrys == null) { // ? String message = "getDifferenceValue() currentEntrys or previousEntrys is null, currentEntrys : " + currentEntrys + ", previousEntrys : " + previousEntrys; m_log.debug(message); throw new CollectedDataNotFoundException(message); } // CurrentTable // ?????????????? Iterator<TableEntry> itr = currentEntrys.iterator(); while (itr.hasNext()) { TableEntry entry = itr.next(); if (m_log.isDebugEnabled()) { m_log.debug("getDifferenceValue() CurrentTable deviceName : " + getCollectMethod() + ", entry.getValue() : " + entry.getValue()); m_log.debug("getDifferenceValue() CurrentTable entry.getValue() is " + (entry.getValue()).getClass().getCanonicalName()); } // value ?String ????????Object?Class check String value = null; if (entry.getValue() instanceof String) { value = (String) entry.getValue(); } else if (entry.getValue() == null) { m_log.debug("entry.getValue() == null"); continue; } else { value = entry.getValue().toString(); } if (value.length() > 128) { // ????128?????? value = value.substring(0, 128); } if (value.equals(deviceName)) { currentIndex = entry.getKey().substring(entry.getKey().lastIndexOf(".")); break; } } // PreviousTable // ?????????????? itr = previousEntrys.iterator(); while (itr.hasNext()) { TableEntry entry = itr.next(); if (m_log.isDebugEnabled()) { m_log.debug("getDifferenceValue() PreviousTable deviceName : " + getCollectMethod() + ", entry.getValue() : " + entry.getValue()); m_log.debug("getDifferenceValue() PreviousTable entry.getValue() is " + (entry.getValue()).getClass().getCanonicalName()); } // value ?String ????????Object?Class check String value = null; if (entry.getValue() instanceof String) { value = (String) entry.getValue(); } else if (entry.getValue() == null) { m_log.debug("entry.getValue() == null."); continue; } else { value = entry.getValue().toString(); } if (value.length() > 128) { // ????128?????? value = value.substring(0, 128); } if (value.equals(deviceName)) { previousIndex = entry.getKey().substring(entry.getKey().lastIndexOf(".")); break; } } if (m_log.isDebugEnabled()) { m_log.debug("getDifferenceValue() deviceName : " + deviceName + ", currentIndex : " + currentIndex + ", previousIndex : " + previousIndex); } // ????????? if (currentIndex == null) { // ? StringBuffer detailMsg = new StringBuffer(); detailMsg.append("DeviceName " + deviceName + " is not exists\n"); detailMsg.append("Search Device Name is ...\n"); itr = currentEntrys.iterator(); while (itr.hasNext()) { TableEntry entry = itr.next(); detailMsg.append(" [" + (String) entry.getValue() + "]\n"); } String message = "getDifferenceValue() deviceName : " + deviceName + ", currentIndex is null"; m_log.info(message); throw new CollectedDataNotFoundException(message); } if (previousIndex == null) { // ? String message = "getDifferenceValue() deviceName : " + deviceName + ", previousIndex is null"; m_log.debug(message); throw new CollectedDataNotFoundException(message); } // pollingTarget?TableEntry??????Key? pollingTarget = data.getPollingTarget(); } catch (CollectedDataNotFoundException e) { throw e; } catch (Exception e) { m_log.warn("getDifferenceValue() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e); } try { return getMibValueDiff(data, currentTable, previousTable, PollerProtocolConstant.getEntryKey(getCollectMethod(), pollingTarget + currentIndex), PollerProtocolConstant.getEntryKey(getCollectMethod(), pollingTarget + previousIndex)); } catch (CollectedDataNotFoundException e) { throw e; } catch (Exception e) { // ??getMibValueDiff()?????failureValue??????polling target??? // NullPointerException?? // ??ver4.0???????????stacktrace?????? String message = "getDifferenceValue() : " + e.getClass().getSimpleName() + ", " + e.getMessage(); m_log.debug(message, e); // ? throw new CollectedDataNotFoundException(message); } } // VM????? else { /* * getCollectMethod()?VMWARE, XEN?????? * getCollectMethod()??? * select * from cc_vm_solution_mst; * ???????????? * ????????throw new CollectedDataNotFound() * ? */ try { // entryKey???"?"?? String pollingTarget = data.getPollingTarget() + "." + deviceName; // VM??????????entryKey??????????????entryKey? return getMibValueDiff(data, currentTable, previousTable, PollerProtocolConstant.getEntryKey(getCollectMethod(), pollingTarget), PollerProtocolConstant.getEntryKey(getCollectMethod(), pollingTarget)); } catch (CollectedDataNotFoundException e) { throw e; } catch (Exception e) { // ??getMibValueDiff()?????failureValue??????polling target??? // NullPointerException?? // ??ver4.0???????????stacktrace?????? String message = "getDifferenceValue() : " + e.getClass().getSimpleName() + ", " + e.getMessage(); m_log.debug(message, e); // ? throw new CollectedDataNotFoundException(message); } } } else if ("*".equals(targetIndex)) { try { // entryKey???"*"?? String pollingTarget = data.getPollingTarget(); // ??????pollingTarget???? Set<TableEntry> entries = currentTable.getValueSetStartWith( PollerProtocolConstant.getEntryKey(getCollectMethod(), pollingTarget)); if (entries == null) { String message = "getDifferenceValue() : entries is null"; m_log.info(message); // ? throw new CollectedDataNotFoundException(message); } // ?? double total = 0; // ?0?? Iterator<TableEntry> itr = entries.iterator(); while (itr.hasNext()) { String tableEntryKey = itr.next().getKey(); total = total + getMibValueDiff(data, currentTable, previousTable, tableEntryKey, tableEntryKey); // entryKey??2???????????????? } return total; } catch (CollectedDataNotFoundException e) { throw e; } catch (Exception e) { // ? String message = "getDifferenceValue() : " + e.getClass().getSimpleName() + ", " + e.getMessage(); m_log.warn(message, e); throw new CollectedDataNotFoundException(message); } } else { try { return getMibValueDiff(data, currentTable, previousTable, PollerProtocolConstant.getEntryKey(getCollectMethod(), entryKey), PollerProtocolConstant.getEntryKey(getCollectMethod(), entryKey)); } catch (CollectedDataNotFoundException e) { throw e; } catch (Exception e) { // ??getMibValueDiff()?????failureValue??????polling target??? // NullPointerException?? // ??ver4.0???????????stacktrace?????? String message = "getDifferenceValue() : " + e.getClass().getSimpleName() + ", " + e.getMessage(); m_log.debug(message, e); throw new CollectedDataNotFoundException(message); } } } private double getValue(String variable, DataTable table, String deviceName) throws CollectedDataNotFoundException, InvalidValueException { if (m_log.isDebugEnabled()) { m_log.debug("getValue() variable : " + variable); } CollectorPollingMstData data = getCollectorPollingMstData(variable); // ??? String entryKey = data.getEntryKey(); String targetIndex = entryKey.substring(entryKey.lastIndexOf(".") + 1); if (m_log.isDebugEnabled()) { m_log.debug("getValue() targetIndex : " + targetIndex); } // ??????? if ("?".equals(targetIndex)) { // SNMPWBEM????? if (getCollectMethod().equals(PollerProtocolConstant.PROTOCOL_SNMP) || getCollectMethod().equals(PollerProtocolConstant.PROTOCOL_WBEM)) { CollectorPollingMstData wbemData = getCollectorPollingMstData("key"); String pollingTarget = wbemData.getPollingTarget(); if (m_log.isDebugEnabled()) { m_log.debug("getValue() targetKey : " + pollingTarget); } // ?????????????? String index = null; try { // ?????????????? Set<TableEntry> entrySet = table.getValueSetStartWith( PollerProtocolConstant.getEntryKey(getCollectMethod(), pollingTarget)); if (entrySet == null) { // ? String message = "getValue() entrySet is null, entrySet : null"; m_log.debug(message); throw new CollectedDataNotFoundException(message); } Iterator<TableEntry> itr = entrySet.iterator(); while (itr.hasNext()) { TableEntry entry = itr.next(); if (m_log.isDebugEnabled()) { m_log.debug("getValue() CurrentTable deviceName : " + getCollectMethod() + ", entry.getValue() : " + entry.getValue()); m_log.debug("getValue() CurrentTable entry.getValue() is " + (entry.getValue()).getClass().getCanonicalName()); } // value ?String ????????Object?Class check String value = null; if (entry.getValue() == null) { continue; } else if (entry.getValue() instanceof String) { value = (String) entry.getValue(); } else { value = entry.getValue().toString(); } if (value.length() > 128) { // ????128?????? value = value.substring(0, 128); } if (value.equals(deviceName)) { index = entry.getKey().substring(entry.getKey().lastIndexOf(".")); break; } } if (m_log.isDebugEnabled()) { m_log.debug("getValue() index : " + index); } if (index == null) { // ? StringBuffer detailMsg = new StringBuffer(); detailMsg.append("DeviceName " + deviceName + " is not exists\n"); detailMsg.append("Search Device Name is ...\n"); itr = entrySet.iterator(); while (itr.hasNext()) { TableEntry entry = itr.next(); detailMsg.append(" [" + entry.getValue() + "]\n"); } String message = "getValue() deviceName : " + deviceName + ", index is null"; m_log.info(message); throw new CollectedDataNotFoundException(message); } // pollingTarget?TableEntry??????Key? pollingTarget = data.getPollingTarget(); } catch (CollectedDataNotFoundException e) { throw e; } catch (Exception e) { m_log.warn("getValue() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e); } try { return getMibValueDouble(table, data, PollerProtocolConstant.getEntryKey(getCollectMethod(), pollingTarget + index)); } catch (CollectedDataNotFoundException e) { throw e; } catch (Exception e) { String message = "getValue() : " + e.getClass().getSimpleName() + ", " + e.getMessage(); m_log.warn(message, e); // ? throw new CollectedDataNotFoundException(message); } } // VM????? else { /* * getCollectMethod()?VMWARE, XEN?????? * getCollectMethod()??? * select * from cc_vm_solution_mst; * ???????????? * ????????throw new CollectedDataNotFound() * ? */ try { // entryKey???"?"?? String pollingTarget = data.getPollingTarget() + "." + deviceName; return getMibValueDouble(table, data, PollerProtocolConstant.getEntryKey(getCollectMethod(), pollingTarget)); } catch (CollectedDataNotFoundException e) { throw e; } catch (Exception e) { String message = "getValue() : " + e.getClass().getSimpleName() + ", " + e.getMessage(); m_log.warn(message, e); // ? throw new CollectedDataNotFoundException(message); } } } else if ("*".equals(targetIndex)) { try { // entryKey???"*"?? String pollingTarget = data.getPollingTarget(); // ??????pollingTarget???? Set<TableEntry> entries = table.getValueSetStartWith( PollerProtocolConstant.getEntryKey(getCollectMethod(), pollingTarget)); if (entries == null) { String message = "getValue() : entries is null"; m_log.info(message); // ? throw new CollectedDataNotFoundException(message); } // ?? double total = 0; // ?0?? Iterator<TableEntry> itr = entries.iterator(); while (itr.hasNext()) { String tableEntryKey = itr.next().getKey(); total = total + getMibValueDouble(table, data, tableEntryKey); // entryKey??2???????????????? } return total; } catch (CollectedDataNotFoundException e) { throw e; } catch (Exception e) { // ? String message = "getValue() : " + e.getClass().getSimpleName() + ", " + e.getMessage(); m_log.warn(message, e); throw new CollectedDataNotFoundException(message); } } else { try { return getMibValueDouble(table, data, PollerProtocolConstant.getEntryKey(getCollectMethod(), entryKey)); } catch (CollectedDataNotFoundException e) { throw e; } catch (Exception e) { // ? String message = "getValue() : " + e.getClass().getSimpleName() + ", " + e.getMessage(); m_log.warn(message, e); throw new CollectedDataNotFoundException(message); } } } /** * * mib?long????? * mib??????????????? * ???convLongToDoble?double????return??????? * * @see #convLongToDouble(long) * * @param table * @param data * @param entryKey * @return * @throws CollectedDataNotFoundException */ private long getMibValueLong(DataTable table, CollectorPollingMstData data, String entryKey) throws CollectedDataNotFoundException { if (m_log.isDebugEnabled()) { m_log.debug("getMibValueLong() entryKey : " + entryKey); } TableEntry entry = table.getValue(entryKey); // ?OID?????? if (entry == null) { if (data.getFailureValue() == null) { String message = "failure value is null"; m_log.debug(message); throw new CollectedDataNotFoundException(message); } // ???????? return Long.parseLong(data.getFailureValue()); } else { // polling?????????? if (!entry.isValid()) { String message = entry.getErrorDetail().getMessage(); throw new CollectedDataNotFoundException(message); } long value = (Long) entry.getValue(); // // ??? // if(value < 0){ // // ??Counter64??? // // java?long???????Long.MAX_VALUE??????????? // if("Counter64".equals(data.getValueType())){ // throw new InvalidValueException(); // } // } return value; } } /** * * mib?double????? * ??????????????????????????? * * @param table * @param data * @param entryKey * @return * @throws CollectedDataNotFoundException */ private double getMibValueDouble(DataTable table, CollectorPollingMstData data, String entryKey) throws CollectedDataNotFoundException { long longValue = getMibValueLong(table, data, entryKey); return convLongToDouble(longValue); } /** * * ???long?double????? * * @param longValue * @return */ private double convLongToDouble(long longValue) { double ret = longValue; if (longValue < 0) { long tmp = longValue - Long.MAX_VALUE; ret = (double) Long.MAX_VALUE + tmp; } return ret; } /** * ???????pollingTarget??????? * @throws CollectedDataNotFoundException */ private double getMibValueDiff(CollectorPollingMstData data, DataTable currentTable, DataTable previousTable, String currentEntryKey, String previousEntryKey) throws CollectedDataNotFoundException { // return? double ret = 0; // ???? long previousValue = getMibValueLong(previousTable, data, previousEntryKey); // ??? long currentValue = getMibValueLong(currentTable, data, currentEntryKey); // long?? long diff = currentValue - previousValue; // ???? if (diff >= 0) { ret = diff; // ?????double??? } else { // ??Counter32??????????? if ("Counter32".equals(data.getValueType()) || "Uint32".equals(data.getValueType())) { diff = COUNTER32_MAX_VALUE + 1 + diff; ret = convLongToDouble(diff); // ??Counter64??????????? } else if ("Counter64".equals(data.getValueType()) || "Uint64".equals(data.getValueType())) { ret = COUTNER64_MAX_VALUE.add(BigInteger.valueOf(diff + 1)).doubleValue(); } } return ret; } /** * ?EntryKey? * * CollectorPollingMstData ????? * ? * * ? * ?? * * ? * ? * ?? */ private CollectorPollingMstData getCollectorPollingMstData(String variable) { CollectorPollingMstData data = m_oidMap.get(variable); // ??????? if (data == null) { registerOidMap(variable); data = m_oidMap.get(variable); // ???????? if (data == null) { m_log.info("data=null"); // ? throw new IllegalStateException("variable=" + variable); } } return data; } private void registerOidMap(String variable) { try { if (m_log.isDebugEnabled()) { m_log.debug(getPlatformId() + ", " + getItemCode() + ", " + variable); } CollectorPollingMstEntity bean = null; try { bean = QueryUtil.getCollectorPollingMstPK(getCollectMethod(), getPlatformId(), getSubPlatformId(), getItemCode(), variable); } catch (CollectorNotFound e) { } if (bean != null) { CollectorPollingMstData data = new CollectorPollingMstData(bean.getId().getCollectMethod(), bean.getId().getPlatformId(), bean.getId().getSubPlatformId(), bean.getId().getItemCode(), bean.getId().getVariableId(), bean.getEntryKey(), bean.getSnmpValueTypeMstEntity().getValueType(), bean.getPollingTarget(), bean.getFailureValue()); m_oidMap.put(variable, data); } } catch (Exception e) { m_log.warn("registerOidMap() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e); } } public String getCollectMethod() { return collectMethod; } public void setCollectMethod(String collectMethod) { this.collectMethod = collectMethod; } public String getPlatformId() { return platformId; } public void setPlatformId(String platformId) { this.platformId = platformId; } public String getSubPlatformId() { return subPlatformId; } public void setSubPlatformId(String subPlatformId) { this.subPlatformId = subPlatformId; } public String getItemCode() { return itemCode; } /** * ???? * ???????????? */ public void setItemCode(String itemCode) { this.itemCode = itemCode; try { // ???? List<CollectorPollingMstEntity> beans = QueryUtil.getAllCollectorPollingMstVariableId( getCollectMethod(), getPlatformId(), getSubPlatformId(), itemCode); // ???????ID getVariables().clear(); // ??????ID?? for (CollectorPollingMstEntity bean : beans) { if (m_log.isDebugEnabled()) { m_log.debug("add " + bean.getId().getVariableId()); } getVariables().add(bean.getId().getVariableId()); } } catch (Exception e) { m_log.warn("setItemCode() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e); } } public List<String> getVariables() { return variables; } public void setVariables(List<String> variables) { this.variables = variables; } public static class InvalidValueException extends Exception { private static final long serialVersionUID = -1102457433444726271L; public InvalidValueException(String messages) { super(messages); } } public class CollectedDataNotFoundException extends Exception { private static final long serialVersionUID = 6306555743811316089L; public CollectedDataNotFoundException(String messages) { super(messages + ", itemcode=" + itemCode); } } public void setExpression(String expression) { this.expression = expression; } public String getExpression() { return expression; } }