123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- #ifndef __SELFCHECKER_FSM_H
- #define __SELFCHECKER_FSM_H
- #pragma once
- #include "stdafx.h"
- #define WIN32_LEAN_AND_MEAN
- #include "tinyxml2.h"
- #include <cerrno>
- #include <cstdlib>
- #include <cstring>
- #include <ctime>
- //#include <crtdbg.h>
- #ifdef RVC_OS_WIN
- #include <windows.h>
- #include <Psapi.h>
- #else
- #endif //RVC_OS_WIN
- //_CrtMemState startMemState;
- //_CrtMemState endMemState;
- #include <fstream>
- #include <string>
- #include <iostream>
- #include <map>
- #include <vector>
- #include "SpFSM.h"
- using namespace tinyxml2;
- using namespace std;
- #include "../mod_healthmanager/HealthManager_client_g.h"
- using namespace HealthManager;
- const int MAX_KEY_VALUE_SIZE = 1024;
- class CSelfCheckerEntity;
- class CSelfCheckerFSM;
- enum EvtType
- {
- USER_EVT_TEST = EVT_USER+1,
- USER_EVT_QUIT,
- USER_EVT_INIT,
- };
- enum CheckPattern
- {
- CHECK_UNKNOWN,
- CHECK_HANDSHAKE,
- CHECK_EXAMINE,
- CHECK_RESET,
- CHECK_RESTART,
- };
- enum TestActionEnum
- {
- ACTION_NOTHINGTODO,
- ACTION_HANDSHAKE,
- ACTION_EXAMINE,
- ACTION_RESET,
- ACTION_CLOSE,
- ACTION_ENTITY_RESTART,
- ACTION_OS_RESTART,
- ACTION_POWER_RESTART,
- };
- struct callback_entry : public IReleasable
- {
- virtual ~callback_entry() {}
- CSimpleStringA EntityName;
- union {
- void *pRawData;
- int state;
- };
- int op;
- ErrorCodeEnum ErrorResult;
- };
- struct ErrorCfgInfo
- {
- //CheckPattern eType;
- ErrorCodeEnum eErrCode;
- TestActionEnum eAction;
- };
- struct EntityCfgInfo
- {
- //int resetCount;
- int entityRestartCount;
- int osRestartCount;
- int powerRestartCount;
- bool bWaitRestart;
- map<ErrorCodeEnum,TestActionEnum> hsInfo;//handshake
- map<ErrorCodeEnum,TestActionEnum> examInfo;//examine
- map<ErrorCodeEnum,TestActionEnum> resetInfo;//reset
- map<ErrorCodeEnum,TestActionEnum> restartInfo;//restart
- };
- struct EntityRunInfo
- {
- bool bGetLoadOpt;
- bool bRestarting;
- int eState;
- int eTest;
- int loadOpt;
- int memoryHighCount;
- int cpuRatio;
- FILETIME prevSysKernel, prevSysUser;
- FILETIME prevProcKernel, prevProcUser;
- };
- struct ProcItem
- {
- DWORD code;
- DWORD action;
- int upgradecount;
- DWORD upgradeaction;
- int upgradetime;
- vector<UINT64>proctime;
- };
- struct EntityCfg
- {
- vector<ProcItem> vShake;
- vector<ProcItem> vWarn;
- };
- struct EntityNameCompare
- {
- public:
- bool operator()(const std::string x, const std::string y)const
- {
- return x.compare(y) < 0;
- }
- };
- enum ProcType
- {
- ProcType_None = 0,
- ProcType_Shake,
- ProcType_Warn,
- };
- class HealthManagerClient : public HealthManagerService_ClientBase
- {
- public:
- HealthManagerClient(CEntityBase *pEntity);
- ~HealthManagerClient() {}
- };
- class CSelfCheckerFSM : public FSMImpl<CSelfCheckerFSM>,public ICallbackListener
- {
- public:
- enum {s0,s1,s2,s3};
- BEGIN_FSM_STATE(CSelfCheckerFSM)
- FSM_STATE_ENTRY(s0, "Init", s0_on_entry, s0_on_exit, s0_on_event)
- FSM_STATE_ENTRY(s1, "Normal", s1_on_entry, s1_on_exit, s1_on_event)
- FSM_STATE_ENTRY(s2, "Check", s2_on_entry, s2_on_exit, s2_on_event)
- FSM_STATE_ENTRY(s3, "SingleCheck", s3_on_entry, s3_on_exit, s3_on_event)
- END_FSM_STATE()
- BEGIN_FSM_RULE(CSelfCheckerFSM, s0)
- FSM_RULE_ENTRY(s0, s1, USER_EVT_INIT, 0)
- END_FSM_RULE()
- CSelfCheckerFSM() :m_warmLevel(0), m_bFirstCalcCpu(true), m_pHealthClient(NULL) , m_diskLastWarnHour(1)
- , m_bEverInMainPage(false){}
- ~CSelfCheckerFSM(){}
- virtual ErrorCodeEnum OnInit();
- virtual ErrorCodeEnum OnExit();
- void s0_on_entry();
- void s0_on_exit();
- unsigned int s0_on_event(FSMEvent* event);
- void s1_on_entry();
- void s1_on_exit();
- unsigned int s1_on_event(FSMEvent* event);
- void s2_on_entry();
- void s2_on_exit();
- unsigned int s2_on_event(FSMEvent* event);
- void s3_on_entry();
- void s3_on_exit();
- unsigned int s3_on_event(FSMEvent* event);
- //ErrorCodeEnum AddEntity(const char *pszEntityName);
- //ErrorCodeEnum RemoveEntity(const char *pszEntityName);
- ErrorCodeEnum ExceptionErrorProcess(const char *pszEntityName,ErrorCodeEnum eErrCode);
- ErrorCodeEnum ExceptionErrorProcessXml(ProcType eType, const char *pszEntityName, DWORD dwAction, bool bDefault = false, const char *pszMessage="");
- void DoOnCreated(const char *pszEntityName,ErrorCodeEnum eOnStartErrorCode,const char *pszCallerEntity);
- void DoOnClosed(const char *pszEntityName,EntityCloseCauseEnum eCloseCause,ErrorCodeEnum eOnCloseErrorCode,const char *pszCallerEntity);
- void DoOnException(const char *pszEntityName,const char *pszFunctionName,EntityStateEnum eState,EntityStateEnum eLastState,ErrorCodeEnum eErrorCode);
- ErrorCodeEnum GetAllLiveEntity(CAutoArray<CSimpleString> &allEntitys);
- ErrorCodeEnum CheckEntity(const char *pszEntityName,EntityTestEnum eTestType);
- int AddEntityState(const char *pszEntityName,EntityStateEnum eState);
- ErrorCodeEnum GetEntityErrorList(int &warmLevel,CSimpleStringA &strList);
- void SetEntityCfgInfo(const char *pszEntityName,int loadOpt){m_entRunInfo[pszEntityName].loadOpt = loadOpt;Dbg("%s,%d,%d",(LPCTSTR)pszEntityName,loadOpt,m_entRunInfo[pszEntityName].loadOpt);};
- void SetEntityRestarting(const char* pszEntityName,bool flag) { m_entRunInfo[pszEntityName].bRestarting = flag;}
- int Proc(string entity, ProcType eType, DWORD dwCode,const char *pszMessage="");
- void SetEverEnterMainPageFlag(bool bValue = true) { m_bEverInMainPage = bValue; }
- private:
- ErrorCodeEnum Initial();
- void OnNormalWorkTimerout(void *pData);
- void OnCalcCpuUsageTimerout(void *pData);
- virtual void OnAnswer(CSmartPointer<IAsynWaitSp> pAsynWaitSp);
- void LogErrInfo(const char* msgHead,const char* msgBody,const int errCode);
- void LogActionProcess(const char *pszEntityName,ErrorCodeEnum errCode,TestActionEnum eAct);
- void UpgradeActionProcess(const char *pszEntityName, const char *pAction);
- void CheckEntityResouce(const char *pszEntityName,CEntityRunInfo &Info);
- void CalcEntityCpuUsage(const char *pszEntityName, CEntityRunInfo &Info, bool bFirst);
- void GetSystemMemoryStatus();
- void GetSystemCPUStatus();
- void GetSystemDiskStatus();
- bool IsKeyEntity(const char *pszEntityName);
- bool ReadXmlFile(const char *szFileName);
- ErrorCodeEnum ConnectToHealthManager();
-
- private:
- map<CSimpleStringA,EntityCfgInfo> m_entCfgInfo;
- map<CSimpleStringA,EntityRunInfo> m_entRunInfo;
- map<string, EntityCfg, EntityNameCompare> m_mapEntity;
- vector<CSimpleStringA> m_activeEntity,m_allEntity,m_vKeyEntity,m_vQueryStateEntity;
- int m_restartNormal,m_restartSpecial,m_maxOsRestart,m_maxPowerRestart
- ,m_cpuHighPercent,m_memHighPercent,m_diskHighPercent,m_warmLevel,m_diskLastWarnHour;
- DWORD m_dwIssueCount,m_dwCaptureCount;
- bool m_bInIssue,m_bFirstCalcCpu, m_bEverInMainPage;
- int m_simulateTest;
- CSimpleStringA m_csKeyEntity,m_csMachineType,m_csSite;
- __int64 m_xIdlePre,m_xKernelPre,m_xUserPre;
- HealthManagerClient *m_pHealthClient;
- };
- template<class T>
- class TimerOutHelper : public ITimerListener
- {
- public:
- typedef void (T::*FuncTimer)(void *pUserdata);
- TimerOutHelper(T *p, FuncTimer pTimerFunc,void *pData,bool bDeleteSelf = false)
- : m_pObject(p),m_pUserData(pData), m_pTimer(pTimerFunc), m_bDeleteSelf(bDeleteSelf) {}
- virtual void OnTimeout(DWORD dwTimerID)
- {
- (m_pObject->*m_pTimer)(m_pUserData);
- if (m_bDeleteSelf)
- delete this;
- }
- private:
- void *m_pUserData;
- T *m_pObject;
- FuncTimer m_pTimer;
- bool m_bDeleteSelf;
- };
- #endif // __SELFCHECKER_FSM_H
|