sqlite_health.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "sqlite_health.h"
  3. #include "sqlite_functions.h"
  4. #define MAX_HEALTH_SQL_SIZE 2048
  5. /* Health related SQL queries
  6. Creates a health log table in sqlite, one per host guid
  7. */
  8. #define SQL_CREATE_HEALTH_LOG_TABLE(guid) "CREATE TABLE IF NOT EXISTS health_log_%s(hostname text, unique_id int, alarm_id int, alarm_event_id int, config_hash_id blob, updated_by_id int, updates_id int, when_key int, duration int, non_clear_duration int, flags int, exec_run_timestamp int, delay_up_to_timestamp int, name text, chart text, family text, exec text, recipient text, source text, units text, info text, exec_code int, new_status real, old_status real, delay int, new_value double, old_value double, last_repeat int, class text, component text, type text);", guid
  9. int sql_create_health_log_table(RRDHOST *host) {
  10. int rc;
  11. char *err_msg = NULL, command[MAX_HEALTH_SQL_SIZE + 1];
  12. if (unlikely(!db_meta)) {
  13. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  14. error_report("HEALTH [%s]: Database has not been initialized", host->hostname);
  15. return 1;
  16. }
  17. char uuid_str[GUID_LEN + 1];
  18. uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
  19. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_CREATE_HEALTH_LOG_TABLE(uuid_str));
  20. rc = sqlite3_exec(db_meta, command, 0, 0, &err_msg);
  21. if (rc != SQLITE_OK) {
  22. error_report("HEALTH [%s]: SQLite error during creation of health log table, rc = %d (%s)", host->hostname, rc, err_msg);
  23. sqlite3_free(err_msg);
  24. return 1;
  25. }
  26. snprintfz(command, MAX_HEALTH_SQL_SIZE, "CREATE INDEX IF NOT EXISTS "
  27. "health_log_index_%s ON health_log_%s (unique_id); ", uuid_str, uuid_str);
  28. db_execute(command);
  29. return 0;
  30. }
  31. /* Health related SQL queries
  32. Updates an entry in the table
  33. */
  34. #define SQL_UPDATE_HEALTH_LOG(guid) "UPDATE health_log_%s set updated_by_id = ?, flags = ?, exec_run_timestamp = ?, exec_code = ? where unique_id = ?;", guid
  35. void sql_health_alarm_log_update(RRDHOST *host, ALARM_ENTRY *ae) {
  36. sqlite3_stmt *res = NULL;
  37. int rc;
  38. char command[MAX_HEALTH_SQL_SIZE + 1];
  39. if (unlikely(!db_meta)) {
  40. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  41. error_report("HEALTH [%s]: Database has not been initialized", host->hostname);
  42. return;
  43. }
  44. char uuid_str[GUID_LEN + 1];
  45. uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
  46. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_UPDATE_HEALTH_LOG(uuid_str));
  47. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  48. if (unlikely(rc != SQLITE_OK)) {
  49. error_report("HEALTH [%s]: Failed to prepare statement for SQL_UPDATE_HEALTH_LOG", host->hostname);
  50. return;
  51. }
  52. rc = sqlite3_bind_int64(res, 1, (sqlite3_int64) ae->updated_by_id);
  53. if (unlikely(rc != SQLITE_OK)) {
  54. error_report("Failed to bind updated_by_id parameter for SQL_UPDATE_HEALTH_LOG");
  55. goto failed;
  56. }
  57. rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) ae->flags);
  58. if (unlikely(rc != SQLITE_OK)) {
  59. error_report("Failed to bind flags parameter for SQL_UPDATE_HEALTH_LOG");
  60. goto failed;
  61. }
  62. rc = sqlite3_bind_int64(res, 3, (sqlite3_int64) ae->exec_run_timestamp);
  63. if (unlikely(rc != SQLITE_OK)) {
  64. error_report("Failed to bind exec_run_timestamp parameter for SQL_UPDATE_HEALTH_LOG");
  65. goto failed;
  66. }
  67. rc = sqlite3_bind_int(res, 4, ae->exec_code);
  68. if (unlikely(rc != SQLITE_OK)) {
  69. error_report("Failed to bind exec_code parameter for SQL_UPDATE_HEALTH_LOG");
  70. goto failed;
  71. }
  72. rc = sqlite3_bind_int64(res, 5, (sqlite3_int64) ae->unique_id);
  73. if (unlikely(rc != SQLITE_OK)) {
  74. error_report("Failed to bind unique_id parameter for SQL_UPDATE_HEALTH_LOG");
  75. goto failed;
  76. }
  77. rc = execute_insert(res);
  78. if (unlikely(rc != SQLITE_DONE)) {
  79. error_report("HEALTH [%s]: Failed to update health log, rc = %d", host->hostname, rc);
  80. }
  81. failed:
  82. if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
  83. error_report("HEALTH [%s]: Failed to finalize the prepared statement for updating health log.", host->hostname);
  84. return;
  85. }
  86. /* Health related SQL queries
  87. Inserts an entry in the table
  88. */
  89. #define SQL_INSERT_HEALTH_LOG(guid) "INSERT INTO health_log_%s(hostname, unique_id, alarm_id, alarm_event_id, " \
  90. "config_hash_id, updated_by_id, updates_id, when_key, duration, non_clear_duration, flags, " \
  91. "exec_run_timestamp, delay_up_to_timestamp, name, chart, family, exec, recipient, source, " \
  92. "units, info, exec_code, new_status, old_status, delay, new_value, old_value, last_repeat, " \
  93. "class, component, type) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);", guid
  94. void sql_health_alarm_log_insert(RRDHOST *host, ALARM_ENTRY *ae) {
  95. sqlite3_stmt *res = NULL;
  96. int rc;
  97. char command[MAX_HEALTH_SQL_SIZE + 1];
  98. if (unlikely(!db_meta)) {
  99. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  100. error_report("HEALTH [%s]: Database has not been initialized", host->hostname);
  101. return;
  102. }
  103. char uuid_str[GUID_LEN + 1];
  104. uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
  105. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_INSERT_HEALTH_LOG(uuid_str));
  106. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  107. if (unlikely(rc != SQLITE_OK)) {
  108. error_report("HEALTH [%s]: Failed to prepare statement for SQL_INSERT_HEALTH_LOG", host->hostname);
  109. return;
  110. }
  111. rc = sqlite3_bind_text(res, 1, host->hostname, -1, SQLITE_STATIC);
  112. if (unlikely(rc != SQLITE_OK)) {
  113. error_report("Failed to bind hostname parameter for SQL_INSERT_HEALTH_LOG");
  114. goto failed;
  115. }
  116. rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) ae->unique_id);
  117. if (unlikely(rc != SQLITE_OK)) {
  118. error_report("Failed to bind unique_id parameter for SQL_INSERT_HEALTH_LOG");
  119. goto failed;
  120. }
  121. rc = sqlite3_bind_int64(res, 3, (sqlite3_int64) ae->alarm_id);
  122. if (unlikely(rc != SQLITE_OK)) {
  123. error_report("Failed to bind alarm_id parameter for SQL_INSERT_HEALTH_LOG");
  124. goto failed;
  125. }
  126. rc = sqlite3_bind_int64(res, 4, (sqlite3_int64) ae->alarm_event_id);
  127. if (unlikely(rc != SQLITE_OK)) {
  128. error_report("Failed to bind alarm_event_id parameter for SQL_INSERT_HEALTH_LOG");
  129. goto failed;
  130. }
  131. rc = sqlite3_bind_blob(res, 5, &ae->config_hash_id, sizeof(ae->config_hash_id), SQLITE_STATIC);
  132. if (unlikely(rc != SQLITE_OK)) {
  133. error_report("Failed to bind config_hash_id parameter for SQL_INSERT_HEALTH_LOG");
  134. goto failed;
  135. }
  136. rc = sqlite3_bind_int64(res, 6, (sqlite3_int64) ae->updated_by_id);
  137. if (unlikely(rc != SQLITE_OK)) {
  138. error_report("Failed to bind updated_by_id parameter for SQL_INSERT_HEALTH_LOG");
  139. goto failed;
  140. }
  141. rc = sqlite3_bind_int64(res, 7, (sqlite3_int64) ae->updates_id);
  142. if (unlikely(rc != SQLITE_OK)) {
  143. error_report("Failed to bind updates_id parameter for SQL_INSERT_HEALTH_LOG");
  144. goto failed;
  145. }
  146. rc = sqlite3_bind_int64(res, 8, (sqlite3_int64) ae->when);
  147. if (unlikely(rc != SQLITE_OK)) {
  148. error_report("Failed to bind when parameter for SQL_INSERT_HEALTH_LOG");
  149. goto failed;
  150. }
  151. rc = sqlite3_bind_int64(res, 9, (sqlite3_int64) ae->duration);
  152. if (unlikely(rc != SQLITE_OK)) {
  153. error_report("Failed to bind duration parameter for SQL_INSERT_HEALTH_LOG");
  154. goto failed;
  155. }
  156. rc = sqlite3_bind_int64(res, 10, (sqlite3_int64) ae->non_clear_duration);
  157. if (unlikely(rc != SQLITE_OK)) {
  158. error_report("Failed to bind non_clear_duration parameter for SQL_INSERT_HEALTH_LOG");
  159. goto failed;
  160. }
  161. rc = sqlite3_bind_int64(res, 11, (sqlite3_int64) ae->flags);
  162. if (unlikely(rc != SQLITE_OK)) {
  163. error_report("Failed to bind flags parameter for SQL_INSERT_HEALTH_LOG");
  164. goto failed;
  165. }
  166. rc = sqlite3_bind_int64(res, 12, (sqlite3_int64) ae->exec_run_timestamp);
  167. if (unlikely(rc != SQLITE_OK)) {
  168. error_report("Failed to bind exec_run_timestamp parameter for SQL_INSERT_HEALTH_LOG");
  169. goto failed;
  170. }
  171. rc = sqlite3_bind_int64(res, 13, (sqlite3_int64) ae->delay_up_to_timestamp);
  172. if (unlikely(rc != SQLITE_OK)) {
  173. error_report("Failed to bind delay_up_to_timestamp parameter for SQL_INSERT_HEALTH_LOG");
  174. goto failed;
  175. }
  176. rc = sqlite3_bind_text(res, 14, ae->name, -1, SQLITE_STATIC);
  177. if (unlikely(rc != SQLITE_OK)) {
  178. error_report("Failed to bind name parameter for SQL_INSERT_HEALTH_LOG");
  179. goto failed;
  180. }
  181. rc = sqlite3_bind_text(res, 15, ae->chart, -1, SQLITE_STATIC);
  182. if (unlikely(rc != SQLITE_OK)) {
  183. error_report("Failed to bind chart parameter for SQL_INSERT_HEALTH_LOG");
  184. goto failed;
  185. }
  186. rc = sqlite3_bind_text(res, 16, ae->family, -1, SQLITE_STATIC);
  187. if (unlikely(rc != SQLITE_OK)) {
  188. error_report("Failed to bind family parameter for SQL_INSERT_HEALTH_LOG");
  189. goto failed;
  190. }
  191. rc = sqlite3_bind_text(res, 17, ae->exec, -1, SQLITE_STATIC);
  192. if (unlikely(rc != SQLITE_OK)) {
  193. error_report("Failed to bind exec parameter for SQL_INSERT_HEALTH_LOG");
  194. goto failed;
  195. }
  196. rc = sqlite3_bind_text(res, 18, ae->recipient, -1, SQLITE_STATIC);
  197. if (unlikely(rc != SQLITE_OK)) {
  198. error_report("Failed to bind recipient parameter for SQL_INSERT_HEALTH_LOG");
  199. goto failed;
  200. }
  201. rc = sqlite3_bind_text(res, 19, ae->source, -1, SQLITE_STATIC);
  202. if (unlikely(rc != SQLITE_OK)) {
  203. error_report("Failed to bind source parameter for SQL_INSERT_HEALTH_LOG");
  204. goto failed;
  205. }
  206. rc = sqlite3_bind_text(res, 20, ae->units, -1, SQLITE_STATIC);
  207. if (unlikely(rc != SQLITE_OK)) {
  208. error_report("Failed to bind host_id parameter to store node instance information");
  209. goto failed;
  210. }
  211. rc = sqlite3_bind_text(res, 21, ae->info, -1, SQLITE_STATIC);
  212. if (unlikely(rc != SQLITE_OK)) {
  213. error_report("Failed to bind info parameter for SQL_INSERT_HEALTH_LOG");
  214. goto failed;
  215. }
  216. rc = sqlite3_bind_int(res, 22, ae->exec_code);
  217. if (unlikely(rc != SQLITE_OK)) {
  218. error_report("Failed to bind exec_code parameter for SQL_INSERT_HEALTH_LOG");
  219. goto failed;
  220. }
  221. rc = sqlite3_bind_int(res, 23, ae->new_status);
  222. if (unlikely(rc != SQLITE_OK)) {
  223. error_report("Failed to bind new_status parameter for SQL_INSERT_HEALTH_LOG");
  224. goto failed;
  225. }
  226. rc = sqlite3_bind_int(res, 24, ae->old_status);
  227. if (unlikely(rc != SQLITE_OK)) {
  228. error_report("Failed to bind old_status parameter for SQL_INSERT_HEALTH_LOG");
  229. goto failed;
  230. }
  231. rc = sqlite3_bind_int(res, 25, ae->delay);
  232. if (unlikely(rc != SQLITE_OK)) {
  233. error_report("Failed to bind delay parameter for SQL_INSERT_HEALTH_LOG");
  234. goto failed;
  235. }
  236. rc = sqlite3_bind_double(res, 26, ae->new_value);
  237. if (unlikely(rc != SQLITE_OK)) {
  238. error_report("Failed to bind new_value parameter for SQL_INSERT_HEALTH_LOG");
  239. goto failed;
  240. }
  241. rc = sqlite3_bind_double(res, 27, ae->old_value);
  242. if (unlikely(rc != SQLITE_OK)) {
  243. error_report("Failed to bind old_value parameter for SQL_INSERT_HEALTH_LOG");
  244. goto failed;
  245. }
  246. rc = sqlite3_bind_int64(res, 28, (sqlite3_int64) ae->last_repeat);
  247. if (unlikely(rc != SQLITE_OK)) {
  248. error_report("Failed to bind last_repeat parameter for SQL_INSERT_HEALTH_LOG");
  249. goto failed;
  250. }
  251. rc = sqlite3_bind_text(res, 29, ae->classification, -1, SQLITE_STATIC);
  252. if (unlikely(rc != SQLITE_OK)) {
  253. error_report("Failed to bind classification parameter for SQL_INSERT_HEALTH_LOG");
  254. goto failed;
  255. }
  256. rc = sqlite3_bind_text(res, 30, ae->component, -1, SQLITE_STATIC);
  257. if (unlikely(rc != SQLITE_OK)) {
  258. error_report("Failed to bind component parameter for SQL_INSERT_HEALTH_LOG");
  259. goto failed;
  260. }
  261. rc = sqlite3_bind_text(res, 31, ae->type, -1, SQLITE_STATIC);
  262. if (unlikely(rc != SQLITE_OK)) {
  263. error_report("Failed to bind type parameter for SQL_INSERT_HEALTH_LOG");
  264. goto failed;
  265. }
  266. rc = execute_insert(res);
  267. if (unlikely(rc != SQLITE_DONE)) {
  268. error_report("HEALTH [%s]: Failed to execute SQL_INSERT_HEALTH_LOG, rc = %d", host->hostname, rc);
  269. goto failed;
  270. }
  271. ae->flags |= HEALTH_ENTRY_FLAG_SAVED;
  272. host->health_log_entries_written++;
  273. failed:
  274. if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
  275. error_report("HEALTH [%s]: Failed to finalize the prepared statement for inserting to health log.", host->hostname);
  276. return;
  277. }
  278. void sql_health_alarm_log_save(RRDHOST *host, ALARM_ENTRY *ae)
  279. {
  280. if (ae->flags & HEALTH_ENTRY_FLAG_SAVED)
  281. sql_health_alarm_log_update(host, ae);
  282. else
  283. sql_health_alarm_log_insert(host, ae);
  284. }
  285. /* Health related SQL queries
  286. Cleans up the health_log table.
  287. */
  288. #define SQL_CLEANUP_HEALTH_LOG(guid,guid2,limit) "DELETE from health_log_%s where unique_id in (SELECT unique_id from health_log_%s order by unique_id asc LIMIT %lu);", guid, guid2, limit
  289. void sql_health_alarm_log_cleanup(RRDHOST *host) {
  290. sqlite3_stmt *res = NULL;
  291. static size_t rotate_every = 0;
  292. int rc;
  293. char command[MAX_HEALTH_SQL_SIZE + 1];
  294. if(unlikely(rotate_every == 0)) {
  295. rotate_every = (size_t)config_get_number(CONFIG_SECTION_HEALTH, "rotate log every lines", 2000);
  296. if(rotate_every < 100) rotate_every = 100;
  297. }
  298. if(likely(host->health_log_entries_written < rotate_every)) {
  299. return;
  300. }
  301. if (unlikely(!db_meta)) {
  302. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  303. error_report("Database has not been initialized");
  304. return;
  305. }
  306. char uuid_str[GUID_LEN + 1];
  307. uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
  308. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_CLEANUP_HEALTH_LOG(uuid_str, uuid_str, host->health_log_entries_written - rotate_every));
  309. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  310. if (unlikely(rc != SQLITE_OK)) {
  311. error_report("Failed to prepare statement to cleanup health log table");
  312. return;
  313. }
  314. rc = sqlite3_step(res);
  315. if (unlikely(rc != SQLITE_DONE))
  316. error_report("Failed to cleanup health log table, rc = %d", rc);
  317. rc = sqlite3_finalize(res);
  318. if (unlikely(rc != SQLITE_OK))
  319. error_report("Failed to finalize the prepared statement to cleanup health log table");
  320. host->health_log_entries_written = rotate_every;
  321. sql_aclk_alert_clean_dead_entries(host);
  322. }
  323. /* Health related SQL queries
  324. Get a count of rows from health log table
  325. */
  326. #define SQL_COUNT_HEALTH_LOG(guid) "SELECT count(1) FROM health_log_%s;", guid
  327. void sql_health_alarm_log_count(RRDHOST *host) {
  328. sqlite3_stmt *res = NULL;
  329. int rc;
  330. char command[MAX_HEALTH_SQL_SIZE + 1];
  331. if (unlikely(!db_meta)) {
  332. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  333. error_report("Database has not been initialized");
  334. return;
  335. }
  336. char uuid_str[GUID_LEN + 1];
  337. uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
  338. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_COUNT_HEALTH_LOG(uuid_str));
  339. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  340. if (unlikely(rc != SQLITE_OK)) {
  341. error_report("Failed to prepare statement to count health log entries from db");
  342. return;
  343. }
  344. rc = sqlite3_step(res);
  345. if (likely(rc == SQLITE_ROW))
  346. host->health_log_entries_written = (size_t) sqlite3_column_int64(res, 0);
  347. rc = sqlite3_finalize(res);
  348. if (unlikely(rc != SQLITE_OK))
  349. error_report("Failed to finalize the prepared statement to count health log entries from db");
  350. info("HEALTH [%s]: Table health_log_%s, contains %lu entries.", host->hostname, uuid_str, host->health_log_entries_written);
  351. }
  352. /* Health related SQL queries
  353. Load from the health log table
  354. */
  355. #define SQL_LOAD_HEALTH_LOG(guid,limit) "SELECT hostname, unique_id, alarm_id, alarm_event_id, config_hash_id, updated_by_id, updates_id, when_key, duration, non_clear_duration, flags, exec_run_timestamp, delay_up_to_timestamp, name, chart, family, exec, recipient, source, units, info, exec_code, new_status, old_status, delay, new_value, old_value, last_repeat, class, component, type FROM (SELECT hostname, unique_id, alarm_id, alarm_event_id, config_hash_id, updated_by_id, updates_id, when_key, duration, non_clear_duration, flags, exec_run_timestamp, delay_up_to_timestamp, name, chart, family, exec, recipient, source, units, info, exec_code, new_status, old_status, delay, new_value, old_value, last_repeat, class, component, type FROM health_log_%s order by unique_id desc limit %u) order by unique_id asc;", guid, limit
  356. void sql_health_alarm_log_load(RRDHOST *host) {
  357. sqlite3_stmt *res = NULL;
  358. int rc;
  359. ssize_t errored = 0, loaded = 0;
  360. char command[MAX_HEALTH_SQL_SIZE + 1];
  361. host->health_log_entries_written = 0;
  362. if (unlikely(!db_meta)) {
  363. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  364. error_report("HEALTH [%s]: Database has not been initialized", host->hostname);
  365. return;
  366. }
  367. char uuid_str[GUID_LEN + 1];
  368. uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
  369. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_LOAD_HEALTH_LOG(uuid_str, host->health_log.max));
  370. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  371. if (unlikely(rc != SQLITE_OK)) {
  372. error_report("HEALTH [%s]: Failed to prepare sql statement to load health log.", host->hostname);
  373. return;
  374. }
  375. netdata_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
  376. while (sqlite3_step(res) == SQLITE_ROW) {
  377. ALARM_ENTRY *ae = NULL;
  378. // check that we have valid ids
  379. uint32_t unique_id = (uint32_t) sqlite3_column_int64(res, 1);
  380. if(!unique_id) {
  381. error_report("HEALTH [%s]: Got invalid unique id. Ignoring it.", host->hostname);
  382. errored++;
  383. continue;
  384. }
  385. uint32_t alarm_id = (uint32_t) sqlite3_column_int64(res, 2);
  386. if(!alarm_id) {
  387. error_report("HEALTH [%s]: Got invalid alarm id. Ignoring it.", host->hostname);
  388. errored++;
  389. continue;
  390. }
  391. //need name, chart and family
  392. if (sqlite3_column_type(res, 13) == SQLITE_NULL) {
  393. error_report("HEALTH [%s]: Got null name field. Ignoring it.", host->hostname);
  394. errored++;
  395. continue;
  396. }
  397. if (sqlite3_column_type(res, 14) == SQLITE_NULL) {
  398. error_report("HEALTH [%s]: Got null chart field. Ignoring it.", host->hostname);
  399. errored++;
  400. continue;
  401. }
  402. if (sqlite3_column_type(res, 15) == SQLITE_NULL) {
  403. error_report("HEALTH [%s]: Got null family field. Ignoring it.", host->hostname);
  404. errored++;
  405. continue;
  406. }
  407. // Check if we got last_repeat field
  408. time_t last_repeat = 0;
  409. last_repeat = (time_t)sqlite3_column_int64(res, 27);
  410. RRDCALC *rc = alarm_max_last_repeat(host, (char *) sqlite3_column_text(res, 14), simple_hash((char *) sqlite3_column_text(res, 14)));
  411. if (!rc) {
  412. for(rc = host->alarms; rc ; rc = rc->next) {
  413. RRDCALC *rdcmp = (RRDCALC *) avl_insert_lock(&(host)->alarms_idx_name, (avl_t *)rc);
  414. if(rdcmp != rc) {
  415. error("Cannot insert the alarm index ID using log %s", rc->name);
  416. }
  417. }
  418. rc = alarm_max_last_repeat(host, (char *) sqlite3_column_text(res, 14), simple_hash((char *) sqlite3_column_text(res, 14)));
  419. }
  420. if(unlikely(rc)) {
  421. if (rrdcalc_isrepeating(rc)) {
  422. rc->last_repeat = last_repeat;
  423. // We iterate through repeating alarm entries only to
  424. // find the latest last_repeat timestamp. Otherwise,
  425. // there is no need to keep them in memory.
  426. continue;
  427. }
  428. }
  429. ae = callocz(1, sizeof(ALARM_ENTRY));
  430. ae->unique_id = unique_id;
  431. ae->alarm_id = alarm_id;
  432. if (sqlite3_column_type(res, 4) != SQLITE_NULL)
  433. uuid_copy(ae->config_hash_id, *((uuid_t *) sqlite3_column_blob(res, 4)));
  434. ae->alarm_event_id = (uint32_t) sqlite3_column_int64(res, 3);
  435. ae->updated_by_id = (uint32_t) sqlite3_column_int64(res, 5);
  436. ae->updates_id = (uint32_t) sqlite3_column_int64(res, 6);
  437. ae->when = (time_t) sqlite3_column_int64(res, 7);
  438. ae->duration = (time_t) sqlite3_column_int64(res, 8);
  439. ae->non_clear_duration = (time_t) sqlite3_column_int64(res, 9);
  440. ae->flags = (uint32_t) sqlite3_column_int64(res, 10);
  441. ae->flags |= HEALTH_ENTRY_FLAG_SAVED;
  442. ae->exec_run_timestamp = (time_t) sqlite3_column_int64(res, 11);
  443. ae->delay_up_to_timestamp = (time_t) sqlite3_column_int64(res, 12);
  444. ae->name = strdupz((char *) sqlite3_column_text(res, 13));
  445. ae->hash_name = simple_hash(ae->name);
  446. ae->chart = strdupz((char *) sqlite3_column_text(res, 14));
  447. ae->hash_chart = simple_hash(ae->chart);
  448. ae->family = strdupz((char *) sqlite3_column_text(res, 15));
  449. if (sqlite3_column_type(res, 16) != SQLITE_NULL)
  450. ae->exec = strdupz((char *) sqlite3_column_text(res, 16));
  451. else
  452. ae->exec = NULL;
  453. if (sqlite3_column_type(res, 17) != SQLITE_NULL)
  454. ae->recipient = strdupz((char *) sqlite3_column_text(res, 17));
  455. else
  456. ae->recipient = NULL;
  457. if (sqlite3_column_type(res, 18) != SQLITE_NULL)
  458. ae->source = strdupz((char *) sqlite3_column_text(res, 18));
  459. else
  460. ae->source = NULL;
  461. if (sqlite3_column_type(res, 19) != SQLITE_NULL)
  462. ae->units = strdupz((char *) sqlite3_column_text(res, 19));
  463. else
  464. ae->units = NULL;
  465. if (sqlite3_column_type(res, 20) != SQLITE_NULL)
  466. ae->info = strdupz((char *) sqlite3_column_text(res, 20));
  467. else
  468. ae->info = NULL;
  469. ae->exec_code = (int) sqlite3_column_int(res, 21);
  470. ae->new_status = (RRDCALC_STATUS) sqlite3_column_int(res, 22);
  471. ae->old_status = (RRDCALC_STATUS)sqlite3_column_int(res, 23);
  472. ae->delay = (int) sqlite3_column_int(res, 24);
  473. ae->new_value = (calculated_number) sqlite3_column_double(res, 25);
  474. ae->old_value = (calculated_number) sqlite3_column_double(res, 26);
  475. ae->last_repeat = last_repeat;
  476. if (sqlite3_column_type(res, 28) != SQLITE_NULL)
  477. ae->classification = strdupz((char *) sqlite3_column_text(res, 28));
  478. else
  479. ae->classification = NULL;
  480. if (sqlite3_column_type(res, 29) != SQLITE_NULL)
  481. ae->component = strdupz((char *) sqlite3_column_text(res, 29));
  482. else
  483. ae->component = NULL;
  484. if (sqlite3_column_type(res, 30) != SQLITE_NULL)
  485. ae->type = strdupz((char *) sqlite3_column_text(res, 30));
  486. else
  487. ae->type = NULL;
  488. char value_string[100 + 1];
  489. freez(ae->old_value_string);
  490. freez(ae->new_value_string);
  491. ae->old_value_string = strdupz(format_value_and_unit(value_string, 100, ae->old_value, ae->units, -1));
  492. ae->new_value_string = strdupz(format_value_and_unit(value_string, 100, ae->new_value, ae->units, -1));
  493. ae->next = host->health_log.alarms;
  494. host->health_log.alarms = ae;
  495. if(unlikely(ae->unique_id > host->health_max_unique_id))
  496. host->health_max_unique_id = ae->unique_id;
  497. if(unlikely(ae->alarm_id >= host->health_max_alarm_id))
  498. host->health_max_alarm_id = ae->alarm_id;
  499. loaded++;
  500. }
  501. netdata_rwlock_unlock(&host->health_log.alarm_log_rwlock);
  502. if(!host->health_max_unique_id) host->health_max_unique_id = (uint32_t)now_realtime_sec();
  503. if(!host->health_max_alarm_id) host->health_max_alarm_id = (uint32_t)now_realtime_sec();
  504. host->health_log.next_log_id = host->health_max_unique_id + 1;
  505. if (unlikely(!host->health_log.next_alarm_id || host->health_log.next_alarm_id <= host->health_max_alarm_id))
  506. host->health_log.next_alarm_id = host->health_max_alarm_id + 1;
  507. info("HEALTH [%s]: Table health_log_%s, loaded %zd alarm entries, errors in %zd entries.", host->hostname, uuid_str, loaded, errored);
  508. rc = sqlite3_finalize(res);
  509. if (unlikely(rc != SQLITE_OK))
  510. error_report("Failed to finalize the health log read statement");
  511. sql_health_alarm_log_count(host);
  512. }
  513. /*
  514. * Store an alert config hash in the database
  515. */
  516. #define SQL_STORE_ALERT_CONFIG_HASH "insert or replace into alert_hash (hash_id, date_updated, alarm, template, " \
  517. "on_key, class, component, type, os, hosts, lookup, every, units, calc, families, plugin, module, " \
  518. "charts, green, red, warn, crit, exec, to_key, info, delay, options, repeat, host_labels, " \
  519. "p_db_lookup_dimensions, p_db_lookup_method, p_db_lookup_options, p_db_lookup_after, " \
  520. "p_db_lookup_before, p_update_every) values (?1,strftime('%s'),?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12," \
  521. "?13,?14,?15,?16,?17,?18,?19,?20,?21,?22,?23,?24,?25,?26,?27,?28,?29,?30,?31,?32,?33,?34);"
  522. int sql_store_alert_config_hash(uuid_t *hash_id, struct alert_config *cfg)
  523. {
  524. static __thread sqlite3_stmt *res = NULL;
  525. int rc, param = 0;
  526. if (unlikely(!db_meta)) {
  527. if (default_rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE)
  528. return 0;
  529. error_report("Database has not been initialized");
  530. return 1;
  531. }
  532. if (unlikely(!res)) {
  533. rc = prepare_statement(db_meta, SQL_STORE_ALERT_CONFIG_HASH, &res);
  534. if (unlikely(rc != SQLITE_OK)) {
  535. error_report("Failed to prepare statement to store alert configuration, rc = %d", rc);
  536. return 1;
  537. }
  538. }
  539. param++;
  540. rc = sqlite3_bind_blob(res, 1, hash_id, sizeof(*hash_id), SQLITE_STATIC);
  541. if (unlikely(rc != SQLITE_OK))
  542. goto bind_fail;
  543. param++;
  544. if (cfg->alarm && *cfg->alarm)
  545. rc = sqlite3_bind_text(res, 2, cfg->alarm, -1, SQLITE_STATIC);
  546. else
  547. rc = sqlite3_bind_null(res, 2);
  548. if (unlikely(rc != SQLITE_OK))
  549. goto bind_fail;
  550. param++;
  551. if (cfg->template_key && *cfg->template_key)
  552. rc = sqlite3_bind_text(res, 3, cfg->template_key, -1, SQLITE_STATIC);
  553. else
  554. rc = sqlite3_bind_null(res, 3);
  555. if (unlikely(rc != SQLITE_OK))
  556. goto bind_fail;
  557. param++;
  558. rc = sqlite3_bind_text(res, 4, cfg->on, -1, SQLITE_STATIC);
  559. if (unlikely(rc != SQLITE_OK))
  560. goto bind_fail;
  561. param++;
  562. rc = sqlite3_bind_text(res, 5, cfg->classification, -1, SQLITE_STATIC);
  563. if (unlikely(rc != SQLITE_OK))
  564. goto bind_fail;
  565. param++;
  566. rc = sqlite3_bind_text(res, 6, cfg->component, -1, SQLITE_STATIC);
  567. if (unlikely(rc != SQLITE_OK))
  568. goto bind_fail;
  569. param++;
  570. rc = sqlite3_bind_text(res, 7, cfg->type, -1, SQLITE_STATIC);
  571. if (unlikely(rc != SQLITE_OK))
  572. goto bind_fail;
  573. param++;
  574. rc = sqlite3_bind_text(res, 8, cfg->os, -1, SQLITE_STATIC);
  575. if (unlikely(rc != SQLITE_OK))
  576. goto bind_fail;
  577. param++;
  578. rc = sqlite3_bind_text(res, 9, cfg->host, -1, SQLITE_STATIC);
  579. if (unlikely(rc != SQLITE_OK))
  580. goto bind_fail;
  581. param++;
  582. rc = sqlite3_bind_text(res, 10, cfg->lookup, -1, SQLITE_STATIC);
  583. if (unlikely(rc != SQLITE_OK))
  584. goto bind_fail;
  585. param++;
  586. rc = sqlite3_bind_text(res, 11, cfg->every, -1, SQLITE_STATIC);
  587. if (unlikely(rc != SQLITE_OK))
  588. goto bind_fail;
  589. param++;
  590. rc = sqlite3_bind_text(res, 12, cfg->units, -1, SQLITE_STATIC);
  591. if (unlikely(rc != SQLITE_OK))
  592. goto bind_fail;
  593. param++;
  594. rc = sqlite3_bind_text(res, 13, cfg->calc, -1, SQLITE_STATIC);
  595. if (unlikely(rc != SQLITE_OK))
  596. goto bind_fail;
  597. param++;
  598. rc = sqlite3_bind_text(res, 14, cfg->families, -1, SQLITE_STATIC);
  599. if (unlikely(rc != SQLITE_OK))
  600. goto bind_fail;
  601. param++;
  602. rc = sqlite3_bind_text(res, 15, cfg->plugin, -1, SQLITE_STATIC);
  603. if (unlikely(rc != SQLITE_OK))
  604. goto bind_fail;
  605. param++;
  606. rc = sqlite3_bind_text(res, 16, cfg->module, -1, SQLITE_STATIC);
  607. if (unlikely(rc != SQLITE_OK))
  608. goto bind_fail;
  609. param++;
  610. rc = sqlite3_bind_text(res, 17, cfg->charts, -1, SQLITE_STATIC);
  611. if (unlikely(rc != SQLITE_OK))
  612. goto bind_fail;
  613. param++;
  614. rc = sqlite3_bind_text(res, 18, cfg->green, -1, SQLITE_STATIC);
  615. if (unlikely(rc != SQLITE_OK))
  616. goto bind_fail;
  617. param++;
  618. rc = sqlite3_bind_text(res, 19, cfg->red, -1, SQLITE_STATIC);
  619. if (unlikely(rc != SQLITE_OK))
  620. goto bind_fail;
  621. param++;
  622. rc = sqlite3_bind_text(res, 20, cfg->warn, -1, SQLITE_STATIC);
  623. if (unlikely(rc != SQLITE_OK))
  624. goto bind_fail;
  625. param++;
  626. rc = sqlite3_bind_text(res, 21, cfg->crit, -1, SQLITE_STATIC);
  627. if (unlikely(rc != SQLITE_OK))
  628. goto bind_fail;
  629. param++;
  630. rc = sqlite3_bind_text(res, 22, cfg->exec, -1, SQLITE_STATIC);
  631. if (unlikely(rc != SQLITE_OK))
  632. goto bind_fail;
  633. param++;
  634. rc = sqlite3_bind_text(res, 23, cfg->to, -1, SQLITE_STATIC);
  635. if (unlikely(rc != SQLITE_OK))
  636. goto bind_fail;
  637. param++;
  638. rc = sqlite3_bind_text(res, 24, cfg->info, -1, SQLITE_STATIC);
  639. if (unlikely(rc != SQLITE_OK))
  640. goto bind_fail;
  641. param++;
  642. rc = sqlite3_bind_text(res, 25, cfg->delay, -1, SQLITE_STATIC);
  643. if (unlikely(rc != SQLITE_OK))
  644. goto bind_fail;
  645. param++;
  646. rc = sqlite3_bind_text(res, 26, cfg->options, -1, SQLITE_STATIC);
  647. if (unlikely(rc != SQLITE_OK))
  648. goto bind_fail;
  649. param++;
  650. rc = sqlite3_bind_text(res, 27, cfg->repeat, -1, SQLITE_STATIC);
  651. if (unlikely(rc != SQLITE_OK))
  652. goto bind_fail;
  653. param++;
  654. rc = sqlite3_bind_text(res, 28, cfg->host_labels, -1, SQLITE_STATIC);
  655. if (unlikely(rc != SQLITE_OK))
  656. goto bind_fail;
  657. if (cfg->p_db_lookup_after) {
  658. param++;
  659. rc = sqlite3_bind_text(res, 29, cfg->p_db_lookup_dimensions, -1, SQLITE_STATIC);
  660. if (unlikely(rc != SQLITE_OK))
  661. goto bind_fail;
  662. param++;
  663. rc = sqlite3_bind_text(res, 30, cfg->p_db_lookup_method, -1, SQLITE_STATIC);
  664. if (unlikely(rc != SQLITE_OK))
  665. goto bind_fail;
  666. param++;
  667. rc = sqlite3_bind_int(res, 31, cfg->p_db_lookup_options);
  668. if (unlikely(rc != SQLITE_OK))
  669. goto bind_fail;
  670. param++;
  671. rc = sqlite3_bind_int(res, 32, cfg->p_db_lookup_after);
  672. if (unlikely(rc != SQLITE_OK))
  673. goto bind_fail;
  674. param++;
  675. rc = sqlite3_bind_int(res, 33, cfg->p_db_lookup_before);
  676. if (unlikely(rc != SQLITE_OK))
  677. goto bind_fail;
  678. } else {
  679. param++;
  680. rc = sqlite3_bind_null(res, 29);
  681. if (unlikely(rc != SQLITE_OK))
  682. goto bind_fail;
  683. param++;
  684. rc = sqlite3_bind_null(res, 30);
  685. if (unlikely(rc != SQLITE_OK))
  686. goto bind_fail;
  687. param++;
  688. rc = sqlite3_bind_null(res, 31);
  689. if (unlikely(rc != SQLITE_OK))
  690. goto bind_fail;
  691. param++;
  692. rc = sqlite3_bind_null(res, 32);
  693. if (unlikely(rc != SQLITE_OK))
  694. goto bind_fail;
  695. param++;
  696. rc = sqlite3_bind_null(res, 33);
  697. if (unlikely(rc != SQLITE_OK))
  698. goto bind_fail;
  699. }
  700. param++;
  701. rc = sqlite3_bind_int(res, 34, cfg->p_update_every);
  702. if (unlikely(rc != SQLITE_OK))
  703. goto bind_fail;
  704. rc = execute_insert(res);
  705. if (unlikely(rc != SQLITE_DONE))
  706. error_report("Failed to store alert config, rc = %d", rc);
  707. rc = sqlite3_reset(res);
  708. if (unlikely(rc != SQLITE_OK))
  709. error_report("Failed to reset statement in alert hash_id store function, rc = %d", rc);
  710. return 0;
  711. bind_fail:
  712. error_report("Failed to bind parameter %d to store alert hash_id, rc = %d", param, rc);
  713. rc = sqlite3_reset(res);
  714. if (unlikely(rc != SQLITE_OK))
  715. error_report("Failed to reset statement in alert hash_id store function, rc = %d", rc);
  716. return 1;
  717. }
  718. /*
  719. alert hashes are used for cloud communication.
  720. if cloud is disabled or openssl is not available (which will prevent cloud connectivity)
  721. skip hash calculations
  722. */
  723. #if !defined DISABLE_CLOUD && defined ENABLE_HTTPS
  724. #define DIGEST_ALERT_CONFIG_VAL(v) ((v) ? EVP_DigestUpdate(evpctx, (v), strlen((v))) : EVP_DigestUpdate(evpctx, "", 1))
  725. #endif
  726. int alert_hash_and_store_config(
  727. uuid_t hash_id,
  728. struct alert_config *cfg,
  729. int store_hash)
  730. {
  731. #if !defined DISABLE_CLOUD && defined ENABLE_HTTPS
  732. EVP_MD_CTX *evpctx;
  733. unsigned char hash_value[EVP_MAX_MD_SIZE];
  734. unsigned int hash_len;
  735. evpctx = EVP_MD_CTX_create();
  736. EVP_DigestInit_ex(evpctx, EVP_sha256(), NULL);
  737. DIGEST_ALERT_CONFIG_VAL(cfg->alarm);
  738. DIGEST_ALERT_CONFIG_VAL(cfg->template_key);
  739. DIGEST_ALERT_CONFIG_VAL(cfg->os);
  740. DIGEST_ALERT_CONFIG_VAL(cfg->host);
  741. DIGEST_ALERT_CONFIG_VAL(cfg->on);
  742. DIGEST_ALERT_CONFIG_VAL(cfg->families);
  743. DIGEST_ALERT_CONFIG_VAL(cfg->plugin);
  744. DIGEST_ALERT_CONFIG_VAL(cfg->module);
  745. DIGEST_ALERT_CONFIG_VAL(cfg->charts);
  746. DIGEST_ALERT_CONFIG_VAL(cfg->lookup);
  747. DIGEST_ALERT_CONFIG_VAL(cfg->calc);
  748. DIGEST_ALERT_CONFIG_VAL(cfg->every);
  749. DIGEST_ALERT_CONFIG_VAL(cfg->green);
  750. DIGEST_ALERT_CONFIG_VAL(cfg->red);
  751. DIGEST_ALERT_CONFIG_VAL(cfg->warn);
  752. DIGEST_ALERT_CONFIG_VAL(cfg->crit);
  753. DIGEST_ALERT_CONFIG_VAL(cfg->exec);
  754. DIGEST_ALERT_CONFIG_VAL(cfg->to);
  755. DIGEST_ALERT_CONFIG_VAL(cfg->units);
  756. DIGEST_ALERT_CONFIG_VAL(cfg->info);
  757. DIGEST_ALERT_CONFIG_VAL(cfg->classification);
  758. DIGEST_ALERT_CONFIG_VAL(cfg->component);
  759. DIGEST_ALERT_CONFIG_VAL(cfg->type);
  760. DIGEST_ALERT_CONFIG_VAL(cfg->delay);
  761. DIGEST_ALERT_CONFIG_VAL(cfg->options);
  762. DIGEST_ALERT_CONFIG_VAL(cfg->repeat);
  763. DIGEST_ALERT_CONFIG_VAL(cfg->host_labels);
  764. EVP_DigestFinal_ex(evpctx, hash_value, &hash_len);
  765. EVP_MD_CTX_destroy(evpctx);
  766. fatal_assert(hash_len > sizeof(uuid_t));
  767. char uuid_str[GUID_LEN + 1];
  768. uuid_unparse_lower(*((uuid_t *)&hash_value), uuid_str);
  769. uuid_copy(hash_id, *((uuid_t *)&hash_value));
  770. /* store everything, so it can be recreated when not in memory or just a subset ? */
  771. if (store_hash)
  772. (void)sql_store_alert_config_hash( (uuid_t *)&hash_value, cfg);
  773. #else
  774. UNUSED(hash_id);
  775. UNUSED(cfg);
  776. UNUSED(store_hash);
  777. #endif
  778. return 1;
  779. }