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.

312 lines
8.0 KiB

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