SelfCheckerFSM.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614
  1. #include "stdafx.h"
  2. #define WIN32_LEAN_AND_MEAN
  3. #include "SelfCheckerFSM.h"
  4. #include "SpHelper.h"
  5. #include "EventCode.h"
  6. #pragma comment(lib,"user32.lib")
  7. class CSelfCheckerEntity;
  8. const int MAX_AYSNC_TIMEOUT = 60000;
  9. const int MAX_CHECK_TIME = 60000;
  10. const int MAX_CPU_CHECK_TIME = 5000;
  11. const int TIMER_ID_CHECK = 0;
  12. const int TIMER_CPU_CHECK = 1;
  13. const int THOUSAND = 1024;
  14. const int MILLION = 1048576;
  15. int ActionStrToInt(const char *pAction)
  16. {
  17. int ret = 0;
  18. char x = *pAction;
  19. if (x >= '0' && x <= '9')
  20. return x - '0';
  21. else
  22. return 0;
  23. }
  24. DWORD GetRadixProduct(int times,int radix)
  25. {
  26. DWORD dwRet = 1;
  27. if (times < 0 || (radix != 10 && radix != 16))
  28. return 0;
  29. else
  30. {
  31. for (int i = 0; i < times; i++)
  32. dwRet *= radix;
  33. }
  34. return dwRet;
  35. }
  36. DWORD CodeStrToInt(const char *pCode)
  37. {
  38. CSimpleStringA csCode(pCode);
  39. csCode = csCode.Trim();
  40. int len = csCode.GetLength();
  41. if (len < 0 || len > 10)
  42. {
  43. return 0;
  44. }
  45. DWORD dwRet = 0;
  46. if (len > 2 && csCode[0] == '0' && (csCode[1] == 'x' || csCode[1] == 'X'))//"0x1234,0X12FF"
  47. {
  48. for (int i = 2; i < len; i++)
  49. {
  50. if (csCode[i] <= '9' && csCode[i] >= '0')
  51. dwRet += (csCode[i] - '0')*GetRadixProduct(len - i - 1,16);
  52. else if (csCode[i] <= 'f' && csCode[i] >= 'a')
  53. dwRet += (csCode[i] - 'a' + 10)*GetRadixProduct(len - i - 1,16);
  54. else if (csCode[i] <= 'F' && csCode[i] >= 'A')
  55. dwRet += (csCode[i] - 'A' + 10)*GetRadixProduct(len - i - 1, 16);
  56. else
  57. return 0;//because there is a invalid char,break process and return 0
  58. }
  59. }
  60. else//"768"
  61. {
  62. for (int i = 0; i < len; i++)
  63. {
  64. if (csCode[i] <= '9' && csCode[i] >= '0')
  65. dwRet += (csCode[i] - '0')*GetRadixProduct(len - i - 1, 10);
  66. else
  67. return 0;//because there is a invalid char,break process and return 0
  68. }
  69. }
  70. return dwRet;
  71. }
  72. unsigned long long GetTickCountRVC() {
  73. #ifdef RVC_OS_WIN
  74. return GetTickCount64();
  75. #else
  76. struct timespec ts;
  77. clock_gettime(CLOCK_MONOTONIC, &ts);
  78. return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
  79. #endif //RVC_OS_WIN
  80. }
  81. ErrorCodeEnum CSelfCheckerFSM::OnInit()
  82. {
  83. m_xIdlePre = m_xKernelPre = m_xUserPre = 0;
  84. ErrorCodeEnum errCode = Initial();
  85. if (errCode != Error_Succeed)
  86. return Error_IO;
  87. return Error_Succeed;
  88. }
  89. ErrorCodeEnum CSelfCheckerFSM::OnExit()
  90. {
  91. return Error_Succeed;
  92. }
  93. void CSelfCheckerFSM::s0_on_entry()
  94. {
  95. LOG_FUNCTION();
  96. ErrorCodeEnum errCode;
  97. CAutoArray<WORD> tmpInstIDs;
  98. CAutoArray<CSimpleStringA> tmpNames;
  99. errCode = GetEntityBase()->GetFunction()->GetAllRegistedEntity(tmpNames,tmpInstIDs);
  100. if (errCode != Error_Succeed)
  101. {
  102. Dbg("Get started entity failed.[%d]",errCode);
  103. }
  104. for (int i = 0; i < tmpNames.GetCount(); ++i)
  105. {
  106. ErrorCodeEnum eErr;
  107. CSmartPointer<IEntityFunction> pFunc = GetEntityBase()->GetFunction();
  108. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  109. CEntityRunInfo runInfo;
  110. eErr = pFunc->GetEntityRunInfo(tmpNames[i], runInfo);
  111. if (runInfo.eState != EntityState_NoStart
  112. || tmpNames[i] == "Download" || tmpNames[i] == "UpgradeRun" || tmpNames[i] == "UpgradeManager")
  113. {
  114. Dbg("Add entity %s",(const char*)tmpNames[i]);
  115. m_allEntity.push_back(tmpNames[i]);
  116. m_activeEntity.push_back(tmpNames[i]);
  117. }
  118. }
  119. //m_pEntity-> m_entityNames.push_back(tmpNames[i]);
  120. FSMEvent *pEvt = new FSMEvent(USER_EVT_INIT);
  121. PostEventFIFO(pEvt);
  122. }
  123. void CSelfCheckerFSM::s0_on_exit()
  124. {
  125. LOG_FUNCTION();
  126. }
  127. unsigned int CSelfCheckerFSM::s0_on_event(FSMEvent* pEvt)
  128. {
  129. LOG_FUNCTION();
  130. switch(pEvt->iEvt)
  131. {
  132. case USER_EVT_INIT:
  133. pEvt->SetHandled();
  134. break;
  135. default:
  136. break;
  137. }
  138. return 0;
  139. }
  140. void CSelfCheckerFSM::s1_on_entry()
  141. {
  142. LOG_FUNCTION();
  143. void *pTmpData = NULL;
  144. ITimerListener *pListener = new TimerOutHelper<CSelfCheckerFSM>(this, &CSelfCheckerFSM::OnNormalWorkTimerout,pTmpData);
  145. GetEntityBase()->GetFunction()->SetTimer(TIMER_ID_CHECK, pListener, MAX_CHECK_TIME);
  146. //oilyang@20170703 add for cpu usage for process
  147. pTmpData = NULL;
  148. pListener = new TimerOutHelper<CSelfCheckerFSM>(this, &CSelfCheckerFSM::OnCalcCpuUsageTimerout, pTmpData);
  149. GetEntityBase()->GetFunction()->SetTimer(TIMER_CPU_CHECK, pListener, MAX_CPU_CHECK_TIME);
  150. }
  151. void CSelfCheckerFSM::s1_on_exit()
  152. {
  153. LOG_FUNCTION();
  154. }
  155. unsigned int CSelfCheckerFSM::s1_on_event(FSMEvent* evt)
  156. {
  157. LOG_FUNCTION();
  158. return 0;
  159. }
  160. void CSelfCheckerFSM::s2_on_entry()
  161. {
  162. LOG_FUNCTION();
  163. }
  164. void CSelfCheckerFSM::s2_on_exit()
  165. {
  166. LOG_FUNCTION();
  167. }
  168. unsigned int CSelfCheckerFSM::s2_on_event(FSMEvent* evt)
  169. {
  170. LOG_FUNCTION();
  171. return 0;
  172. }
  173. void CSelfCheckerFSM::s3_on_entry()
  174. {
  175. LOG_FUNCTION();
  176. }
  177. void CSelfCheckerFSM::s3_on_exit()
  178. {
  179. LOG_FUNCTION();
  180. }
  181. unsigned int CSelfCheckerFSM::s3_on_event(FSMEvent* evt)
  182. {
  183. LOG_FUNCTION();
  184. return 0;
  185. }
  186. int ch2int(char ch)
  187. {
  188. if (ch >= '0' && ch <= '9')
  189. return ch-'0';
  190. else if (ch >= 'a' && ch <= 'f')
  191. return ch-'a'+10;
  192. else if (ch >= 'A' && ch <= 'F')
  193. return ch-'A'+10;
  194. return 0;
  195. }
  196. long hexstr2int(const char *str, int len)
  197. {
  198. long result = 0;
  199. for (int i = 0; i < len; ++i)
  200. {
  201. result += (ch2int(str[i]) << ((len-i-1)*4));
  202. }
  203. return result;
  204. }
  205. bool StrEqualNoCase(const char *s1, const char *s2,int len)
  206. {
  207. if (strlen(s1) != strlen(s2))
  208. return false;
  209. for (int i = 0; i < len; ++i)
  210. {
  211. if (toupper(s1[i]) != toupper(s2[i]))
  212. return false;
  213. }
  214. return true;
  215. }
  216. ErrorCodeEnum CSelfCheckerFSM::Initial()
  217. {
  218. ErrorCodeEnum err;
  219. CSmartPointer<IConfigInfo> spConfig;
  220. err = GetEntityBase()->GetFunction()->OpenConfig(Config_Software, spConfig);
  221. if (err != Error_Succeed) {
  222. Dbg("open cfg file failed!");
  223. return Error_IO;
  224. }
  225. m_restartNormal = 3;
  226. m_restartSpecial = 5;
  227. m_maxOsRestart = 5;
  228. m_maxPowerRestart = 5;
  229. spConfig->ReadConfigValueInt("init","RestartNormal",m_restartNormal);
  230. spConfig->ReadConfigValueInt("init","RestartSpecial",m_restartSpecial);
  231. spConfig->ReadConfigValueInt("init","MaxOsRestart",m_maxOsRestart);
  232. spConfig->ReadConfigValueInt("init","MaxPowerRestart",m_maxPowerRestart);
  233. spConfig->ReadConfigValueInt("init","SimulateTestFlag",m_simulateTest);
  234. spConfig->ReadConfigValue("init","KeyEntity",m_csKeyEntity);
  235. spConfig->ReadConfigValueInt("init","CpuTooHighPercent",m_cpuHighPercent);
  236. spConfig->ReadConfigValueInt("init","MemoryTooHighPercent",m_memHighPercent);
  237. spConfig->ReadConfigValueInt("init","HardDiskTooHighPercent",m_diskHighPercent);
  238. ifstream is;
  239. CSimpleStringA cfgPath(""),cfgXml("");
  240. err = GetEntityBase()->GetFunction()->GetPath("cfg",cfgPath);
  241. //cfgPath = cfgPath + "\\SelfChecker.ini";
  242. cfgXml = cfgPath + "/SelfCheckerProc.xml";
  243. ReadXmlFile(cfgXml);
  244. Dbg("cfgxml[%s]", cfgXml);
  245. //is.open (cfgPath, ios::binary);
  246. //if (!is.is_open())
  247. // return Error_IO;
  248. //string line;
  249. //long curr,end;
  250. //is.seekg(0,ios_base::end);
  251. //end = is.tellg();
  252. //is.seekg(0,ios_base::beg);
  253. //CheckPattern eSection = CHECK_UNKNOWN;
  254. ////load config file
  255. //do
  256. //{
  257. // getline(is,line);
  258. // if (line[0] == '[')
  259. // {
  260. // size_t secEnd = line.find(']',1);
  261. // if (secEnd != string::npos)
  262. // {
  263. // string strSec = line.substr(1,secEnd-1);
  264. // if (StrEqualNoCase(strSec.c_str(),"ShakeHand",strSec.length()))
  265. // eSection = CHECK_HANDSHAKE;
  266. // else if (StrEqualNoCase(strSec.c_str(),"Examine",strSec.length()))
  267. // eSection = CHECK_EXAMINE;
  268. // else if (StrEqualNoCase(strSec.c_str(),"Reset",strSec.length()))
  269. // eSection = CHECK_RESET;
  270. // else if (StrEqualNoCase(strSec.c_str(),"Restart",strSec.length()))
  271. // eSection = CHECK_RESTART;
  272. // else
  273. // eSection = CHECK_UNKNOWN;
  274. // }
  275. // }
  276. // else if (((unsigned)(line[0]+1) > 256) || line[0] == ';')
  277. // {//Chinese and other...
  278. // curr = is.tellg();
  279. // continue;
  280. // }
  281. // else if (isalpha(line[0]))
  282. // {
  283. // if (eSection == CHECK_UNKNOWN)
  284. // {
  285. // curr = is.tellg();
  286. // continue;
  287. // }
  288. // size_t keyPos = line.find('=',0);
  289. // if (keyPos != string::npos)
  290. // {
  291. // string keyName = line.substr(0,keyPos);
  292. // size_t douPos = line.find(',',keyPos);
  293. // size_t fenPos,start = keyPos;
  294. // m_entCfgInfo[keyName.c_str()].entityRestartCount = 0;
  295. // m_entCfgInfo[keyName.c_str()].osRestartCount = 0;
  296. // m_entCfgInfo[keyName.c_str()].powerRestartCount = 0;
  297. // m_entCfgInfo[keyName.c_str()].bWaitRestart = false;
  298. // for (;douPos != string::npos;)
  299. // {
  300. // ErrorCodeEnum evtCode = (ErrorCodeEnum)hexstr2int(line.substr(start+1+2,douPos-start-2-1).c_str(),douPos-start-2-1);
  301. // fenPos = line.find(';',douPos);
  302. // TestActionEnum actCode;
  303. // if (fenPos != string::npos)
  304. // {
  305. // actCode = (TestActionEnum)atoi(line.substr(douPos+1,fenPos-douPos-1).c_str());
  306. // }
  307. // else
  308. // {
  309. // int xx = line.length()-douPos-1;
  310. // actCode = (TestActionEnum)atoi(line.substr(douPos+1,line.length()-douPos-1).c_str());
  311. // }
  312. // switch(eSection)
  313. // {
  314. // case CHECK_HANDSHAKE:
  315. // m_entCfgInfo[keyName.c_str()].hsInfo[evtCode] = actCode;
  316. // break;
  317. // case CHECK_EXAMINE:
  318. // break;
  319. // case CHECK_RESET:
  320. // m_entCfgInfo[keyName.c_str()].resetInfo[evtCode] = actCode;
  321. // break;
  322. // case CHECK_RESTART:
  323. // break;
  324. // }
  325. // douPos = line.find(',',fenPos);
  326. // start = fenPos;
  327. // }
  328. // }
  329. // }
  330. // curr = is.tellg();
  331. //}while(curr < end);
  332. auto list = m_csKeyEntity.Split(',');
  333. for (int i = 0; i < list.GetCount(); ++i)
  334. {
  335. CSimpleStringA entity = list[i];
  336. Dbg("%s",LPCTSTR(entity));
  337. m_vKeyEntity.push_back(entity);
  338. }
  339. CSystemStaticInfo sysInfo;
  340. err = GetEntityBase()->GetFunction()->GetSystemStaticInfo(sysInfo);
  341. if (err != Error_Succeed)
  342. {
  343. Dbg("Get System Static info failed(%d).",err);
  344. return Error_Unexpect;
  345. }
  346. m_csMachineType = sysInfo.strMachineType;
  347. m_csSite = sysInfo.strSite;
  348. CSmartPointer<IConfigInfo> spConfigRun;
  349. err = GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  350. if (err == Error_Succeed) {
  351. spConfigRun->ReadConfigValueInt("WarnRecord", "disk", m_diskLastWarnHour);
  352. }
  353. return Error_Succeed;
  354. }
  355. ErrorCodeEnum CSelfCheckerFSM::ExceptionErrorProcess(const char *pszEntityName, ErrorCodeEnum eCode)
  356. {
  357. CSmartPointer<IEntityFunction> pFunc = GetEntityBase()->GetFunction();
  358. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  359. CSmartPointer<IAsynWaitSp> spWait;
  360. Dbg("proc:%s,%d", pszEntityName, eCode);
  361. //do nothing
  362. if (eCode == Error_Cancel)
  363. return Error_Succeed;
  364. bool bUpgrade = false;
  365. //need to do something
  366. ErrorCodeEnum eErrCode = Error_Succeed;
  367. if ((m_entCfgInfo.find(pszEntityName) != m_entCfgInfo.end())
  368. && ((m_entCfgInfo[pszEntityName]).hsInfo.find(eCode) != (m_entCfgInfo[pszEntityName]).hsInfo.end()))
  369. {
  370. TestActionEnum eAction = (m_entCfgInfo[pszEntityName]).hsInfo.find(eCode)->second;
  371. Dbg("action:%d", eAction);
  372. switch (eAction)
  373. {
  374. case ACTION_EXAMINE:
  375. break;
  376. case ACTION_RESET:
  377. break;
  378. case ACTION_CLOSE:
  379. eErrCode = pFuncPrivilege->CloseEntity(pszEntityName, spWait);
  380. if (eErrCode == Error_Succeed)
  381. {
  382. }
  383. break;
  384. case ACTION_ENTITY_RESTART:
  385. {
  386. if (m_entCfgInfo[pszEntityName].bWaitRestart)
  387. {
  388. Dbg("waiting restart...");
  389. break;
  390. }
  391. if (m_entRunInfo[pszEntityName].loadOpt == 99)
  392. {
  393. Dbg("not configure? name:[%s]", pszEntityName);
  394. break;
  395. }
  396. //LogErrInfo("restart ",pszEntityName,eCode);
  397. m_entCfgInfo[pszEntityName].entityRestartCount++;
  398. Dbg("normalcount:%d,count:%d", m_restartNormal, m_entCfgInfo[pszEntityName].entityRestartCount);
  399. if (m_entCfgInfo[pszEntityName].entityRestartCount > m_restartNormal)
  400. {
  401. if ((!strnicmp(m_csMachineType, "RVC.Pad", strlen("RVC.Pad")) && !strnicmp(m_csSite, "cmb.FLB", strlen("cmb.FLB")))
  402. || (!strnicmp(m_csMachineType, "RPM.Stand1S", strlen("RPM.Stand1S"))))
  403. {
  404. if (m_entRunInfo[pszEntityName].loadOpt == 0)
  405. m_entCfgInfo[pszEntityName].entityRestartCount = 0;
  406. }
  407. else
  408. {
  409. LogErrInfo(pszEntityName, " restart too many,upgrade process.", eCode);
  410. bUpgrade = true;
  411. m_entCfgInfo[pszEntityName].bWaitRestart = true;
  412. }
  413. eErrCode = Error_Succeed;
  414. break;
  415. }
  416. eErrCode = pFuncPrivilege->StopEntity(pszEntityName, spWait);
  417. if (eErrCode == Error_Succeed)
  418. {
  419. eErrCode = spWait->WaitAnswer(MAX_AYSNC_TIMEOUT);
  420. if (eErrCode != Error_Succeed)
  421. {
  422. Dbg("spwait stop %s failed: %s.", pszEntityName, SpStrError(eErrCode));
  423. eErrCode = pFuncPrivilege->TerminateEntity(pszEntityName, spWait);
  424. eErrCode = spWait->WaitAnswer(MAX_AYSNC_TIMEOUT);
  425. if (eErrCode != Error_Succeed)
  426. {
  427. Dbg("spwait terminate %s failed: %s.", pszEntityName, SpStrError(eErrCode));
  428. break;
  429. }
  430. }
  431. }
  432. else
  433. {
  434. Dbg("Stop %s failed(%d).", pszEntityName, eErrCode);
  435. break;
  436. }
  437. Sleep(2000);
  438. CSimpleStringA csIEUrl;
  439. if (_strnicmp("IEBrowser", pszEntityName, strlen("IEBrowser")) == 0)
  440. {
  441. GetEntityBase()->GetFunction()->GetSysVar("IEUrl", csIEUrl);
  442. Dbg("Url:[%s]", (const char*)csIEUrl);
  443. eErrCode = pFuncPrivilege->StartEntity(pszEntityName, csIEUrl, spWait);
  444. }
  445. else
  446. eErrCode = pFuncPrivilege->StartEntity(pszEntityName, NULL, spWait);
  447. if (eErrCode == Error_Succeed)
  448. {
  449. eErrCode = spWait->WaitAnswer(MAX_AYSNC_TIMEOUT);
  450. if (eErrCode != Error_Succeed)
  451. {
  452. Dbg("spwait start %s failed(%d).", pszEntityName, eErrCode);
  453. break;
  454. }
  455. Dbg("Start entity %s suc.", pszEntityName);
  456. }
  457. else
  458. {
  459. Dbg("(re)Start %s failed(%d).", pszEntityName, eErrCode);
  460. break;
  461. }
  462. }
  463. break;
  464. case ACTION_OS_RESTART:
  465. Dbg("test os restart");
  466. if (m_entRunInfo[pszEntityName].loadOpt == 1 || m_entRunInfo[pszEntityName].loadOpt == 2)
  467. LogEvent(Severity_Middle, LOG_EVT_SELFCHECK_OS_RESTART, pszEntityName);
  468. break;
  469. case ACTION_POWER_RESTART:
  470. Dbg("test power restart");
  471. if (m_entRunInfo[pszEntityName].loadOpt == 1 || m_entRunInfo[pszEntityName].loadOpt == 2)
  472. LogEvent(Severity_Middle, LOG_EVT_SELFCHECK_POWER_RESTART, pszEntityName);
  473. break;
  474. default:
  475. break;
  476. }
  477. if (bUpgrade)
  478. {
  479. LogActionProcess(pszEntityName, eCode, eAction);
  480. }
  481. }
  482. else
  483. {
  484. Dbg("%s not configured,use default setting(%d)", LPCTSTR(pszEntityName), eCode);
  485. switch (eCode)
  486. {
  487. case Error_TimeOut:
  488. case Error_Unexpect:
  489. case Error_InvalidState:
  490. {
  491. eErrCode = pFuncPrivilege->StopEntity(pszEntityName, spWait);
  492. if (eErrCode == Error_Succeed)
  493. {
  494. eErrCode = spWait->WaitAnswer(MAX_AYSNC_TIMEOUT);
  495. if (eErrCode != Error_Succeed)
  496. {
  497. Dbg("spwait stop %s failed: %s.", pszEntityName, SpStrError(eErrCode));
  498. eErrCode = pFuncPrivilege->TerminateEntity(pszEntityName, spWait);
  499. eErrCode = spWait->WaitAnswer(MAX_AYSNC_TIMEOUT);
  500. if (eErrCode != Error_Succeed)
  501. {
  502. Dbg("spwait terminate %s failed: %s.", pszEntityName, SpStrError(eErrCode));
  503. break;
  504. }
  505. }
  506. }
  507. else
  508. {
  509. Dbg("Stop %s failed(%d).", pszEntityName, eErrCode);
  510. break;
  511. }
  512. Sleep(5000);
  513. CSimpleStringA csIEUrl;
  514. if (_strnicmp("IEBrowser", pszEntityName, strlen("IEBrowser")) == 0)
  515. {
  516. GetEntityBase()->GetFunction()->GetSysVar("IEUrl", csIEUrl);
  517. Dbg("Url:[%s]", (const char*)csIEUrl);
  518. eErrCode = pFuncPrivilege->StartEntity(pszEntityName, csIEUrl, spWait);
  519. }
  520. else
  521. eErrCode = pFuncPrivilege->StartEntity(pszEntityName, NULL, spWait);
  522. if (eErrCode == Error_Succeed)
  523. {
  524. eErrCode = spWait->WaitAnswer(MAX_AYSNC_TIMEOUT);
  525. if (eErrCode != Error_Succeed)
  526. {
  527. Dbg("spwait start %s failed(%d).", pszEntityName, eErrCode);
  528. break;
  529. }
  530. }
  531. else
  532. {
  533. Dbg("(re)Start %s failed(%d).", pszEntityName, eErrCode);
  534. break;
  535. }
  536. }
  537. break;
  538. default:
  539. break;
  540. }
  541. }
  542. return eErrCode;
  543. }
  544. ErrorCodeEnum CSelfCheckerFSM::CheckEntity(const char *pszEntityName,EntityTestEnum eTestType)
  545. {
  546. //oilyang@20170926 no need to check by self.Let the HealthManager entity to do it.
  547. if (pszEntityName != NULL && strnicmp(pszEntityName, GetEntityBase()->GetEntityName(), strlen(GetEntityBase()->GetEntityName())) == 0)
  548. return Error_Succeed;
  549. CSmartPointer<IEntityFunction> pFunc = GetEntityBase()->GetFunction();
  550. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  551. CSmartPointer<IAsynWaitSp> spWait;
  552. ErrorCodeEnum errCode;
  553. errCode = pFuncPrivilege->TestEntity(pszEntityName,eTestType,spWait);
  554. if (errCode == Error_Succeed)
  555. {
  556. callback_entry *entry = new callback_entry();
  557. entry->pRawData = NULL;
  558. entry->EntityName = pszEntityName;
  559. entry->ErrorResult = Error_Unexpect;
  560. entry->op = Test_ShakeHand;
  561. spWait->SetCallback(this, entry);
  562. }
  563. else
  564. Dbg("Test %s,%d",pszEntityName,errCode);
  565. //errCode = ExceptionErrorProcess(pszEntityName,errCode);
  566. return errCode;
  567. }
  568. void CSelfCheckerFSM::OnNormalWorkTimerout(void *pData)
  569. {
  570. CSmartPointer<IEntityFunction> pFunc = GetEntityBase()->GetFunction();
  571. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  572. CSmartPointer<IAsynWaitSp> spWait;
  573. ErrorCodeEnum errCode;
  574. //CSelfCheckerEntity* pEntity = ((CSelfCheckerEntity*)m_pEntity);
  575. // pEntity->GetActiveCount();
  576. int activeEnCount = m_activeEntity.size();
  577. vector<CSimpleStringA>::iterator it;
  578. //oiltmp 20131219
  579. //GetSystemCPUStatus();
  580. //GetSystemMemoryStatus();
  581. //GetSystemDiskStatus();
  582. for (it = m_activeEntity.begin();it != m_activeEntity.end(); ++it)
  583. {
  584. errCode = CheckEntity(*it,Test_ShakeHand);
  585. CEntityRunInfo runInfo;
  586. pFunc->GetEntityRunInfo(*it,runInfo);
  587. CheckEntityResouce(*it,runInfo);
  588. }
  589. GetEntityBase()->GetFunction()->ResetTimer(TIMER_ID_CHECK,MAX_CHECK_TIME);
  590. if (m_simulateTest)//oiltest
  591. {
  592. Dbg("simulate logevent framework restart");
  593. LogEvent(Severity_Middle,Event_Req_Framework_Restart,"oiltest");
  594. m_simulateTest = false;
  595. }
  596. }
  597. void CSelfCheckerFSM::OnCalcCpuUsageTimerout(void *pData)
  598. {
  599. CSmartPointer<IEntityFunction> pFunc = GetEntityBase()->GetFunction();
  600. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  601. CSmartPointer<IAsynWaitSp> spWait;
  602. ErrorCodeEnum errCode;
  603. int activeEnCount = m_activeEntity.size();
  604. vector<CSimpleStringA>::iterator it;
  605. for (it = m_activeEntity.begin(); it != m_activeEntity.end(); ++it)
  606. {
  607. CEntityRunInfo runInfo;
  608. pFunc->GetEntityRunInfo(*it, runInfo);
  609. CalcEntityCpuUsage(*it, runInfo, m_bFirstCalcCpu);
  610. CheckEntityResouce(*it, runInfo);
  611. }
  612. GetSystemCPUStatus();
  613. GetSystemMemoryStatus();
  614. GetSystemDiskStatus();
  615. m_bFirstCalcCpu = !m_bFirstCalcCpu;
  616. GetEntityBase()->GetFunction()->ResetTimer(TIMER_CPU_CHECK, MAX_CPU_CHECK_TIME*2);
  617. }
  618. ErrorCodeEnum CSelfCheckerFSM::GetAllLiveEntity(CAutoArray<CSimpleString> &allEntitys)
  619. {
  620. allEntitys.Clear();
  621. vector<CSimpleStringA>::iterator it;
  622. int start = 0;
  623. int size = m_allEntity.size();
  624. allEntitys.Init(size);
  625. CSimpleStringA testStr = "";
  626. for (it = m_allEntity.begin(); it != m_allEntity.end(); ++it,++start)
  627. {
  628. CSimpleStringA tmpName = *it;
  629. allEntitys[start] = tmpName;
  630. testStr += tmpName;
  631. testStr += ";";
  632. }
  633. //Dbg("allentity[%d][%s]",allEntitys.GetCount(),(const char*)testStr);
  634. return Error_Succeed;
  635. }
  636. //ErrorCodeEnum CSelfCheckerFSM::AddEntity(const char *pszEntityName)
  637. //{
  638. // if (pszEntityName == NULL)
  639. // return Error_Null;
  640. // m_activeEntity.push_back(pszEntityName);
  641. // m_allEntity.push_back(pszEntityName);
  642. //
  643. // return Error_Succeed;
  644. //}
  645. //ErrorCodeEnum CSelfCheckerFSM::RemoveEntity(const char *pszEntityName)
  646. //{
  647. // if (pszEntityName == NULL)
  648. // return Error_Null;
  649. // vector<CSimpleStringA>::iterator it;
  650. // for (it = m_activeEntity.begin(); it != m_activeEntity.end(); ++it)
  651. // {
  652. // if(!strncmp(pszEntityName,*it,it->GetLength()))
  653. // {
  654. // m_activeEntity.erase(it);
  655. // break;
  656. // }
  657. // }
  658. // return Error_Succeed;
  659. //}
  660. void CSelfCheckerFSM::DoOnCreated(const char *pszEntityName,ErrorCodeEnum eOnStartErrorCode,const char *pszCallerEntity)
  661. {
  662. if (eOnStartErrorCode == Error_Succeed)
  663. {
  664. vector<CSimpleStringA>::iterator it,itAct;
  665. bool bFound = false, bActFound = false;
  666. Dbg("oncreated %s",pszEntityName);
  667. for (it = m_allEntity.begin(); it != m_allEntity.end(); ++it)
  668. {
  669. if(!strncmp(pszEntityName,*it,it->GetLength()))
  670. {
  671. Dbg("already exist %s",pszEntityName);
  672. bFound = true;
  673. break;
  674. }
  675. }
  676. if(!bFound)
  677. m_allEntity.push_back(pszEntityName);
  678. for (itAct = m_activeEntity.begin(); itAct != m_activeEntity.end(); ++itAct)
  679. {
  680. if(!strncmp(pszEntityName,*itAct,itAct->GetLength()))
  681. {
  682. Dbg("active entity already exist %s",pszEntityName);
  683. bActFound = true;
  684. break;
  685. }
  686. }
  687. if (!bActFound)
  688. {
  689. m_activeEntity.push_back(pszEntityName);
  690. }
  691. m_entCfgInfo[pszEntityName].entityRestartCount = 0;
  692. }
  693. }
  694. void CSelfCheckerFSM::DoOnClosed(const char *pszEntityName,EntityCloseCauseEnum eCloseCause,ErrorCodeEnum eOnCloseErrorCode,const char *pszCallerEntity)
  695. {
  696. //not close by selfchecker
  697. if (strncmp(pszCallerEntity,GetEntityBase()->GetEntityName(),strlen(pszCallerEntity)))
  698. {
  699. if (eCloseCause == CloseCause_Self || eCloseCause == CloseCause_Other)
  700. {
  701. vector<CSimpleStringA>::iterator it;
  702. for (it = m_activeEntity.begin(); it != m_activeEntity.end(); ++it)
  703. {
  704. if(!strncmp(pszEntityName,*it,it->GetLength()))
  705. {
  706. Dbg("onclosed %s",pszEntityName);
  707. //oilyang@20210425 as almost all entity's start configure have been set to 0(can be ignored)
  708. //no need to erase the m_activeEntity any more
  709. //m_activeEntity.erase(it);
  710. break;
  711. }
  712. }
  713. }
  714. }
  715. CAutoArray<CSimpleString> testAutoArray;
  716. GetAllLiveEntity(testAutoArray);
  717. }
  718. void CSelfCheckerFSM::DoOnException(const char *pszEntityName,const char *pszFunctionName,EntityStateEnum eState,EntityStateEnum eLastState,ErrorCodeEnum eErrorCode)
  719. {
  720. Dbg("OnException:%s,%s,%d,%d,%d",pszEntityName,pszFunctionName,eState,eLastState,eErrorCode);
  721. }
  722. void CSelfCheckerFSM::OnAnswer(CSmartPointer<IAsynWaitSp> pAsynWaitSp)
  723. {
  724. CSmartPointer<ICallbackListener> spCallback;
  725. CSmartPointer<IReleasable> pData;
  726. pAsynWaitSp->GetCallback(spCallback, pData);
  727. //LOG_ASSERT(pData);
  728. callback_entry *entry = dynamic_cast<callback_entry*>((IReleasable*)pData.GetRawPointer());
  729. entry->ErrorResult = pAsynWaitSp->AsyncGetAnswer();
  730. callback_entry *new_entry = new callback_entry();
  731. new_entry->EntityName = entry->EntityName;
  732. new_entry->ErrorResult = entry->ErrorResult;
  733. new_entry->op = entry->op;
  734. new_entry->state = entry->state;
  735. m_entRunInfo[new_entry->EntityName].eTest = new_entry->ErrorResult; //add test result oilyang 20150616
  736. //m_entRunInfo[new_entry->EntityName].eState = 9; //add test result oilyang 20150616
  737. //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);
  738. if (new_entry->op == Test_ShakeHand && new_entry->ErrorResult != Error_Succeed) {
  739. Dbg("oiltmp shakehand %s turns out %s, entity state: %s",
  740. (LPCTSTR)new_entry->EntityName, SpStrError(new_entry->ErrorResult), SpStrEntityState((EntityStateEnum)new_entry->state));
  741. }
  742. if (new_entry->ErrorResult != Error_Succeed)
  743. {
  744. ErrorCodeEnum eErr;
  745. CSmartPointer<IEntityFunction> pFunc = GetEntityBase()->GetFunction();
  746. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  747. if (_strnicmp("MediaController", (const char*)new_entry->EntityName, strlen("MediaController")) == 0
  748. && !m_bEverInMainPage)
  749. {
  750. Dbg("On loading stage,don't process MediaController exception.");
  751. }
  752. else
  753. //ExceptionErrorProcess(new_entry->EntityName,new_entry->ErrorResult);
  754. Proc((const char*)new_entry->EntityName,ProcType_Shake,new_entry->ErrorResult);
  755. }
  756. else
  757. {
  758. CSmartPointer<IEntityFunction> spEntityFunction = GetEntityBase()->GetFunction();
  759. CSmartPointer<IConfigInfo> spConfig;
  760. ErrorCodeEnum eErr = spEntityFunction->OpenConfig(Config_Run, spConfig);
  761. if (eErr != Error_Succeed) {
  762. Dbg("open run cfg file failed!");
  763. return;
  764. }
  765. spConfig->WriteConfigValueInt(new_entry->EntityName,"OsRestart",0);
  766. spConfig->WriteConfigValueInt(new_entry->EntityName,"PowerRestart",0);
  767. }
  768. }
  769. void CSelfCheckerFSM::LogErrInfo(const char* msgHead,const char* msgBody,const int errCode)
  770. {
  771. //oiltest to redifine this
  772. Dbg("%s,%s,%d",msgHead,msgBody,errCode);
  773. }
  774. void CSelfCheckerFSM::LogActionProcess(const char *pszEntityName,ErrorCodeEnum errCode,TestActionEnum eAct)
  775. {
  776. Dbg("LogActionProcess:entity[%s],errCode[%d],eAction[%d]",pszEntityName,errCode,eAct);
  777. CSmartPointer<IEntityFunction> spEntityFunction = GetEntityBase()->GetFunction();
  778. CSmartPointer<IConfigInfo> spConfig;
  779. ErrorCodeEnum eErr = spEntityFunction->OpenConfig(Config_Run, spConfig);
  780. if (eErr != Error_Succeed) {
  781. Dbg("open run cfg file failed!");
  782. return;
  783. }
  784. int osTimes,powerTimes;
  785. osTimes = powerTimes = 0;
  786. spConfig->ReadConfigValueInt(pszEntityName,"OsRestart",osTimes);
  787. spConfig->ReadConfigValueInt(pszEntityName,"PowerRestart",powerTimes);
  788. switch(eAct)
  789. {
  790. case ACTION_ENTITY_RESTART:
  791. if (osTimes > m_maxOsRestart || powerTimes > m_maxPowerRestart)
  792. {
  793. Dbg("restart too much,give up[%d][%d].",osTimes,powerTimes);
  794. break;
  795. }
  796. //if not the KEY entity,don't upgrade to restart computer
  797. //if (!IsKeyEntity(pszEntityName))
  798. break;
  799. osTimes++;
  800. powerTimes++;
  801. spConfig->WriteConfigValueInt(pszEntityName,"OsRestart",osTimes);
  802. spConfig->WriteConfigValueInt(pszEntityName,"PowerRestart",powerTimes);
  803. //for simple and effective,use power restart only
  804. LogEvent(Severity_Middle,LOG_EVT_SELFCHECK_POWER_RESTART,pszEntityName);
  805. m_entCfgInfo[pszEntityName].bWaitRestart = true;
  806. break;
  807. default:
  808. break;
  809. }
  810. }
  811. #ifdef RVC_OS_WIN
  812. ULONGLONG subtractTime(const FILETIME &a, const FILETIME &b)
  813. {
  814. LARGE_INTEGER la, lb;
  815. la.LowPart = a.dwLowDateTime;
  816. la.HighPart = a.dwHighDateTime;
  817. lb.LowPart = b.dwLowDateTime;
  818. lb.HighPart = b.dwHighDateTime;
  819. return la.QuadPart - lb.QuadPart;
  820. }
  821. float getUsage(HANDLE hProcess,FILETIME *prevSysKernel, FILETIME *prevSysUser,
  822. FILETIME *prevProcKernel, FILETIME *prevProcUser,
  823. bool firstRun = false)
  824. {
  825. FILETIME sysIdle, sysKernel, sysUser;
  826. FILETIME procCreation, procExit, procKernel, procUser;
  827. if (!GetSystemTimes(&sysIdle, &sysKernel, &sysUser) ||
  828. !GetProcessTimes(hProcess, &procCreation, &procExit, &procKernel, &procUser))
  829. {
  830. // can't get time info so return
  831. return -1.;
  832. }
  833. // check for first call
  834. if (firstRun)
  835. {
  836. // save time info before return
  837. prevSysKernel->dwLowDateTime = sysKernel.dwLowDateTime;
  838. prevSysKernel->dwHighDateTime = sysKernel.dwHighDateTime;
  839. prevSysUser->dwLowDateTime = sysUser.dwLowDateTime;
  840. prevSysUser->dwHighDateTime = sysUser.dwHighDateTime;
  841. prevProcKernel->dwLowDateTime = procKernel.dwLowDateTime;
  842. prevProcKernel->dwHighDateTime = procKernel.dwHighDateTime;
  843. prevProcUser->dwLowDateTime = procUser.dwLowDateTime;
  844. prevProcUser->dwHighDateTime = procUser.dwHighDateTime;
  845. return -1.;
  846. }
  847. ULONGLONG sysKernelDiff = subtractTime(sysKernel, *prevSysKernel);
  848. ULONGLONG sysUserDiff = subtractTime(sysUser, *prevSysUser);
  849. ULONGLONG procKernelDiff = subtractTime(procKernel, *prevProcKernel);
  850. ULONGLONG procUserDiff = subtractTime(procUser, *prevProcUser);
  851. ULONGLONG sysTotal = sysKernelDiff + sysUserDiff;
  852. ULONGLONG procTotal = procKernelDiff + procUserDiff;
  853. return (float)((100.0 * procTotal) / sysTotal);
  854. }
  855. #else
  856. //oiltestlinux
  857. #endif //RVC_OS_WIN
  858. void CSelfCheckerFSM::CheckEntityResouce(const char *pszEntityName,CEntityRunInfo &info)
  859. {
  860. if (info.eState != EntityState_Idle)
  861. return;
  862. #ifdef RVC_OS_WIN
  863. HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_QUERY_LIMITED_INFORMATION,FALSE,info.dwProcessID);
  864. if (hProcess == NULL)
  865. {
  866. Dbg("OpenProcess %s failed %d.",pszEntityName,GetLastError());
  867. return;
  868. }
  869. //GetSystemInfo
  870. PIO_COUNTERS pIOCounters = new IO_COUNTERS;
  871. BOOL ret = GetProcessIoCounters(hProcess,pIOCounters);
  872. if (ret == 0)
  873. {
  874. Dbg("GetProcessIoCounters %s failed %d.",pszEntityName,GetLastError());
  875. }
  876. PROCESS_MEMORY_COUNTERS pmc;
  877. const int showSize = 20;
  878. if (GetProcessMemoryInfo(hProcess,&pmc,sizeof(pmc)))
  879. {
  880. if ((pmc.WorkingSetSize/MILLION > showSize) && (pmc.PeakWorkingSetSize/MILLION > showSize)
  881. && (pmc.PagefileUsage/MILLION > showSize) && (pmc.PeakPagefileUsage/MILLION > showSize))
  882. {
  883. m_entRunInfo[pszEntityName].memoryHighCount++;
  884. if (m_entRunInfo[pszEntityName].memoryHighCount > ((60000 / MAX_CPU_CHECK_TIME)) * 2)//more than 2 minutes
  885. {
  886. m_entRunInfo[pszEntityName].memoryHighCount = 0;
  887. Dbg("%s,WorkingSetSize %u, Peak %u,PageFileUsage %u, Peak %u", (LPCTSTR)pszEntityName, pmc.WorkingSetSize / MILLION, pmc.PeakWorkingSetSize / MILLION, pmc.PagefileUsage / MILLION, pmc.PeakPagefileUsage / MILLION);
  888. }
  889. }
  890. }
  891. //Dbg can't support the ULONGLONG...oilyang 20140730
  892. //Dbg("Entity %s:",pszEntityName);
  893. //Dbg("read op %u,write op %u",pIOCounters->ReadOperationCount,pIOCounters->WriteOperationCount);
  894. //Dbg("read transfer %u,write transfer %u",pIOCounters->ReadTransferCount,pIOCounters->WriteTransferCount);
  895. //Dbg("other op %u,other transfer %u",pIOCounters->OtherOperationCount,pIOCounters->OtherTransferCount);
  896. CloseHandle(hProcess);
  897. #else
  898. return;//oiltestlinux
  899. #endif //RVC_OS_WIN
  900. }
  901. void CSelfCheckerFSM::CalcEntityCpuUsage(const char *pszEntityName, CEntityRunInfo &info,bool bFirst)
  902. {
  903. if (info.eState != EntityState_Idle)
  904. return;
  905. #ifdef RVC_OS_WIN
  906. HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_QUERY_LIMITED_INFORMATION, FALSE, info.dwProcessID);
  907. if (hProcess == NULL)
  908. {
  909. Dbg("OpenProcess %s failed %d.", pszEntityName, GetLastError());
  910. return;
  911. }
  912. float ret = getUsage(hProcess, &(m_entRunInfo[pszEntityName].prevSysKernel), &(m_entRunInfo[pszEntityName].prevSysUser)
  913. , &(m_entRunInfo[pszEntityName].prevProcKernel), &(m_entRunInfo[pszEntityName].prevProcUser),bFirst);
  914. if (!bFirst && ret > 2 && ret < 100)//where a entity have been restarted,the bFirst flag maybe wrong,the 'ret' can be big than 100!
  915. {
  916. Dbg("entity %s,cpu ratio:%f", pszEntityName, ret);
  917. m_entRunInfo[pszEntityName].cpuRatio = ret;
  918. }
  919. CloseHandle(hProcess);
  920. #else
  921. return;//oiltestlinux
  922. #endif //RVC_OS_WIN
  923. }
  924. void CSelfCheckerFSM::GetSystemMemoryStatus()
  925. {
  926. #ifdef RVC_OS_WIN
  927. // Use to convert bytes to KB
  928. #define DIV 1024
  929. // Specify the width of the field in which to print the numbers.
  930. // The asterisk in the format specifier "%*I64d" takes an integer
  931. // argument and uses it to pad and right justify the number.
  932. #define WIDTH 7
  933. MEMORYSTATUSEX statex;
  934. statex.dwLength = sizeof (statex);
  935. GlobalMemoryStatusEx (&statex);
  936. if (statex.dwMemoryLoad > m_memHighPercent)
  937. {
  938. Dbg("memory used: %*ld .", WIDTH, statex.dwMemoryLoad);
  939. LogWarn(Severity_Low, Error_Resource, LOG_EVT_SELFCHECK_MEMORY_TOO_HIGH, "Free memory is few.");
  940. }
  941. //Dbg ("(physical)%*I64d,(phy free)%*I64d,(paging)%*I64d,(pg free)%*I64d,(virtaul)%*I64d,(vt free)%*I64d",
  942. // WIDTH, statex.ullTotalPhys/DIV,WIDTH, statex.ullAvailPhys/DIV,WIDTH, statex.ullTotalPageFile/DIV
  943. // ,WIDTH, statex.ullAvailPageFile/DIV,WIDTH, statex.ullTotalVirtual/DIV,WIDTH, statex.ullAvailVirtual/DIV);
  944. // Show the amount of extended memory available.
  945. //Dbg ("There are %*I64d free Kbytes of extended memory.\n",WIDTH, statex.ullAvailExtendedVirtual/DIV);
  946. #else
  947. //oiltestlinux
  948. #endif
  949. }
  950. void CSelfCheckerFSM::GetSystemCPUStatus()
  951. {
  952. #ifdef RVC_OS_WIN
  953. #define _WIN32_WINNT 0x0601
  954. FILETIME idleTime,kernelTime,userTime;
  955. BOOL ret = GetSystemTimes(&idleTime,&kernelTime,&userTime);
  956. if (ret == 0)
  957. {
  958. Dbg("GetSystemCPUStatus.GetSystemTimes failed(%d).",GetLastError());
  959. return;
  960. }
  961. __int64 xIdle,xKernel,xUser;
  962. xIdle = idleTime.dwHighDateTime;
  963. xIdle <<= 32;
  964. xIdle |= idleTime.dwLowDateTime;
  965. xKernel = kernelTime.dwHighDateTime;
  966. xKernel <<= 32;
  967. xKernel |= kernelTime.dwLowDateTime;
  968. xUser = userTime.dwHighDateTime;
  969. xUser <<= 32;
  970. xUser |= userTime.dwLowDateTime;
  971. //Dbg("%u,%u,%u,%u,%u,%u",idleTime.dwHighDateTime,idleTime.dwLowDateTime,kernelTime.dwHighDateTime,kernelTime.dwLowDateTime,userTime.dwHighDateTime,userTime.dwLowDateTime);
  972. if (m_xIdlePre != 0)
  973. {
  974. __int64 xI,xK,xU;
  975. xI = xIdle - m_xIdlePre;
  976. xK = xKernel - m_xKernelPre;
  977. xU = xUser - m_xUserPre;
  978. int ratio = 0;
  979. if ((xK +xU) != 0)
  980. ratio = (xK - xI + xU) * 100 / (xK + xU);
  981. if (ratio > 50)//oiltmp@20170919 need to read from configure file?maybe or not
  982. Dbg("cpu %d",ratio);
  983. if (ratio > m_cpuHighPercent)
  984. {
  985. CSimpleStringA cpuHighEntitys("");
  986. char cpuRatioBuf[16];
  987. ZeroMemory(cpuRatioBuf, sizeof(cpuRatioBuf));
  988. map<CSimpleStringA, EntityRunInfo>::iterator it;
  989. for (it = m_entRunInfo.begin(); it != m_entRunInfo.end(); ++it)
  990. {
  991. if (it->second.cpuRatio > 0 && it->second.cpuRatio < 100)
  992. {
  993. itoa(it->second.cpuRatio, cpuRatioBuf, 10);
  994. cpuHighEntitys.Append(it->first + ":" + cpuRatioBuf + "||");
  995. }
  996. }
  997. Dbg("cpu ratio:%s",(const char*)cpuHighEntitys);
  998. LogWarn(Severity_Low, Error_Resource, LOG_EVT_SELFCHECK_CPU_TOO_HIGH, (const char*)cpuHighEntitys);
  999. }
  1000. }
  1001. m_xIdlePre = xIdle;
  1002. m_xKernelPre = xKernel;
  1003. m_xUserPre = xUser;
  1004. int warnLevel;
  1005. CSimpleStringA strList("");
  1006. #else
  1007. //oiltestlinux
  1008. #endif //RVC_OS_WIN
  1009. }
  1010. void CSelfCheckerFSM::GetSystemDiskStatus()
  1011. {
  1012. #ifdef RVC_OS_WIN
  1013. ULARGE_INTEGER ulAvailFree,ulTotalBytes,ulTotalFree;
  1014. BOOL ret = GetDiskFreeSpaceEx(NULL,&ulAvailFree,&ulTotalBytes,&ulTotalFree);
  1015. if (ret == 0)
  1016. {
  1017. Dbg("GetSystemDiskStatus.GetDiskFreeSpaceEx failed(%d).",GetLastError());
  1018. return;
  1019. }
  1020. //Dbg("%d,%d,%d",(ulAvailFree.QuadPart/MILLION)*100,ulTotalBytes.QuadPart,(ulTotalBytes.QuadPart/MILLION));
  1021. DWORD dwAvFree = ulAvailFree.QuadPart/MILLION;
  1022. DWORD dwTotal = ulTotalBytes.QuadPart/MILLION;
  1023. DWORD dwTotalFree = ulTotalFree.QuadPart/MILLION;
  1024. int ratio = dwTotalFree*100/dwTotal;
  1025. if ((100 - ratio) > m_diskHighPercent)
  1026. {
  1027. Dbg("The disk has %d MB(%d%) available.\n", dwTotalFree, ratio);
  1028. //oilyang@20200526 根据wq建议,降低磁盘空间偏少的告警频率
  1029. SYSTEMTIME localTime;
  1030. GetLocalTime(&localTime);
  1031. if (m_diskLastWarnHour != localTime.wHour)
  1032. {
  1033. m_diskLastWarnHour = localTime.wHour;
  1034. CSmartPointer<IConfigInfo> spConfigRun;
  1035. ErrorCodeEnum eErr = GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  1036. if (eErr == Error_Succeed) {
  1037. spConfigRun->WriteConfigValueInt("WarnRecord", "disk", m_diskLastWarnHour);
  1038. }
  1039. LogWarn(Severity_Low, Error_Resource, LOG_EVT_SELFCHECK_HARDDISK_TOO_HIGH, "Harddisk free space is few.");
  1040. }
  1041. }
  1042. #else
  1043. //oiltestlinux
  1044. #endif //RVC_OS_WIN
  1045. }
  1046. bool CSelfCheckerFSM::IsKeyEntity(const char *pszEntityName)
  1047. {
  1048. vector<CSimpleStringA>::iterator it;
  1049. for (it = m_vKeyEntity.begin(); it != m_vKeyEntity.end(); ++it)
  1050. {
  1051. if (!_strnicmp(pszEntityName,*it,strlen(pszEntityName)))
  1052. return true;
  1053. }
  1054. return false;
  1055. }
  1056. int CSelfCheckerFSM::AddEntityState(const char *pszEntityName,EntityStateEnum eState)
  1057. {
  1058. if (eState == EntityState_Starting)
  1059. {
  1060. m_entRunInfo[pszEntityName].bGetLoadOpt = false;
  1061. m_entRunInfo[pszEntityName].bRestarting = false;
  1062. m_entRunInfo[pszEntityName].loadOpt = 99;
  1063. m_entRunInfo[pszEntityName].eState = eState;
  1064. m_entRunInfo[pszEntityName].eTest = Error_Succeed;
  1065. }
  1066. else
  1067. m_entRunInfo[pszEntityName].eState = eState;
  1068. Dbg("ADDDDD %s,%d,%d,%d",pszEntityName,m_entRunInfo[pszEntityName].eState,m_entRunInfo[pszEntityName].eTest,eState);
  1069. if (eState == EntityState_Idle)
  1070. {
  1071. m_entRunInfo[pszEntityName].eTest = Error_Succeed;
  1072. m_entRunInfo[pszEntityName].memoryHighCount = 0;
  1073. }
  1074. m_entRunInfo[pszEntityName].cpuRatio = 0;
  1075. return 0;
  1076. }
  1077. ErrorCodeEnum CSelfCheckerFSM::GetEntityErrorList(int &warmLevel,CSimpleStringA &strList)
  1078. {
  1079. map<CSimpleStringA,EntityRunInfo>::iterator it;
  1080. CSimpleStringA tmpStr("");
  1081. bool bLost = false;
  1082. for (it = m_entRunInfo.begin(); it != m_entRunInfo.end(); ++it)
  1083. {
  1084. if (m_entRunInfo[it->first].eState == EntityState_Lost)
  1085. bLost = true;
  1086. char buf[16],bufTest[16];
  1087. ZeroMemory(buf,16);
  1088. ZeroMemory(bufTest, 16);
  1089. //Dbg("oiltest20160425[%s],%d,%d", (const char*)it->first, m_entRunInfo[it->first].eState, m_entRunInfo[it->first].eTest);
  1090. if (m_entRunInfo[it->first].eState == EntityState_Lost || m_entRunInfo[it->first].eState == EntityState_Close
  1091. || m_entRunInfo[it->first].eState == EntityState_Killed || m_entRunInfo[it->first].eTest != Error_Succeed)
  1092. {
  1093. tmpStr += it->first;
  1094. tmpStr += "=";
  1095. if (m_entRunInfo[it->first].eState == EntityState_Lost || m_entRunInfo[it->first].eState == EntityState_Close
  1096. || m_entRunInfo[it->first].eState == EntityState_Killed)
  1097. {
  1098. _itoa(m_entRunInfo[it->first].eState, buf, 10);
  1099. tmpStr += buf;
  1100. }
  1101. if (m_entRunInfo[it->first].eTest != Error_Succeed)
  1102. {
  1103. Dbg("[%s],%s,%s", (const char*)it->first, SpStrEntityState((EntityStateEnum)m_entRunInfo[it->first].eState), SpStrError((ErrorCodeEnum)m_entRunInfo[it->first].eTest));
  1104. _itoa(m_entRunInfo[it->first].eTest, bufTest, 10);
  1105. tmpStr += ",(selfcheck code):";
  1106. tmpStr += bufTest;
  1107. }
  1108. tmpStr += ";";
  1109. }
  1110. }
  1111. if (tmpStr.GetLength() < 2)
  1112. m_warmLevel = warmLevel = 0;
  1113. if (bLost)
  1114. m_warmLevel = warmLevel = 3;
  1115. m_warmLevel = warmLevel = 1;//for temp set 20150617
  1116. strList = tmpStr;
  1117. if (strList.GetLength() > 2)
  1118. Dbg("warnlevel:%d,ErrorList [%s]", m_warmLevel,(LPCTSTR)strList);
  1119. return Error_Succeed;
  1120. }
  1121. int CSelfCheckerFSM::Proc(string entity, ProcType eType, DWORD dwCode, const char *pszMessage)
  1122. {
  1123. ConnectToHealthManager();
  1124. if (!m_entRunInfo[entity.c_str()].bGetLoadOpt)
  1125. {
  1126. if (m_pHealthClient != NULL)
  1127. {
  1128. HealthManagerService_GetEntityCfgInfo_Req req;
  1129. HealthManagerService_GetEntityCfgInfo_Ans ans;
  1130. req.name = entity.c_str();
  1131. ErrorCodeEnum errCode = m_pHealthClient->GetEntityCfgInfo(req, ans, 10000);
  1132. if (errCode == Error_Succeed)
  1133. {
  1134. m_entRunInfo[entity.c_str()].bGetLoadOpt = true;
  1135. Dbg("to get entity cfg info suc:%d", ans.loadOpt);
  1136. SetEntityCfgInfo(entity.c_str(), ans.loadOpt);
  1137. }
  1138. }
  1139. }
  1140. map<string, EntityCfg, EntityNameCompare>::iterator it;
  1141. if ((it = m_mapEntity.find(entity.c_str())) == m_mapEntity.end())
  1142. {
  1143. Dbg("can't find entity %s configure setting,using default setting.",entity.c_str());
  1144. //Error_TimeOut:
  1145. //Error_Unexpect:
  1146. //Error_InvalidState:
  1147. if (eType == ProcType_Shake && (dwCode == Error_TimeOut || dwCode == Error_Unexpect || dwCode == Error_InvalidState))
  1148. ExceptionErrorProcessXml(eType,entity.c_str(), ACTION_ENTITY_RESTART,true);
  1149. else if (eType == ProcType_Warn)
  1150. ExceptionErrorProcessXml(eType, entity.c_str(), dwCode, true, pszMessage);
  1151. else
  1152. Dbg("What's this:type:%d,receive code:%x,msg:%s, from entity %s.", eType, dwCode, pszMessage, entity.c_str());
  1153. return -1;
  1154. }
  1155. vector<ProcItem>::iterator vIt, vEnd;
  1156. if (eType == ProcType_Shake)
  1157. {
  1158. vIt = it->second.vShake.begin();
  1159. vEnd = it->second.vShake.end();
  1160. }
  1161. else if (eType == ProcType_Warn)
  1162. {
  1163. vIt = it->second.vWarn.begin();
  1164. vEnd = it->second.vWarn.end();
  1165. }
  1166. for (; vIt != vEnd; vIt++)
  1167. {
  1168. if (vIt->code == dwCode)
  1169. {
  1170. if (vIt->upgradecount > 0)
  1171. {
  1172. Dbg("action size:%d,upgradecount:%d", vIt->proctime.size(), vIt->upgradecount);
  1173. UINT64 happentime = GetTickCountRVC();
  1174. if (vIt->proctime.size() < vIt->upgradecount - 1)//just add record,do nothing
  1175. {
  1176. vIt->proctime.push_back(happentime);
  1177. Dbg("just add record");
  1178. return 0;
  1179. }
  1180. else
  1181. {
  1182. //to find the 1st record happened in last upgradetime minute
  1183. //and remove the record happened far ago(upgradetime minutes before)
  1184. vector<UINT64>::iterator ittime, itFisrtInPeriod, xxxIt;
  1185. bool bFar = false;
  1186. int count = 0;
  1187. for (ittime = vIt->proctime.begin(); ittime != vIt->proctime.end(); ittime++)
  1188. {
  1189. itFisrtInPeriod = ittime;
  1190. UINT64 difftime = happentime - *ittime;
  1191. if (difftime / (1000 * 60) < vIt->upgradetime)
  1192. break;
  1193. else
  1194. {
  1195. count++;
  1196. Dbg("the %d th action",count);
  1197. bFar = true;
  1198. }
  1199. }
  1200. if (!bFar)
  1201. {
  1202. //to do upgrade action
  1203. Dbg("to do upgrade action");
  1204. ExceptionErrorProcessXml(eType,entity.c_str(), vIt->upgradeaction);
  1205. vIt->proctime.clear();
  1206. return 0;
  1207. }
  1208. else
  1209. {
  1210. if (count == 1 && vIt->proctime.size() == 1)
  1211. vIt->proctime.clear();
  1212. else
  1213. vIt->proctime.erase(vIt->proctime.begin(), itFisrtInPeriod);
  1214. for (xxxIt = vIt->proctime.begin(); xxxIt != vIt->proctime.end(); xxxIt++)
  1215. {
  1216. cout << *xxxIt << " # ";
  1217. }
  1218. cout << endl;
  1219. vIt->proctime.push_back(happentime);
  1220. for (xxxIt = vIt->proctime.begin(); xxxIt != vIt->proctime.end(); xxxIt++)
  1221. {
  1222. cout << *xxxIt << " * ";
  1223. }
  1224. Dbg("after clear,add record");
  1225. return 0;
  1226. }
  1227. }
  1228. }
  1229. Dbg("Entity %s receive %d,to do action:%d", entity.c_str(), dwCode, vIt->action);
  1230. ExceptionErrorProcessXml(eType,entity.c_str(), vIt->action,true, pszMessage);
  1231. return 0;
  1232. }
  1233. }
  1234. Dbg("can't find corresponding action of entity %s,type:%d,code:%x",entity.c_str(),eType,dwCode);
  1235. Dbg("use default process...");
  1236. if (eType == ProcType_Shake && (dwCode == Error_TimeOut || dwCode == Error_Unexpect || dwCode == Error_InvalidState))
  1237. ExceptionErrorProcessXml(eType, entity.c_str(), ACTION_ENTITY_RESTART, true);
  1238. else if (eType == ProcType_Warn)
  1239. ExceptionErrorProcessXml(eType, entity.c_str(), dwCode, true, pszMessage);
  1240. return -1;
  1241. }
  1242. bool CSelfCheckerFSM::ReadXmlFile(const char *szFileName)
  1243. {//读取Xml文件,并遍历
  1244. //MessageBox(0, 0, 0, 0);
  1245. LOG_FUNCTION();
  1246. tinyxml2::XMLDocument *doc = new tinyxml2::XMLDocument();
  1247. XMLError err = doc->LoadFile(szFileName);
  1248. if (err != XML_SUCCESS)
  1249. {
  1250. Dbg("open file %s failed.GetLastError:%d",szFileName,GetLastError());
  1251. return false;
  1252. }
  1253. //doc->GetDocument();
  1254. string out = "";
  1255. XMLNode *pF = doc->FirstChild();
  1256. XMLElement *pRoot = doc->RootElement();
  1257. //pF->FirstChildElement();
  1258. if (pRoot == NULL)
  1259. {
  1260. Dbg("Get root element failed.");
  1261. return false;
  1262. }
  1263. else
  1264. pF = pRoot->FirstChild();
  1265. while (pF != NULL)
  1266. {
  1267. if (pF->ToElement() == NULL)
  1268. {
  1269. pF = pF->FirstChild();
  1270. continue;
  1271. }
  1272. const char *pName = pF->ToElement()->Name();
  1273. if (!strncmp(pName, "SelfCheckerConfig", strlen("SelfCheckerConfig")))
  1274. {
  1275. pF = pF->FirstChild();
  1276. continue;
  1277. }
  1278. else if (!strncmp(pName, "Entity", strlen("Entity")))
  1279. {
  1280. EntityCfg entity;
  1281. const char *attrName = pF->ToElement()->Attribute("name");
  1282. out += const_cast<char*>(attrName);
  1283. out += "\r\n";
  1284. XMLNode *pChild = pF->FirstChild();
  1285. while (pChild != NULL)
  1286. {
  1287. if (!strncmp(pChild->Value(), "shakehandproc", strlen("shakehandproc")))
  1288. {
  1289. XMLNode *pProc = pChild->FirstChild();
  1290. while (pProc != NULL)
  1291. {
  1292. ProcItem item;
  1293. const char *code, *action, *upgradeaction, *upgradetime, *upgradecount;
  1294. code = action = upgradeaction = upgradetime = upgradecount = NULL;
  1295. if (pProc->ToElement() != NULL)
  1296. {
  1297. code = pProc->ToElement()->Attribute("code");
  1298. action = pProc->ToElement()->Attribute("action");
  1299. upgradeaction = pProc->ToElement()->Attribute("upgradeaction");
  1300. upgradetime = pProc->ToElement()->Attribute("upgradetime");
  1301. upgradecount = pProc->ToElement()->Attribute("upgradecount");
  1302. }
  1303. if (code != NULL)
  1304. {
  1305. item.code = CodeStrToInt(code);
  1306. Dbg("oiltest code:%s,%d", code, item.code);
  1307. out += const_cast<char*>(code);
  1308. out += " , ";
  1309. }
  1310. if (action != NULL)
  1311. {
  1312. item.action = ActionStrToInt(action);
  1313. out += const_cast<char*>(action);
  1314. out += " , ";
  1315. }
  1316. if (upgradeaction != NULL)
  1317. {
  1318. item.upgradeaction = ActionStrToInt(upgradeaction);
  1319. out += const_cast<char*>(upgradeaction);
  1320. }
  1321. if (upgradetime != NULL)
  1322. {
  1323. item.upgradetime = atoi(upgradetime);
  1324. }
  1325. if (upgradecount != NULL)
  1326. {
  1327. item.upgradecount = atoi(upgradecount);
  1328. }
  1329. else
  1330. item.upgradecount = 0;
  1331. entity.vShake.push_back(item);
  1332. out += "; ";
  1333. pProc = pProc->NextSibling();
  1334. }
  1335. }
  1336. else if (!strncmp(pChild->Value(), "eventproc", strlen("eventproc")))
  1337. {
  1338. XMLNode *pProc = pChild->FirstChild();
  1339. while (pProc != NULL)
  1340. {
  1341. ProcItem item;
  1342. const char *code, *action, *upgradeaction, *upgradetime, *upgradecount;
  1343. code = action = upgradeaction = upgradetime = upgradecount = NULL;
  1344. if (pProc->ToElement() != NULL)
  1345. {
  1346. code = pProc->ToElement()->Attribute("code");
  1347. action = pProc->ToElement()->Attribute("action");
  1348. upgradeaction = pProc->ToElement()->Attribute("upgradeaction");
  1349. upgradetime = pProc->ToElement()->Attribute("upgradetime");
  1350. upgradecount = pProc->ToElement()->Attribute("upgradecount");
  1351. }
  1352. if (code != NULL)
  1353. {
  1354. item.code = CodeStrToInt(code);
  1355. out += const_cast<char*>(code);
  1356. out += ",";
  1357. }
  1358. if (action != NULL)
  1359. {
  1360. item.action = ActionStrToInt(action);
  1361. out += const_cast<char*>(action);
  1362. out += ",";
  1363. }
  1364. if (upgradeaction != NULL)
  1365. {
  1366. item.upgradeaction = ActionStrToInt(upgradeaction);
  1367. out += const_cast<char*>(upgradeaction);
  1368. }
  1369. if (upgradetime != NULL)
  1370. {
  1371. item.upgradetime = atoi(upgradetime);
  1372. }
  1373. if (upgradecount != NULL)
  1374. {
  1375. item.upgradecount = atoi(upgradecount);
  1376. }
  1377. else
  1378. item.upgradecount = 0;
  1379. entity.vWarn.push_back(item);
  1380. out += "; ";
  1381. pProc = pProc->NextSibling();
  1382. }
  1383. }
  1384. pChild = pChild->NextSibling();
  1385. }
  1386. m_mapEntity[attrName] = entity;
  1387. Dbg("add %s,vShake size:%d,vWarn size:%d", attrName, entity.vShake.size(), entity.vWarn.size());
  1388. }
  1389. out += "\r\n";
  1390. pF = pF->NextSibling();
  1391. }
  1392. //cout << out.c_str() << endl;
  1393. return true;
  1394. }
  1395. ErrorCodeEnum CSelfCheckerFSM::ExceptionErrorProcessXml(ProcType eType, const char *pszEntityName, DWORD dwAction, bool bDefault,const char *pszMessage)
  1396. {
  1397. CSmartPointer<IEntityFunction> pFunc = GetEntityBase()->GetFunction();
  1398. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  1399. CSmartPointer<IAsynWaitSp> spWait;
  1400. ErrorCodeEnum eErrCode = Error_Unexpect;
  1401. switch (dwAction)
  1402. {
  1403. case ACTION_EXAMINE:
  1404. break;
  1405. case ACTION_RESET:
  1406. break;
  1407. case ACTION_CLOSE:
  1408. eErrCode = pFuncPrivilege->CloseEntity(pszEntityName, spWait);
  1409. if (eErrCode == Error_Succeed)
  1410. {
  1411. }
  1412. break;
  1413. case ACTION_ENTITY_RESTART:
  1414. {
  1415. if (m_entCfgInfo[pszEntityName].bWaitRestart)
  1416. {
  1417. if (!bDefault)
  1418. {
  1419. Dbg("waiting restart...");
  1420. break;
  1421. }
  1422. }
  1423. if (m_entRunInfo[pszEntityName].loadOpt == 99)
  1424. {
  1425. Dbg("not configure? name:[%s]", pszEntityName);
  1426. break;
  1427. }
  1428. //oilyang@20200407 if being restarted by selfchecker,break
  1429. if (m_entRunInfo[pszEntityName].bRestarting)
  1430. {
  1431. Dbg("%s is being restarted by selfchecker.", pszEntityName);
  1432. break;
  1433. }
  1434. //oilyang@20200403
  1435. //for fwb test,PinPad&CardSwiper can't be stop immediately
  1436. //huchen@20210220,SIPPhone can't be stop immediately
  1437. if (_strnicmp("PinPad", pszEntityName, strlen("PinPad")) == 0
  1438. || _strnicmp("CardSwiper", pszEntityName, strlen("CardSwiper")) == 0
  1439. //oilyang@20210220 for huchen add SIPPhone
  1440. || _strnicmp("SIPPhone", pszEntityName, strlen("SIPPhone")) == 0
  1441. || _strnicmp("IDCertificate", pszEntityName, strlen("IDCertificate")) == 0
  1442. || _strnicmp("FingerPrint", pszEntityName, strlen("FingerPrint")) == 0
  1443. || _strnicmp("DeviceControl", pszEntityName, strlen("DeviceControl")) == 0)
  1444. eErrCode = pFuncPrivilege->TerminateEntity(pszEntityName, spWait);
  1445. else
  1446. eErrCode = pFuncPrivilege->StopEntity(pszEntityName, spWait);
  1447. if (eErrCode == Error_Succeed)
  1448. {
  1449. eErrCode = spWait->WaitAnswer(MAX_AYSNC_TIMEOUT);
  1450. if (eErrCode != Error_Succeed)
  1451. {
  1452. Dbg("spwait stop %s failed: %s.", pszEntityName, SpStrError(eErrCode));
  1453. eErrCode = pFuncPrivilege->TerminateEntity(pszEntityName, spWait);
  1454. eErrCode = spWait->WaitAnswer(MAX_AYSNC_TIMEOUT);
  1455. if (eErrCode != Error_Succeed)
  1456. {
  1457. Dbg("spwait terminate %s failed: %s.", pszEntityName, SpStrError(eErrCode));
  1458. break;
  1459. }
  1460. }
  1461. }
  1462. else
  1463. {
  1464. Dbg("Stop %s failed(%d).", pszEntityName, eErrCode);
  1465. break;
  1466. }
  1467. Sleep(2000);
  1468. CSimpleStringA csIEUrl;
  1469. if (_strnicmp("IEBrowser", pszEntityName, strlen("IEBrowser")) == 0)
  1470. {
  1471. GetEntityBase()->GetFunction()->GetSysVar("IEUrl", csIEUrl);
  1472. Dbg("Url:[%s]", (const char*)csIEUrl);
  1473. eErrCode = pFuncPrivilege->StartEntity(pszEntityName, csIEUrl, spWait);
  1474. }
  1475. else
  1476. eErrCode = pFuncPrivilege->StartEntity(pszEntityName, NULL, spWait);
  1477. if (eErrCode == Error_Succeed)
  1478. {
  1479. eErrCode = spWait->WaitAnswer(MAX_AYSNC_TIMEOUT);
  1480. if (eErrCode != Error_Succeed)
  1481. {
  1482. Dbg("spwait start %s failed(%d).", pszEntityName, eErrCode);
  1483. break;
  1484. }
  1485. Dbg("Start entity %s suc.", pszEntityName);
  1486. }
  1487. else
  1488. {
  1489. Dbg("(re)Start %s failed(%d).", pszEntityName, eErrCode);
  1490. break;
  1491. }
  1492. }
  1493. break;
  1494. case ACTION_OS_RESTART:
  1495. Dbg("test os restart");
  1496. if (m_entRunInfo[pszEntityName].loadOpt == 1 || m_entRunInfo[pszEntityName].loadOpt == 2)
  1497. LogEvent(Severity_Middle, LOG_EVT_SELFCHECK_OS_RESTART, pszEntityName);
  1498. break;
  1499. case ACTION_POWER_RESTART:
  1500. Dbg("test power restart");
  1501. if (m_entRunInfo[pszEntityName].loadOpt == 1 || m_entRunInfo[pszEntityName].loadOpt == 2)
  1502. LogEvent(Severity_Middle, LOG_EVT_SELFCHECK_POWER_RESTART, pszEntityName);
  1503. break;
  1504. default:
  1505. break;
  1506. }
  1507. return eErrCode;
  1508. }
  1509. void CSelfCheckerFSM::UpgradeActionProcess(const char *pszEntityName, const char *pAction)
  1510. {
  1511. return;
  1512. }
  1513. ErrorCodeEnum CSelfCheckerFSM::ConnectToHealthManager()
  1514. {
  1515. LOG_FUNCTION();
  1516. if (m_pHealthClient == NULL || m_pHealthClient->QuerySessionClosed())
  1517. {
  1518. m_pHealthClient = new HealthManagerService_ClientBase(GetEntityBase());
  1519. ErrorCodeEnum ErrorConn = m_pHealthClient->Connect();
  1520. if (ErrorConn != Error_Succeed) {
  1521. m_pHealthClient->SafeDelete();
  1522. m_pHealthClient = NULL;
  1523. Dbg("Connect to Healthmanager failed.%d", ErrorConn);
  1524. return Error_Unexpect;
  1525. }
  1526. else
  1527. {
  1528. Dbg("HealthManager connected.");
  1529. }
  1530. }
  1531. return Error_Succeed;
  1532. }
  1533. void CSelfCheckerFSM::SetEverEnterMainPageFlag(bool bValue)
  1534. {
  1535. Dbg("set m_bEverInMainPage:%d,m_bHaveGetEntityList:%d",bValue, m_bHaveGetEntityList);
  1536. m_bEverInMainPage = bValue;
  1537. if (!m_bHaveGetEntityList)
  1538. {
  1539. ConnectToHealthManager();
  1540. if (m_pHealthClient != NULL)
  1541. {
  1542. HealthManagerService_GetEntityCfgInfo_Req req;
  1543. HealthManagerService_GetEntityCfgInfo_Ans ans;
  1544. req.name = "AllConfiguredEntity";
  1545. ErrorCodeEnum errCode = m_pHealthClient->GetEntityCfgInfo(req, ans, 5000);
  1546. if (errCode == Error_Succeed)
  1547. {
  1548. m_bHaveGetEntityList = true;
  1549. CAutoArray<CSimpleStringA> allCfgEntity = ans.reserved2.Split(',');
  1550. m_activeEntity.clear();
  1551. for (int i = 0; i < allCfgEntity.GetCount(); i++)
  1552. {
  1553. if (allCfgEntity[i].Trim().GetLength() > 1)//except for empty entity name
  1554. m_activeEntity.push_back(allCfgEntity[i]);
  1555. }
  1556. Dbg("to get all entity list:%s", ans.reserved2);
  1557. }
  1558. }
  1559. }
  1560. }