12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586 |
- #include "stdafx.h"
- #define WIN32_LEAN_AND_MEAN
- #include "SelfCheckerFSM.h"
- #include "EventCode.h"
- #pragma comment(lib,"user32.lib")
- class CSelfCheckerEntity;
- const int MAX_AYSNC_TIMEOUT = 60000;
- const int MAX_CHECK_TIME = 60000;
- const int MAX_CPU_CHECK_TIME = 5000;
- const int TIMER_ID_CHECK = 0;
- const int TIMER_CPU_CHECK = 1;
- const int THOUSAND = 1024;
- const int MILLION = 1048576;
- int ActionStrToInt(const char *pAction)
- {
- int ret = 0;
- char x = *pAction;
- if (x >= '0' && x <= '9')
- return x - '0';
- else
- return 0;
- }
- DWORD GetRadixProduct(int times,int radix)
- {
- DWORD dwRet = 1;
- if (times < 0 || (radix != 10 && radix != 16))
- return 0;
- else
- {
- for (int i = 0; i < times; i++)
- dwRet *= radix;
- }
- return dwRet;
- }
- DWORD CodeStrToInt(const char *pCode)
- {
- CSimpleStringA csCode(pCode);
- csCode = csCode.Trim();
- int len = csCode.GetLength();
- if (len < 0 || len > 10)
- {
- return 0;
- }
- DWORD dwRet = 0;
- if (len > 2 && csCode[0] == '0' && (csCode[1] == 'x' || csCode[1] == 'X'))//"0x1234,0X12FF"
- {
- for (int i = 2; i < len; i++)
- {
- if (csCode[i] <= '9' && csCode[i] >= '0')
- dwRet += (csCode[i] - '0')*GetRadixProduct(len - i - 1,16);
- else if (csCode[i] <= 'f' && csCode[i] >= 'a')
- dwRet += (csCode[i] - 'a' + 10)*GetRadixProduct(len - i - 1,16);
- else if (csCode[i] <= 'F' && csCode[i] >= 'A')
- dwRet += (csCode[i] - 'A' + 10)*GetRadixProduct(len - i - 1, 16);
- else
- return 0;//because there is a invalid char,break process and return 0
- }
- }
- else//"768"
- {
- for (int i = 0; i < len; i++)
- {
-
- if (csCode[i] <= '9' && csCode[i] >= '0')
- dwRet += (csCode[i] - '0')*GetRadixProduct(len - i - 1, 10);
- else
- return 0;//because there is a invalid char,break process and return 0
- }
- }
- return dwRet;
- }
- unsigned long long GetTickCountRVC() {
- #ifdef RVC_OS_WIN
- return GetTickCount64();
- #else
- struct timespec ts;
- clock_gettime(CLOCK_MONOTONIC, &ts);
- return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
- #endif //RVC_OS_WIN
- }
- ErrorCodeEnum CSelfCheckerFSM::OnInit()
- {
- m_xIdlePre = m_xKernelPre = m_xUserPre = 0;
- ErrorCodeEnum errCode = Initial();
- if (errCode != Error_Succeed)
- return Error_IO;
- return Error_Succeed;
- }
- ErrorCodeEnum CSelfCheckerFSM::OnExit()
- {
- return Error_Succeed;
- }
- void CSelfCheckerFSM::s0_on_entry()
- {
- LOG_FUNCTION();
- ErrorCodeEnum errCode;
- CAutoArray<WORD> tmpInstIDs;
- CAutoArray<CSimpleStringA> tmpNames;
- errCode = GetEntityBase()->GetFunction()->GetAllRegistedEntity(tmpNames,tmpInstIDs);
- if (errCode != Error_Succeed)
- {
- Dbg("Get started entity failed.[%d]",errCode);
- }
- for (int i = 0; i < tmpNames.GetCount(); ++i)
- {
- ErrorCodeEnum eErr;
- CSmartPointer<IEntityFunction> pFunc = GetEntityBase()->GetFunction();
- CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
- CEntityRunInfo runInfo;
- eErr = pFunc->GetEntityRunInfo(tmpNames[i], runInfo);
- if (runInfo.eState != EntityState_NoStart)
- {
- Dbg("Add entity %s",(const char*)tmpNames[i]);
- m_allEntity.push_back(tmpNames[i]);
- m_activeEntity.push_back(tmpNames[i]);
- }
- }
- //m_pEntity-> m_entityNames.push_back(tmpNames[i]);
- FSMEvent *pEvt = new FSMEvent(USER_EVT_INIT);
- PostEventFIFO(pEvt);
- }
- void CSelfCheckerFSM::s0_on_exit()
- {
- LOG_FUNCTION();
- }
- unsigned int CSelfCheckerFSM::s0_on_event(FSMEvent* pEvt)
- {
- LOG_FUNCTION();
- switch(pEvt->iEvt)
- {
- case USER_EVT_INIT:
- pEvt->SetHandled();
- break;
- default:
- break;
- }
- return 0;
- }
- void CSelfCheckerFSM::s1_on_entry()
- {
- LOG_FUNCTION();
- void *pTmpData = NULL;
- ITimerListener *pListener = new TimerOutHelper<CSelfCheckerFSM>(this, &CSelfCheckerFSM::OnNormalWorkTimerout,pTmpData);
- GetEntityBase()->GetFunction()->SetTimer(TIMER_ID_CHECK, pListener, MAX_CHECK_TIME);
- //oilyang@20170703 add for cpu usage for process
- pTmpData = NULL;
- pListener = new TimerOutHelper<CSelfCheckerFSM>(this, &CSelfCheckerFSM::OnCalcCpuUsageTimerout, pTmpData);
- GetEntityBase()->GetFunction()->SetTimer(TIMER_CPU_CHECK, pListener, MAX_CPU_CHECK_TIME);
- }
- void CSelfCheckerFSM::s1_on_exit()
- {
- LOG_FUNCTION();
- }
- unsigned int CSelfCheckerFSM::s1_on_event(FSMEvent* evt)
- {
- LOG_FUNCTION();
- return 0;
- }
- void CSelfCheckerFSM::s2_on_entry()
- {
- LOG_FUNCTION();
- }
- void CSelfCheckerFSM::s2_on_exit()
- {
- LOG_FUNCTION();
- }
- unsigned int CSelfCheckerFSM::s2_on_event(FSMEvent* evt)
- {
- LOG_FUNCTION();
- return 0;
- }
- void CSelfCheckerFSM::s3_on_entry()
- {
- LOG_FUNCTION();
- }
- void CSelfCheckerFSM::s3_on_exit()
- {
- LOG_FUNCTION();
- }
- unsigned int CSelfCheckerFSM::s3_on_event(FSMEvent* evt)
- {
- LOG_FUNCTION();
- return 0;
- }
- int ch2int(char ch)
- {
- if (ch >= '0' && ch <= '9')
- return ch-'0';
- else if (ch >= 'a' && ch <= 'f')
- return ch-'a'+10;
- else if (ch >= 'A' && ch <= 'F')
- return ch-'A'+10;
- return 0;
- }
- long hexstr2int(const char *str, int len)
- {
- long result = 0;
- for (int i = 0; i < len; ++i)
- {
- result += (ch2int(str[i]) << ((len-i-1)*4));
- }
- return result;
- }
- bool StrEqualNoCase(const char *s1, const char *s2,int len)
- {
- if (strlen(s1) != strlen(s2))
- return false;
- for (int i = 0; i < len; ++i)
- {
- if (toupper(s1[i]) != toupper(s2[i]))
- return false;
- }
- return true;
- }
- ErrorCodeEnum CSelfCheckerFSM::Initial()
- {
- ErrorCodeEnum err;
- CSmartPointer<IConfigInfo> spConfig;
- err = GetEntityBase()->GetFunction()->OpenConfig(Config_Software, spConfig);
- if (err != Error_Succeed) {
- Dbg("open cfg file failed!");
- return Error_IO;
- }
- m_restartNormal = 3;
- m_restartSpecial = 5;
- m_maxOsRestart = 5;
- m_maxPowerRestart = 5;
- spConfig->ReadConfigValueInt("init","RestartNormal",m_restartNormal);
- spConfig->ReadConfigValueInt("init","RestartSpecial",m_restartSpecial);
- spConfig->ReadConfigValueInt("init","MaxOsRestart",m_maxOsRestart);
- spConfig->ReadConfigValueInt("init","MaxPowerRestart",m_maxPowerRestart);
- spConfig->ReadConfigValueInt("init","SimulateTestFlag",m_simulateTest);
- spConfig->ReadConfigValue("init","KeyEntity",m_csKeyEntity);
- spConfig->ReadConfigValueInt("init","CpuTooHighPercent",m_cpuHighPercent);
- spConfig->ReadConfigValueInt("init","MemoryTooHighPercent",m_memHighPercent);
- spConfig->ReadConfigValueInt("init","HardDiskTooHighPercent",m_diskHighPercent);
- ifstream is;
-
- CSimpleStringA cfgPath(""),cfgXml("");
- err = GetEntityBase()->GetFunction()->GetPath("cfg",cfgPath);
- //cfgPath = cfgPath + "\\SelfChecker.ini";
- cfgXml = cfgPath + "/SelfCheckerProc.xml";
- ReadXmlFile(cfgXml);
- Dbg("cfgxml[%s]", cfgXml);
- //is.open (cfgPath, ios::binary);
- //if (!is.is_open())
- // return Error_IO;
- //string line;
- //long curr,end;
- //is.seekg(0,ios_base::end);
- //end = is.tellg();
- //is.seekg(0,ios_base::beg);
- //CheckPattern eSection = CHECK_UNKNOWN;
- ////load config file
- //do
- //{
- // getline(is,line);
- // if (line[0] == '[')
- // {
- // size_t secEnd = line.find(']',1);
- // if (secEnd != string::npos)
- // {
- // string strSec = line.substr(1,secEnd-1);
- // if (StrEqualNoCase(strSec.c_str(),"ShakeHand",strSec.length()))
- // eSection = CHECK_HANDSHAKE;
- // else if (StrEqualNoCase(strSec.c_str(),"Examine",strSec.length()))
- // eSection = CHECK_EXAMINE;
- // else if (StrEqualNoCase(strSec.c_str(),"Reset",strSec.length()))
- // eSection = CHECK_RESET;
- // else if (StrEqualNoCase(strSec.c_str(),"Restart",strSec.length()))
- // eSection = CHECK_RESTART;
- // else
- // eSection = CHECK_UNKNOWN;
- // }
- // }
- // else if (((unsigned)(line[0]+1) > 256) || line[0] == ';')
- // {//Chinese and other...
- // curr = is.tellg();
- // continue;
- // }
- // else if (isalpha(line[0]))
- // {
- // if (eSection == CHECK_UNKNOWN)
- // {
- // curr = is.tellg();
- // continue;
- // }
- // size_t keyPos = line.find('=',0);
- // if (keyPos != string::npos)
- // {
- // string keyName = line.substr(0,keyPos);
- // size_t douPos = line.find(',',keyPos);
- // size_t fenPos,start = keyPos;
- // m_entCfgInfo[keyName.c_str()].entityRestartCount = 0;
- // m_entCfgInfo[keyName.c_str()].osRestartCount = 0;
- // m_entCfgInfo[keyName.c_str()].powerRestartCount = 0;
- // m_entCfgInfo[keyName.c_str()].bWaitRestart = false;
- // for (;douPos != string::npos;)
- // {
- // ErrorCodeEnum evtCode = (ErrorCodeEnum)hexstr2int(line.substr(start+1+2,douPos-start-2-1).c_str(),douPos-start-2-1);
- // fenPos = line.find(';',douPos);
- // TestActionEnum actCode;
- // if (fenPos != string::npos)
- // {
- // actCode = (TestActionEnum)atoi(line.substr(douPos+1,fenPos-douPos-1).c_str());
- // }
- // else
- // {
- // int xx = line.length()-douPos-1;
- // actCode = (TestActionEnum)atoi(line.substr(douPos+1,line.length()-douPos-1).c_str());
- // }
- // switch(eSection)
- // {
- // case CHECK_HANDSHAKE:
- // m_entCfgInfo[keyName.c_str()].hsInfo[evtCode] = actCode;
- // break;
- // case CHECK_EXAMINE:
- // break;
- // case CHECK_RESET:
- // m_entCfgInfo[keyName.c_str()].resetInfo[evtCode] = actCode;
- // break;
- // case CHECK_RESTART:
- // break;
- // }
- // douPos = line.find(',',fenPos);
- // start = fenPos;
- // }
- // }
- // }
- // curr = is.tellg();
- //}while(curr < end);
- auto list = m_csKeyEntity.Split(',');
- for (int i = 0; i < list.GetCount(); ++i)
- {
- CSimpleStringA entity = list[i];
- Dbg("%s",LPCTSTR(entity));
- m_vKeyEntity.push_back(entity);
- }
- CSystemStaticInfo sysInfo;
- err = GetEntityBase()->GetFunction()->GetSystemStaticInfo(sysInfo);
- if (err != Error_Succeed)
- {
- Dbg("Get System Static info failed(%d).",err);
- return Error_Unexpect;
- }
- m_csMachineType = sysInfo.strMachineType;
- m_csSite = sysInfo.strSite;
- CSmartPointer<IConfigInfo> spConfigRun;
- err = GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
- if (err == Error_Succeed) {
- spConfigRun->ReadConfigValueInt("WarnRecord", "disk", m_diskLastWarnHour);
- }
- return Error_Succeed;
- }
- ErrorCodeEnum CSelfCheckerFSM::ExceptionErrorProcess(const char *pszEntityName, ErrorCodeEnum eCode)
- {
- CSmartPointer<IEntityFunction> pFunc = GetEntityBase()->GetFunction();
- CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
- CSmartPointer<IAsynWaitSp> spWait;
- Dbg("proc:%s,%d", pszEntityName, eCode);
- //do nothing
- if (eCode == Error_Cancel)
- return Error_Succeed;
- bool bUpgrade = false;
- //need to do something
- ErrorCodeEnum eErrCode = Error_Succeed;
- if ((m_entCfgInfo.find(pszEntityName) != m_entCfgInfo.end())
- && ((m_entCfgInfo[pszEntityName]).hsInfo.find(eCode) != (m_entCfgInfo[pszEntityName]).hsInfo.end()))
- {
- TestActionEnum eAction = (m_entCfgInfo[pszEntityName]).hsInfo.find(eCode)->second;
- Dbg("action:%d", eAction);
- switch (eAction)
- {
- case ACTION_EXAMINE:
- break;
- case ACTION_RESET:
- break;
- case ACTION_CLOSE:
- eErrCode = pFuncPrivilege->CloseEntity(pszEntityName, spWait);
- if (eErrCode == Error_Succeed)
- {
- }
- break;
- case ACTION_ENTITY_RESTART:
- {
- if (m_entCfgInfo[pszEntityName].bWaitRestart)
- {
- Dbg("waiting restart...");
- break;
- }
- if (m_entRunInfo[pszEntityName].loadOpt == 99)
- {
- Dbg("not configure? name:[%s]", pszEntityName);
- break;
- }
- //LogErrInfo("restart ",pszEntityName,eCode);
- m_entCfgInfo[pszEntityName].entityRestartCount++;
- Dbg("normalcount:%d,count:%d", m_restartNormal, m_entCfgInfo[pszEntityName].entityRestartCount);
- if (m_entCfgInfo[pszEntityName].entityRestartCount > m_restartNormal)
- {
- if ((!strnicmp(m_csMachineType, "RVC.Pad", strlen("RVC.Pad")) && !strnicmp(m_csSite, "cmb.FLB", strlen("cmb.FLB")))
- || (!strnicmp(m_csMachineType, "RPM.Stand1S", strlen("RPM.Stand1S"))))
- {
- if (m_entRunInfo[pszEntityName].loadOpt == 0)
- m_entCfgInfo[pszEntityName].entityRestartCount = 0;
- }
- else
- {
- LogErrInfo(pszEntityName, " restart too many,upgrade process.", eCode);
- bUpgrade = true;
- m_entCfgInfo[pszEntityName].bWaitRestart = true;
- }
- eErrCode = Error_Succeed;
- break;
- }
- eErrCode = pFuncPrivilege->StopEntity(pszEntityName, spWait);
- if (eErrCode == Error_Succeed)
- {
- eErrCode = spWait->WaitAnswer(MAX_AYSNC_TIMEOUT);
- if (eErrCode != Error_Succeed)
- {
- Dbg("spwait stop %s failed(%d).", pszEntityName, eErrCode);
- eErrCode = pFuncPrivilege->TerminateEntity(pszEntityName, spWait);
- eErrCode = spWait->WaitAnswer(MAX_AYSNC_TIMEOUT);
- if (eErrCode != Error_Succeed)
- {
- Dbg("spwait terminate %s failed(%d).", pszEntityName, eErrCode);
- break;
- }
- }
- }
- else
- {
- Dbg("Stop %s failed(%d).", pszEntityName, eErrCode);
- break;
- }
- Sleep(2000);
- CSimpleStringA csIEUrl;
- if (_strnicmp("IEBrowser", pszEntityName, strlen("IEBrowser")) == 0)
- {
- GetEntityBase()->GetFunction()->GetSysVar("IEUrl", csIEUrl);
- Dbg("Url:[%s]", (const char*)csIEUrl);
- eErrCode = pFuncPrivilege->StartEntity(pszEntityName, csIEUrl, spWait);
- }
- else
- eErrCode = pFuncPrivilege->StartEntity(pszEntityName, NULL, spWait);
- if (eErrCode == Error_Succeed)
- {
- eErrCode = spWait->WaitAnswer(MAX_AYSNC_TIMEOUT);
- if (eErrCode != Error_Succeed)
- {
- Dbg("spwait start %s failed(%d).", pszEntityName, eErrCode);
- break;
- }
- Dbg("Start entity %s suc.", pszEntityName);
- }
- else
- {
- Dbg("(re)Start %s failed(%d).", pszEntityName, eErrCode);
- break;
- }
- }
- break;
- case ACTION_OS_RESTART:
- Dbg("test os restart");
- if (m_entRunInfo[pszEntityName].loadOpt == 1 || m_entRunInfo[pszEntityName].loadOpt == 2)
- LogEvent(Severity_Middle, LOG_EVT_SELFCHECK_OS_RESTART, pszEntityName);
- break;
- case ACTION_POWER_RESTART:
- Dbg("test power restart");
- if (m_entRunInfo[pszEntityName].loadOpt == 1 || m_entRunInfo[pszEntityName].loadOpt == 2)
- LogEvent(Severity_Middle, LOG_EVT_SELFCHECK_POWER_RESTART, pszEntityName);
- break;
- default:
- break;
- }
- if (bUpgrade)
- {
- LogActionProcess(pszEntityName, eCode, eAction);
- }
- }
- else
- {
- Dbg("%s not configured,use default setting(%d)", LPCTSTR(pszEntityName), eCode);
- switch (eCode)
- {
- case Error_TimeOut:
- case Error_Unexpect:
- case Error_InvalidState:
- {
- eErrCode = pFuncPrivilege->StopEntity(pszEntityName, spWait);
- if (eErrCode == Error_Succeed)
- {
- eErrCode = spWait->WaitAnswer(MAX_AYSNC_TIMEOUT);
- if (eErrCode != Error_Succeed)
- {
- Dbg("spwait stop %s failed(%d).", pszEntityName, eErrCode);
- eErrCode = pFuncPrivilege->TerminateEntity(pszEntityName, spWait);
- eErrCode = spWait->WaitAnswer(MAX_AYSNC_TIMEOUT);
- if (eErrCode != Error_Succeed)
- {
- Dbg("spwait terminate %s failed(%d).", pszEntityName, eErrCode);
- break;
- }
- }
- }
- else
- {
- Dbg("Stop %s failed(%d).", pszEntityName, eErrCode);
- break;
- }
- Sleep(5000);
- CSimpleStringA csIEUrl;
- if (_strnicmp("IEBrowser", pszEntityName, strlen("IEBrowser")) == 0)
- {
- GetEntityBase()->GetFunction()->GetSysVar("IEUrl", csIEUrl);
- Dbg("Url:[%s]", (const char*)csIEUrl);
- eErrCode = pFuncPrivilege->StartEntity(pszEntityName, csIEUrl, spWait);
- }
- else
- eErrCode = pFuncPrivilege->StartEntity(pszEntityName, NULL, spWait);
- if (eErrCode == Error_Succeed)
- {
- eErrCode = spWait->WaitAnswer(MAX_AYSNC_TIMEOUT);
- if (eErrCode != Error_Succeed)
- {
- Dbg("spwait start %s failed(%d).", pszEntityName, eErrCode);
- break;
- }
- }
- else
- {
- Dbg("(re)Start %s failed(%d).", pszEntityName, eErrCode);
- break;
- }
- }
- break;
- default:
- break;
- }
- }
- return eErrCode;
- }
- ErrorCodeEnum CSelfCheckerFSM::CheckEntity(const char *pszEntityName,EntityTestEnum eTestType)
- {
- //oilyang@20170926 no need to check by self.Let the HealthManager entity to do it.
- if (pszEntityName != NULL && strnicmp(pszEntityName, GetEntityBase()->GetEntityName(), strlen(GetEntityBase()->GetEntityName())) == 0)
- return Error_Succeed;
- CSmartPointer<IEntityFunction> pFunc = GetEntityBase()->GetFunction();
- CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
- CSmartPointer<IAsynWaitSp> spWait;
- ErrorCodeEnum errCode;
- errCode = pFuncPrivilege->TestEntity(pszEntityName,eTestType,spWait);
-
- if (errCode == Error_Succeed)
- {
- callback_entry *entry = new callback_entry();
- entry->pRawData = NULL;
- entry->EntityName = pszEntityName;
- entry->ErrorResult = Error_Unexpect;
- entry->op = Test_ShakeHand;
- spWait->SetCallback(this, entry);
- }
- else
- Dbg("Test %s,%d",pszEntityName,errCode);
-
- //errCode = ExceptionErrorProcess(pszEntityName,errCode);
- return errCode;
- }
- void CSelfCheckerFSM::OnNormalWorkTimerout(void *pData)
- {
- CSmartPointer<IEntityFunction> pFunc = GetEntityBase()->GetFunction();
- CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
- CSmartPointer<IAsynWaitSp> spWait;
- ErrorCodeEnum errCode;
- //CSelfCheckerEntity* pEntity = ((CSelfCheckerEntity*)m_pEntity);
- // pEntity->GetActiveCount();
- int activeEnCount = m_activeEntity.size();
- vector<CSimpleStringA>::iterator it;
- //oiltmp 20131219
- //GetSystemCPUStatus();
- //GetSystemMemoryStatus();
- //GetSystemDiskStatus();
- for (it = m_activeEntity.begin();it != m_activeEntity.end(); ++it)
- {
- errCode = CheckEntity(*it,Test_ShakeHand);
- CEntityRunInfo runInfo;
- pFunc->GetEntityRunInfo(*it,runInfo);
- CheckEntityResouce(*it,runInfo);
- }
- GetEntityBase()->GetFunction()->ResetTimer(TIMER_ID_CHECK,MAX_CHECK_TIME);
- if (m_simulateTest)//oiltest
- {
- Dbg("simulate logevent framework restart");
- LogEvent(Severity_Middle,Event_Req_Framework_Restart,"oiltest");
- m_simulateTest = false;
- }
- }
- void CSelfCheckerFSM::OnCalcCpuUsageTimerout(void *pData)
- {
- CSmartPointer<IEntityFunction> pFunc = GetEntityBase()->GetFunction();
- CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
- CSmartPointer<IAsynWaitSp> spWait;
- ErrorCodeEnum errCode;
- int activeEnCount = m_activeEntity.size();
- vector<CSimpleStringA>::iterator it;
- for (it = m_activeEntity.begin(); it != m_activeEntity.end(); ++it)
- {
- CEntityRunInfo runInfo;
- pFunc->GetEntityRunInfo(*it, runInfo);
- CalcEntityCpuUsage(*it, runInfo, m_bFirstCalcCpu);
- CheckEntityResouce(*it, runInfo);
- }
- GetSystemCPUStatus();
- GetSystemMemoryStatus();
- GetSystemDiskStatus();
- m_bFirstCalcCpu = !m_bFirstCalcCpu;
- GetEntityBase()->GetFunction()->ResetTimer(TIMER_CPU_CHECK, MAX_CPU_CHECK_TIME*2);
- }
- ErrorCodeEnum CSelfCheckerFSM::GetAllLiveEntity(CAutoArray<CSimpleString> &allEntitys)
- {
- allEntitys.Clear();
- vector<CSimpleStringA>::iterator it;
- int start = 0;
- int size = m_allEntity.size();
- allEntitys.Init(size);
- CSimpleStringA testStr = "";
- for (it = m_allEntity.begin(); it != m_allEntity.end(); ++it,++start)
- {
- CSimpleStringA tmpName = *it;
- allEntitys[start] = tmpName;
- testStr += tmpName;
- testStr += ";";
- }
- Dbg("allentity[%d][%s]",allEntitys.GetCount(),(const char*)testStr);
- return Error_Succeed;
- }
- //ErrorCodeEnum CSelfCheckerFSM::AddEntity(const char *pszEntityName)
- //{
- // if (pszEntityName == NULL)
- // return Error_Null;
- // m_activeEntity.push_back(pszEntityName);
- // m_allEntity.push_back(pszEntityName);
- //
- // return Error_Succeed;
- //}
- //ErrorCodeEnum CSelfCheckerFSM::RemoveEntity(const char *pszEntityName)
- //{
- // if (pszEntityName == NULL)
- // return Error_Null;
- // vector<CSimpleStringA>::iterator it;
- // for (it = m_activeEntity.begin(); it != m_activeEntity.end(); ++it)
- // {
- // if(!strncmp(pszEntityName,*it,it->GetLength()))
- // {
- // m_activeEntity.erase(it);
- // break;
- // }
- // }
- // return Error_Succeed;
- //}
- void CSelfCheckerFSM::DoOnCreated(const char *pszEntityName,ErrorCodeEnum eOnStartErrorCode,const char *pszCallerEntity)
- {
- if (eOnStartErrorCode == Error_Succeed)
- {
- vector<CSimpleStringA>::iterator it,itAct;
- bool bFound = false, bActFound = false;
- Dbg("oncreated %s",pszEntityName);
-
- for (it = m_allEntity.begin(); it != m_allEntity.end(); ++it)
- {
- if(!strncmp(pszEntityName,*it,it->GetLength()))
- {
- Dbg("already exist %s",pszEntityName);
- bFound = true;
- break;
- }
- }
- if(!bFound)
- m_allEntity.push_back(pszEntityName);
- for (itAct = m_activeEntity.begin(); itAct != m_activeEntity.end(); ++itAct)
- {
- if(!strncmp(pszEntityName,*itAct,itAct->GetLength()))
- {
- Dbg("active entity already exist %s",pszEntityName);
- bActFound = true;
- break;
- }
- }
- if (!bActFound)
- {
- m_activeEntity.push_back(pszEntityName);
- }
- m_entCfgInfo[pszEntityName].entityRestartCount = 0;
- }
- }
- void CSelfCheckerFSM::DoOnClosed(const char *pszEntityName,EntityCloseCauseEnum eCloseCause,ErrorCodeEnum eOnCloseErrorCode,const char *pszCallerEntity)
- {
- //not close by selfchecker
- if (strncmp(pszCallerEntity,GetEntityBase()->GetEntityName(),strlen(pszCallerEntity)))
- {
- if (eCloseCause == CloseCause_Self || eCloseCause == CloseCause_Other)
- {
- vector<CSimpleStringA>::iterator it;
- for (it = m_activeEntity.begin(); it != m_activeEntity.end(); ++it)
- {
- if(!strncmp(pszEntityName,*it,it->GetLength()))
- {
- Dbg("onclosed %s",pszEntityName);
- m_activeEntity.erase(it);
- break;
- }
- }
- }
- }
- CAutoArray<CSimpleString> testAutoArray;
- GetAllLiveEntity(testAutoArray);
- }
- void CSelfCheckerFSM::DoOnException(const char *pszEntityName,const char *pszFunctionName,EntityStateEnum eState,EntityStateEnum eLastState,ErrorCodeEnum eErrorCode)
- {
- Dbg("OnException:%s,%s,%d,%d,%d",pszEntityName,pszFunctionName,eState,eLastState,eErrorCode);
- }
- void CSelfCheckerFSM::OnAnswer(CSmartPointer<IAsynWaitSp> pAsynWaitSp)
- {
- CSmartPointer<ICallbackListener> spCallback;
- CSmartPointer<IReleasable> pData;
- pAsynWaitSp->GetCallback(spCallback, pData);
- //LOG_ASSERT(pData);
- callback_entry *entry = dynamic_cast<callback_entry*>((IReleasable*)pData.GetRawPointer());
- entry->ErrorResult = pAsynWaitSp->AsyncGetAnswer();
- callback_entry *new_entry = new callback_entry();
- new_entry->EntityName = entry->EntityName;
- new_entry->ErrorResult = entry->ErrorResult;
- new_entry->op = entry->op;
- new_entry->state = entry->state;
- m_entRunInfo[new_entry->EntityName].eTest = new_entry->ErrorResult; //add test result oilyang 20150616
- //m_entRunInfo[new_entry->EntityName].eState = 9; //add test result oilyang 20150616
- //Dbg("oiltest [%s]%d,%d,%d.",(LPCTSTR)new_entry->EntityName,m_entRunInfo[new_entry->EntityName].eState,m_entRunInfo[new_entry->EntityName].eTest,new_entry->ErrorResult);
-
-
- if (new_entry->op == Test_ShakeHand && new_entry->ErrorResult != Error_Succeed)
- Dbg("oiltmp shakehand %s,(r)%d,(state)%d",(LPCTSTR)new_entry->EntityName,new_entry->ErrorResult,new_entry->state);
- if (new_entry->ErrorResult != Error_Succeed)
- {
- ErrorCodeEnum eErr;
- CSmartPointer<IEntityFunction> pFunc = GetEntityBase()->GetFunction();
- CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
- CEntityRunInfo ieInfo;
- eErr = pFunc->GetEntityRunInfo("IEBrowser",ieInfo);
- bool bIEStarted = false;
- if (eErr == Error_Succeed && ieInfo.eState != EntityState_NoStart)
- bIEStarted = true;
- if (_strnicmp("MediaController", (const char*)new_entry->EntityName, strlen("MediaController")) == 0
- && !bIEStarted)
- {
- Dbg("On loading stage,don't process MediaController exception.");
- }
- else
- //ExceptionErrorProcess(new_entry->EntityName,new_entry->ErrorResult);
- Proc((const char*)new_entry->EntityName,ProcType_Shake,new_entry->ErrorResult);
- }
- else
- {
- CSmartPointer<IEntityFunction> spEntityFunction = GetEntityBase()->GetFunction();
- CSmartPointer<IConfigInfo> spConfig;
- ErrorCodeEnum eErr = spEntityFunction->OpenConfig(Config_Run, spConfig);
- if (eErr != Error_Succeed) {
- Dbg("open run cfg file failed!");
- return;
- }
- spConfig->WriteConfigValueInt(new_entry->EntityName,"OsRestart",0);
- spConfig->WriteConfigValueInt(new_entry->EntityName,"PowerRestart",0);
- }
- }
- void CSelfCheckerFSM::LogErrInfo(const char* msgHead,const char* msgBody,const int errCode)
- {
- //oiltest to redifine this
- Dbg("%s,%s,%d",msgHead,msgBody,errCode);
- }
- void CSelfCheckerFSM::LogActionProcess(const char *pszEntityName,ErrorCodeEnum errCode,TestActionEnum eAct)
- {
- Dbg("LogActionProcess:entity[%s],errCode[%d],eAction[%d]",pszEntityName,errCode,eAct);
- CSmartPointer<IEntityFunction> spEntityFunction = GetEntityBase()->GetFunction();
- CSmartPointer<IConfigInfo> spConfig;
- ErrorCodeEnum eErr = spEntityFunction->OpenConfig(Config_Run, spConfig);
- if (eErr != Error_Succeed) {
- Dbg("open run cfg file failed!");
- return;
- }
- int osTimes,powerTimes;
- osTimes = powerTimes = 0;
- spConfig->ReadConfigValueInt(pszEntityName,"OsRestart",osTimes);
- spConfig->ReadConfigValueInt(pszEntityName,"PowerRestart",powerTimes);
- switch(eAct)
- {
- case ACTION_ENTITY_RESTART:
- if (osTimes > m_maxOsRestart || powerTimes > m_maxPowerRestart)
- {
- Dbg("restart too much,give up[%d][%d].",osTimes,powerTimes);
- break;
- }
- //if not the KEY entity,don't upgrade to restart computer
- //if (!IsKeyEntity(pszEntityName))
- break;
- osTimes++;
- powerTimes++;
- spConfig->WriteConfigValueInt(pszEntityName,"OsRestart",osTimes);
- spConfig->WriteConfigValueInt(pszEntityName,"PowerRestart",powerTimes);
- //for simple and effective,use power restart only
- LogEvent(Severity_Middle,LOG_EVT_SELFCHECK_POWER_RESTART,pszEntityName);
- m_entCfgInfo[pszEntityName].bWaitRestart = true;
- break;
- default:
- break;
- }
- }
- #ifdef RVC_OS_WIN
- ULONGLONG subtractTime(const FILETIME &a, const FILETIME &b)
- {
- LARGE_INTEGER la, lb;
- la.LowPart = a.dwLowDateTime;
- la.HighPart = a.dwHighDateTime;
- lb.LowPart = b.dwLowDateTime;
- lb.HighPart = b.dwHighDateTime;
- return la.QuadPart - lb.QuadPart;
- }
- float getUsage(HANDLE hProcess,FILETIME *prevSysKernel, FILETIME *prevSysUser,
- FILETIME *prevProcKernel, FILETIME *prevProcUser,
- bool firstRun = false)
- {
- FILETIME sysIdle, sysKernel, sysUser;
- FILETIME procCreation, procExit, procKernel, procUser;
- if (!GetSystemTimes(&sysIdle, &sysKernel, &sysUser) ||
- !GetProcessTimes(hProcess, &procCreation, &procExit, &procKernel, &procUser))
- {
- // can't get time info so return
- return -1.;
- }
- // check for first call
- if (firstRun)
- {
- // save time info before return
- prevSysKernel->dwLowDateTime = sysKernel.dwLowDateTime;
- prevSysKernel->dwHighDateTime = sysKernel.dwHighDateTime;
- prevSysUser->dwLowDateTime = sysUser.dwLowDateTime;
- prevSysUser->dwHighDateTime = sysUser.dwHighDateTime;
- prevProcKernel->dwLowDateTime = procKernel.dwLowDateTime;
- prevProcKernel->dwHighDateTime = procKernel.dwHighDateTime;
- prevProcUser->dwLowDateTime = procUser.dwLowDateTime;
- prevProcUser->dwHighDateTime = procUser.dwHighDateTime;
- return -1.;
- }
- ULONGLONG sysKernelDiff = subtractTime(sysKernel, *prevSysKernel);
- ULONGLONG sysUserDiff = subtractTime(sysUser, *prevSysUser);
- ULONGLONG procKernelDiff = subtractTime(procKernel, *prevProcKernel);
- ULONGLONG procUserDiff = subtractTime(procUser, *prevProcUser);
- ULONGLONG sysTotal = sysKernelDiff + sysUserDiff;
- ULONGLONG procTotal = procKernelDiff + procUserDiff;
- return (float)((100.0 * procTotal) / sysTotal);
- }
- #else
- //oiltestlinux
- #endif //RVC_OS_WIN
- void CSelfCheckerFSM::CheckEntityResouce(const char *pszEntityName,CEntityRunInfo &info)
- {
- if (info.eState != EntityState_Idle)
- return;
- #ifdef RVC_OS_WIN
- HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_QUERY_LIMITED_INFORMATION,FALSE,info.dwProcessID);
- if (hProcess == NULL)
- {
- Dbg("OpenProcess %s failed %d.",pszEntityName,GetLastError());
- return;
- }
- //GetSystemInfo
- PIO_COUNTERS pIOCounters = new IO_COUNTERS;
- BOOL ret = GetProcessIoCounters(hProcess,pIOCounters);
- if (ret == 0)
- {
- Dbg("GetProcessIoCounters %s failed %d.",pszEntityName,GetLastError());
- }
- PROCESS_MEMORY_COUNTERS pmc;
- const int showSize = 20;
- if (GetProcessMemoryInfo(hProcess,&pmc,sizeof(pmc)))
- {
- if ((pmc.WorkingSetSize/MILLION > showSize) && (pmc.PeakWorkingSetSize/MILLION > showSize)
- && (pmc.PagefileUsage/MILLION > showSize) && (pmc.PeakPagefileUsage/MILLION > showSize))
- {
- m_entRunInfo[pszEntityName].memoryHighCount++;
- if (m_entRunInfo[pszEntityName].memoryHighCount > ((60000 / MAX_CPU_CHECK_TIME)) * 2)//more than 2 minutes
- {
- m_entRunInfo[pszEntityName].memoryHighCount = 0;
- Dbg("%s,WorkingSetSize %u, Peak %u,PageFileUsage %u, Peak %u", (LPCTSTR)pszEntityName, pmc.WorkingSetSize / MILLION, pmc.PeakWorkingSetSize / MILLION, pmc.PagefileUsage / MILLION, pmc.PeakPagefileUsage / MILLION);
- }
- }
- }
- //Dbg can't support the ULONGLONG...oilyang 20140730
- //Dbg("Entity %s:",pszEntityName);
- //Dbg("read op %u,write op %u",pIOCounters->ReadOperationCount,pIOCounters->WriteOperationCount);
- //Dbg("read transfer %u,write transfer %u",pIOCounters->ReadTransferCount,pIOCounters->WriteTransferCount);
- //Dbg("other op %u,other transfer %u",pIOCounters->OtherOperationCount,pIOCounters->OtherTransferCount);
- CloseHandle(hProcess);
- #else
- return;//oiltestlinux
- #endif //RVC_OS_WIN
- }
- void CSelfCheckerFSM::CalcEntityCpuUsage(const char *pszEntityName, CEntityRunInfo &info,bool bFirst)
- {
- if (info.eState != EntityState_Idle)
- return;
- #ifdef RVC_OS_WIN
- HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_QUERY_LIMITED_INFORMATION, FALSE, info.dwProcessID);
- if (hProcess == NULL)
- {
- Dbg("OpenProcess %s failed %d.", pszEntityName, GetLastError());
- return;
- }
- float ret = getUsage(hProcess, &(m_entRunInfo[pszEntityName].prevSysKernel), &(m_entRunInfo[pszEntityName].prevSysUser)
- , &(m_entRunInfo[pszEntityName].prevProcKernel), &(m_entRunInfo[pszEntityName].prevProcUser),bFirst);
- if (!bFirst && ret > 2 && ret < 100)//where a entity have been restarted,the bFirst flag maybe wrong,the 'ret' can be big than 100!
- {
- Dbg("entity %s,cpu ratio:%f", pszEntityName, ret);
- m_entRunInfo[pszEntityName].cpuRatio = ret;
- }
- CloseHandle(hProcess);
- #else
- return;//oiltestlinux
- #endif //RVC_OS_WIN
- }
- void CSelfCheckerFSM::GetSystemMemoryStatus()
- {
- #ifdef RVC_OS_WIN
- // Use to convert bytes to KB
- #define DIV 1024
- // Specify the width of the field in which to print the numbers.
- // The asterisk in the format specifier "%*I64d" takes an integer
- // argument and uses it to pad and right justify the number.
- #define WIDTH 7
- MEMORYSTATUSEX statex;
- statex.dwLength = sizeof (statex);
- GlobalMemoryStatusEx (&statex);
- if (statex.dwMemoryLoad > m_memHighPercent)
- {
- Dbg("memory used: %*ld .", WIDTH, statex.dwMemoryLoad);
- LogWarn(Severity_Low, Error_Resource, LOG_EVT_SELFCHECK_MEMORY_TOO_HIGH, "Free memory is few.");
- }
-
- //Dbg ("(physical)%*I64d,(phy free)%*I64d,(paging)%*I64d,(pg free)%*I64d,(virtaul)%*I64d,(vt free)%*I64d",
- // WIDTH, statex.ullTotalPhys/DIV,WIDTH, statex.ullAvailPhys/DIV,WIDTH, statex.ullTotalPageFile/DIV
- // ,WIDTH, statex.ullAvailPageFile/DIV,WIDTH, statex.ullTotalVirtual/DIV,WIDTH, statex.ullAvailVirtual/DIV);
- // Show the amount of extended memory available.
- //Dbg ("There are %*I64d free Kbytes of extended memory.\n",WIDTH, statex.ullAvailExtendedVirtual/DIV);
- #else
- //oiltestlinux
- #endif
- }
- void CSelfCheckerFSM::GetSystemCPUStatus()
- {
- #ifdef RVC_OS_WIN
- #define _WIN32_WINNT 0x0601
- FILETIME idleTime,kernelTime,userTime;
- BOOL ret = GetSystemTimes(&idleTime,&kernelTime,&userTime);
- if (ret == 0)
- {
- Dbg("GetSystemCPUStatus.GetSystemTimes failed(%d).",GetLastError());
- return;
- }
- __int64 xIdle,xKernel,xUser;
- xIdle = idleTime.dwHighDateTime;
- xIdle <<= 32;
- xIdle |= idleTime.dwLowDateTime;
- xKernel = kernelTime.dwHighDateTime;
- xKernel <<= 32;
- xKernel |= kernelTime.dwLowDateTime;
- xUser = userTime.dwHighDateTime;
- xUser <<= 32;
- xUser |= userTime.dwLowDateTime;
- //Dbg("%u,%u,%u,%u,%u,%u",idleTime.dwHighDateTime,idleTime.dwLowDateTime,kernelTime.dwHighDateTime,kernelTime.dwLowDateTime,userTime.dwHighDateTime,userTime.dwLowDateTime);
- if (m_xIdlePre != 0)
- {
- __int64 xI,xK,xU;
- xI = xIdle - m_xIdlePre;
- xK = xKernel - m_xKernelPre;
- xU = xUser - m_xUserPre;
- int ratio = 0;
- if ((xK +xU) != 0)
- ratio = (xK - xI + xU) * 100 / (xK + xU);
- if (ratio > 50)//oiltmp@20170919 need to read from configure file?maybe or not
- Dbg("cpu %d",ratio);
- if (ratio > m_cpuHighPercent)
- {
- CSimpleStringA cpuHighEntitys("");
- char cpuRatioBuf[16];
- ZeroMemory(cpuRatioBuf, sizeof(cpuRatioBuf));
- map<CSimpleStringA, EntityRunInfo>::iterator it;
- for (it = m_entRunInfo.begin(); it != m_entRunInfo.end(); ++it)
- {
- if (it->second.cpuRatio > 0 && it->second.cpuRatio < 100)
- {
- itoa(it->second.cpuRatio, cpuRatioBuf, 10);
- cpuHighEntitys.Append(it->first + ":" + cpuRatioBuf + "||");
- }
- }
- Dbg("cpu ratio:%s",(const char*)cpuHighEntitys);
- LogWarn(Severity_Low, Error_Resource, LOG_EVT_SELFCHECK_CPU_TOO_HIGH, (const char*)cpuHighEntitys);
- }
- }
- m_xIdlePre = xIdle;
- m_xKernelPre = xKernel;
- m_xUserPre = xUser;
- int warnLevel;
- CSimpleStringA strList("");
- #else
- //oiltestlinux
- #endif //RVC_OS_WIN
- }
- void CSelfCheckerFSM::GetSystemDiskStatus()
- {
- #ifdef RVC_OS_WIN
- ULARGE_INTEGER ulAvailFree,ulTotalBytes,ulTotalFree;
- BOOL ret = GetDiskFreeSpaceEx(NULL,&ulAvailFree,&ulTotalBytes,&ulTotalFree);
- if (ret == 0)
- {
- Dbg("GetSystemDiskStatus.GetDiskFreeSpaceEx failed(%d).",GetLastError());
- return;
- }
- //Dbg("%d,%d,%d",(ulAvailFree.QuadPart/MILLION)*100,ulTotalBytes.QuadPart,(ulTotalBytes.QuadPart/MILLION));
- DWORD dwAvFree = ulAvailFree.QuadPart/MILLION;
- DWORD dwTotal = ulTotalBytes.QuadPart/MILLION;
- DWORD dwTotalFree = ulTotalFree.QuadPart/MILLION;
- int ratio = dwTotalFree*100/dwTotal;
- if ((100 - ratio) > m_diskHighPercent)
- {
- Dbg("The disk has %d MB(%d%) available.\n", dwTotalFree, ratio);
- //oilyang@20200526 根据wq建议,降低磁盘空间偏少的告警频率
- SYSTEMTIME localTime;
- GetLocalTime(&localTime);
- if (m_diskLastWarnHour != localTime.wHour)
- {
- m_diskLastWarnHour = localTime.wHour;
- CSmartPointer<IConfigInfo> spConfigRun;
- ErrorCodeEnum eErr = GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
- if (eErr == Error_Succeed) {
- spConfigRun->WriteConfigValueInt("WarnRecord", "disk", m_diskLastWarnHour);
- }
- LogWarn(Severity_Low, Error_Resource, LOG_EVT_SELFCHECK_HARDDISK_TOO_HIGH, "Harddisk free space is few.");
- }
- }
- #else
- //oiltestlinux
- #endif //RVC_OS_WIN
- }
- bool CSelfCheckerFSM::IsKeyEntity(const char *pszEntityName)
- {
- vector<CSimpleStringA>::iterator it;
- for (it = m_vKeyEntity.begin(); it != m_vKeyEntity.end(); ++it)
- {
- if (!_strnicmp(pszEntityName,*it,strlen(pszEntityName)))
- return true;
- }
- return false;
- }
- int CSelfCheckerFSM::AddEntityState(const char *pszEntityName,EntityStateEnum eState)
- {
- if (eState == EntityState_Starting)
- {
- m_entRunInfo[pszEntityName].bGetLoadOpt = false;
- m_entRunInfo[pszEntityName].bRestarting = false;
- m_entRunInfo[pszEntityName].loadOpt = 99;
- m_entRunInfo[pszEntityName].eState = eState;
- m_entRunInfo[pszEntityName].eTest = Error_Succeed;
- }
- else
- m_entRunInfo[pszEntityName].eState = eState;
- Dbg("ADDDDD %s,%d,%d,%d",pszEntityName,m_entRunInfo[pszEntityName].eState,m_entRunInfo[pszEntityName].eTest,eState);
- if (eState == EntityState_Idle)
- {
- m_entRunInfo[pszEntityName].eTest = Error_Succeed;
- m_entRunInfo[pszEntityName].memoryHighCount = 0;
- }
- m_entRunInfo[pszEntityName].cpuRatio = 0;
- return 0;
- }
- ErrorCodeEnum CSelfCheckerFSM::GetEntityErrorList(int &warmLevel,CSimpleStringA &strList)
- {
- map<CSimpleStringA,EntityRunInfo>::iterator it;
- CSimpleStringA tmpStr("");
- bool bLost = false;
- for (it = m_entRunInfo.begin(); it != m_entRunInfo.end(); ++it)
- {
- if (m_entRunInfo[it->first].eState == EntityState_Lost)
- bLost = true;
- char buf[16],bufTest[16];
- ZeroMemory(buf,16);
- ZeroMemory(bufTest, 16);
- //Dbg("oiltest20160425[%s],%d,%d", (const char*)it->first, m_entRunInfo[it->first].eState, m_entRunInfo[it->first].eTest);
- if (m_entRunInfo[it->first].eState == EntityState_Lost || m_entRunInfo[it->first].eState == EntityState_Close
- || m_entRunInfo[it->first].eState == EntityState_Killed || m_entRunInfo[it->first].eTest != Error_Succeed)
- {
- tmpStr += it->first;
- tmpStr += "=";
- if (m_entRunInfo[it->first].eState == EntityState_Lost || m_entRunInfo[it->first].eState == EntityState_Close
- || m_entRunInfo[it->first].eState == EntityState_Killed)
- {
- _itoa(m_entRunInfo[it->first].eState, buf, 10);
- tmpStr += buf;
- }
- if (m_entRunInfo[it->first].eTest != Error_Succeed)
- {
- Dbg("[%s],%d,%d", (const char*)it->first, m_entRunInfo[it->first].eState, m_entRunInfo[it->first].eTest);
- _itoa(m_entRunInfo[it->first].eTest, bufTest, 10);
- tmpStr += ",(selfcheck code):";
- tmpStr += bufTest;
- }
- tmpStr += ";";
- }
- }
- if (tmpStr.GetLength() < 2)
- m_warmLevel = warmLevel = 0;
- if (bLost)
- m_warmLevel = warmLevel = 3;
- m_warmLevel = warmLevel = 1;//for temp set 20150617
- strList = tmpStr;
- if (strList.GetLength() > 2)
- Dbg("warnlevel:%d,ErrorList [%s]", m_warmLevel,(LPCTSTR)strList);
- return Error_Succeed;
- }
- int CSelfCheckerFSM::Proc(string entity, ProcType eType, DWORD dwCode, const char *pszMessage)
- {
- ConnectToHealthManager();
- if (!m_entRunInfo[entity.c_str()].bGetLoadOpt)
- {
- if (m_pHealthClient != NULL)
- {
- HealthManagerService_GetEntityCfgInfo_Req req;
- HealthManagerService_GetEntityCfgInfo_Ans ans;
- req.name = entity.c_str();
- ErrorCodeEnum errCode = m_pHealthClient->GetEntityCfgInfo(req, ans, 10000);
- if (errCode == Error_Succeed)
- {
- m_entRunInfo[entity.c_str()].bGetLoadOpt = true;
- Dbg("to get entity cfg info suc:%d", ans.loadOpt);
- SetEntityCfgInfo(entity.c_str(), ans.loadOpt);
- }
- }
- }
- map<string, EntityCfg, EntityNameCompare>::iterator it;
- if ((it = m_mapEntity.find(entity.c_str())) == m_mapEntity.end())
- {
- Dbg("can't find entity %s configure setting,using default setting.",entity.c_str());
- //Error_TimeOut:
- //Error_Unexpect:
- //Error_InvalidState:
- if (eType == ProcType_Shake && (dwCode == Error_TimeOut || dwCode == Error_Unexpect || dwCode == Error_InvalidState))
- ExceptionErrorProcessXml(eType,entity.c_str(), ACTION_ENTITY_RESTART,true);
- else if (eType == ProcType_Warn)
- ExceptionErrorProcessXml(eType, entity.c_str(), dwCode, true, pszMessage);
- else
- Dbg("What's this:type:%d,receive code:%x,msg:%s, from entity %s.", eType, dwCode, pszMessage, entity.c_str());
- return -1;
- }
- vector<ProcItem>::iterator vIt, vEnd;
- if (eType == ProcType_Shake)
- {
- vIt = it->second.vShake.begin();
- vEnd = it->second.vShake.end();
- }
- else if (eType == ProcType_Warn)
- {
- vIt = it->second.vWarn.begin();
- vEnd = it->second.vWarn.end();
- }
- for (; vIt != vEnd; vIt++)
- {
- if (vIt->code == dwCode)
- {
- if (vIt->upgradecount > 0)
- {
- Dbg("action size:%d,upgradecount:%d", vIt->proctime.size(), vIt->upgradecount);
- UINT64 happentime = GetTickCountRVC();
- if (vIt->proctime.size() < vIt->upgradecount - 1)//just add record,do nothing
- {
- vIt->proctime.push_back(happentime);
- Dbg("just add record");
- return 0;
- }
- else
- {
- //to find the 1st record happened in last upgradetime minute
- //and remove the record happened far ago(upgradetime minutes before)
- vector<UINT64>::iterator ittime, itFisrtInPeriod, xxxIt;
- bool bFar = false;
- int count = 0;
- for (ittime = vIt->proctime.begin(); ittime != vIt->proctime.end(); ittime++)
- {
- itFisrtInPeriod = ittime;
- UINT64 difftime = happentime - *ittime;
- if (difftime / (1000 * 60) < vIt->upgradetime)
- break;
- else
- {
- count++;
- Dbg("the %d th action",count);
- bFar = true;
- }
- }
- if (!bFar)
- {
- //to do upgrade action
- Dbg("to do upgrade action");
- ExceptionErrorProcessXml(eType,entity.c_str(), vIt->upgradeaction);
- vIt->proctime.clear();
- return 0;
- }
- else
- {
- if (count == 1 && vIt->proctime.size() == 1)
- vIt->proctime.clear();
- else
- vIt->proctime.erase(vIt->proctime.begin(), itFisrtInPeriod);
- for (xxxIt = vIt->proctime.begin(); xxxIt != vIt->proctime.end(); xxxIt++)
- {
- cout << *xxxIt << " # ";
- }
- cout << endl;
- vIt->proctime.push_back(happentime);
- for (xxxIt = vIt->proctime.begin(); xxxIt != vIt->proctime.end(); xxxIt++)
- {
- cout << *xxxIt << " * ";
- }
- Dbg("after clear,add record");
- return 0;
- }
- }
- }
- Dbg("Entity %s receive %d,to do action:%d", entity.c_str(), dwCode, vIt->action);
- ExceptionErrorProcessXml(eType,entity.c_str(), vIt->action,true, pszMessage);
- return 0;
- }
- }
- Dbg("can't find corresponding action of entity %s,type:%d,code:%x",entity.c_str(),eType,dwCode);
- Dbg("use default process...");
- if (eType == ProcType_Shake && (dwCode == Error_TimeOut || dwCode == Error_Unexpect || dwCode == Error_InvalidState))
- ExceptionErrorProcessXml(eType, entity.c_str(), ACTION_ENTITY_RESTART, true);
- else if (eType == ProcType_Warn)
- ExceptionErrorProcessXml(eType, entity.c_str(), dwCode, true, pszMessage);
- return -1;
- }
- bool CSelfCheckerFSM::ReadXmlFile(const char *szFileName)
- {//读取Xml文件,并遍历
- //MessageBox(0, 0, 0, 0);
- LOG_FUNCTION();
- tinyxml2::XMLDocument *doc = new tinyxml2::XMLDocument();
- XMLError err = doc->LoadFile(szFileName);
- if (err != XML_SUCCESS)
- {
- Dbg("open file %s failed.GetLastError:%d",szFileName,GetLastError());
- return false;
- }
- //doc->GetDocument();
- string out = "";
- XMLNode *pF = doc->FirstChild();
- XMLElement *pRoot = doc->RootElement();
- //pF->FirstChildElement();
- if (pRoot == NULL)
- {
- Dbg("Get root element failed.");
- return false;
- }
- else
- pF = pRoot->FirstChild();
- while (pF != NULL)
- {
- if (pF->ToElement() == NULL)
- {
- pF = pF->FirstChild();
- continue;
- }
- const char *pName = pF->ToElement()->Name();
- if (!strncmp(pName, "SelfCheckerConfig", strlen("SelfCheckerConfig")))
- {
- pF = pF->FirstChild();
- continue;
- }
- else if (!strncmp(pName, "Entity", strlen("Entity")))
- {
- EntityCfg entity;
- const char *attrName = pF->ToElement()->Attribute("name");
- out += const_cast<char*>(attrName);
- out += "\r\n";
- XMLNode *pChild = pF->FirstChild();
- while (pChild != NULL)
- {
- if (!strncmp(pChild->Value(), "shakehandproc", strlen("shakehandproc")))
- {
- XMLNode *pProc = pChild->FirstChild();
- while (pProc != NULL)
- {
- ProcItem item;
- const char *code, *action, *upgradeaction, *upgradetime, *upgradecount;
- code = action = upgradeaction = upgradetime = upgradecount = NULL;
- if (pProc->ToElement() != NULL)
- {
- code = pProc->ToElement()->Attribute("code");
- action = pProc->ToElement()->Attribute("action");
- upgradeaction = pProc->ToElement()->Attribute("upgradeaction");
- upgradetime = pProc->ToElement()->Attribute("upgradetime");
- upgradecount = pProc->ToElement()->Attribute("upgradecount");
- }
- if (code != NULL)
- {
-
- item.code = CodeStrToInt(code);
- Dbg("oiltest code:%s,%d", code, item.code);
- out += const_cast<char*>(code);
- out += " , ";
- }
- if (action != NULL)
- {
- item.action = ActionStrToInt(action);
- out += const_cast<char*>(action);
- out += " , ";
- }
- if (upgradeaction != NULL)
- {
- item.upgradeaction = ActionStrToInt(upgradeaction);
- out += const_cast<char*>(upgradeaction);
- }
- if (upgradetime != NULL)
- {
- item.upgradetime = atoi(upgradetime);
- }
- if (upgradecount != NULL)
- {
- item.upgradecount = atoi(upgradecount);
- }
- else
- item.upgradecount = 0;
- entity.vShake.push_back(item);
- out += "; ";
- pProc = pProc->NextSibling();
- }
- }
- else if (!strncmp(pChild->Value(), "eventproc", strlen("eventproc")))
- {
- XMLNode *pProc = pChild->FirstChild();
- while (pProc != NULL)
- {
- ProcItem item;
- const char *code, *action, *upgradeaction, *upgradetime, *upgradecount;
- code = action = upgradeaction = upgradetime = upgradecount = NULL;
- if (pProc->ToElement() != NULL)
- {
- code = pProc->ToElement()->Attribute("code");
- action = pProc->ToElement()->Attribute("action");
- upgradeaction = pProc->ToElement()->Attribute("upgradeaction");
- upgradetime = pProc->ToElement()->Attribute("upgradetime");
- upgradecount = pProc->ToElement()->Attribute("upgradecount");
- }
- if (code != NULL)
- {
- item.code = CodeStrToInt(code);
- out += const_cast<char*>(code);
- out += ",";
- }
- if (action != NULL)
- {
- item.action = ActionStrToInt(action);
- out += const_cast<char*>(action);
- out += ",";
- }
- if (upgradeaction != NULL)
- {
- item.upgradeaction = ActionStrToInt(upgradeaction);
- out += const_cast<char*>(upgradeaction);
- }
- if (upgradetime != NULL)
- {
- item.upgradetime = atoi(upgradetime);
- }
- if (upgradecount != NULL)
- {
- item.upgradecount = atoi(upgradecount);
- }
- else
- item.upgradecount = 0;
- entity.vWarn.push_back(item);
- out += "; ";
- pProc = pProc->NextSibling();
- }
- }
- pChild = pChild->NextSibling();
- }
- m_mapEntity[attrName] = entity;
- Dbg("add %s,vShake size:%d,vWarn size:%d", attrName, entity.vShake.size(), entity.vWarn.size());
- }
- out += "\r\n";
- pF = pF->NextSibling();
- }
- //cout << out.c_str() << endl;
- return true;
- }
- ErrorCodeEnum CSelfCheckerFSM::ExceptionErrorProcessXml(ProcType eType, const char *pszEntityName, DWORD dwAction, bool bDefault,const char *pszMessage)
- {
- CSmartPointer<IEntityFunction> pFunc = GetEntityBase()->GetFunction();
- CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
- CSmartPointer<IAsynWaitSp> spWait;
- ErrorCodeEnum eErrCode = Error_Unexpect;
- switch (dwAction)
- {
- case ACTION_EXAMINE:
- break;
- case ACTION_RESET:
- break;
- case ACTION_CLOSE:
- eErrCode = pFuncPrivilege->CloseEntity(pszEntityName, spWait);
- if (eErrCode == Error_Succeed)
- {
- }
- break;
- case ACTION_ENTITY_RESTART:
- {
- if (m_entCfgInfo[pszEntityName].bWaitRestart)
- {
- if (!bDefault)
- {
- Dbg("waiting restart...");
- break;
- }
- }
- if (m_entRunInfo[pszEntityName].loadOpt == 99)
- {
- Dbg("not configure? name:[%s]", pszEntityName);
- break;
- }
- //oilyang@20200407 if being restarted by selfchecker,break
- if (m_entRunInfo[pszEntityName].bRestarting)
- {
- Dbg("%s is being restarted by selfchecker.", pszEntityName);
- break;
- }
- //oilyang@20200403
- //for fwb test,PinPad&CardSwiper can't be stop immediately
- if (_strnicmp("PinPad", pszEntityName, strlen("PinPad")) == 0
- || _strnicmp("CardSwiper", pszEntityName, strlen("CardSwiper")) == 0
- || _strnicmp("Sensors", pszEntityName, strlen("Sensors")) == 0
- || _strnicmp("IDCertificate", pszEntityName, strlen("IDCertificate")) == 0
- || _strnicmp("FingerPrint", pszEntityName, strlen("FingerPrint")) == 0
- || _strnicmp("DeviceControl", pszEntityName, strlen("DeviceControl")) == 0)
- eErrCode = pFuncPrivilege->TerminateEntity(pszEntityName, spWait);
- else
- eErrCode = pFuncPrivilege->StopEntity(pszEntityName, spWait);
- if (eErrCode == Error_Succeed)
- {
- eErrCode = spWait->WaitAnswer(MAX_AYSNC_TIMEOUT);
- if (eErrCode != Error_Succeed)
- {
- Dbg("spwait stop %s failed(%d).", pszEntityName, eErrCode);
- eErrCode = pFuncPrivilege->TerminateEntity(pszEntityName, spWait);
- eErrCode = spWait->WaitAnswer(MAX_AYSNC_TIMEOUT);
- if (eErrCode != Error_Succeed)
- {
- Dbg("spwait terminate %s failed(%d).", pszEntityName, eErrCode);
- break;
- }
- }
- }
- else
- {
- Dbg("Stop %s failed(%d).", pszEntityName, eErrCode);
- break;
- }
- Sleep(2000);
- CSimpleStringA csIEUrl;
- if (_strnicmp("IEBrowser", pszEntityName, strlen("IEBrowser")) == 0)
- {
- GetEntityBase()->GetFunction()->GetSysVar("IEUrl", csIEUrl);
- Dbg("Url:[%s]", (const char*)csIEUrl);
- eErrCode = pFuncPrivilege->StartEntity(pszEntityName, csIEUrl, spWait);
- }
- else
- eErrCode = pFuncPrivilege->StartEntity(pszEntityName, NULL, spWait);
- if (eErrCode == Error_Succeed)
- {
- eErrCode = spWait->WaitAnswer(MAX_AYSNC_TIMEOUT);
- if (eErrCode != Error_Succeed)
- {
- Dbg("spwait start %s failed(%d).", pszEntityName, eErrCode);
- break;
- }
- Dbg("Start entity %s suc.", pszEntityName);
- }
- else
- {
- Dbg("(re)Start %s failed(%d).", pszEntityName, eErrCode);
- break;
- }
- }
- break;
- case ACTION_OS_RESTART:
- Dbg("test os restart");
- if (m_entRunInfo[pszEntityName].loadOpt == 1 || m_entRunInfo[pszEntityName].loadOpt == 2)
- LogEvent(Severity_Middle, LOG_EVT_SELFCHECK_OS_RESTART, pszEntityName);
- break;
- case ACTION_POWER_RESTART:
- Dbg("test power restart");
- if (m_entRunInfo[pszEntityName].loadOpt == 1 || m_entRunInfo[pszEntityName].loadOpt == 2)
- LogEvent(Severity_Middle, LOG_EVT_SELFCHECK_POWER_RESTART, pszEntityName);
- break;
- default:
- //oilyang@20200526 As all the LogWarn & LogError have beed collect to ELK platform,no need to throw LowWarn repeatly
- //if (eType == ProcType_Warn)
- //LogWarn(Severity_High, Error_Unexpect,(ErrorCodeEnum)dwAction,pszMessage);
- break;
- }
- return eErrCode;
- }
- void CSelfCheckerFSM::UpgradeActionProcess(const char *pszEntityName, const char *pAction)
- {
- return;
- }
- ErrorCodeEnum CSelfCheckerFSM::ConnectToHealthManager()
- {
- LOG_FUNCTION();
- if (m_pHealthClient == NULL)
- {
- m_pHealthClient = new HealthManagerClient(GetEntityBase());
- ErrorCodeEnum ErrorConn = m_pHealthClient->Connect();
- if (ErrorConn != Error_Succeed) {
- //m_pHealthClient->SafeDelete();
- m_pHealthClient = NULL;
- Dbg("Connect to Healthmanager failed.%d", ErrorConn);
- return Error_Unexpect;
- }
- else
- {
- Dbg("HealthManager connected.");
- }
- }
- return Error_Succeed;
- }
- HealthManagerClient::HealthManagerClient(CEntityBase *pEntity) :HealthManagerService_ClientBase(pEntity)
- {
- }
|