sqlite_health.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "sqlite_health.h"
  3. #include "sqlite_functions.h"
  4. #include "sqlite_db_migration.h"
  5. #define MAX_HEALTH_SQL_SIZE 2048
  6. #define sqlite3_bind_string_or_null(res,key,param) ((key) ? sqlite3_bind_text(res, param, string2str(key), -1, SQLITE_STATIC) : sqlite3_bind_null(res, param))
  7. /* Health related SQL queries
  8. Creates a health log table in sqlite, one per host guid
  9. */
  10. #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, chart_context text, transition_id blob);", guid
  11. int sql_create_health_log_table(RRDHOST *host) {
  12. int rc;
  13. char command[MAX_HEALTH_SQL_SIZE + 1];
  14. if (unlikely(!db_meta)) {
  15. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  16. error_report("HEALTH [%s]: Database has not been initialized", rrdhost_hostname(host));
  17. return 1;
  18. }
  19. char uuid_str[UUID_STR_LEN];
  20. uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
  21. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_CREATE_HEALTH_LOG_TABLE(uuid_str));
  22. rc = db_execute(db_meta, command);
  23. if (unlikely(rc))
  24. error_report("HEALTH [%s]: SQLite error during creation of health log table", rrdhost_hostname(host));
  25. else {
  26. snprintfz(command, MAX_HEALTH_SQL_SIZE, "CREATE INDEX IF NOT EXISTS health_log_index_%s ON health_log_%s (unique_id); ", uuid_str, uuid_str);
  27. rc = db_execute(db_meta, command);
  28. if (unlikely(unlikely(rc)))
  29. error_report("HEALTH [%s]: SQLite error during creation of health log table index", rrdhost_hostname(host));
  30. }
  31. return rc;
  32. }
  33. /* Health related SQL queries
  34. Updates an entry in the table
  35. */
  36. #define SQL_UPDATE_HEALTH_LOG(guid) "UPDATE health_log_%s set updated_by_id = ?, flags = ?, exec_run_timestamp = ?, exec_code = ? where unique_id = ?;", guid
  37. void sql_health_alarm_log_update(RRDHOST *host, ALARM_ENTRY *ae) {
  38. sqlite3_stmt *res = NULL;
  39. int rc;
  40. char command[MAX_HEALTH_SQL_SIZE + 1];
  41. if (unlikely(!db_meta)) {
  42. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  43. error_report("HEALTH [%s]: Database has not been initialized", rrdhost_hostname(host));
  44. return;
  45. }
  46. char uuid_str[UUID_STR_LEN];
  47. uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
  48. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_UPDATE_HEALTH_LOG(uuid_str));
  49. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  50. if (unlikely(rc != SQLITE_OK)) {
  51. sql_create_health_log_table(host);
  52. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  53. if (unlikely(rc != SQLITE_OK)) {
  54. error_report("HEALTH [%s]: Failed to prepare statement for SQL_INSERT_HEALTH_LOG", rrdhost_hostname(host));
  55. return;
  56. }
  57. }
  58. rc = sqlite3_bind_int64(res, 1, (sqlite3_int64) ae->updated_by_id);
  59. if (unlikely(rc != SQLITE_OK)) {
  60. error_report("Failed to bind updated_by_id parameter for SQL_UPDATE_HEALTH_LOG");
  61. goto failed;
  62. }
  63. rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) ae->flags);
  64. if (unlikely(rc != SQLITE_OK)) {
  65. error_report("Failed to bind flags parameter for SQL_UPDATE_HEALTH_LOG");
  66. goto failed;
  67. }
  68. rc = sqlite3_bind_int64(res, 3, (sqlite3_int64) ae->exec_run_timestamp);
  69. if (unlikely(rc != SQLITE_OK)) {
  70. error_report("Failed to bind exec_run_timestamp parameter for SQL_UPDATE_HEALTH_LOG");
  71. goto failed;
  72. }
  73. rc = sqlite3_bind_int(res, 4, ae->exec_code);
  74. if (unlikely(rc != SQLITE_OK)) {
  75. error_report("Failed to bind exec_code parameter for SQL_UPDATE_HEALTH_LOG");
  76. goto failed;
  77. }
  78. rc = sqlite3_bind_int64(res, 5, (sqlite3_int64) ae->unique_id);
  79. if (unlikely(rc != SQLITE_OK)) {
  80. error_report("Failed to bind unique_id parameter for SQL_UPDATE_HEALTH_LOG");
  81. goto failed;
  82. }
  83. rc = execute_insert(res);
  84. if (unlikely(rc != SQLITE_DONE)) {
  85. error_report("HEALTH [%s]: Failed to update health log, rc = %d", rrdhost_hostname(host), rc);
  86. }
  87. failed:
  88. if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
  89. error_report("HEALTH [%s]: Failed to finalize the prepared statement for updating health log.", rrdhost_hostname(host));
  90. }
  91. /* Health related SQL queries
  92. Inserts an entry in the table
  93. */
  94. #define SQL_INSERT_HEALTH_LOG(guid) "INSERT INTO health_log_%s(hostname, unique_id, alarm_id, alarm_event_id, " \
  95. "config_hash_id, updated_by_id, updates_id, when_key, duration, non_clear_duration, flags, " \
  96. "exec_run_timestamp, delay_up_to_timestamp, name, chart, family, exec, recipient, source, " \
  97. "units, info, exec_code, new_status, old_status, delay, new_value, old_value, last_repeat, " \
  98. "class, component, type, chart_context, transition_id) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);", guid
  99. void sql_health_alarm_log_insert(RRDHOST *host, ALARM_ENTRY *ae) {
  100. sqlite3_stmt *res = NULL;
  101. int rc;
  102. char command[MAX_HEALTH_SQL_SIZE + 1];
  103. if (unlikely(!db_meta)) {
  104. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  105. error_report("HEALTH [%s]: Database has not been initialized", rrdhost_hostname(host));
  106. return;
  107. }
  108. char uuid_str[UUID_STR_LEN];
  109. uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
  110. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_INSERT_HEALTH_LOG(uuid_str));
  111. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  112. if (unlikely(rc != SQLITE_OK)) {
  113. sql_create_health_log_table(host);
  114. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  115. if (unlikely(rc != SQLITE_OK)) {
  116. error_report("HEALTH [%s]: Failed to prepare statement for SQL_INSERT_HEALTH_LOG", rrdhost_hostname(host));
  117. return;
  118. }
  119. }
  120. rc = sqlite3_bind_text(res, 1, rrdhost_hostname(host), -1, SQLITE_STATIC);
  121. if (unlikely(rc != SQLITE_OK)) {
  122. error_report("Failed to bind hostname parameter for SQL_INSERT_HEALTH_LOG");
  123. goto failed;
  124. }
  125. rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) ae->unique_id);
  126. if (unlikely(rc != SQLITE_OK)) {
  127. error_report("Failed to bind unique_id parameter for SQL_INSERT_HEALTH_LOG");
  128. goto failed;
  129. }
  130. rc = sqlite3_bind_int64(res, 3, (sqlite3_int64) ae->alarm_id);
  131. if (unlikely(rc != SQLITE_OK)) {
  132. error_report("Failed to bind alarm_id parameter for SQL_INSERT_HEALTH_LOG");
  133. goto failed;
  134. }
  135. rc = sqlite3_bind_int64(res, 4, (sqlite3_int64) ae->alarm_event_id);
  136. if (unlikely(rc != SQLITE_OK)) {
  137. error_report("Failed to bind alarm_event_id parameter for SQL_INSERT_HEALTH_LOG");
  138. goto failed;
  139. }
  140. rc = sqlite3_bind_blob(res, 5, &ae->config_hash_id, sizeof(ae->config_hash_id), SQLITE_STATIC);
  141. if (unlikely(rc != SQLITE_OK)) {
  142. error_report("Failed to bind config_hash_id parameter for SQL_INSERT_HEALTH_LOG");
  143. goto failed;
  144. }
  145. rc = sqlite3_bind_int64(res, 6, (sqlite3_int64) ae->updated_by_id);
  146. if (unlikely(rc != SQLITE_OK)) {
  147. error_report("Failed to bind updated_by_id parameter for SQL_INSERT_HEALTH_LOG");
  148. goto failed;
  149. }
  150. rc = sqlite3_bind_int64(res, 7, (sqlite3_int64) ae->updates_id);
  151. if (unlikely(rc != SQLITE_OK)) {
  152. error_report("Failed to bind updates_id parameter for SQL_INSERT_HEALTH_LOG");
  153. goto failed;
  154. }
  155. rc = sqlite3_bind_int64(res, 8, (sqlite3_int64) ae->when);
  156. if (unlikely(rc != SQLITE_OK)) {
  157. error_report("Failed to bind when parameter for SQL_INSERT_HEALTH_LOG");
  158. goto failed;
  159. }
  160. rc = sqlite3_bind_int64(res, 9, (sqlite3_int64) ae->duration);
  161. if (unlikely(rc != SQLITE_OK)) {
  162. error_report("Failed to bind duration parameter for SQL_INSERT_HEALTH_LOG");
  163. goto failed;
  164. }
  165. rc = sqlite3_bind_int64(res, 10, (sqlite3_int64) ae->non_clear_duration);
  166. if (unlikely(rc != SQLITE_OK)) {
  167. error_report("Failed to bind non_clear_duration parameter for SQL_INSERT_HEALTH_LOG");
  168. goto failed;
  169. }
  170. rc = sqlite3_bind_int64(res, 11, (sqlite3_int64) ae->flags);
  171. if (unlikely(rc != SQLITE_OK)) {
  172. error_report("Failed to bind flags parameter for SQL_INSERT_HEALTH_LOG");
  173. goto failed;
  174. }
  175. rc = sqlite3_bind_int64(res, 12, (sqlite3_int64) ae->exec_run_timestamp);
  176. if (unlikely(rc != SQLITE_OK)) {
  177. error_report("Failed to bind exec_run_timestamp parameter for SQL_INSERT_HEALTH_LOG");
  178. goto failed;
  179. }
  180. rc = sqlite3_bind_int64(res, 13, (sqlite3_int64) ae->delay_up_to_timestamp);
  181. if (unlikely(rc != SQLITE_OK)) {
  182. error_report("Failed to bind delay_up_to_timestamp parameter for SQL_INSERT_HEALTH_LOG");
  183. goto failed;
  184. }
  185. rc = sqlite3_bind_string_or_null(res, ae->name, 14);
  186. if (unlikely(rc != SQLITE_OK)) {
  187. error_report("Failed to bind name parameter for SQL_INSERT_HEALTH_LOG");
  188. goto failed;
  189. }
  190. rc = sqlite3_bind_string_or_null(res, ae->chart, 15);
  191. if (unlikely(rc != SQLITE_OK)) {
  192. error_report("Failed to bind chart parameter for SQL_INSERT_HEALTH_LOG");
  193. goto failed;
  194. }
  195. rc = sqlite3_bind_string_or_null(res, ae->family, 16);
  196. if (unlikely(rc != SQLITE_OK)) {
  197. error_report("Failed to bind family parameter for SQL_INSERT_HEALTH_LOG");
  198. goto failed;
  199. }
  200. rc = sqlite3_bind_string_or_null(res, ae->exec, 17);
  201. if (unlikely(rc != SQLITE_OK)) {
  202. error_report("Failed to bind exec parameter for SQL_INSERT_HEALTH_LOG");
  203. goto failed;
  204. }
  205. rc = sqlite3_bind_string_or_null(res, ae->recipient, 18);
  206. if (unlikely(rc != SQLITE_OK)) {
  207. error_report("Failed to bind recipient parameter for SQL_INSERT_HEALTH_LOG");
  208. goto failed;
  209. }
  210. rc = sqlite3_bind_string_or_null(res, ae->source, 19);
  211. if (unlikely(rc != SQLITE_OK)) {
  212. error_report("Failed to bind source parameter for SQL_INSERT_HEALTH_LOG");
  213. goto failed;
  214. }
  215. rc = sqlite3_bind_string_or_null(res, ae->units, 20);
  216. if (unlikely(rc != SQLITE_OK)) {
  217. error_report("Failed to bind host_id parameter to store node instance information");
  218. goto failed;
  219. }
  220. rc = sqlite3_bind_string_or_null(res, ae->info, 21);
  221. if (unlikely(rc != SQLITE_OK)) {
  222. error_report("Failed to bind info parameter for SQL_INSERT_HEALTH_LOG");
  223. goto failed;
  224. }
  225. rc = sqlite3_bind_int(res, 22, ae->exec_code);
  226. if (unlikely(rc != SQLITE_OK)) {
  227. error_report("Failed to bind exec_code parameter for SQL_INSERT_HEALTH_LOG");
  228. goto failed;
  229. }
  230. rc = sqlite3_bind_int(res, 23, ae->new_status);
  231. if (unlikely(rc != SQLITE_OK)) {
  232. error_report("Failed to bind new_status parameter for SQL_INSERT_HEALTH_LOG");
  233. goto failed;
  234. }
  235. rc = sqlite3_bind_int(res, 24, ae->old_status);
  236. if (unlikely(rc != SQLITE_OK)) {
  237. error_report("Failed to bind old_status parameter for SQL_INSERT_HEALTH_LOG");
  238. goto failed;
  239. }
  240. rc = sqlite3_bind_int(res, 25, ae->delay);
  241. if (unlikely(rc != SQLITE_OK)) {
  242. error_report("Failed to bind delay parameter for SQL_INSERT_HEALTH_LOG");
  243. goto failed;
  244. }
  245. rc = sqlite3_bind_double(res, 26, ae->new_value);
  246. if (unlikely(rc != SQLITE_OK)) {
  247. error_report("Failed to bind new_value parameter for SQL_INSERT_HEALTH_LOG");
  248. goto failed;
  249. }
  250. rc = sqlite3_bind_double(res, 27, ae->old_value);
  251. if (unlikely(rc != SQLITE_OK)) {
  252. error_report("Failed to bind old_value parameter for SQL_INSERT_HEALTH_LOG");
  253. goto failed;
  254. }
  255. rc = sqlite3_bind_int64(res, 28, (sqlite3_int64) ae->last_repeat);
  256. if (unlikely(rc != SQLITE_OK)) {
  257. error_report("Failed to bind last_repeat parameter for SQL_INSERT_HEALTH_LOG");
  258. goto failed;
  259. }
  260. rc = sqlite3_bind_string_or_null(res, ae->classification, 29);
  261. if (unlikely(rc != SQLITE_OK)) {
  262. error_report("Failed to bind classification parameter for SQL_INSERT_HEALTH_LOG");
  263. goto failed;
  264. }
  265. rc = sqlite3_bind_string_or_null(res, ae->component, 30);
  266. if (unlikely(rc != SQLITE_OK)) {
  267. error_report("Failed to bind component parameter for SQL_INSERT_HEALTH_LOG");
  268. goto failed;
  269. }
  270. rc = sqlite3_bind_string_or_null(res, ae->type, 31);
  271. if (unlikely(rc != SQLITE_OK)) {
  272. error_report("Failed to bind type parameter for SQL_INSERT_HEALTH_LOG");
  273. goto failed;
  274. }
  275. rc = sqlite3_bind_string_or_null(res, ae->chart_context, 32);
  276. if (unlikely(rc != SQLITE_OK)) {
  277. error_report("Failed to bind chart_context parameter for SQL_INSERT_HEALTH_LOG");
  278. goto failed;
  279. }
  280. rc = sqlite3_bind_blob(res, 33, &ae->transition_id, sizeof(ae->transition_id), SQLITE_STATIC);
  281. if (unlikely(rc != SQLITE_OK)) {
  282. error_report("Failed to bind transition_id parameter for SQL_INSERT_HEALTH_LOG");
  283. goto failed;
  284. }
  285. rc = execute_insert(res);
  286. if (unlikely(rc != SQLITE_DONE)) {
  287. error_report("HEALTH [%s]: Failed to execute SQL_INSERT_HEALTH_LOG, rc = %d", rrdhost_hostname(host), rc);
  288. goto failed;
  289. }
  290. ae->flags |= HEALTH_ENTRY_FLAG_SAVED;
  291. host->health.health_log_entries_written++;
  292. failed:
  293. if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
  294. error_report("HEALTH [%s]: Failed to finalize the prepared statement for inserting to health log.", rrdhost_hostname(host));
  295. }
  296. void sql_health_alarm_log_save(RRDHOST *host, ALARM_ENTRY *ae)
  297. {
  298. if (ae->flags & HEALTH_ENTRY_FLAG_SAVED)
  299. sql_health_alarm_log_update(host, ae);
  300. else {
  301. sql_health_alarm_log_insert(host, ae);
  302. #ifdef ENABLE_ACLK
  303. if (netdata_cloud_setting) {
  304. sql_queue_alarm_to_aclk(host, ae, 0);
  305. }
  306. #endif
  307. }
  308. }
  309. /* Health related SQL queries
  310. Get a count of rows from health log table
  311. */
  312. #define SQL_COUNT_HEALTH_LOG(guid) "SELECT count(1) FROM health_log_%s;", guid
  313. void sql_health_alarm_log_count(RRDHOST *host) {
  314. sqlite3_stmt *res = NULL;
  315. int rc;
  316. char command[MAX_HEALTH_SQL_SIZE + 1];
  317. if (unlikely(!db_meta)) {
  318. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  319. error_report("Database has not been initialized");
  320. return;
  321. }
  322. char uuid_str[UUID_STR_LEN];
  323. uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
  324. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_COUNT_HEALTH_LOG(uuid_str));
  325. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  326. if (unlikely(rc != SQLITE_OK)) {
  327. error_report("Failed to prepare statement to count health log entries from db");
  328. return;
  329. }
  330. rc = sqlite3_step_monitored(res);
  331. if (likely(rc == SQLITE_ROW))
  332. host->health.health_log_entries_written = (size_t) sqlite3_column_int64(res, 0);
  333. rc = sqlite3_finalize(res);
  334. if (unlikely(rc != SQLITE_OK))
  335. error_report("Failed to finalize the prepared statement to count health log entries from db");
  336. info("HEALTH [%s]: Table health_log_%s, contains %lu entries.", rrdhost_hostname(host), uuid_str, (unsigned long int) host->health.health_log_entries_written);
  337. }
  338. /* Health related SQL queries
  339. Cleans up the health_log table on a non-claimed host
  340. */
  341. #define SQL_CLEANUP_HEALTH_LOG_NOT_CLAIMED(guid,limit) "DELETE FROM health_log_%s ORDER BY unique_id ASC LIMIT %lu;", guid, limit
  342. void sql_health_alarm_log_cleanup_not_claimed(RRDHOST *host, size_t rotate_every) {
  343. sqlite3_stmt *res = NULL;
  344. int rc;
  345. char command[MAX_HEALTH_SQL_SIZE + 1];
  346. if (unlikely(!db_meta)) {
  347. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  348. error_report("Database has not been initialized");
  349. return;
  350. }
  351. char uuid_str[UUID_STR_LEN];
  352. uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
  353. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_CLEANUP_HEALTH_LOG_NOT_CLAIMED(uuid_str, (unsigned long int) (host->health.health_log_entries_written - rotate_every)));
  354. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  355. if (unlikely(rc != SQLITE_OK)) {
  356. error_report("Failed to prepare statement to cleanup health log table");
  357. return;
  358. }
  359. rc = sqlite3_step_monitored(res);
  360. if (unlikely(rc != SQLITE_DONE))
  361. error_report("Failed to cleanup health log table, rc = %d", rc);
  362. rc = sqlite3_finalize(res);
  363. if (unlikely(rc != SQLITE_OK))
  364. error_report("Failed to finalize the prepared statement to cleanup health log table");
  365. host->health.health_log_entries_written = rotate_every;
  366. snprintfz(command, MAX_HEALTH_SQL_SIZE, "aclk_alert_%s", uuid_str);
  367. if (unlikely(table_exists_in_database(command))) {
  368. sql_aclk_alert_clean_dead_entries(host);
  369. }
  370. }
  371. /* Health related SQL queries
  372. Cleans up the health_log table on a claimed host
  373. */
  374. #define SQL_CLEANUP_HEALTH_LOG_CLAIMED(guid, guid2, guid3, limit) "DELETE from health_log_%s WHERE unique_id NOT IN (SELECT filtered_alert_unique_id FROM aclk_alert_%s) AND unique_id IN (SELECT unique_id FROM health_log_%s ORDER BY unique_id asc LIMIT %lu);", guid, guid2, guid3, limit
  375. void sql_health_alarm_log_cleanup_claimed(RRDHOST *host, size_t rotate_every) {
  376. sqlite3_stmt *res = NULL;
  377. int rc;
  378. char command[MAX_HEALTH_SQL_SIZE + 1];
  379. if (unlikely(!db_meta)) {
  380. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  381. error_report("Database has not been initialized");
  382. return;
  383. }
  384. char uuid_str[UUID_STR_LEN];
  385. uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
  386. snprintfz(command, MAX_HEALTH_SQL_SIZE, "aclk_alert_%s", uuid_str);
  387. if (!table_exists_in_database(command)) {
  388. sql_health_alarm_log_cleanup_not_claimed(host, rotate_every);
  389. return;
  390. }
  391. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_CLEANUP_HEALTH_LOG_CLAIMED(uuid_str, uuid_str, uuid_str, (unsigned long int) (host->health.health_log_entries_written - rotate_every)));
  392. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  393. if (unlikely(rc != SQLITE_OK)) {
  394. error_report("Failed to prepare statement to cleanup health log table");
  395. return;
  396. }
  397. rc = sqlite3_step_monitored(res);
  398. if (unlikely(rc != SQLITE_DONE))
  399. error_report("Failed to cleanup health log table, rc = %d", rc);
  400. rc = sqlite3_finalize(res);
  401. if (unlikely(rc != SQLITE_OK))
  402. error_report("Failed to finalize the prepared statement to cleanup health log table");
  403. sql_health_alarm_log_count(host);
  404. sql_aclk_alert_clean_dead_entries(host);
  405. }
  406. /* Health related SQL queries
  407. Cleans up the health_log table.
  408. */
  409. void sql_health_alarm_log_cleanup(RRDHOST *host) {
  410. static size_t rotate_every = 0;
  411. if(unlikely(rotate_every == 0)) {
  412. rotate_every = (size_t)config_get_number(CONFIG_SECTION_HEALTH, "rotate log every lines", 2000);
  413. if(rotate_every < 100) rotate_every = 100;
  414. }
  415. if(likely(host->health.health_log_entries_written < rotate_every)) {
  416. return;
  417. }
  418. if (!claimed()) {
  419. sql_health_alarm_log_cleanup_not_claimed(host, rotate_every);
  420. } else
  421. sql_health_alarm_log_cleanup_claimed(host, rotate_every);
  422. }
  423. #define SQL_INJECT_REMOVED(guid, guid2) "insert into health_log_%s (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, " \
  424. "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, chart_context, transition_id) " \
  425. "select hostname, ?1, ?2, ?3, config_hash_id, 0, ?4, unixepoch(), 0, 0, flags, exec_run_timestamp, " \
  426. "unixepoch(), name, chart, family, exec, recipient, source, units, info, exec_code, -2, new_status, delay, NULL, new_value, 0, class, component, type, chart_context, ?5 " \
  427. "from health_log_%s where unique_id = ?6", guid, guid2
  428. #define SQL_INJECT_REMOVED_UPDATE(guid) "update health_log_%s set flags = flags | ?1, updated_by_id = ?2 where unique_id = ?3; ", guid
  429. void sql_inject_removed_status(char *uuid_str, uint32_t alarm_id, uint32_t alarm_event_id, uint32_t unique_id, uint32_t max_unique_id)
  430. {
  431. int rc;
  432. char command[MAX_HEALTH_SQL_SIZE + 1];
  433. if (!alarm_id || !alarm_event_id || !unique_id || !max_unique_id)
  434. return;
  435. sqlite3_stmt *res = NULL;
  436. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_INJECT_REMOVED(uuid_str, uuid_str));
  437. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  438. if (rc != SQLITE_OK) {
  439. error_report("Failed to prepare statement when trying to inject removed event");
  440. return;
  441. }
  442. rc = sqlite3_bind_int64(res, 1, (sqlite3_int64) max_unique_id);
  443. if (unlikely(rc != SQLITE_OK)) {
  444. error_report("Failed to bind max_unique_id parameter for SQL_INJECT_REMOVED");
  445. goto failed;
  446. }
  447. rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) alarm_id);
  448. if (unlikely(rc != SQLITE_OK)) {
  449. error_report("Failed to bind alarm_id parameter for SQL_INJECT_REMOVED");
  450. goto failed;
  451. }
  452. rc = sqlite3_bind_int64(res, 3, (sqlite3_int64) alarm_event_id + 1);
  453. if (unlikely(rc != SQLITE_OK)) {
  454. error_report("Failed to bind alarm_event_id parameter for SQL_INJECT_REMOVED");
  455. goto failed;
  456. }
  457. rc = sqlite3_bind_int64(res, 4, (sqlite3_int64) unique_id);
  458. if (unlikely(rc != SQLITE_OK)) {
  459. error_report("Failed to bind unique_id parameter for SQL_INJECT_REMOVED");
  460. goto failed;
  461. }
  462. uuid_t transition_id;
  463. uuid_generate_random(transition_id);
  464. rc = sqlite3_bind_blob(res, 5, &transition_id, sizeof(transition_id), SQLITE_STATIC);
  465. if (unlikely(rc != SQLITE_OK)) {
  466. error_report("Failed to bind config_hash_id parameter for SQL_INSERT_HEALTH_LOG");
  467. goto failed;
  468. }
  469. rc = sqlite3_bind_int64(res, 6, (sqlite3_int64) unique_id);
  470. if (unlikely(rc != SQLITE_OK)) {
  471. error_report("Failed to bind unique_id parameter for SQL_INJECT_REMOVED");
  472. goto failed;
  473. }
  474. rc = execute_insert(res);
  475. if (unlikely(rc != SQLITE_DONE)) {
  476. error_report("HEALTH [N/A]: Failed to execute SQL_INJECT_REMOVED, rc = %d", rc);
  477. goto failed;
  478. }
  479. if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
  480. error_report("HEALTH [N/A]: Failed to finalize the prepared statement for injecting removed event.");
  481. //update the old entry
  482. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_INJECT_REMOVED_UPDATE(uuid_str));
  483. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  484. if (rc != SQLITE_OK) {
  485. error_report("Failed to prepare statement when trying to update during inject removed event");
  486. return;
  487. }
  488. rc = sqlite3_bind_int64(res, 1, (sqlite3_int64) HEALTH_ENTRY_FLAG_UPDATED);
  489. if (unlikely(rc != SQLITE_OK)) {
  490. error_report("Failed to bind flags parameter for SQL_INJECT_REMOVED (update)");
  491. goto failed;
  492. }
  493. rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) max_unique_id);
  494. if (unlikely(rc != SQLITE_OK)) {
  495. error_report("Failed to bind max_unique_id parameter for SQL_INJECT_REMOVED (update)");
  496. goto failed;
  497. }
  498. rc = sqlite3_bind_int64(res, 3, (sqlite3_int64) unique_id);
  499. if (unlikely(rc != SQLITE_OK)) {
  500. error_report("Failed to bind unique_id parameter for SQL_INJECT_REMOVED (update)");
  501. goto failed;
  502. }
  503. rc = execute_insert(res);
  504. if (unlikely(rc != SQLITE_DONE)) {
  505. error_report("HEALTH [N/A]: Failed to execute SQL_INJECT_REMOVED_UPDATE, rc = %d", rc);
  506. goto failed;
  507. }
  508. failed:
  509. if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
  510. error_report("HEALTH [N/A]: Failed to finalize the prepared statement for injecting removed event.");
  511. }
  512. #define SQL_SELECT_MAX_UNIQUE_ID(guid) "SELECT MAX(unique_id) from health_log_%s", guid
  513. uint32_t sql_get_max_unique_id (char *uuid_str)
  514. {
  515. int rc;
  516. char command[MAX_HEALTH_SQL_SIZE + 1];
  517. uint32_t max_unique_id = 0;
  518. sqlite3_stmt *res = NULL;
  519. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_SELECT_MAX_UNIQUE_ID(uuid_str));
  520. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  521. if (rc != SQLITE_OK) {
  522. error_report("Failed to prepare statement when trying to get max unique id");
  523. return 0;
  524. }
  525. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  526. max_unique_id = (uint32_t) sqlite3_column_int64(res, 0);
  527. }
  528. rc = sqlite3_finalize(res);
  529. if (unlikely(rc != SQLITE_OK))
  530. error_report("Failed to finalize the statement");
  531. return max_unique_id;
  532. }
  533. #define SQL_SELECT_LAST_STATUSES(guid) "SELECT new_status, unique_id, alarm_id, alarm_event_id from health_log_%s group by alarm_id having max(alarm_event_id)", guid
  534. void sql_check_removed_alerts_state(char *uuid_str)
  535. {
  536. int rc;
  537. char command[MAX_HEALTH_SQL_SIZE + 1];
  538. uint32_t max_unique_id = 0;
  539. sqlite3_stmt *res = NULL;
  540. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_SELECT_LAST_STATUSES(uuid_str));
  541. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  542. if (rc != SQLITE_OK) {
  543. error_report("Failed to prepare statement when trying to check removed statuses");
  544. return;
  545. }
  546. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  547. uint32_t alarm_id, alarm_event_id, unique_id;
  548. RRDCALC_STATUS status;
  549. status = (RRDCALC_STATUS) sqlite3_column_int(res, 0);
  550. unique_id = (uint32_t) sqlite3_column_int64(res, 1);
  551. alarm_id = (uint32_t) sqlite3_column_int64(res, 2);
  552. alarm_event_id = (uint32_t) sqlite3_column_int64(res, 3);
  553. if (unlikely(status != RRDCALC_STATUS_REMOVED)) {
  554. if (unlikely(!max_unique_id))
  555. max_unique_id = sql_get_max_unique_id (uuid_str);
  556. sql_inject_removed_status (uuid_str, alarm_id, alarm_event_id, unique_id, ++max_unique_id);
  557. }
  558. }
  559. rc = sqlite3_finalize(res);
  560. if (unlikely(rc != SQLITE_OK))
  561. error_report("Failed to finalize the statement");
  562. }
  563. /* Health related SQL queries
  564. Load from the health log table
  565. */
  566. #define SQL_LOAD_HEALTH_LOG(guid) "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, chart_context, transition_id FROM health_log_%s group by alarm_id having max(alarm_event_id);", guid
  567. void sql_health_alarm_log_load(RRDHOST *host) {
  568. sqlite3_stmt *res = NULL;
  569. int ret;
  570. ssize_t errored = 0, loaded = 0;
  571. char command[MAX_HEALTH_SQL_SIZE + 1];
  572. host->health.health_log_entries_written = 0;
  573. if (unlikely(!db_meta)) {
  574. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  575. error_report("HEALTH [%s]: Database has not been initialized", rrdhost_hostname(host));
  576. return;
  577. }
  578. char uuid_str[UUID_STR_LEN];
  579. uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
  580. sql_check_removed_alerts_state(uuid_str);
  581. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_LOAD_HEALTH_LOG(uuid_str));
  582. ret = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  583. if (unlikely(ret != SQLITE_OK)) {
  584. error_report("HEALTH [%s]: Failed to prepare sql statement to load health log.", rrdhost_hostname(host));
  585. return;
  586. }
  587. DICTIONARY *all_rrdcalcs = dictionary_create(
  588. DICT_OPTION_NAME_LINK_DONT_CLONE | DICT_OPTION_VALUE_LINK_DONT_CLONE | DICT_OPTION_DONT_OVERWRITE_VALUE);
  589. RRDCALC *rc;
  590. foreach_rrdcalc_in_rrdhost_read(host, rc) {
  591. dictionary_set(all_rrdcalcs, rrdcalc_name(rc), rc, sizeof(*rc));
  592. }
  593. foreach_rrdcalc_in_rrdhost_done(rc);
  594. netdata_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
  595. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  596. ALARM_ENTRY *ae = NULL;
  597. // check that we have valid ids
  598. uint32_t unique_id = (uint32_t) sqlite3_column_int64(res, 1);
  599. if(!unique_id) {
  600. error_report("HEALTH [%s]: Got invalid unique id. Ignoring it.", rrdhost_hostname(host));
  601. errored++;
  602. continue;
  603. }
  604. uint32_t alarm_id = (uint32_t) sqlite3_column_int64(res, 2);
  605. if(!alarm_id) {
  606. error_report("HEALTH [%s]: Got invalid alarm id. Ignoring it.", rrdhost_hostname(host));
  607. errored++;
  608. continue;
  609. }
  610. //need name, chart and family
  611. if (sqlite3_column_type(res, 13) == SQLITE_NULL) {
  612. error_report("HEALTH [%s]: Got null name field. Ignoring it.", rrdhost_hostname(host));
  613. errored++;
  614. continue;
  615. }
  616. if (sqlite3_column_type(res, 14) == SQLITE_NULL) {
  617. error_report("HEALTH [%s]: Got null chart field. Ignoring it.", rrdhost_hostname(host));
  618. errored++;
  619. continue;
  620. }
  621. if (sqlite3_column_type(res, 15) == SQLITE_NULL) {
  622. error_report("HEALTH [%s]: Got null family field. Ignoring it.", rrdhost_hostname(host));
  623. errored++;
  624. continue;
  625. }
  626. // Check if we got last_repeat field
  627. time_t last_repeat = (time_t)sqlite3_column_int64(res, 27);
  628. rc = dictionary_get(all_rrdcalcs, (char *) sqlite3_column_text(res, 14));
  629. if(unlikely(rc)) {
  630. if (rrdcalc_isrepeating(rc)) {
  631. rc->last_repeat = last_repeat;
  632. // We iterate through repeating alarm entries only to
  633. // find the latest last_repeat timestamp. Otherwise,
  634. // there is no need to keep them in memory.
  635. continue;
  636. }
  637. }
  638. ae = callocz(1, sizeof(ALARM_ENTRY));
  639. ae->unique_id = unique_id;
  640. ae->alarm_id = alarm_id;
  641. if (sqlite3_column_type(res, 4) != SQLITE_NULL)
  642. uuid_copy(ae->config_hash_id, *((uuid_t *) sqlite3_column_blob(res, 4)));
  643. ae->alarm_event_id = (uint32_t) sqlite3_column_int64(res, 3);
  644. ae->updated_by_id = (uint32_t) sqlite3_column_int64(res, 5);
  645. ae->updates_id = (uint32_t) sqlite3_column_int64(res, 6);
  646. ae->when = (time_t) sqlite3_column_int64(res, 7);
  647. ae->duration = (time_t) sqlite3_column_int64(res, 8);
  648. ae->non_clear_duration = (time_t) sqlite3_column_int64(res, 9);
  649. ae->flags = (uint32_t) sqlite3_column_int64(res, 10);
  650. ae->flags |= HEALTH_ENTRY_FLAG_SAVED;
  651. ae->exec_run_timestamp = (time_t) sqlite3_column_int64(res, 11);
  652. ae->delay_up_to_timestamp = (time_t) sqlite3_column_int64(res, 12);
  653. ae->name = string_strdupz((char *) sqlite3_column_text(res, 13));
  654. ae->chart = string_strdupz((char *) sqlite3_column_text(res, 14));
  655. ae->family = string_strdupz((char *) sqlite3_column_text(res, 15));
  656. if (sqlite3_column_type(res, 16) != SQLITE_NULL)
  657. ae->exec = string_strdupz((char *) sqlite3_column_text(res, 16));
  658. else
  659. ae->exec = NULL;
  660. if (sqlite3_column_type(res, 17) != SQLITE_NULL)
  661. ae->recipient = string_strdupz((char *) sqlite3_column_text(res, 17));
  662. else
  663. ae->recipient = NULL;
  664. if (sqlite3_column_type(res, 18) != SQLITE_NULL)
  665. ae->source = string_strdupz((char *) sqlite3_column_text(res, 18));
  666. else
  667. ae->source = NULL;
  668. if (sqlite3_column_type(res, 19) != SQLITE_NULL)
  669. ae->units = string_strdupz((char *) sqlite3_column_text(res, 19));
  670. else
  671. ae->units = NULL;
  672. if (sqlite3_column_type(res, 20) != SQLITE_NULL)
  673. ae->info = string_strdupz((char *) sqlite3_column_text(res, 20));
  674. else
  675. ae->info = NULL;
  676. ae->exec_code = (int) sqlite3_column_int(res, 21);
  677. ae->new_status = (RRDCALC_STATUS) sqlite3_column_int(res, 22);
  678. ae->old_status = (RRDCALC_STATUS)sqlite3_column_int(res, 23);
  679. ae->delay = (int) sqlite3_column_int(res, 24);
  680. ae->new_value = (NETDATA_DOUBLE) sqlite3_column_double(res, 25);
  681. ae->old_value = (NETDATA_DOUBLE) sqlite3_column_double(res, 26);
  682. ae->last_repeat = last_repeat;
  683. if (sqlite3_column_type(res, 28) != SQLITE_NULL)
  684. ae->classification = string_strdupz((char *) sqlite3_column_text(res, 28));
  685. else
  686. ae->classification = NULL;
  687. if (sqlite3_column_type(res, 29) != SQLITE_NULL)
  688. ae->component = string_strdupz((char *) sqlite3_column_text(res, 29));
  689. else
  690. ae->component = NULL;
  691. if (sqlite3_column_type(res, 30) != SQLITE_NULL)
  692. ae->type = string_strdupz((char *) sqlite3_column_text(res, 30));
  693. else
  694. ae->type = NULL;
  695. if (sqlite3_column_type(res, 31) != SQLITE_NULL)
  696. ae->chart_context = string_strdupz((char *) sqlite3_column_text(res, 31));
  697. else
  698. ae->chart_context = NULL;
  699. if (sqlite3_column_type(res, 32) != SQLITE_NULL)
  700. uuid_copy(ae->transition_id, *((uuid_t *) sqlite3_column_blob(res, 32)));
  701. char value_string[100 + 1];
  702. string_freez(ae->old_value_string);
  703. string_freez(ae->new_value_string);
  704. ae->old_value_string = string_strdupz(format_value_and_unit(value_string, 100, ae->old_value, ae_units(ae), -1));
  705. ae->new_value_string = string_strdupz(format_value_and_unit(value_string, 100, ae->new_value, ae_units(ae), -1));
  706. ae->next = host->health_log.alarms;
  707. host->health_log.alarms = ae;
  708. if(unlikely(ae->unique_id > host->health_max_unique_id))
  709. host->health_max_unique_id = ae->unique_id;
  710. if(unlikely(ae->alarm_id >= host->health_max_alarm_id))
  711. host->health_max_alarm_id = ae->alarm_id;
  712. loaded++;
  713. }
  714. netdata_rwlock_unlock(&host->health_log.alarm_log_rwlock);
  715. dictionary_destroy(all_rrdcalcs);
  716. all_rrdcalcs = NULL;
  717. if(!host->health_max_unique_id) host->health_max_unique_id = (uint32_t)now_realtime_sec();
  718. if(!host->health_max_alarm_id) host->health_max_alarm_id = (uint32_t)now_realtime_sec();
  719. host->health_log.next_log_id = host->health_max_unique_id + 1;
  720. if (unlikely(!host->health_log.next_alarm_id || host->health_log.next_alarm_id <= host->health_max_alarm_id))
  721. host->health_log.next_alarm_id = host->health_max_alarm_id + 1;
  722. log_health("[%s]: Table health_log_%s, loaded %zd alarm entries, errors in %zd entries.", rrdhost_hostname(host), uuid_str, loaded, errored);
  723. ret = sqlite3_finalize(res);
  724. if (unlikely(ret != SQLITE_OK))
  725. error_report("Failed to finalize the health log read statement");
  726. sql_health_alarm_log_count(host);
  727. }
  728. /*
  729. * Store an alert config hash in the database
  730. */
  731. #define SQL_STORE_ALERT_CONFIG_HASH "insert or replace into alert_hash (hash_id, date_updated, alarm, template, " \
  732. "on_key, class, component, type, os, hosts, lookup, every, units, calc, families, plugin, module, " \
  733. "charts, green, red, warn, crit, exec, to_key, info, delay, options, repeat, host_labels, " \
  734. "p_db_lookup_dimensions, p_db_lookup_method, p_db_lookup_options, p_db_lookup_after, " \
  735. "p_db_lookup_before, p_update_every) values (?1,unixepoch(),?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12," \
  736. "?13,?14,?15,?16,?17,?18,?19,?20,?21,?22,?23,?24,?25,?26,?27,?28,?29,?30,?31,?32,?33,?34);"
  737. int sql_store_alert_config_hash(uuid_t *hash_id, struct alert_config *cfg)
  738. {
  739. static __thread sqlite3_stmt *res = NULL;
  740. int rc, param = 0;
  741. if (unlikely(!db_meta)) {
  742. if (default_rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE)
  743. return 0;
  744. error_report("Database has not been initialized");
  745. return 1;
  746. }
  747. if (unlikely(!res)) {
  748. rc = prepare_statement(db_meta, SQL_STORE_ALERT_CONFIG_HASH, &res);
  749. if (unlikely(rc != SQLITE_OK)) {
  750. error_report("Failed to prepare statement to store alert configuration, rc = %d", rc);
  751. return 1;
  752. }
  753. }
  754. rc = sqlite3_bind_blob(res, ++param, hash_id, sizeof(*hash_id), SQLITE_STATIC);
  755. if (unlikely(rc != SQLITE_OK))
  756. goto bind_fail;
  757. rc = sqlite3_bind_string_or_null(res, cfg->alarm, ++param);
  758. if (unlikely(rc != SQLITE_OK))
  759. goto bind_fail;
  760. rc = sqlite3_bind_string_or_null(res, cfg->template_key, ++param);
  761. if (unlikely(rc != SQLITE_OK))
  762. goto bind_fail;
  763. rc = sqlite3_bind_string_or_null(res, cfg->on, ++param);
  764. if (unlikely(rc != SQLITE_OK))
  765. goto bind_fail;
  766. rc = sqlite3_bind_string_or_null(res, cfg->classification, ++param);
  767. if (unlikely(rc != SQLITE_OK))
  768. goto bind_fail;
  769. rc = sqlite3_bind_string_or_null(res, cfg->component, ++param);
  770. if (unlikely(rc != SQLITE_OK))
  771. goto bind_fail;
  772. rc = sqlite3_bind_string_or_null(res, cfg->type, ++param);
  773. if (unlikely(rc != SQLITE_OK))
  774. goto bind_fail;
  775. rc = sqlite3_bind_string_or_null(res, cfg->os, ++param);
  776. if (unlikely(rc != SQLITE_OK))
  777. goto bind_fail;
  778. rc = sqlite3_bind_string_or_null(res, cfg->host, ++param);
  779. if (unlikely(rc != SQLITE_OK))
  780. goto bind_fail;
  781. rc = sqlite3_bind_string_or_null(res, cfg->lookup, ++param);
  782. if (unlikely(rc != SQLITE_OK))
  783. goto bind_fail;
  784. rc = sqlite3_bind_string_or_null(res, cfg->every, ++param);
  785. if (unlikely(rc != SQLITE_OK))
  786. goto bind_fail;
  787. rc = sqlite3_bind_string_or_null(res, cfg->units, ++param);
  788. if (unlikely(rc != SQLITE_OK))
  789. goto bind_fail;
  790. rc = sqlite3_bind_string_or_null(res, cfg->calc, ++param);
  791. if (unlikely(rc != SQLITE_OK))
  792. goto bind_fail;
  793. rc = sqlite3_bind_string_or_null(res, cfg->families, ++param);
  794. if (unlikely(rc != SQLITE_OK))
  795. goto bind_fail;
  796. rc = sqlite3_bind_string_or_null(res, cfg->plugin, ++param);
  797. if (unlikely(rc != SQLITE_OK))
  798. goto bind_fail;
  799. rc = sqlite3_bind_string_or_null(res, cfg->module, ++param);
  800. if (unlikely(rc != SQLITE_OK))
  801. goto bind_fail;
  802. rc = sqlite3_bind_string_or_null(res, cfg->charts, ++param);
  803. if (unlikely(rc != SQLITE_OK))
  804. goto bind_fail;
  805. rc = sqlite3_bind_string_or_null(res, cfg->green, ++param);
  806. if (unlikely(rc != SQLITE_OK))
  807. goto bind_fail;
  808. rc = sqlite3_bind_string_or_null(res, cfg->red, ++param);
  809. if (unlikely(rc != SQLITE_OK))
  810. goto bind_fail;
  811. rc = sqlite3_bind_string_or_null(res, cfg->warn, ++param);
  812. if (unlikely(rc != SQLITE_OK))
  813. goto bind_fail;
  814. rc = sqlite3_bind_string_or_null(res, cfg->crit, ++param);
  815. if (unlikely(rc != SQLITE_OK))
  816. goto bind_fail;
  817. rc = sqlite3_bind_string_or_null(res, cfg->exec, ++param);
  818. if (unlikely(rc != SQLITE_OK))
  819. goto bind_fail;
  820. rc = sqlite3_bind_string_or_null(res, cfg->to, ++param);
  821. if (unlikely(rc != SQLITE_OK))
  822. goto bind_fail;
  823. rc = sqlite3_bind_string_or_null(res, cfg->info, ++param);
  824. if (unlikely(rc != SQLITE_OK))
  825. goto bind_fail;
  826. rc = sqlite3_bind_string_or_null(res, cfg->delay, ++param);
  827. if (unlikely(rc != SQLITE_OK))
  828. goto bind_fail;
  829. rc = sqlite3_bind_string_or_null(res, cfg->options, ++param);
  830. if (unlikely(rc != SQLITE_OK))
  831. goto bind_fail;
  832. rc = sqlite3_bind_string_or_null(res, cfg->repeat, ++param);
  833. if (unlikely(rc != SQLITE_OK))
  834. goto bind_fail;
  835. rc = sqlite3_bind_string_or_null(res, cfg->host_labels, ++param);
  836. if (unlikely(rc != SQLITE_OK))
  837. goto bind_fail;
  838. if (cfg->p_db_lookup_after) {
  839. rc = sqlite3_bind_string_or_null(res, cfg->p_db_lookup_dimensions, ++param);
  840. if (unlikely(rc != SQLITE_OK))
  841. goto bind_fail;
  842. rc = sqlite3_bind_string_or_null(res, cfg->p_db_lookup_method, ++param);
  843. if (unlikely(rc != SQLITE_OK))
  844. goto bind_fail;
  845. rc = sqlite3_bind_int(res, ++param, (int) cfg->p_db_lookup_options);
  846. if (unlikely(rc != SQLITE_OK))
  847. goto bind_fail;
  848. rc = sqlite3_bind_int(res, ++param, (int) cfg->p_db_lookup_after);
  849. if (unlikely(rc != SQLITE_OK))
  850. goto bind_fail;
  851. rc = sqlite3_bind_int(res, ++param, (int) cfg->p_db_lookup_before);
  852. if (unlikely(rc != SQLITE_OK))
  853. goto bind_fail;
  854. } else {
  855. rc = sqlite3_bind_null(res, ++param);
  856. if (unlikely(rc != SQLITE_OK))
  857. goto bind_fail;
  858. rc = sqlite3_bind_null(res, ++param);
  859. if (unlikely(rc != SQLITE_OK))
  860. goto bind_fail;
  861. rc = sqlite3_bind_null(res, ++param);
  862. if (unlikely(rc != SQLITE_OK))
  863. goto bind_fail;
  864. rc = sqlite3_bind_null(res, ++param);
  865. if (unlikely(rc != SQLITE_OK))
  866. goto bind_fail;
  867. rc = sqlite3_bind_null(res, ++param);
  868. if (unlikely(rc != SQLITE_OK))
  869. goto bind_fail;
  870. }
  871. rc = sqlite3_bind_int(res, ++param, cfg->p_update_every);
  872. if (unlikely(rc != SQLITE_OK))
  873. goto bind_fail;
  874. rc = execute_insert(res);
  875. if (unlikely(rc != SQLITE_DONE))
  876. error_report("Failed to store alert config, rc = %d", rc);
  877. rc = sqlite3_reset(res);
  878. if (unlikely(rc != SQLITE_OK))
  879. error_report("Failed to reset statement in alert hash_id store function, rc = %d", rc);
  880. return 0;
  881. bind_fail:
  882. error_report("Failed to bind parameter %d to store alert hash_id, rc = %d", param, rc);
  883. rc = sqlite3_reset(res);
  884. if (unlikely(rc != SQLITE_OK))
  885. error_report("Failed to reset statement in alert hash_id store function, rc = %d", rc);
  886. return 1;
  887. }
  888. /*
  889. alert hashes are used for cloud communication.
  890. if cloud is disabled or openssl is not available (which will prevent cloud connectivity)
  891. skip hash calculations
  892. */
  893. #if !defined DISABLE_CLOUD && defined ENABLE_HTTPS
  894. #define DIGEST_ALERT_CONFIG_VAL(v) ((v) ? EVP_DigestUpdate(evpctx, (string2str(v)), string_strlen((v))) : EVP_DigestUpdate(evpctx, "", 1))
  895. #endif
  896. int alert_hash_and_store_config(
  897. uuid_t hash_id,
  898. struct alert_config *cfg,
  899. int store_hash)
  900. {
  901. #if !defined DISABLE_CLOUD && defined ENABLE_HTTPS
  902. EVP_MD_CTX *evpctx;
  903. unsigned char hash_value[EVP_MAX_MD_SIZE];
  904. unsigned int hash_len;
  905. evpctx = EVP_MD_CTX_create();
  906. EVP_DigestInit_ex(evpctx, EVP_sha256(), NULL);
  907. DIGEST_ALERT_CONFIG_VAL(cfg->alarm);
  908. DIGEST_ALERT_CONFIG_VAL(cfg->template_key);
  909. DIGEST_ALERT_CONFIG_VAL(cfg->os);
  910. DIGEST_ALERT_CONFIG_VAL(cfg->host);
  911. DIGEST_ALERT_CONFIG_VAL(cfg->on);
  912. DIGEST_ALERT_CONFIG_VAL(cfg->families);
  913. DIGEST_ALERT_CONFIG_VAL(cfg->plugin);
  914. DIGEST_ALERT_CONFIG_VAL(cfg->module);
  915. DIGEST_ALERT_CONFIG_VAL(cfg->charts);
  916. DIGEST_ALERT_CONFIG_VAL(cfg->lookup);
  917. DIGEST_ALERT_CONFIG_VAL(cfg->calc);
  918. DIGEST_ALERT_CONFIG_VAL(cfg->every);
  919. DIGEST_ALERT_CONFIG_VAL(cfg->green);
  920. DIGEST_ALERT_CONFIG_VAL(cfg->red);
  921. DIGEST_ALERT_CONFIG_VAL(cfg->warn);
  922. DIGEST_ALERT_CONFIG_VAL(cfg->crit);
  923. DIGEST_ALERT_CONFIG_VAL(cfg->exec);
  924. DIGEST_ALERT_CONFIG_VAL(cfg->to);
  925. DIGEST_ALERT_CONFIG_VAL(cfg->units);
  926. DIGEST_ALERT_CONFIG_VAL(cfg->info);
  927. DIGEST_ALERT_CONFIG_VAL(cfg->classification);
  928. DIGEST_ALERT_CONFIG_VAL(cfg->component);
  929. DIGEST_ALERT_CONFIG_VAL(cfg->type);
  930. DIGEST_ALERT_CONFIG_VAL(cfg->delay);
  931. DIGEST_ALERT_CONFIG_VAL(cfg->options);
  932. DIGEST_ALERT_CONFIG_VAL(cfg->repeat);
  933. DIGEST_ALERT_CONFIG_VAL(cfg->host_labels);
  934. DIGEST_ALERT_CONFIG_VAL(cfg->chart_labels);
  935. EVP_DigestFinal_ex(evpctx, hash_value, &hash_len);
  936. EVP_MD_CTX_destroy(evpctx);
  937. fatal_assert(hash_len > sizeof(uuid_t));
  938. char uuid_str[UUID_STR_LEN];
  939. uuid_unparse_lower(*((uuid_t *)&hash_value), uuid_str);
  940. uuid_copy(hash_id, *((uuid_t *)&hash_value));
  941. /* store everything, so it can be recreated when not in memory or just a subset ? */
  942. if (store_hash)
  943. (void)sql_store_alert_config_hash( (uuid_t *)&hash_value, cfg);
  944. #else
  945. UNUSED(hash_id);
  946. UNUSED(cfg);
  947. UNUSED(store_hash);
  948. #endif
  949. return 1;
  950. }
  951. #define SQL_SELECT_HEALTH_LAST_EXECUTED_EVENT "SELECT new_status FROM health_log_%s WHERE alarm_id = %u AND unique_id != %u AND flags & %d ORDER BY unique_id DESC LIMIT 1"
  952. int sql_health_get_last_executed_event(RRDHOST *host, ALARM_ENTRY *ae, RRDCALC_STATUS *last_executed_status)
  953. {
  954. int rc = 0, ret = -1;
  955. char command[MAX_HEALTH_SQL_SIZE + 1];
  956. char uuid_str[UUID_STR_LEN];
  957. uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
  958. sqlite3_stmt *res = NULL;
  959. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_SELECT_HEALTH_LAST_EXECUTED_EVENT, uuid_str, ae->alarm_id, ae->unique_id, HEALTH_ENTRY_FLAG_EXEC_RUN);
  960. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  961. if (rc != SQLITE_OK) {
  962. error_report("Failed to prepare statement when trying to get last executed status");
  963. return ret;
  964. }
  965. ret = 0;
  966. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  967. *last_executed_status = (RRDCALC_STATUS) sqlite3_column_int(res, 0);
  968. ret = 1;
  969. }
  970. rc = sqlite3_finalize(res);
  971. if (unlikely(rc != SQLITE_OK))
  972. error_report("Failed to finalize the statement.");
  973. return ret;
  974. }
  975. #define SQL_SELECT_HEALTH_LOG(guid) "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, chart_context, transition_id FROM health_log_%s WHERE 1=1 ", guid
  976. void sql_health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after, char *chart) {
  977. buffer_strcat(wb, "[");
  978. unsigned int max = host->health_log.max;
  979. unsigned int count = 0;
  980. sqlite3_stmt *res = NULL;
  981. int rc;
  982. BUFFER *command = buffer_create(MAX_HEALTH_SQL_SIZE, NULL);
  983. char uuid_str[UUID_STR_LEN];
  984. uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
  985. buffer_sprintf(command, SQL_SELECT_HEALTH_LOG(uuid_str));
  986. if (chart) {
  987. char chart_sql[MAX_HEALTH_SQL_SIZE + 1];
  988. snprintfz(chart_sql, MAX_HEALTH_SQL_SIZE, "AND chart = '%s' ", chart);
  989. buffer_strcat(command, chart_sql);
  990. }
  991. if (after) {
  992. char after_sql[MAX_HEALTH_SQL_SIZE + 1];
  993. snprintfz(after_sql, MAX_HEALTH_SQL_SIZE, "AND unique_id > %u ", after);
  994. buffer_strcat(command, after_sql);
  995. }
  996. {
  997. char limit_sql[MAX_HEALTH_SQL_SIZE + 1];
  998. snprintfz(limit_sql, MAX_HEALTH_SQL_SIZE, "ORDER BY unique_id DESC LIMIT %u ", max);
  999. buffer_strcat(command, limit_sql);
  1000. }
  1001. rc = sqlite3_prepare_v2(db_meta, buffer_tostring(command), -1, &res, 0);
  1002. if (unlikely(rc != SQLITE_OK)) {
  1003. error_report("Failed to prepare statement SQL_SELECT_HEALTH_LOG");
  1004. buffer_free(command);
  1005. return;
  1006. }
  1007. while (sqlite3_step(res) == SQLITE_ROW) {
  1008. char old_value_string[100 + 1];
  1009. char new_value_string[100 + 1];
  1010. char config_hash_id[UUID_STR_LEN];
  1011. uuid_unparse_lower(*((uuid_t *) sqlite3_column_blob(res, 4)), config_hash_id);
  1012. char transition_id[UUID_STR_LEN] = {0};
  1013. if (sqlite3_column_type(res, 32) != SQLITE_NULL)
  1014. uuid_unparse_lower(*((uuid_t *) sqlite3_column_blob(res, 32)), transition_id);
  1015. char *edit_command = health_edit_command_from_source((char *)sqlite3_column_text(res, 18));
  1016. if (count)
  1017. buffer_sprintf(wb, ",");
  1018. count++;
  1019. buffer_sprintf(
  1020. wb,
  1021. "\n\t{\n"
  1022. "\t\t\"hostname\": \"%s\",\n"
  1023. "\t\t\"utc_offset\": %d,\n"
  1024. "\t\t\"timezone\": \"%s\",\n"
  1025. "\t\t\"unique_id\": %u,\n"
  1026. "\t\t\"alarm_id\": %u,\n"
  1027. "\t\t\"alarm_event_id\": %u,\n"
  1028. "\t\t\"config_hash_id\": \"%s\",\n"
  1029. "\t\t\"transition_id\": \"%s\",\n"
  1030. "\t\t\"name\": \"%s\",\n"
  1031. "\t\t\"chart\": \"%s\",\n"
  1032. "\t\t\"context\": \"%s\",\n"
  1033. "\t\t\"family\": \"%s\",\n"
  1034. "\t\t\"class\": \"%s\",\n"
  1035. "\t\t\"component\": \"%s\",\n"
  1036. "\t\t\"type\": \"%s\",\n"
  1037. "\t\t\"processed\": %s,\n"
  1038. "\t\t\"updated\": %s,\n"
  1039. "\t\t\"exec_run\": %lu,\n"
  1040. "\t\t\"exec_failed\": %s,\n"
  1041. "\t\t\"exec\": \"%s\",\n"
  1042. "\t\t\"recipient\": \"%s\",\n"
  1043. "\t\t\"exec_code\": %d,\n"
  1044. "\t\t\"source\": \"%s\",\n"
  1045. "\t\t\"command\": \"%s\",\n"
  1046. "\t\t\"units\": \"%s\",\n"
  1047. "\t\t\"when\": %lu,\n"
  1048. "\t\t\"duration\": %lu,\n"
  1049. "\t\t\"non_clear_duration\": %lu,\n"
  1050. "\t\t\"status\": \"%s\",\n"
  1051. "\t\t\"old_status\": \"%s\",\n"
  1052. "\t\t\"delay\": %d,\n"
  1053. "\t\t\"delay_up_to_timestamp\": %lu,\n"
  1054. "\t\t\"updated_by_id\": %u,\n"
  1055. "\t\t\"updates_id\": %u,\n"
  1056. "\t\t\"value_string\": \"%s\",\n"
  1057. "\t\t\"old_value_string\": \"%s\",\n"
  1058. "\t\t\"last_repeat\": \"%lu\",\n"
  1059. "\t\t\"silenced\": \"%s\",\n",
  1060. sqlite3_column_text(res, 0),
  1061. host->utc_offset,
  1062. rrdhost_abbrev_timezone(host),
  1063. (unsigned int) sqlite3_column_int64(res, 1),
  1064. (unsigned int) sqlite3_column_int64(res, 2),
  1065. (unsigned int) sqlite3_column_int64(res, 3),
  1066. config_hash_id,
  1067. transition_id,
  1068. sqlite3_column_text(res, 13),
  1069. sqlite3_column_text(res, 14),
  1070. sqlite3_column_text(res, 31),
  1071. sqlite3_column_text(res, 15),
  1072. sqlite3_column_text(res, 28) ? (const char *) sqlite3_column_text(res, 28) : (char *) "Unknown",
  1073. sqlite3_column_text(res, 29) ? (const char *) sqlite3_column_text(res, 29) : (char *) "Unknown",
  1074. sqlite3_column_text(res, 30) ? (const char *) sqlite3_column_text(res, 30) : (char *) "Unknown",
  1075. (sqlite3_column_int64(res, 10) & HEALTH_ENTRY_FLAG_PROCESSED)?"true":"false",
  1076. (sqlite3_column_int64(res, 10) & HEALTH_ENTRY_FLAG_UPDATED)?"true":"false",
  1077. (long unsigned int)sqlite3_column_int64(res, 11),
  1078. (sqlite3_column_int64(res, 10) & HEALTH_ENTRY_FLAG_EXEC_FAILED)?"true":"false",
  1079. sqlite3_column_text(res, 16) ? (const char *) sqlite3_column_text(res, 16) : string2str(host->health.health_default_exec),
  1080. sqlite3_column_text(res, 17) ? (const char *) sqlite3_column_text(res, 17) : string2str(host->health.health_default_recipient),
  1081. sqlite3_column_int(res, 21),
  1082. sqlite3_column_text(res, 18),
  1083. edit_command,
  1084. sqlite3_column_text(res, 19),
  1085. (long unsigned int)sqlite3_column_int64(res, 7),
  1086. (long unsigned int)sqlite3_column_int64(res, 8),
  1087. (long unsigned int)sqlite3_column_int64(res, 9),
  1088. rrdcalc_status2string(sqlite3_column_int(res, 22)),
  1089. rrdcalc_status2string(sqlite3_column_int(res, 23)),
  1090. sqlite3_column_int(res, 24),
  1091. (long unsigned int)sqlite3_column_int64(res, 12),
  1092. (unsigned int)sqlite3_column_int64(res, 5),
  1093. (unsigned int)sqlite3_column_int64(res, 6),
  1094. sqlite3_column_type(res, 25) == SQLITE_NULL ? "-" : format_value_and_unit(new_value_string, 100, sqlite3_column_double(res, 25), (char *) sqlite3_column_text(res, 19), -1),
  1095. sqlite3_column_type(res, 26) == SQLITE_NULL ? "-" : format_value_and_unit(old_value_string, 100, sqlite3_column_double(res, 26), (char *) sqlite3_column_text(res, 19), -1),
  1096. (long unsigned int)sqlite3_column_int64(res, 27),
  1097. (sqlite3_column_int64(res, 10) & HEALTH_ENTRY_FLAG_SILENCED)?"true":"false");
  1098. health_string2json(wb, "\t\t", "info", (char *) sqlite3_column_text(res, 20), ",\n");
  1099. if(unlikely(sqlite3_column_int64(res, 10) & HEALTH_ENTRY_FLAG_NO_CLEAR_NOTIFICATION)) {
  1100. buffer_strcat(wb, "\t\t\"no_clear_notification\": true,\n");
  1101. }
  1102. buffer_strcat(wb, "\t\t\"value\":");
  1103. if (sqlite3_column_type(res, 25) == SQLITE_NULL)
  1104. buffer_strcat(wb, "null");
  1105. else
  1106. buffer_print_netdata_double(wb, sqlite3_column_double(res, 25));
  1107. buffer_strcat(wb, ",\n");
  1108. buffer_strcat(wb, "\t\t\"old_value\":");
  1109. if (sqlite3_column_type(res, 26) == SQLITE_NULL)
  1110. buffer_strcat(wb, "null");
  1111. else
  1112. buffer_print_netdata_double(wb, sqlite3_column_double(res, 26));
  1113. buffer_strcat(wb, "\n");
  1114. buffer_strcat(wb, "\t}");
  1115. freez(edit_command);
  1116. }
  1117. buffer_strcat(wb, "\n]");
  1118. rc = sqlite3_finalize(res);
  1119. if (unlikely(rc != SQLITE_OK))
  1120. error_report("Failed to finalize statement for SQL_SELECT_HEALTH_LOG");
  1121. buffer_free(command);
  1122. }