SelfCheckerFSM.cpp 50 KB

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