Beispiel #1
0
unsigned long CSVSnmpOID::AvgValue(const MonitorResult &ResultList)
{
    if(!m_bSum)
    {
        m_szSumValue = 0;
        m_szIndexCount = 0;
        m_ulMax = 0;
        m_ulMin = 0;

        unsigned long uValue = 0;

        for(resultConstItem it = ResultList.begin(); it != ResultList.end(); it ++)
        {
            if(!m_szSelName.empty() && it->second.m_szValue == m_szSelName)
            {
                m_szIndexCount ++;
                uValue = atoi(it->second.m_szValue.c_str());
                m_szSumValue += uValue;
                if(m_ulMax < uValue)
                    m_ulMax = uValue;
                if(m_ulMin > uValue)
                    m_ulMin = uValue;
            }
            else if(m_szSelName.empty())
            {
                m_szIndexCount ++;
                uValue = atoi(it->second.m_szValue.c_str());
                m_szSumValue += uValue;
                if(m_ulMax < uValue)
                    m_ulMax = uValue;
                if(m_ulMin > uValue)
                    m_ulMin = uValue;
            }
        }
        //m_szIndexCount = static_cast<int>(ResultList.size());

        m_bSum = true;
    }
    if(m_szIndexCount > 0)
        return static_cast<unsigned long>(m_szSumValue / m_szIndexCount);
    else
        return 0;
}
Beispiel #2
0
/////////////////////////////////////////////////////////////////////////////
// 函数:GetResult                                                         //
// 说明:根据已经配置好的参数来进行计算                                    //
// 参数:                                                                  //
//      chReturn, [in]类型,计算结果                                       //
// 返回值:                                                                //
//      成果返回true,否则返回false                                         //
/////////////////////////////////////////////////////////////////////////////
bool CSnmpOperate::GetResult(char* chReturn, int & nSize)
{
	int i;
	CSVSnmpOID objOID;

	if(InitSNMP(objOID) == 0)
	{//初始化SNMP 
		if(!ReadConfig())
        {//读取配置文件失败
            strcpy(chReturn, chResult);
			nSize = static_cast<int>(strlen(chResult));
            return FALSE;
        }		
        bool bReturn = true;
		svutil::TTime curTime = svutil::TTime::GetCurrentTimeEx();
		char chOID[MAX_BUFF_LEN] = {0};

        CSVExpression expression;
        MonitorResult resultList;
        resultItem  resultIt;
		for(i = 0 ; i < m_nOidCount; i++)
		{//得到每一个oid的结果
            if(pOidList[i].nType == 1)
            {
				if(strlen(pOidList[i].chOIDIndex)==0)
				{
					objOID.SetOIDType(1);
					sprintf(chOID, "%s", pOidList[i].chOID);
				}
				else
				{
					objOID.SetOIDType(0);
                    sprintf(chOID, "%s.%s", pOidList[i].chOID , pOidList[i].chOIDIndex);
				}
            }
            else 
            {                
                sprintf(chOID, "%s", pOidList[i].chOID);
            }

			objOID.SetSelName(chSelValue);
			objOID.SetOIDValue(chOID);
            resultList.clear();

			int nResult = objOID.GetResult(resultList);
		    if(nResult ==0)
			{//得到结果成功
                resultIt = resultList.begin();

/*//				FILE *stream;
//				
//				sprintf(str,"%s\n%s:%s,%s\n",str,resultIt->second.m_szOID.c_str(),
//											resultIt->second.m_szValue.c_str(),
//											resultIt->second.m_szIndex.c_str());
//
//				stream = fopen( "fread.txt", "a+" );
//				fwrite( str, sizeof( char ), strlen(str), stream );
//				fclose(stream);
*///

                if(resultIt != resultList.end())
                {
                    unsigned long ulHisValue = 0, ulValue = 0,uSubValue = 0;
                    ulHisValue = (unsigned long)atoi(pOidList[i].chHisValue);
                    ulValue = (unsigned long)atoi(resultIt->second.m_szValue.c_str());
				    uSubValue = ulValue;
                    if(pOidList[i].lHisTime == -99 && pOidList[i].nHasHistory == 1)
                    { 
                        bReturn = false;
                    }
                    else
                    {   
                        unsigned long lTime = 0;//时间差值
                        if(pOidList[i].nHasHistory == 1)
                        {
							if(ulValue < ulHisValue)
							{
								//ulValue = 0;
								char chMsg[MAX_BUFF_LEN] = {0};
								char chSection[MAX_BUFF_LEN] = {0};
								char chTempFile[MAX_BUFF_LEN] = {0};
								sprintf(chTempFile, "snmp_%s_%s_%s.ini", chHostIP, chIfIndex, chMonitorID);
								sprintf(chSection, "InterFace_%d_%s_%d", m_nTPLID, chIfIndex, i+1);
								strcpy(pOidList[i].chHisValue, resultIt->second.m_szValue.c_str());
								WritePrivateProfileString1(chSection, "HistoryValue", pOidList[i].chHisValue, chTempFile);
								sprintf(chMsg, "%ld", curTime.GetTime());
								WritePrivateProfileString1(chSection, "ifTime", chMsg, chTempFile);
								Sleep(1000);
								GetResult(chReturn, nSize);
							}
							else
								uSubValue = ulValue - ulHisValue;

							if(uSubValue > ulValue)
							{
								ofstream fout("snmperror.log",ios::app);
								fout << "oid" << i + 1<<":hisvalue="<< pOidList[i].chHisValue<<":"<<ulHisValue << 
									", curvalue="<<resultIt->second.m_szValue<<":"<<ulValue<<
									", subvalue="<<uSubValue<<", time="<<lTime<<", curtime="<<curTime.Format()<<"\r\n"; 
								fout << flush; 
								fout.close(); 
							}

				            TTime bTime(pOidList[i].lHisTime);//上次采集数据时间
		                    TTimeSpan spanTime = curTime - bTime; //时间差                     
                            strlwr(pOidList[i].chUnit);//单位所有字符变换为小写
                            if(strcmp(pOidList[i].chUnit, "second") == 0)
                            {//单位是秒
                                lTime = spanTime.GetTotalSeconds();
                            }
                            else if(strcmp(pOidList[i].chUnit, "minute") == 0)
                            {//单位是分钟
                                lTime = spanTime.GetTotalMinutes();
                            }
                            else if(strcmp(pOidList[i].chUnit, "hours") == 0)
                            {//单位是小时
						        lTime = spanTime.GetHours();
                            }
                            else
                            {//缺省条件以秒计算
                                lTime = spanTime.GetTotalSeconds();
                            }

                            if(lTime <= 0)
                            {
                                //lTime = 1;
								char chMsg[MAX_BUFF_LEN] = {0};
								char chSection[MAX_BUFF_LEN] = {0};
								char chTempFile[MAX_BUFF_LEN] = {0};
								sprintf(chTempFile, "snmp_%s_%s_%s.ini", chHostIP, chIfIndex, chMonitorID);
								sprintf(chSection, "InterFace_%d_%s_%d", m_nTPLID, chIfIndex, i+1);
								strcpy(pOidList[i].chHisValue, resultIt->second.m_szValue.c_str());
								WritePrivateProfileString1(chSection, "HistoryValue", pOidList[i].chHisValue, chTempFile);
								sprintf(chMsg, "%ld", curTime.GetTime());
								WritePrivateProfileString1(chSection, "ifTime", chMsg, chTempFile);
								Sleep(1000);
								GetResult(chReturn, nSize);
                            }
                        }

                        char chOIDName[8] = {0};
                        sprintf(chOIDName, "oid%d", i + 1);
                        string szOIDName = chOIDName;

                        expression.AddFields(szOIDName, "value", uSubValue);
                        expression.AddFields(szOIDName, "time", lTime);
                        expression.AddFields(szOIDName, "avg",  objOID.AvgValue(resultList));
                        expression.AddFields(szOIDName, "max", objOID.MaxValue(resultList));
                        expression.AddFields(szOIDName, "min", objOID.MinValue(resultList));
                        expression.AddFields(szOIDName, "total", objOID.SumValue(resultList));
                        expression.AddFields(szOIDName, "indexcount", objOID.IndexCount(resultList));
                    }
                    strcpy(pOidList[i].chHisValue, resultIt->second.m_szValue.c_str());
                }
                else
                {
                    bReturn = false;
                }
			}
			else
            {//如果获取oid值失败
                bReturn = false;
				//(TPLMIB-%d-oid-%d)  用户无法理解
                sprintf(chReturn, "error=%s", objOID.GetErrorMsg(nResult), m_nTPLID, i+1);

				nSize = static_cast<int>(strlen(chReturn));
                break;
			}
		}

	    chResult[0] = '\0';
		int retlen = 0;
        if(bReturn)
        {
		    try
            {
            for(map<string, string, less<string> >::iterator it = m_mapExpList.begin(); it != m_mapExpList.end(); it ++)
		    {//根据每一个公式计算结果
			    string rpn;//公式拆分结果
                double dResult = 0.0f;
			    int err = expression.calculateDouble(it->first, dResult);
                if(err == CSVExpression::eval_ok )
				{//计算成功     
					if( i == 0)
                    {//如果是第一条计算结果					
						sprintf(chReturn, "%s=%.2f", it->second.c_str(), dResult);
						retlen = static_cast<int>(strlen(chReturn)) + 1;
						nSize = retlen;							
                    }
					else
                    {//不是第一条计算结果
                        char chTemp[MAX_BUFF_LEN] = {0};
						char chTemp1[MAX_BUFF_LEN] = {0};
						memcpy(chTemp, chReturn, retlen);
                        //格式化结果字符串
						sprintf(chTemp1, "%s=%.2f", it->second.c_str(), dResult);
						int retlen1 = static_cast<int>(strlen(chTemp1));
						memcpy(chReturn + retlen, chTemp1, retlen1);
						retlen += (retlen1 + 1);
						nSize = retlen;

                    }
				}
				else
				{//如果计算公式中存在错误或者计算中出现错误(如:除数为0)					
                    if(err == CSVExpression::eval_idiv)
                    {
                        if( i == 0)
                        {//如果是第一条计算结果
						    sprintf(chReturn, "%s=0.0", it->second.c_str());
							retlen = static_cast<int>(strlen(chReturn)) + 1;
                        }
					    else
                        {//不是第一条计算结果
                            char chTemp[MAX_BUFF_LEN] = {0};
							char chTemp1[MAX_BUFF_LEN] = {0};
							memcpy(chTemp, chReturn, retlen);
                            //格式化结果字符串
							sprintf(chTemp1, "%s=0.0f", it->second.c_str());
							int retlen1 = static_cast<int>(strlen(chTemp1));
							memcpy(chReturn + retlen, chTemp1, retlen1);
							retlen += (retlen1 + 1);
                        }
						nSize = retlen;
                    }
			    }
		    }
            }
            catch(...)
            {
                DWORD dwError = GetLastError();
                char szMsg[512] = {0};
                int nlen = sprintf(szMsg, "Error Number is %08X --*Get Result*---", dwError);
                DumpLog("snmpmonitor-exp.log", szMsg, nlen);
            }
        }
        else
        {
			if(strlen(chReturn)<1)
			{
				std::string m_TempStr = FuncGetStringFromIDS("SV_SNMP_MONITOR", "SNMP_NO_HISTORY_DATA");
				sprintf(chReturn, "error=%s", m_TempStr.c_str());
				nSize = static_cast<int>(strlen(chReturn));
			}
			
        }	
        char chMsg[MAX_BUFF_LEN] = {0};
        char chSection[MAX_BUFF_LEN] = {0};
        char chTempFile[MAX_BUFF_LEN] = {0};
        sprintf(chTempFile, "snmp_%s_%s_%s.ini", chHostIP, chIfIndex, chMonitorID);
        for(i = 0 ; i < m_nOidCount; i++)
        {
            sprintf(chSection, "InterFace_%d_%s_%d", m_nTPLID, chIfIndex, i+1);
		    WritePrivateProfileString1(chSection, "HistoryValue", pOidList[i].chHisValue, chTempFile);
		    sprintf(chMsg, "%ld", curTime.GetTime());
		    WritePrivateProfileString1(chSection, "ifTime", chMsg, chTempFile);
        }
	}
    else
    {//初始化SNMP失败
	    std::string m_IDS = FuncGetStringFromIDS("IDS_InitSNMPFailed");
        sprintf(chReturn, "error=%s", (char*)m_IDS.c_str());
		nSize = static_cast<int>(strlen(chReturn));
        return false;
    }
	return true;
}