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.

215 lines
8.4 KiB

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