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.

208 lines
8.2 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. uint64_t min_id = ptr->module_id[0];
  72. for ( uint8_t i = 0; i < 4; i++ ) {
  73. if (ptr->module_monitored[i]) {
  74. if( ptr->module_status[i] > min_lvl ) {
  75. min_lvl = ptr->module_status[i];
  76. min_id = ptr->module_id[i];
  77. };
  78. };
  79. };
  80. if( min_lvl != ptr->lvl ) {
  81. retVal = false;
  82. ptr->lvl = min_lvl;
  83. ptr->lvl_id = min_id;
  84. };
  85. };
  86. return retVal;
  87. };
  88. uint8_t servaLifeMonitoring::validateModule( uint64_t module_id ){
  89. uint8_t retVal = 0;
  90. if ( module_id ) {
  91. uint32_t cts = time( NULL );
  92. uint32_t mts = m_pDBC->getCloudLTS(ARGUS_OBJECT_MODULE, module_id );
  93. uint8_t ms = m_pDBC->getCloudStatus( ARGUS_OBJECT_MODULE, module_id );
  94. if ( mts ) {
  95. if ((cts - mts) > 30) {
  96. retVal = ARGUS_LIFE_STATUS_LVL_4;
  97. } else {
  98. //Online - Check Status
  99. if( ms < ARGUS_CLOUD_STATUS_E_GENERIC ) {
  100. retVal = ARGUS_LIFE_STATUS_CLEAN;
  101. } else {
  102. switch ( ms ) {
  103. case ARGUS_CLOUD_STATUS_E_GENERIC: break;
  104. case ARGUS_CLOUD_STATUS_E_CAM_GENERIC:
  105. case ARGUS_CLOUD_STATUS_E_CAM_NC:
  106. case ARGUS_CLOUD_STATUS_E_CAM_NPTZ:
  107. case ARGUS_CLOUD_STATUS_E_CAM_NIM: retVal = ARGUS_LIFE_STATUS_LVL_1; break;
  108. case ARGUS_CLOUD_STATUS_E_SEN_GENERIC:
  109. case ARGUS_CLOUD_STATUS_E_US_LRF:
  110. case ARGUS_CLOUD_STATUS_E_BS_LRF: retVal = ARGUS_LIFE_STATUS_LVL_2; break;
  111. case ARGUS_CLOUD_STATUS_E_US_NC:
  112. case ARGUS_CLOUD_STATUS_E_BS_NC: retVal = ARGUS_LIFE_STATUS_LVL_3; break;
  113. default: retVal = ARGUS_LIFE_STATUS_LVL_3;
  114. };
  115. };
  116. };
  117. } else {
  118. retVal = ARGUS_LIFE_STATUS_LVL_4;
  119. };
  120. if ( retVal == ARGUS_LIFE_STATUS_LVL_4 ) {
  121. DBC_IS_MODULE_DATA *tData = m_pDBC->getModuleData( module_id );
  122. if ( tData ) {
  123. if( !pingIP4( tData->ip4 ) ) {
  124. #ifdef DEBUG_EXTRA
  125. LOG_ERROR("No Network Access to Platform!");
  126. #endif
  127. retVal = ARGUS_LIFE_STATUS_LVL_5;
  128. };
  129. free( tData );
  130. };
  131. };
  132. };
  133. return retVal;
  134. };
  135. bool servaLifeMonitoring::validate(){
  136. uint32_t cts = time(NULL);
  137. if ( ( cts - m_nStartupTS ) < LM_SKEEP_MONITORING_SECONDS ) return true;
  138. m_mMonitoringZonesProtector.lock();
  139. for ( V_HEALTH_MONITORING_ZONES::iterator idx = m_vMonitoringZones.begin(); idx != m_vMonitoringZones.end(); idx++ ) {
  140. if (!validateZone(*idx)) {
  141. LOG("Zone Health is Changed");
  142. MODULE_HEALTH_STATUS *tData = (MODULE_HEALTH_STATUS*) malloc(sizeof(MODULE_HEALTH_STATUS));
  143. if ( tData ) {
  144. tData->status = (*idx)->lvl;
  145. tData->module_id = (*idx)->lvl_id;
  146. tData->platform_id = (*idx)->platform_id;
  147. #ifdef DEBUG
  148. char tT[128];
  149. sprintf( tT, "gen Event HEALTH %d", tData->status );
  150. LOG( tT );
  151. #endif
  152. EVENT_CONTAINER *tEvt = createBasicEvent(0, EVT_CLASS_SYSTEM, EVT_SRV_MODULE_HEALTH, NULL, NULL, tData );
  153. if ( tEvt ) {
  154. basicEventHeap::getInstance()->broadcast( tEvt );
  155. } else free( tData );
  156. } else LOG_ERROR("No Data Fetched from DB");
  157. #ifdef DEBUG_EXTRA
  158. } else {
  159. char tT[128];
  160. sprintf( tT, "Zone Health is same %d",(*idx)->lvl );
  161. LOG( tT );
  162. #endif
  163. };
  164. };
  165. m_mMonitoringZonesProtector.unlock();
  166. return true;
  167. };
  168. void servaLifeMonitoring::m_fThreadProc( void ){
  169. m_bThreadStatus = TH_WORKING;
  170. uint32_t ots = time(NULL);
  171. while( m_bThreadStatus < TH_ENDED ) {
  172. uint32_t cts = time(NULL);
  173. if ( ( cts - m_nStartupTS ) > LM_SKEEP_MONITORING_SECONDS ){
  174. if ( ots != cts ) {
  175. ots = cts;
  176. // Taking to validate one record then return it to a queue
  177. HEALTH_MONITORING_ZONE * tCWZ = NULL;
  178. m_mMonitoringZonesProtector.lock();
  179. if ( m_vMonitoringZones.size() ) {
  180. tCWZ = m_vMonitoringZones.at(0);
  181. m_vMonitoringZones.erase(m_vMonitoringZones.begin());
  182. };
  183. m_mMonitoringZonesProtector.unlock();
  184. if ( tCWZ ) {
  185. for ( uint8_t i = 0; i < 4; i++ ) {
  186. if (tCWZ->module_monitored[i]) tCWZ->module_status[i] = validateModule( tCWZ->module_id[i] );
  187. };
  188. m_mMonitoringZonesProtector.lock();
  189. m_vMonitoringZones.push_back( tCWZ );
  190. m_mMonitoringZonesProtector.unlock();
  191. };
  192. };
  193. };
  194. SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  195. };
  196. };
  197. #undef LOG
  198. #undef LOG_ERROR