You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
7.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "servaLifeMonitoring.h"
  2. #include "../../components/base/basicEvent.h"
  3. #include "../../components/base/basicEventHeap.h"
  4. #define LOG( X ) m_pLog->Log( "HELZ", this, X )
  5. #ifdef DEBUG
  6. #define LOG_ERROR( X ) {\
  7. char _tBfrErrMSG2w34[256];\
  8. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  9. m_pLog->Log( "HELZ", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  10. }
  11. #else
  12. #define LOG_ERROR( X ) m_pLog->Log( "HELZ", this, _LOG_ERROR, X );
  13. #endif
  14. servaLifeMonitoring *servaLifeMonitoring::_Instance = 0;
  15. servaLifeMonitoring *servaLifeMonitoring::getInstance(){
  16. if( _Instance == 0 ){
  17. _Instance = new servaLifeMonitoring();
  18. };
  19. return _Instance;
  20. };
  21. servaLifeMonitoring::servaLifeMonitoring():basicValidatable(),basicThread(){
  22. m_pLog = basicLogCollection::getInstance();
  23. m_pDBC = servaMySQLDBC::getInstance();
  24. DBC_UINT64_LIST *tList = m_pDBC->listPlatforms('E');
  25. if ( tList ) {
  26. if ( tList->size() ) {
  27. for ( DBC_UINT64_LIST::iterator idx = tList->begin(); idx != tList->end(); idx++ ) {
  28. DBC_UINT64_LIST *tML = m_pDBC->listPlatformModules( *idx );
  29. if ( tML ) {
  30. if ( tML->size() ) {
  31. HEALTH_MONITORING_ZONE *tHZ = (HEALTH_MONITORING_ZONE*) malloc( sizeof( HEALTH_MONITORING_ZONE));
  32. if (tHZ) {
  33. memset( tHZ, 0, sizeof(HEALTH_MONITORING_ZONE));
  34. tHZ->platform_id = *idx;
  35. uint8_t cid = 0;
  36. tHZ->lvl = 255; // To clean prev State
  37. for ( DBC_UINT64_LIST::iterator jdx = tML->begin(); jdx != tML->end(); jdx++ ) {
  38. tHZ->module_id[cid] = *jdx;
  39. tHZ->module_monitored[cid] = true;
  40. cid++;
  41. };
  42. m_mMonitoringZonesProtector.lock();
  43. m_vMonitoringZones.push_back( tHZ );
  44. m_mMonitoringZonesProtector.unlock();
  45. };
  46. };
  47. delete(tML);
  48. };
  49. };
  50. };
  51. delete( tList );
  52. };
  53. m_nStartupTS = time(NULL);
  54. init();
  55. };
  56. servaLifeMonitoring::~servaLifeMonitoring(){
  57. waitThreadEnd( true );
  58. m_mMonitoringZonesProtector.lock();
  59. if ( m_vMonitoringZones.size() ) {
  60. for ( V_HEALTH_MONITORING_ZONES::iterator idx = m_vMonitoringZones.begin(); idx != m_vMonitoringZones.end(); idx++ ) {
  61. free( *idx );
  62. };
  63. m_vMonitoringZones.clear();
  64. };
  65. m_mMonitoringZonesProtector.unlock();
  66. };
  67. bool servaLifeMonitoring::validateZone( HEALTH_MONITORING_ZONE *ptr ){
  68. bool retVal = true;
  69. if ( ptr ) {
  70. uint8_t min_lvl = 0;
  71. for ( uint8_t i = 0; i < 4; i++ ) {
  72. if (ptr->module_monitored[i]) {
  73. if( ptr->module_status[i] > min_lvl ) min_lvl = ptr->module_status[i];
  74. };
  75. };
  76. if( min_lvl != ptr->lvl ) {
  77. retVal = false;
  78. ptr->lvl = min_lvl;
  79. };
  80. };
  81. return retVal;
  82. };
  83. uint8_t servaLifeMonitoring::validateModule( uint64_t module_id ){
  84. uint8_t retVal = 0;
  85. if ( module_id ) {
  86. uint32_t cts = time( NULL );
  87. uint32_t mts = m_pDBC->getCloudLTS(ARGUS_OBJECT_MODULE, module_id );
  88. uint8_t ms = m_pDBC->getCloudStatus( ARGUS_OBJECT_MODULE, module_id );
  89. if ( mts ) {
  90. if ((cts - mts) > 30) {
  91. retVal = ARGUS_LIFE_STATUS_LVL_4;
  92. } else {
  93. //Online - Check Status
  94. if( ms < ARGUS_CLOUD_STATUS_E_GENERIC ) {
  95. retVal = ARGUS_LIFE_STATUS_CLEAN;
  96. } else {
  97. switch ( ms ) {
  98. case ARGUS_CLOUD_STATUS_E_GENERIC: break;
  99. case ARGUS_CLOUD_STATUS_E_CAM_GENERIC:
  100. case ARGUS_CLOUD_STATUS_E_CAM_NC:
  101. case ARGUS_CLOUD_STATUS_E_CAM_NPTZ:
  102. case ARGUS_CLOUD_STATUS_E_CAM_NIM: retVal = ARGUS_LIFE_STATUS_LVL_1; break;
  103. case ARGUS_CLOUD_STATUS_E_SEN_GENERIC:
  104. case ARGUS_CLOUD_STATUS_E_US_LRF:
  105. case ARGUS_CLOUD_STATUS_E_BS_LRF: retVal = ARGUS_LIFE_STATUS_LVL_2; break;
  106. case ARGUS_CLOUD_STATUS_E_US_NC:
  107. case ARGUS_CLOUD_STATUS_E_BS_NC: retVal = ARGUS_LIFE_STATUS_LVL_3; break;
  108. default: retVal = ARGUS_LIFE_STATUS_LVL_3;
  109. };
  110. };
  111. };
  112. } else {
  113. retVal = ARGUS_LIFE_STATUS_LVL_4;
  114. };
  115. if ( retVal == ARGUS_LIFE_STATUS_LVL_4 ) {
  116. DBC_IS_MODULE_DATA *tData = m_pDBC->getModuleData( module_id );
  117. if ( tData ) {
  118. if( !pingIP4( tData->ip4 ) ) {
  119. #ifdef DEBUG_EXTRA
  120. LOG_ERROR("No Network Access to Platform!");
  121. #endif
  122. retVal = ARGUS_LIFE_STATUS_LVL_5;
  123. };
  124. free( tData );
  125. };
  126. };
  127. };
  128. return retVal;
  129. };
  130. bool servaLifeMonitoring::validate(){
  131. uint32_t cts = time(NULL);
  132. if ( ( cts - m_nStartupTS ) < LM_SKEEP_MONITORING_SECONDS ) return true;
  133. m_mMonitoringZonesProtector.lock();
  134. for ( V_HEALTH_MONITORING_ZONES::iterator idx = m_vMonitoringZones.begin(); idx != m_vMonitoringZones.end(); idx++ ) {
  135. if (!validateZone(*idx)) {
  136. LOG("Zone Health is Changed");
  137. MODULE_HEALTH_STATUS *tData = (MODULE_HEALTH_STATUS*) malloc(sizeof(MODULE_HEALTH_STATUS));
  138. if ( tData ) {
  139. tData->status = (*idx)->lvl;
  140. tData->platform_id = (*idx)->platform_id;
  141. char tT[128];
  142. sprintf( tT, "gen Event HEALTH %d", tData->status );
  143. LOG( tT );
  144. EVENT_CONTAINER *tEvt = createBasicEvent(0, EVT_CLASS_SYSTEM, EVT_SRV_MODULE_HEALTH, NULL, NULL, tData );
  145. if ( tEvt ) {
  146. basicEventHeap::getInstance()->broadcast( tEvt );
  147. } else free( tData );
  148. } else LOG_ERROR("No Data Fetched from DB");
  149. #ifdef DEBUG_EXTRA
  150. } else {
  151. char tT[128];
  152. sprintf( tT, "Zone Health is same %d",(*idx)->lvl );
  153. LOG( tT );
  154. #endif
  155. };
  156. };
  157. m_mMonitoringZonesProtector.unlock();
  158. return true;
  159. };
  160. void servaLifeMonitoring::m_fThreadProc( void ){
  161. m_bThreadStatus = TH_WORKING;
  162. uint32_t ots = time(NULL);
  163. while( m_bThreadStatus < TH_ENDED ) {
  164. uint32_t cts = time(NULL);
  165. if ( ( cts - m_nStartupTS ) > LM_SKEEP_MONITORING_SECONDS ){
  166. if ( ots != cts ) {
  167. ots = cts;
  168. // Taking to validate one record then return it to a queue
  169. HEALTH_MONITORING_ZONE * tCWZ = NULL;
  170. m_mMonitoringZonesProtector.lock();
  171. if ( m_vMonitoringZones.size() ) {
  172. tCWZ = m_vMonitoringZones.at(0);
  173. m_vMonitoringZones.erase(m_vMonitoringZones.begin());
  174. };
  175. m_mMonitoringZonesProtector.unlock();
  176. if ( tCWZ ) {
  177. for ( uint8_t i = 0; i < 4; i++ ) {
  178. if (tCWZ->module_monitored[i]) tCWZ->module_status[i] = validateModule( tCWZ->module_id[i] );
  179. };
  180. m_mMonitoringZonesProtector.lock();
  181. m_vMonitoringZones.push_back( tCWZ );
  182. m_mMonitoringZonesProtector.unlock();
  183. };
  184. };
  185. };
  186. SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  187. };
  188. };
  189. #undef LOG
  190. #undef LOG_ERROR