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.

263 lines
9.0 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "mojoDBConnector.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. mojoDBC *mojoDBC::_instance = NULL;
  13. /**
  14. * Returns the single instance of the object.
  15. */
  16. mojoDBC *mojoDBC::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 mojoDBC( Settings );
  21. };
  22. // return the single instance
  23. return _instance;
  24. };
  25. mojoDBC::mojoDBC( config_setting_t * Settings ){
  26. m_pLog = basicLogCollection::getInstance();
  27. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "DATABASE" );
  28. m_bIsConnected = false;
  29. m_bIsServerLocal = true;
  30. m_pDataStorage = NULL;
  31. m_pDataMemory = NULL;
  32. };
  33. mojoDBC::~mojoDBC(){
  34. if ( m_pDataStorage ) delete( m_pDataStorage );
  35. if ( m_pDataMemory ) delete( m_pDataMemory );
  36. _instance = NULL;
  37. };
  38. bool mojoDBC::connect( void ){
  39. if ( m_pSettings == NULL) return false;
  40. bool bRecreateMemStructures = false;
  41. config_setting_t * pSetting;
  42. const char * tBuff;
  43. //Disconnect if Connected
  44. m_bIsConnected = false;
  45. char web_access_user[64];
  46. char web_access_group[64];
  47. //Apply New Config
  48. #ifdef DEBUG
  49. LOG( "Reading DB Settings ... " );
  50. #endif
  51. if(!config_setting_lookup_string(m_pSettings, "DATA_FILE_NAME", (const char**)&tBuff )){
  52. pSetting = config_setting_add(m_pSettings, "DATA_FILE_NAME", CONFIG_TYPE_STRING);
  53. sprintf( m_pDbFile, "data/mysqlitedb.db" );
  54. config_setting_set_string( pSetting, m_pDbFile );
  55. basicSettingsProvider::getInstance()->compromise();
  56. LOG("Data Storage not set - using default");
  57. } else {
  58. sprintf( m_pDbFile, tBuff );
  59. };
  60. if(!config_setting_lookup_string(m_pSettings, "WEB_ACCESS_USER", (const char**)&tBuff )){
  61. pSetting = config_setting_add(m_pSettings, "WEB_ACCESS_USER", CONFIG_TYPE_STRING);
  62. sprintf( web_access_user, "www-data" );
  63. config_setting_set_string( pSetting, web_access_user );
  64. basicSettingsProvider::getInstance()->compromise();
  65. LOG("Web Access User not set - using default");
  66. } else {
  67. sprintf( web_access_user, tBuff );
  68. };
  69. if(!config_setting_lookup_string(m_pSettings, "WEB_ACCESS_GROUP", (const char**)&tBuff )){
  70. pSetting = config_setting_add(m_pSettings, "WEB_ACCESS_GROUP", CONFIG_TYPE_STRING);
  71. sprintf( web_access_group, "www-data" );
  72. config_setting_set_string( pSetting, web_access_group );
  73. basicSettingsProvider::getInstance()->compromise();
  74. LOG("Web Access Group not set - using default");
  75. } else {
  76. sprintf( web_access_group, tBuff );
  77. };
  78. if( !fileExist( DB_SHMEM_POINT )) {
  79. bRecreateMemStructures = true;
  80. mkdir( DB_SHMEM_POINT, 0775 );// S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH );
  81. chown( DB_SHMEM_POINT, web_access_user, web_access_group ); //To make Accessed from PHP
  82. chmod( DB_SHMEM_POINT, 0775 );
  83. };
  84. if (!config_setting_lookup_string(m_pSettings, "DATA_MEMORY_NAME", (const char **)&tBuff)) {
  85. pSetting = config_setting_add(m_pSettings, "DATA_MEMORY_NAME", CONFIG_TYPE_STRING);
  86. sprintf( m_pDbMemory, "mem_db.db" );
  87. config_setting_set_string( pSetting, m_pDbMemory );
  88. basicSettingsProvider::getInstance()->compromise();
  89. LOG("Memory Storage not set - using default");
  90. };
  91. sprintf( m_pDbMemory, "%s/%s", DB_SHMEM_POINT, tBuff );
  92. basicSettingsProvider::getInstance()->validate();
  93. // Kill Connection if any
  94. if ( m_pDataStorage ) delete( m_pDataStorage );
  95. if ( m_pDataMemory ) delete( m_pDataMemory );
  96. #ifdef DEBUG
  97. LOG( m_pDbFile );
  98. #endif
  99. // Create New Connections
  100. m_pDataStorage = new SQLite_Connector();
  101. if ( m_pDataStorage ) {
  102. if( m_pDataStorage->Connect( m_pDbFile, 0, true ) ){
  103. #ifdef DEBUG
  104. LOG("DB File Opened");
  105. #endif
  106. chown( m_pDbFile, web_access_user, web_access_group );
  107. chmod( m_pDbFile, 0775 );
  108. } else{
  109. LOG_ERROR("Cannot Connect to DB File");
  110. delete( m_pDataStorage );
  111. m_pDataStorage = NULL;
  112. LOG("Storage Deleted!");
  113. return false;
  114. };
  115. };
  116. #ifdef DEBUG
  117. LOG( m_pDbMemory );
  118. #endif
  119. if( !fileExist( m_pDbMemory )) {
  120. bRecreateMemStructures = true;
  121. };
  122. m_pDataMemory = new SQLite_Connector();
  123. if ( m_pDataMemory ) {
  124. if( m_pDataMemory->Connect( m_pDbMemory, 0, true ) ){
  125. #ifdef DEBUG
  126. LOG("DB Memory Opened");
  127. #endif
  128. chown( m_pDbMemory, web_access_user, web_access_group );
  129. chmod( m_pDbMemory, 0775 );
  130. if ( bRecreateMemStructures ) initMemDB();
  131. } else{
  132. LOG_ERROR("Cannot Connect to DB Memory");
  133. delete( m_pDataStorage );
  134. delete( m_pDataMemory );
  135. m_pDataStorage = NULL;
  136. m_pDataMemory = NULL;
  137. return false;
  138. };
  139. };
  140. m_bIsConnected = true;
  141. return m_bIsConnected;
  142. };
  143. void mojoDBC::initMemDB( void ){
  144. m_pDataMemory->Query("CREATE TABLE \"nodes_qcq\" ( \
  145. \"id\" INTEGER NOT NULL,\
  146. \"node_id\" INTEGER NOT NULL,\
  147. \"cmd\" INTEGER NOT NULL,\
  148. \"status\" INTEGER DEFAULT 0,\
  149. \"parameter\" TEXT,\
  150. PRIMARY KEY(\"id\" AUTOINCREMENT)\
  151. );");
  152. m_pDataMemory->Query("CREATE TABLE \"servers\" (\
  153. \"id\" INTEGER NOT NULL,\
  154. \"online\" INTEGER DEFAULT 0,\
  155. \"ts\" INTEGER DEFAULT 0,\
  156. \"version_string\" TEXT,\
  157. \"identity\" TEXT,\
  158. \"pid\" INTEGER DEFAULT 0,\
  159. \"cpu_cores\" INTEGER DEFAULT 0.0,\
  160. \"stat_cpu1\" REAL DEFAULT 0.0,\
  161. \"stat_cpu5\" REAL DEFAULT 0.0,\
  162. \"stat_cpu15\" REAL DEFAULT 0.0,\
  163. \"stat_mem\" REAL DEFAULT 0.0,\
  164. \"stat_disk\" REAL DEFAULT 0.0,\
  165. \"val_mem_tottal\" INTEGER DEFAULT 0,\
  166. \"val_mem_current\" INTEGER DEFAULT 0,\
  167. \"val_disk_tottal\" INTEGER DEFAULT 0,\
  168. \"val_disk_current\" INTEGER DEFAULT 0,\
  169. \"temp0\" REAL DEFAULT 0.0,\
  170. PRIMARY KEY(\"id\" AUTOINCREMENT)\
  171. );");
  172. };
  173. DBC_PUSHED_CMD *mojoDBC::getNodePushedCMD( uint32_t NodeID ){
  174. DBC_PUSHED_CMD * RetVal = NULL;
  175. if ( m_bIsConnected ) {
  176. char QS[MAX_PATH];
  177. AAssoc * result;
  178. sprintf( QS, "select * FROM `nodes_qcq` WHERE `node_id`=%d AND `status`=0 ORDER BY `id` LIMIT 1", NodeID );
  179. result = m_pDataMemory->Query( QS );
  180. if ( result ) {
  181. if( result->size() > 0 ) {
  182. RetVal = (DBC_PUSHED_CMD*)malloc(sizeof(DBC_PUSHED_CMD));
  183. if( RetVal ) {
  184. Assoc * cArr;
  185. Assoc::iterator it;
  186. memset( RetVal, 0, sizeof(DBC_PUSHED_CMD));
  187. cArr = result->at(0);
  188. it = cArr->find("id");
  189. if( it != cArr->end() ){
  190. RetVal->id = atoi( it->second.c_str() );
  191. };
  192. it = cArr->find("cmd");
  193. if( it != cArr->end() ){
  194. RetVal->CMD = atoi( it->second.c_str() );
  195. };
  196. it = cArr->find("parameter");
  197. if( it != cArr->end() ){
  198. strncpy( RetVal->PARAM, it->second.c_str(), 256 );
  199. RetVal->PARAM[255] = (char)0;
  200. };
  201. };
  202. };
  203. };
  204. m_pDataMemory->deleteResult( result );
  205. };
  206. return RetVal;
  207. };
  208. bool mojoDBC::updateNodePushedCMD( uint32_t CmdID, uint8_t status ){
  209. if ( m_bIsConnected ) {
  210. char QS[MAX_PATH];
  211. sprintf( QS, "UPDATE `nodes_qcq` SET `status`=%d WHERE `id`=%d ", status, CmdID );
  212. return m_pDataMemory->SilentQuery( QS );
  213. };
  214. return false;
  215. };
  216. bool mojoDBC::cleanNodePushedCMD( void ){
  217. if ( m_bIsConnected ) {
  218. return m_pDataMemory->SilentQuery( "DELETE FROM `nodes_qcq` WHERE `status` > 200" );
  219. };
  220. return false;
  221. };
  222. /********************************************************************
  223. * *
  224. * Slow DB Functions *
  225. * *
  226. ********************************************************************/
  227. DBC_NET_IF_DATA *mojoDBC::getNIF_Data( uint32_t ID ){
  228. };
  229. uint32_t mojoDBC::getNIF_ID( CSTR key ){
  230. };
  231. uint32_t mojoDBC::getNIF_IDbyDevice( CSTR device ){
  232. };
  233. #undef LOG
  234. #undef LOG_ERROR