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.

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