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.

238 lines
8.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "serverMySQL_dataBinder.h"
  2. #define LOG( X ) m_pLog->Log( "MDBC", this, X )
  3. #ifdef DEBUG
  4. #define LOG_ERROR( X ) {\
  5. char _tBfrErrMSG2w34[256];\
  6. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  7. m_pLog->Log( "MDBC", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  8. }
  9. #else
  10. #define LOG_ERROR( X ) m_pLog->Log( "MDBC", this, _LOG_ERROR, X );
  11. #endif
  12. servaMySQLDBC *servaMySQLDBC::_instance = NULL;
  13. /**
  14. * Returns the single instance of the object.
  15. */
  16. servaMySQLDBC* servaMySQLDBC::getInstance( config_setting_t * Settings ) {
  17. // check if the instance has been created yet
  18. if( 0 == _instance ){
  19. // if not, then create it
  20. _instance = new servaMySQLDBC( Settings );
  21. };
  22. // return the single instance
  23. return _instance;
  24. };
  25. servaMySQLDBC::servaMySQLDBC( config_setting_t * Settings ):serverDataBinder(){
  26. m_pLog = basicLogCollection::getInstance();
  27. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "DBC" );
  28. m_bIsConnected = false;
  29. m_pDatabase = NULL;
  30. };
  31. servaMySQLDBC::~servaMySQLDBC(){
  32. m_bIsConnected = false;
  33. if( m_pDatabase != NULL ) delete( m_pDatabase );
  34. m_pDatabase = NULL;
  35. _instance = NULL;
  36. };
  37. bool servaMySQLDBC::connect( void ){
  38. config_setting_t * tSetting;
  39. m_bIsConnected = false;
  40. if( m_pSettings!= NULL ) {
  41. tSetting = config_setting_get_member( m_pSettings, "DATABASE_HOST" );
  42. if( tSetting == NULL ){
  43. tSetting = config_setting_add(m_pSettings, "DATABASE_HOST", CONFIG_TYPE_STRING );
  44. config_setting_set_string( tSetting, "localhost" );
  45. basicSettingsProvider::getInstance()->compromise();
  46. LOG("No DB Host Setting! Using default!");
  47. };
  48. sprintf(m_pDbHost, "%s", config_setting_get_string(tSetting));
  49. tSetting = config_setting_get_member(m_pSettings, "DATABASE_NAME");
  50. if( tSetting == NULL ){
  51. tSetting = config_setting_add(m_pSettings, "DATABASE_NAME", CONFIG_TYPE_STRING );
  52. config_setting_set_string( tSetting, "dfsl_gunter" );
  53. basicSettingsProvider::getInstance()->compromise();
  54. LOG("No DB Database Setting! Using default!");
  55. };
  56. sprintf(m_pDbDatabase, "%s", config_setting_get_string(tSetting));
  57. tSetting = config_setting_get_member( m_pSettings, "DATABASE_USER" );
  58. if( tSetting == NULL ){
  59. tSetting = config_setting_add(m_pSettings, "DATABASE_USER", CONFIG_TYPE_STRING );
  60. config_setting_set_string( tSetting, "dbroot" );
  61. basicSettingsProvider::getInstance()->compromise();
  62. LOG("No DB User Setting! Using default!");
  63. };
  64. sprintf(m_pDbUsername, "%s", config_setting_get_string(tSetting));
  65. tSetting = config_setting_get_member( m_pSettings, "DATABASE_PASSWORD" );
  66. if( tSetting == NULL ){
  67. tSetting = config_setting_add(m_pSettings, "DATABASE_PASSWORD", CONFIG_TYPE_STRING );
  68. config_setting_set_string( tSetting, "123567" );
  69. basicSettingsProvider::getInstance()->compromise();
  70. LOG("No DB Password Setting! Using default!");
  71. };
  72. sprintf(m_pDbPassword, "%s", config_setting_get_string(tSetting));
  73. basicSettingsProvider::getInstance()->validate();
  74. m_pDatabase = new MySQL_Connector();
  75. if( m_pDatabase == NULL )return false;
  76. m_bIsConnected = m_pDatabase->Connect( m_pDbDatabase, m_pDbHost, m_pDbUsername, m_pDbPassword );
  77. } else {
  78. LOG_ERROR("No Settings Provided!");
  79. };
  80. return m_bIsConnected;
  81. };
  82. bool servaMySQLDBC::putIntoLog( char *Msg ){
  83. if(m_bIsConnected){
  84. char tQBuf[MAX_PATH_LEN];
  85. sprintf( tQBuf, "INSERT INTO `log` SET `ts`=NOW(), `msg`='%s'", Msg );
  86. m_pDatabase->SilentQuery( tQBuf );
  87. };
  88. };
  89. bool servaMySQLDBC::registerServer( uint32_t SiteID, CSTR identity, int PID, uint32_t *ServID, uint32_t *ServNode ){
  90. if(m_bIsConnected){
  91. //m_nSiteID = SiteID;
  92. char tQBuf[MAX_PATH_LEN];
  93. sprintf( tQBuf, "INSERT INTO servers SET `site_id`=%d, `identity`='%s', `version_string`='%s', `online`=1, `ts`=UNIX_TIMESTAMP(now()), pid=%d ON DUPLICATE KEY UPDATE `version_string`='%s', `ts`=UNIX_TIMESTAMP(now()), `online`=1, pid=%d",
  94. SiteID, identity, _BUILD_STRING_, PID, _BUILD_STRING_, PID );
  95. #ifdef DEBUG
  96. LOG(tQBuf);
  97. #endif
  98. m_pDatabase->SilentQuery( tQBuf );
  99. //*ServID = getServerID( identity );
  100. //m_nServerID = *ServID;
  101. if( *ServID ) {
  102. //sprintf(tQBuf,"INSERT INTO nodes SET `site_id`=%d, `aux_id`='%d', `type`= 0, `online`=1, `name`='Server Node' ON DUPLICATE KEY UPDATE `online`=1", m_nSiteID, *ServID );
  103. #ifdef DEBUG_ALL
  104. LOG(tQBuf);
  105. #endif
  106. *ServNode = ( uint32_t ) m_pDatabase->QueryID( tQBuf );
  107. //if( *ServNode == 0 ){
  108. // *ServNode = getServerNode( *ServID );
  109. //};
  110. //m_nServerNodeID = *ServNode;
  111. // Clenup Nodes and services ...
  112. sprintf(tQBuf,"UPDATE nodes SET `online`=0 WHERE `server_id`=%d ", *ServID );
  113. #ifdef DEBUG_ALL
  114. LOG(tQBuf);
  115. #endif
  116. m_pDatabase->SilentQuery( tQBuf );
  117. sprintf(tQBuf,"UPDATE cameras SET `online`=0 WHERE `site_id`=%d ", SiteID );
  118. #ifdef DEBUG_ALL
  119. LOG(tQBuf);
  120. #endif
  121. m_pDatabase->SilentQuery( tQBuf );
  122. sprintf(tQBuf,"UPDATE media_streams SET `valid`=0 WHERE `site_id`=%d ", SiteID );
  123. #ifdef DEBUG_ALL
  124. LOG(tQBuf);
  125. #endif
  126. m_pDatabase->SilentQuery( tQBuf );
  127. sprintf(tQBuf,"UPDATE media_server_streams SET `valid`=0, `online`=0 WHERE `site_id`=%d ", SiteID );
  128. #ifdef DEBUG_ALL
  129. LOG(tQBuf);
  130. #endif
  131. m_pDatabase->SilentQuery( tQBuf );
  132. //cleanResources( tServerNodeID );
  133. return setNodeOnline( 0, 1 );//m_nServerNodeID );
  134. }
  135. };
  136. return false;
  137. };
  138. bool servaMySQLDBC::unregisterServer(){
  139. if( setNodeOffline( 0 ) ) { //m_nServerNodeID ) ){
  140. char tQBuf[CTEXT];
  141. sprintf(tQBuf,"UPDATE servers SET `online`=0, `ts`=UNIX_TIMESTAMP(now()) WHERE `id`=%d", 1 );//m_nServerID );
  142. #ifdef DEBUG_ALL
  143. LOG(tQBuf);
  144. #endif
  145. m_pDatabase->SilentQuery( tQBuf );
  146. return true;
  147. };
  148. return false;
  149. };
  150. bool servaMySQLDBC::setNodeOnline( uint32_t NodeID, uint8_t status ){
  151. if(m_bIsConnected){
  152. char tQBuf[CTEXT];
  153. if( NodeID ) {
  154. sprintf(tQBuf,"UPDATE nodes SET `online`=%d WHERE `id`=%d", status, NodeID );
  155. #ifdef DEBUG_ALL
  156. LOG(tQBuf);
  157. #endif
  158. m_pDatabase->SilentQuery( tQBuf );
  159. sprintf(tQBuf,"UPDATE `resources` SET `active`=1 WHERE `node_id`=%d", NodeID );
  160. #ifdef DEBUG_ALL
  161. LOG(tQBuf);
  162. #endif
  163. m_pDatabase->SilentQuery( tQBuf );
  164. return true;
  165. };
  166. };
  167. return false;
  168. };
  169. bool servaMySQLDBC::setNodeOffline( uint32_t NodeID ){
  170. if(m_bIsConnected){
  171. char tQBuf[CTEXT];
  172. if( NodeID ) {
  173. sprintf(tQBuf,"UPDATE nodes SET `online`=0 WHERE `id` = %d", NodeID );
  174. #ifdef DEBUG_ALL
  175. LOG(tQBuf);
  176. #endif
  177. m_pDatabase->SilentQuery( tQBuf );
  178. sprintf(tQBuf,"UPDATE `resources` SET `active`=0 WHERE `node_id` = %d", NodeID );
  179. #ifdef DEBUG_ALL
  180. LOG(tQBuf);
  181. #endif
  182. m_pDatabase->SilentQuery( tQBuf );
  183. return true;
  184. };
  185. };
  186. return false;
  187. };
  188. //bool servaMySQLDBC::updateServerUtilisation( uint32_t ServerID, uint32_t nCores, float cpu1, float cpu5, float cpu15, float mem, float disk, uint64_t dtsize, uint64_t dcsize, uint64_t mtsize, uint64_t mcsize ){
  189. bool servaMySQLDBC::updateServerUtilisation( DBC_SYS_LOAD_INFO *ptr ){
  190. if( m_bIsConnected ) {
  191. char tQBuf[MAX_PATH_LEN];
  192. sprintf( tQBuf, "UPDATE `servers` SET `ts`=UNIX_TIMESTAMP(now()), `cpu_cores`=%d, `stat_cpu1`=%0.2f, `stat_cpu5`=%0.2f, `stat_cpu15`=%0.2f, `stat_mem`=%0.2f, `stat_disk`=%0.2f, `temp0`=%0.1f, `val_mem_tottal`=%" PRIu64 ", `val_mem_current`=%" PRIu64 ", `val_disk_tottal`=%" PRIu64 ", `val_disk_current`=%" PRIu64 " WHERE `id`=%d",
  193. ptr->CPU_CORES,
  194. ptr->cpu1,
  195. ptr->cpu5,
  196. ptr->cpu15,
  197. ptr->mem,
  198. ptr->disk,
  199. ptr->temperature,
  200. ptr->T_DISK,
  201. ptr->C_DISK,
  202. ptr->T_MEM,
  203. ptr->C_MEM,
  204. 0//m_nServerID
  205. );
  206. #ifdef DEBUG_ALL
  207. LOG(tQBuf);
  208. #endif
  209. m_pDatabase->SilentQuery( tQBuf );
  210. return true;
  211. };
  212. return false;
  213. };
  214. #undef LOG
  215. #undef LOG_ERROR