SelfCheckerFSM.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #ifndef __SELFCHECKER_FSM_H
  2. #define __SELFCHECKER_FSM_H
  3. #pragma once
  4. #include "stdafx.h"
  5. #define WIN32_LEAN_AND_MEAN
  6. #include "tinyxml2.h"
  7. #include <cerrno>
  8. #include <cstdlib>
  9. #include <cstring>
  10. #include <ctime>
  11. //#include <crtdbg.h>
  12. #ifdef RVC_OS_WIN
  13. #include <windows.h>
  14. #include <Psapi.h>
  15. #else
  16. #endif //RVC_OS_WIN
  17. //_CrtMemState startMemState;
  18. //_CrtMemState endMemState;
  19. #include <fstream>
  20. #include <string>
  21. #include <iostream>
  22. #include <map>
  23. #include <vector>
  24. #include "SpFSM.h"
  25. using namespace tinyxml2;
  26. using namespace std;
  27. #include "../mod_healthmanager/HealthManager_client_g.h"
  28. using namespace HealthManager;
  29. const int MAX_KEY_VALUE_SIZE = 1024;
  30. class CSelfCheckerEntity;
  31. class CSelfCheckerFSM;
  32. enum EvtType
  33. {
  34. USER_EVT_TEST = EVT_USER+1,
  35. USER_EVT_QUIT,
  36. USER_EVT_INIT,
  37. };
  38. enum CheckPattern
  39. {
  40. CHECK_UNKNOWN,
  41. CHECK_HANDSHAKE,
  42. CHECK_EXAMINE,
  43. CHECK_RESET,
  44. CHECK_RESTART,
  45. };
  46. enum TestActionEnum
  47. {
  48. ACTION_NOTHINGTODO,
  49. ACTION_HANDSHAKE,
  50. ACTION_EXAMINE,
  51. ACTION_RESET,
  52. ACTION_CLOSE,
  53. ACTION_ENTITY_RESTART,
  54. ACTION_OS_RESTART,
  55. ACTION_POWER_RESTART,
  56. };
  57. struct callback_entry : public IReleasable
  58. {
  59. virtual ~callback_entry() {}
  60. CSimpleStringA EntityName;
  61. union {
  62. void *pRawData;
  63. int state;
  64. };
  65. int op;
  66. ErrorCodeEnum ErrorResult;
  67. };
  68. struct ErrorCfgInfo
  69. {
  70. //CheckPattern eType;
  71. ErrorCodeEnum eErrCode;
  72. TestActionEnum eAction;
  73. };
  74. struct EntityCfgInfo
  75. {
  76. //int resetCount;
  77. int entityRestartCount;
  78. int osRestartCount;
  79. int powerRestartCount;
  80. bool bWaitRestart;
  81. map<ErrorCodeEnum,TestActionEnum> hsInfo;//handshake
  82. map<ErrorCodeEnum,TestActionEnum> examInfo;//examine
  83. map<ErrorCodeEnum,TestActionEnum> resetInfo;//reset
  84. map<ErrorCodeEnum,TestActionEnum> restartInfo;//restart
  85. };
  86. struct EntityRunInfo
  87. {
  88. bool bGetLoadOpt;
  89. bool bRestarting;
  90. int eState;
  91. int eTest;
  92. int loadOpt;
  93. int memoryHighCount;
  94. int cpuRatio;
  95. FILETIME prevSysKernel, prevSysUser;
  96. FILETIME prevProcKernel, prevProcUser;
  97. };
  98. struct ProcItem
  99. {
  100. DWORD code;
  101. DWORD action;
  102. int upgradecount;
  103. DWORD upgradeaction;
  104. int upgradetime;
  105. vector<UINT64>proctime;
  106. };
  107. struct EntityCfg
  108. {
  109. vector<ProcItem> vShake;
  110. vector<ProcItem> vWarn;
  111. };
  112. struct EntityNameCompare
  113. {
  114. public:
  115. bool operator()(const std::string x, const std::string y)const
  116. {
  117. return x.compare(y) < 0;
  118. }
  119. };
  120. enum ProcType
  121. {
  122. ProcType_None = 0,
  123. ProcType_Shake,
  124. ProcType_Warn,
  125. };
  126. class HealthManagerClient : public HealthManagerService_ClientBase
  127. {
  128. public:
  129. HealthManagerClient(CEntityBase *pEntity);
  130. ~HealthManagerClient() {}
  131. };
  132. class CSelfCheckerFSM : public FSMImpl<CSelfCheckerFSM>,public ICallbackListener
  133. {
  134. public:
  135. enum {s0,s1,s2,s3};
  136. BEGIN_FSM_STATE(CSelfCheckerFSM)
  137. FSM_STATE_ENTRY(s0, "Init", s0_on_entry, s0_on_exit, s0_on_event)
  138. FSM_STATE_ENTRY(s1, "Normal", s1_on_entry, s1_on_exit, s1_on_event)
  139. FSM_STATE_ENTRY(s2, "Check", s2_on_entry, s2_on_exit, s2_on_event)
  140. FSM_STATE_ENTRY(s3, "SingleCheck", s3_on_entry, s3_on_exit, s3_on_event)
  141. END_FSM_STATE()
  142. BEGIN_FSM_RULE(CSelfCheckerFSM, s0)
  143. FSM_RULE_ENTRY(s0, s1, USER_EVT_INIT, 0)
  144. END_FSM_RULE()
  145. CSelfCheckerFSM() :m_warmLevel(0), m_bFirstCalcCpu(true), m_pHealthClient(NULL) , m_diskLastWarnHour(1)
  146. , m_bEverInMainPage(false){}
  147. ~CSelfCheckerFSM(){}
  148. virtual ErrorCodeEnum OnInit();
  149. virtual ErrorCodeEnum OnExit();
  150. void s0_on_entry();
  151. void s0_on_exit();
  152. unsigned int s0_on_event(FSMEvent* event);
  153. void s1_on_entry();
  154. void s1_on_exit();
  155. unsigned int s1_on_event(FSMEvent* event);
  156. void s2_on_entry();
  157. void s2_on_exit();
  158. unsigned int s2_on_event(FSMEvent* event);
  159. void s3_on_entry();
  160. void s3_on_exit();
  161. unsigned int s3_on_event(FSMEvent* event);
  162. //ErrorCodeEnum AddEntity(const char *pszEntityName);
  163. //ErrorCodeEnum RemoveEntity(const char *pszEntityName);
  164. ErrorCodeEnum ExceptionErrorProcess(const char *pszEntityName,ErrorCodeEnum eErrCode);
  165. ErrorCodeEnum ExceptionErrorProcessXml(ProcType eType, const char *pszEntityName, DWORD dwAction, bool bDefault = false, const char *pszMessage="");
  166. void DoOnCreated(const char *pszEntityName,ErrorCodeEnum eOnStartErrorCode,const char *pszCallerEntity);
  167. void DoOnClosed(const char *pszEntityName,EntityCloseCauseEnum eCloseCause,ErrorCodeEnum eOnCloseErrorCode,const char *pszCallerEntity);
  168. void DoOnException(const char *pszEntityName,const char *pszFunctionName,EntityStateEnum eState,EntityStateEnum eLastState,ErrorCodeEnum eErrorCode);
  169. ErrorCodeEnum GetAllLiveEntity(CAutoArray<CSimpleString> &allEntitys);
  170. ErrorCodeEnum CheckEntity(const char *pszEntityName,EntityTestEnum eTestType);
  171. int AddEntityState(const char *pszEntityName,EntityStateEnum eState);
  172. ErrorCodeEnum GetEntityErrorList(int &warmLevel,CSimpleStringA &strList);
  173. void SetEntityCfgInfo(const char *pszEntityName,int loadOpt){m_entRunInfo[pszEntityName].loadOpt = loadOpt;Dbg("%s,%d,%d",(LPCTSTR)pszEntityName,loadOpt,m_entRunInfo[pszEntityName].loadOpt);};
  174. void SetEntityRestarting(const char* pszEntityName,bool flag) { m_entRunInfo[pszEntityName].bRestarting = flag;}
  175. int Proc(string entity, ProcType eType, DWORD dwCode,const char *pszMessage="");
  176. void SetEverEnterMainPageFlag(bool bValue = true) { m_bEverInMainPage = bValue; }
  177. private:
  178. ErrorCodeEnum Initial();
  179. void OnNormalWorkTimerout(void *pData);
  180. void OnCalcCpuUsageTimerout(void *pData);
  181. virtual void OnAnswer(CSmartPointer<IAsynWaitSp> pAsynWaitSp);
  182. void LogErrInfo(const char* msgHead,const char* msgBody,const int errCode);
  183. void LogActionProcess(const char *pszEntityName,ErrorCodeEnum errCode,TestActionEnum eAct);
  184. void UpgradeActionProcess(const char *pszEntityName, const char *pAction);
  185. void CheckEntityResouce(const char *pszEntityName,CEntityRunInfo &Info);
  186. void CalcEntityCpuUsage(const char *pszEntityName, CEntityRunInfo &Info, bool bFirst);
  187. void GetSystemMemoryStatus();
  188. void GetSystemCPUStatus();
  189. void GetSystemDiskStatus();
  190. bool IsKeyEntity(const char *pszEntityName);
  191. bool ReadXmlFile(const char *szFileName);
  192. ErrorCodeEnum ConnectToHealthManager();
  193. private:
  194. map<CSimpleStringA,EntityCfgInfo> m_entCfgInfo;
  195. map<CSimpleStringA,EntityRunInfo> m_entRunInfo;
  196. map<string, EntityCfg, EntityNameCompare> m_mapEntity;
  197. vector<CSimpleStringA> m_activeEntity,m_allEntity,m_vKeyEntity,m_vQueryStateEntity;
  198. int m_restartNormal,m_restartSpecial,m_maxOsRestart,m_maxPowerRestart
  199. ,m_cpuHighPercent,m_memHighPercent,m_diskHighPercent,m_warmLevel,m_diskLastWarnHour;
  200. DWORD m_dwIssueCount,m_dwCaptureCount;
  201. bool m_bInIssue,m_bFirstCalcCpu, m_bEverInMainPage;
  202. int m_simulateTest;
  203. CSimpleStringA m_csKeyEntity,m_csMachineType,m_csSite;
  204. __int64 m_xIdlePre,m_xKernelPre,m_xUserPre;
  205. HealthManagerClient *m_pHealthClient;
  206. };
  207. template<class T>
  208. class TimerOutHelper : public ITimerListener
  209. {
  210. public:
  211. typedef void (T::*FuncTimer)(void *pUserdata);
  212. TimerOutHelper(T *p, FuncTimer pTimerFunc,void *pData,bool bDeleteSelf = false)
  213. : m_pObject(p),m_pUserData(pData), m_pTimer(pTimerFunc), m_bDeleteSelf(bDeleteSelf) {}
  214. virtual void OnTimeout(DWORD dwTimerID)
  215. {
  216. (m_pObject->*m_pTimer)(m_pUserData);
  217. if (m_bDeleteSelf)
  218. delete this;
  219. }
  220. private:
  221. void *m_pUserData;
  222. T *m_pObject;
  223. FuncTimer m_pTimer;
  224. bool m_bDeleteSelf;
  225. };
  226. #endif // __SELFCHECKER_FSM_H