1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591 |
- #include "stdafx.h"
- #define WIN32_LEAN_AND_MEAN
- #include "SelfCheckerFSM.h"
- #include "SpHelper.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: %s.", pszEntityName, SpStrError(eErrCode));
- eErrCode = pFuncPrivilege->TerminateEntity(pszEntityName, spWait);
- eErrCode = spWait->WaitAnswer(MAX_AYSNC_TIMEOUT);
- if (eErrCode != Error_Succeed)
- {
- Dbg("spwait terminate %s failed: %s.", pszEntityName, SpStrError(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: %s.", pszEntityName, SpStrError(eErrCode));
- eErrCode = pFuncPrivilege->TerminateEntity(pszEntityName, spWait);
- eErrCode = spWait->WaitAnswer(MAX_AYSNC_TIMEOUT);
- if (eErrCode != Error_Succeed)
- {
- Dbg("spwait terminate %s failed: %s.", pszEntityName, SpStrError(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 turns out %s, entity state: %s",
- (LPCTSTR)new_entry->EntityName, SpStrError(new_entry->ErrorResult), SpStrEntityState((EntityStateEnum)new_entry->state));
- }
-
- if (new_entry->ErrorResult != Error_Succeed)
- {
- ErrorCodeEnum eErr;
- CSmartPointer<IEntityFunction> pFunc = GetEntityBase()->GetFunction();
- CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
- //TODO(Gifur@20210507) Linux Platform has no IEBrowser!!
- CEntityRunInfo ieInfo;
- eErr = pFunc->GetEntityRunInfo("IEBrowser",ieInfo);
- const bool bIEStarted = (eErr == Error_Succeed && ieInfo.eState != EntityState_NoStart);
- 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],%s,%s", (const char*)it->first, SpStrEntityState((EntityStateEnum)m_entRunInfo[it->first].eState), SpStrError((ErrorCodeEnum)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: %s.", pszEntityName, SpStrError(eErrCode));
- eErrCode = pFuncPrivilege->TerminateEntity(pszEntityName, spWait);
- eErrCode = spWait->WaitAnswer(MAX_AYSNC_TIMEOUT);
- if (eErrCode != Error_Succeed)
- {
- Dbg("spwait terminate %s failed: %s.", pszEntityName, SpStrError(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;
- }
- 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)
- {
- }
|