sqlite_health.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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, chart_context 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, chart_context) 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 = sqlite3_bind_text(res, 32, ae->chart_context, -1, SQLITE_STATIC);
  267. if (unlikely(rc != SQLITE_OK)) {
  268. error_report("Failed to bind chart_context parameter for SQL_INSERT_HEALTH_LOG");
  269. goto failed;
  270. }
  271. rc = execute_insert(res);
  272. if (unlikely(rc != SQLITE_DONE)) {
  273. error_report("HEALTH [%s]: Failed to execute SQL_INSERT_HEALTH_LOG, rc = %d", host->hostname, rc);
  274. goto failed;
  275. }
  276. ae->flags |= HEALTH_ENTRY_FLAG_SAVED;
  277. host->health_log_entries_written++;
  278. failed:
  279. if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
  280. error_report("HEALTH [%s]: Failed to finalize the prepared statement for inserting to health log.", host->hostname);
  281. return;
  282. }
  283. void sql_health_alarm_log_save(RRDHOST *host, ALARM_ENTRY *ae)
  284. {
  285. if (ae->flags & HEALTH_ENTRY_FLAG_SAVED)
  286. sql_health_alarm_log_update(host, ae);
  287. else
  288. sql_health_alarm_log_insert(host, ae);
  289. }
  290. /* Health related SQL queries
  291. Cleans up the health_log table.
  292. */
  293. #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
  294. void sql_health_alarm_log_cleanup(RRDHOST *host) {
  295. sqlite3_stmt *res = NULL;
  296. static size_t rotate_every = 0;
  297. int rc;
  298. char command[MAX_HEALTH_SQL_SIZE + 1];
  299. if(unlikely(rotate_every == 0)) {
  300. rotate_every = (size_t)config_get_number(CONFIG_SECTION_HEALTH, "rotate log every lines", 2000);
  301. if(rotate_every < 100) rotate_every = 100;
  302. }
  303. if(likely(host->health_log_entries_written < rotate_every)) {
  304. return;
  305. }
  306. if (unlikely(!db_meta)) {
  307. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  308. error_report("Database has not been initialized");
  309. return;
  310. }
  311. char uuid_str[GUID_LEN + 1];
  312. uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
  313. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_CLEANUP_HEALTH_LOG(uuid_str, uuid_str, host->health_log_entries_written - rotate_every));
  314. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  315. if (unlikely(rc != SQLITE_OK)) {
  316. error_report("Failed to prepare statement to cleanup health log table");
  317. return;
  318. }
  319. rc = sqlite3_step(res);
  320. if (unlikely(rc != SQLITE_DONE))
  321. error_report("Failed to cleanup health log table, rc = %d", rc);
  322. rc = sqlite3_finalize(res);
  323. if (unlikely(rc != SQLITE_OK))
  324. error_report("Failed to finalize the prepared statement to cleanup health log table");
  325. host->health_log_entries_written = rotate_every;
  326. sql_aclk_alert_clean_dead_entries(host);
  327. }
  328. /* Health related SQL queries
  329. Get a count of rows from health log table
  330. */
  331. #define SQL_COUNT_HEALTH_LOG(guid) "SELECT count(1) FROM health_log_%s;", guid
  332. void sql_health_alarm_log_count(RRDHOST *host) {
  333. sqlite3_stmt *res = NULL;
  334. int rc;
  335. char command[MAX_HEALTH_SQL_SIZE + 1];
  336. if (unlikely(!db_meta)) {
  337. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  338. error_report("Database has not been initialized");
  339. return;
  340. }
  341. char uuid_str[GUID_LEN + 1];
  342. uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
  343. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_COUNT_HEALTH_LOG(uuid_str));
  344. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  345. if (unlikely(rc != SQLITE_OK)) {
  346. error_report("Failed to prepare statement to count health log entries from db");
  347. return;
  348. }
  349. rc = sqlite3_step(res);
  350. if (likely(rc == SQLITE_ROW))
  351. host->health_log_entries_written = (size_t) sqlite3_column_int64(res, 0);
  352. rc = sqlite3_finalize(res);
  353. if (unlikely(rc != SQLITE_OK))
  354. error_report("Failed to finalize the prepared statement to count health log entries from db");
  355. info("HEALTH [%s]: Table health_log_%s, contains %lu entries.", host->hostname, uuid_str, host->health_log_entries_written);
  356. }
  357. #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, " \
  358. "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) " \
  359. "select hostname, ?1, ?2, ?3, config_hash_id, 0, ?4, unixepoch(), 0, 0, flags, exec_run_timestamp, " \
  360. "unixepoch(), name, chart, family, exec, recipient, source, units, info, exec_code, -2, new_status, delay, NULL, new_value, 0, class, component, type, chart_context " \
  361. "from health_log_%s where unique_id = ?5", guid, guid2
  362. #define SQL_INJECT_REMOVED_UPDATE(guid) "update health_log_%s set flags = flags | ?1, updated_by_id = ?2 where unique_id = ?3; ", guid
  363. 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)
  364. {
  365. int rc = 0;
  366. char command[MAX_HEALTH_SQL_SIZE + 1];
  367. if (!alarm_id || !alarm_event_id || !unique_id || !max_unique_id)
  368. return;
  369. sqlite3_stmt *res = NULL;
  370. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_INJECT_REMOVED(uuid_str, uuid_str));
  371. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  372. if (rc != SQLITE_OK) {
  373. error_report("Failed to prepare statement when trying to inject removed event");
  374. return;
  375. }
  376. rc = sqlite3_bind_int64(res, 1, (sqlite3_int64) max_unique_id);
  377. if (unlikely(rc != SQLITE_OK)) {
  378. error_report("Failed to bind max_unique_id parameter for SQL_INJECT_REMOVED");
  379. goto failed;
  380. }
  381. rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) alarm_id);
  382. if (unlikely(rc != SQLITE_OK)) {
  383. error_report("Failed to bind alarm_id parameter for SQL_INJECT_REMOVED");
  384. goto failed;
  385. }
  386. rc = sqlite3_bind_int64(res, 3, (sqlite3_int64) alarm_event_id + 1);
  387. if (unlikely(rc != SQLITE_OK)) {
  388. error_report("Failed to bind alarm_event_id parameter for SQL_INJECT_REMOVED");
  389. goto failed;
  390. }
  391. rc = sqlite3_bind_int64(res, 4, (sqlite3_int64) unique_id);
  392. if (unlikely(rc != SQLITE_OK)) {
  393. error_report("Failed to bind unique_id parameter for SQL_INJECT_REMOVED");
  394. goto failed;
  395. }
  396. rc = sqlite3_bind_int64(res, 5, (sqlite3_int64) unique_id);
  397. if (unlikely(rc != SQLITE_OK)) {
  398. error_report("Failed to bind unique_id parameter for SQL_INJECT_REMOVED");
  399. goto failed;
  400. }
  401. rc = execute_insert(res);
  402. if (unlikely(rc != SQLITE_DONE)) {
  403. error_report("HEALTH [N/A]: Failed to execute SQL_INJECT_REMOVED, rc = %d", rc);
  404. goto failed;
  405. }
  406. if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
  407. error_report("HEALTH [N/A]: Failed to finalize the prepared statement for injecting removed event.");
  408. //update the old entry
  409. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_INJECT_REMOVED_UPDATE(uuid_str));
  410. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  411. if (rc != SQLITE_OK) {
  412. error_report("Failed to prepare statement when trying to update during inject removed event");
  413. return;
  414. }
  415. rc = sqlite3_bind_int64(res, 1, (sqlite3_int64) HEALTH_ENTRY_FLAG_UPDATED);
  416. if (unlikely(rc != SQLITE_OK)) {
  417. error_report("Failed to bind flags parameter for SQL_INJECT_REMOVED (update)");
  418. goto failed;
  419. }
  420. rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) max_unique_id);
  421. if (unlikely(rc != SQLITE_OK)) {
  422. error_report("Failed to bind max_unique_id parameter for SQL_INJECT_REMOVED (update)");
  423. goto failed;
  424. }
  425. rc = sqlite3_bind_int64(res, 3, (sqlite3_int64) unique_id);
  426. if (unlikely(rc != SQLITE_OK)) {
  427. error_report("Failed to bind unique_id parameter for SQL_INJECT_REMOVED (update)");
  428. goto failed;
  429. }
  430. rc = execute_insert(res);
  431. if (unlikely(rc != SQLITE_DONE)) {
  432. error_report("HEALTH [N/A]: Failed to execute SQL_INJECT_REMOVED_UPDATE, rc = %d", rc);
  433. goto failed;
  434. }
  435. failed:
  436. if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
  437. error_report("HEALTH [N/A]: Failed to finalize the prepared statement for injecting removed event.");
  438. return;
  439. }
  440. #define SQL_SELECT_MAX_UNIQUE_ID(guid) "SELECT MAX(unique_id) from health_log_%s", guid
  441. uint32_t sql_get_max_unique_id (char *uuid_str)
  442. {
  443. int rc = 0;
  444. char command[MAX_HEALTH_SQL_SIZE + 1];
  445. uint32_t max_unique_id = 0;
  446. sqlite3_stmt *res = NULL;
  447. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_SELECT_MAX_UNIQUE_ID(uuid_str));
  448. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  449. if (rc != SQLITE_OK) {
  450. error_report("Failed to prepare statement when trying to get max unique id");
  451. return 0;
  452. }
  453. while (sqlite3_step(res) == SQLITE_ROW) {
  454. max_unique_id = (uint32_t) sqlite3_column_int64(res, 0);
  455. }
  456. rc = sqlite3_finalize(res);
  457. if (unlikely(rc != SQLITE_OK))
  458. error_report("Failed to finalize the statement");
  459. return max_unique_id;
  460. }
  461. #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
  462. void sql_check_removed_alerts_state(char *uuid_str)
  463. {
  464. int rc = 0;
  465. char command[MAX_HEALTH_SQL_SIZE + 1];
  466. RRDCALC_STATUS status;
  467. uint32_t alarm_id = 0, alarm_event_id = 0, unique_id = 0, max_unique_id = 0;
  468. sqlite3_stmt *res = NULL;
  469. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_SELECT_LAST_STATUSES(uuid_str));
  470. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  471. if (rc != SQLITE_OK) {
  472. error_report("Failed to prepare statement when trying to check removed statuses");
  473. return;
  474. }
  475. while (sqlite3_step(res) == SQLITE_ROW) {
  476. status = (RRDCALC_STATUS) sqlite3_column_int(res, 0);
  477. unique_id = (uint32_t) sqlite3_column_int64(res, 1);
  478. alarm_id = (uint32_t) sqlite3_column_int64(res, 2);
  479. alarm_event_id = (uint32_t) sqlite3_column_int64(res, 3);
  480. if (unlikely(status != RRDCALC_STATUS_REMOVED)) {
  481. if (unlikely(!max_unique_id))
  482. max_unique_id = sql_get_max_unique_id (uuid_str);
  483. sql_inject_removed_status (uuid_str, alarm_id, alarm_event_id, unique_id, ++max_unique_id);
  484. }
  485. }
  486. rc = sqlite3_finalize(res);
  487. if (unlikely(rc != SQLITE_OK))
  488. error_report("Failed to finalize the statement");
  489. }
  490. /* Health related SQL queries
  491. Load from the health log table
  492. */
  493. #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, chart_context 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, chart_context FROM health_log_%s order by unique_id desc limit %u) order by unique_id asc;", guid, limit
  494. void sql_health_alarm_log_load(RRDHOST *host) {
  495. sqlite3_stmt *res = NULL;
  496. int rc;
  497. ssize_t errored = 0, loaded = 0;
  498. char command[MAX_HEALTH_SQL_SIZE + 1];
  499. host->health_log_entries_written = 0;
  500. if (unlikely(!db_meta)) {
  501. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  502. error_report("HEALTH [%s]: Database has not been initialized", host->hostname);
  503. return;
  504. }
  505. char uuid_str[GUID_LEN + 1];
  506. uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
  507. sql_check_removed_alerts_state(uuid_str);
  508. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_LOAD_HEALTH_LOG(uuid_str, host->health_log.max));
  509. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  510. if (unlikely(rc != SQLITE_OK)) {
  511. error_report("HEALTH [%s]: Failed to prepare sql statement to load health log.", host->hostname);
  512. return;
  513. }
  514. netdata_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
  515. while (sqlite3_step(res) == SQLITE_ROW) {
  516. ALARM_ENTRY *ae = NULL;
  517. // check that we have valid ids
  518. uint32_t unique_id = (uint32_t) sqlite3_column_int64(res, 1);
  519. if(!unique_id) {
  520. error_report("HEALTH [%s]: Got invalid unique id. Ignoring it.", host->hostname);
  521. errored++;
  522. continue;
  523. }
  524. uint32_t alarm_id = (uint32_t) sqlite3_column_int64(res, 2);
  525. if(!alarm_id) {
  526. error_report("HEALTH [%s]: Got invalid alarm id. Ignoring it.", host->hostname);
  527. errored++;
  528. continue;
  529. }
  530. //need name, chart and family
  531. if (sqlite3_column_type(res, 13) == SQLITE_NULL) {
  532. error_report("HEALTH [%s]: Got null name field. Ignoring it.", host->hostname);
  533. errored++;
  534. continue;
  535. }
  536. if (sqlite3_column_type(res, 14) == SQLITE_NULL) {
  537. error_report("HEALTH [%s]: Got null chart field. Ignoring it.", host->hostname);
  538. errored++;
  539. continue;
  540. }
  541. if (sqlite3_column_type(res, 15) == SQLITE_NULL) {
  542. error_report("HEALTH [%s]: Got null family field. Ignoring it.", host->hostname);
  543. errored++;
  544. continue;
  545. }
  546. // Check if we got last_repeat field
  547. time_t last_repeat = 0;
  548. last_repeat = (time_t)sqlite3_column_int64(res, 27);
  549. RRDCALC *rc = alarm_max_last_repeat(host, (char *) sqlite3_column_text(res, 14), simple_hash((char *) sqlite3_column_text(res, 14)));
  550. if (!rc) {
  551. for(rc = host->alarms; rc ; rc = rc->next) {
  552. RRDCALC *rdcmp = (RRDCALC *) avl_insert_lock(&(host)->alarms_idx_name, (avl_t *)rc);
  553. if(rdcmp != rc) {
  554. error("Cannot insert the alarm index ID using log %s", rc->name);
  555. }
  556. }
  557. rc = alarm_max_last_repeat(host, (char *) sqlite3_column_text(res, 14), simple_hash((char *) sqlite3_column_text(res, 14)));
  558. }
  559. if(unlikely(rc)) {
  560. if (rrdcalc_isrepeating(rc)) {
  561. rc->last_repeat = last_repeat;
  562. // We iterate through repeating alarm entries only to
  563. // find the latest last_repeat timestamp. Otherwise,
  564. // there is no need to keep them in memory.
  565. continue;
  566. }
  567. }
  568. ae = callocz(1, sizeof(ALARM_ENTRY));
  569. ae->unique_id = unique_id;
  570. ae->alarm_id = alarm_id;
  571. if (sqlite3_column_type(res, 4) != SQLITE_NULL)
  572. uuid_copy(ae->config_hash_id, *((uuid_t *) sqlite3_column_blob(res, 4)));
  573. ae->alarm_event_id = (uint32_t) sqlite3_column_int64(res, 3);
  574. ae->updated_by_id = (uint32_t) sqlite3_column_int64(res, 5);
  575. ae->updates_id = (uint32_t) sqlite3_column_int64(res, 6);
  576. ae->when = (time_t) sqlite3_column_int64(res, 7);
  577. ae->duration = (time_t) sqlite3_column_int64(res, 8);
  578. ae->non_clear_duration = (time_t) sqlite3_column_int64(res, 9);
  579. ae->flags = (uint32_t) sqlite3_column_int64(res, 10);
  580. ae->flags |= HEALTH_ENTRY_FLAG_SAVED;
  581. ae->exec_run_timestamp = (time_t) sqlite3_column_int64(res, 11);
  582. ae->delay_up_to_timestamp = (time_t) sqlite3_column_int64(res, 12);
  583. ae->name = strdupz((char *) sqlite3_column_text(res, 13));
  584. ae->hash_name = simple_hash(ae->name);
  585. ae->chart = strdupz((char *) sqlite3_column_text(res, 14));
  586. ae->hash_chart = simple_hash(ae->chart);
  587. ae->family = strdupz((char *) sqlite3_column_text(res, 15));
  588. if (sqlite3_column_type(res, 16) != SQLITE_NULL)
  589. ae->exec = strdupz((char *) sqlite3_column_text(res, 16));
  590. else
  591. ae->exec = NULL;
  592. if (sqlite3_column_type(res, 17) != SQLITE_NULL)
  593. ae->recipient = strdupz((char *) sqlite3_column_text(res, 17));
  594. else
  595. ae->recipient = NULL;
  596. if (sqlite3_column_type(res, 18) != SQLITE_NULL)
  597. ae->source = strdupz((char *) sqlite3_column_text(res, 18));
  598. else
  599. ae->source = NULL;
  600. if (sqlite3_column_type(res, 19) != SQLITE_NULL)
  601. ae->units = strdupz((char *) sqlite3_column_text(res, 19));
  602. else
  603. ae->units = NULL;
  604. if (sqlite3_column_type(res, 20) != SQLITE_NULL)
  605. ae->info = strdupz((char *) sqlite3_column_text(res, 20));
  606. else
  607. ae->info = NULL;
  608. ae->exec_code = (int) sqlite3_column_int(res, 21);
  609. ae->new_status = (RRDCALC_STATUS) sqlite3_column_int(res, 22);
  610. ae->old_status = (RRDCALC_STATUS)sqlite3_column_int(res, 23);
  611. ae->delay = (int) sqlite3_column_int(res, 24);
  612. ae->new_value = (NETDATA_DOUBLE) sqlite3_column_double(res, 25);
  613. ae->old_value = (NETDATA_DOUBLE) sqlite3_column_double(res, 26);
  614. ae->last_repeat = last_repeat;
  615. if (sqlite3_column_type(res, 28) != SQLITE_NULL)
  616. ae->classification = strdupz((char *) sqlite3_column_text(res, 28));
  617. else
  618. ae->classification = NULL;
  619. if (sqlite3_column_type(res, 29) != SQLITE_NULL)
  620. ae->component = strdupz((char *) sqlite3_column_text(res, 29));
  621. else
  622. ae->component = NULL;
  623. if (sqlite3_column_type(res, 30) != SQLITE_NULL)
  624. ae->type = strdupz((char *) sqlite3_column_text(res, 30));
  625. else
  626. ae->type = NULL;
  627. if (sqlite3_column_type(res, 31) != SQLITE_NULL)
  628. ae->chart_context = strdupz((char *) sqlite3_column_text(res, 31));
  629. else
  630. ae->chart_context = NULL;
  631. char value_string[100 + 1];
  632. freez(ae->old_value_string);
  633. freez(ae->new_value_string);
  634. ae->old_value_string = strdupz(format_value_and_unit(value_string, 100, ae->old_value, ae->units, -1));
  635. ae->new_value_string = strdupz(format_value_and_unit(value_string, 100, ae->new_value, ae->units, -1));
  636. ae->next = host->health_log.alarms;
  637. host->health_log.alarms = ae;
  638. if(unlikely(ae->unique_id > host->health_max_unique_id))
  639. host->health_max_unique_id = ae->unique_id;
  640. if(unlikely(ae->alarm_id >= host->health_max_alarm_id))
  641. host->health_max_alarm_id = ae->alarm_id;
  642. loaded++;
  643. }
  644. netdata_rwlock_unlock(&host->health_log.alarm_log_rwlock);
  645. if(!host->health_max_unique_id) host->health_max_unique_id = (uint32_t)now_realtime_sec();
  646. if(!host->health_max_alarm_id) host->health_max_alarm_id = (uint32_t)now_realtime_sec();
  647. host->health_log.next_log_id = host->health_max_unique_id + 1;
  648. if (unlikely(!host->health_log.next_alarm_id || host->health_log.next_alarm_id <= host->health_max_alarm_id))
  649. host->health_log.next_alarm_id = host->health_max_alarm_id + 1;
  650. info("HEALTH [%s]: Table health_log_%s, loaded %zd alarm entries, errors in %zd entries.", host->hostname, uuid_str, loaded, errored);
  651. rc = sqlite3_finalize(res);
  652. if (unlikely(rc != SQLITE_OK))
  653. error_report("Failed to finalize the health log read statement");
  654. sql_health_alarm_log_count(host);
  655. }
  656. /*
  657. * Store an alert config hash in the database
  658. */
  659. #define SQL_STORE_ALERT_CONFIG_HASH "insert or replace into alert_hash (hash_id, date_updated, alarm, template, " \
  660. "on_key, class, component, type, os, hosts, lookup, every, units, calc, families, plugin, module, " \
  661. "charts, green, red, warn, crit, exec, to_key, info, delay, options, repeat, host_labels, " \
  662. "p_db_lookup_dimensions, p_db_lookup_method, p_db_lookup_options, p_db_lookup_after, " \
  663. "p_db_lookup_before, p_update_every) values (?1,unixepoch(),?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12," \
  664. "?13,?14,?15,?16,?17,?18,?19,?20,?21,?22,?23,?24,?25,?26,?27,?28,?29,?30,?31,?32,?33,?34);"
  665. int sql_store_alert_config_hash(uuid_t *hash_id, struct alert_config *cfg)
  666. {
  667. static __thread sqlite3_stmt *res = NULL;
  668. int rc, param = 0;
  669. if (unlikely(!db_meta)) {
  670. if (default_rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE)
  671. return 0;
  672. error_report("Database has not been initialized");
  673. return 1;
  674. }
  675. if (unlikely(!res)) {
  676. rc = prepare_statement(db_meta, SQL_STORE_ALERT_CONFIG_HASH, &res);
  677. if (unlikely(rc != SQLITE_OK)) {
  678. error_report("Failed to prepare statement to store alert configuration, rc = %d", rc);
  679. return 1;
  680. }
  681. }
  682. param++;
  683. rc = sqlite3_bind_blob(res, 1, hash_id, sizeof(*hash_id), SQLITE_STATIC);
  684. if (unlikely(rc != SQLITE_OK))
  685. goto bind_fail;
  686. param++;
  687. if (cfg->alarm && *cfg->alarm)
  688. rc = sqlite3_bind_text(res, 2, cfg->alarm, -1, SQLITE_STATIC);
  689. else
  690. rc = sqlite3_bind_null(res, 2);
  691. if (unlikely(rc != SQLITE_OK))
  692. goto bind_fail;
  693. param++;
  694. if (cfg->template_key && *cfg->template_key)
  695. rc = sqlite3_bind_text(res, 3, cfg->template_key, -1, SQLITE_STATIC);
  696. else
  697. rc = sqlite3_bind_null(res, 3);
  698. if (unlikely(rc != SQLITE_OK))
  699. goto bind_fail;
  700. param++;
  701. rc = sqlite3_bind_text(res, 4, cfg->on, -1, SQLITE_STATIC);
  702. if (unlikely(rc != SQLITE_OK))
  703. goto bind_fail;
  704. param++;
  705. rc = sqlite3_bind_text(res, 5, cfg->classification, -1, SQLITE_STATIC);
  706. if (unlikely(rc != SQLITE_OK))
  707. goto bind_fail;
  708. param++;
  709. rc = sqlite3_bind_text(res, 6, cfg->component, -1, SQLITE_STATIC);
  710. if (unlikely(rc != SQLITE_OK))
  711. goto bind_fail;
  712. param++;
  713. rc = sqlite3_bind_text(res, 7, cfg->type, -1, SQLITE_STATIC);
  714. if (unlikely(rc != SQLITE_OK))
  715. goto bind_fail;
  716. param++;
  717. rc = sqlite3_bind_text(res, 8, cfg->os, -1, SQLITE_STATIC);
  718. if (unlikely(rc != SQLITE_OK))
  719. goto bind_fail;
  720. param++;
  721. rc = sqlite3_bind_text(res, 9, cfg->host, -1, SQLITE_STATIC);
  722. if (unlikely(rc != SQLITE_OK))
  723. goto bind_fail;
  724. param++;
  725. rc = sqlite3_bind_text(res, 10, cfg->lookup, -1, SQLITE_STATIC);
  726. if (unlikely(rc != SQLITE_OK))
  727. goto bind_fail;
  728. param++;
  729. rc = sqlite3_bind_text(res, 11, cfg->every, -1, SQLITE_STATIC);
  730. if (unlikely(rc != SQLITE_OK))
  731. goto bind_fail;
  732. param++;
  733. rc = sqlite3_bind_text(res, 12, cfg->units, -1, SQLITE_STATIC);
  734. if (unlikely(rc != SQLITE_OK))
  735. goto bind_fail;
  736. param++;
  737. rc = sqlite3_bind_text(res, 13, cfg->calc, -1, SQLITE_STATIC);
  738. if (unlikely(rc != SQLITE_OK))
  739. goto bind_fail;
  740. param++;
  741. rc = sqlite3_bind_text(res, 14, cfg->families, -1, SQLITE_STATIC);
  742. if (unlikely(rc != SQLITE_OK))
  743. goto bind_fail;
  744. param++;
  745. rc = sqlite3_bind_text(res, 15, cfg->plugin, -1, SQLITE_STATIC);
  746. if (unlikely(rc != SQLITE_OK))
  747. goto bind_fail;
  748. param++;
  749. rc = sqlite3_bind_text(res, 16, cfg->module, -1, SQLITE_STATIC);
  750. if (unlikely(rc != SQLITE_OK))
  751. goto bind_fail;
  752. param++;
  753. rc = sqlite3_bind_text(res, 17, cfg->charts, -1, SQLITE_STATIC);
  754. if (unlikely(rc != SQLITE_OK))
  755. goto bind_fail;
  756. param++;
  757. rc = sqlite3_bind_text(res, 18, cfg->green, -1, SQLITE_STATIC);
  758. if (unlikely(rc != SQLITE_OK))
  759. goto bind_fail;
  760. param++;
  761. rc = sqlite3_bind_text(res, 19, cfg->red, -1, SQLITE_STATIC);
  762. if (unlikely(rc != SQLITE_OK))
  763. goto bind_fail;
  764. param++;
  765. rc = sqlite3_bind_text(res, 20, cfg->warn, -1, SQLITE_STATIC);
  766. if (unlikely(rc != SQLITE_OK))
  767. goto bind_fail;
  768. param++;
  769. rc = sqlite3_bind_text(res, 21, cfg->crit, -1, SQLITE_STATIC);
  770. if (unlikely(rc != SQLITE_OK))
  771. goto bind_fail;
  772. param++;
  773. rc = sqlite3_bind_text(res, 22, cfg->exec, -1, SQLITE_STATIC);
  774. if (unlikely(rc != SQLITE_OK))
  775. goto bind_fail;
  776. param++;
  777. rc = sqlite3_bind_text(res, 23, cfg->to, -1, SQLITE_STATIC);
  778. if (unlikely(rc != SQLITE_OK))
  779. goto bind_fail;
  780. param++;
  781. rc = sqlite3_bind_text(res, 24, cfg->info, -1, SQLITE_STATIC);
  782. if (unlikely(rc != SQLITE_OK))
  783. goto bind_fail;
  784. param++;
  785. rc = sqlite3_bind_text(res, 25, cfg->delay, -1, SQLITE_STATIC);
  786. if (unlikely(rc != SQLITE_OK))
  787. goto bind_fail;
  788. param++;
  789. rc = sqlite3_bind_text(res, 26, cfg->options, -1, SQLITE_STATIC);
  790. if (unlikely(rc != SQLITE_OK))
  791. goto bind_fail;
  792. param++;
  793. rc = sqlite3_bind_text(res, 27, cfg->repeat, -1, SQLITE_STATIC);
  794. if (unlikely(rc != SQLITE_OK))
  795. goto bind_fail;
  796. param++;
  797. rc = sqlite3_bind_text(res, 28, cfg->host_labels, -1, SQLITE_STATIC);
  798. if (unlikely(rc != SQLITE_OK))
  799. goto bind_fail;
  800. if (cfg->p_db_lookup_after) {
  801. param++;
  802. rc = sqlite3_bind_text(res, 29, cfg->p_db_lookup_dimensions, -1, SQLITE_STATIC);
  803. if (unlikely(rc != SQLITE_OK))
  804. goto bind_fail;
  805. param++;
  806. rc = sqlite3_bind_text(res, 30, cfg->p_db_lookup_method, -1, SQLITE_STATIC);
  807. if (unlikely(rc != SQLITE_OK))
  808. goto bind_fail;
  809. param++;
  810. rc = sqlite3_bind_int(res, 31, cfg->p_db_lookup_options);
  811. if (unlikely(rc != SQLITE_OK))
  812. goto bind_fail;
  813. param++;
  814. rc = sqlite3_bind_int(res, 32, cfg->p_db_lookup_after);
  815. if (unlikely(rc != SQLITE_OK))
  816. goto bind_fail;
  817. param++;
  818. rc = sqlite3_bind_int(res, 33, cfg->p_db_lookup_before);
  819. if (unlikely(rc != SQLITE_OK))
  820. goto bind_fail;
  821. } else {
  822. param++;
  823. rc = sqlite3_bind_null(res, 29);
  824. if (unlikely(rc != SQLITE_OK))
  825. goto bind_fail;
  826. param++;
  827. rc = sqlite3_bind_null(res, 30);
  828. if (unlikely(rc != SQLITE_OK))
  829. goto bind_fail;
  830. param++;
  831. rc = sqlite3_bind_null(res, 31);
  832. if (unlikely(rc != SQLITE_OK))
  833. goto bind_fail;
  834. param++;
  835. rc = sqlite3_bind_null(res, 32);
  836. if (unlikely(rc != SQLITE_OK))
  837. goto bind_fail;
  838. param++;
  839. rc = sqlite3_bind_null(res, 33);
  840. if (unlikely(rc != SQLITE_OK))
  841. goto bind_fail;
  842. }
  843. param++;
  844. rc = sqlite3_bind_int(res, 34, cfg->p_update_every);
  845. if (unlikely(rc != SQLITE_OK))
  846. goto bind_fail;
  847. rc = execute_insert(res);
  848. if (unlikely(rc != SQLITE_DONE))
  849. error_report("Failed to store alert config, rc = %d", rc);
  850. rc = sqlite3_reset(res);
  851. if (unlikely(rc != SQLITE_OK))
  852. error_report("Failed to reset statement in alert hash_id store function, rc = %d", rc);
  853. return 0;
  854. bind_fail:
  855. error_report("Failed to bind parameter %d to store alert hash_id, rc = %d", param, rc);
  856. rc = sqlite3_reset(res);
  857. if (unlikely(rc != SQLITE_OK))
  858. error_report("Failed to reset statement in alert hash_id store function, rc = %d", rc);
  859. return 1;
  860. }
  861. /*
  862. alert hashes are used for cloud communication.
  863. if cloud is disabled or openssl is not available (which will prevent cloud connectivity)
  864. skip hash calculations
  865. */
  866. #if !defined DISABLE_CLOUD && defined ENABLE_HTTPS
  867. #define DIGEST_ALERT_CONFIG_VAL(v) ((v) ? EVP_DigestUpdate(evpctx, (v), strlen((v))) : EVP_DigestUpdate(evpctx, "", 1))
  868. #endif
  869. int alert_hash_and_store_config(
  870. uuid_t hash_id,
  871. struct alert_config *cfg,
  872. int store_hash)
  873. {
  874. #if !defined DISABLE_CLOUD && defined ENABLE_HTTPS
  875. EVP_MD_CTX *evpctx;
  876. unsigned char hash_value[EVP_MAX_MD_SIZE];
  877. unsigned int hash_len;
  878. evpctx = EVP_MD_CTX_create();
  879. EVP_DigestInit_ex(evpctx, EVP_sha256(), NULL);
  880. DIGEST_ALERT_CONFIG_VAL(cfg->alarm);
  881. DIGEST_ALERT_CONFIG_VAL(cfg->template_key);
  882. DIGEST_ALERT_CONFIG_VAL(cfg->os);
  883. DIGEST_ALERT_CONFIG_VAL(cfg->host);
  884. DIGEST_ALERT_CONFIG_VAL(cfg->on);
  885. DIGEST_ALERT_CONFIG_VAL(cfg->families);
  886. DIGEST_ALERT_CONFIG_VAL(cfg->plugin);
  887. DIGEST_ALERT_CONFIG_VAL(cfg->module);
  888. DIGEST_ALERT_CONFIG_VAL(cfg->charts);
  889. DIGEST_ALERT_CONFIG_VAL(cfg->lookup);
  890. DIGEST_ALERT_CONFIG_VAL(cfg->calc);
  891. DIGEST_ALERT_CONFIG_VAL(cfg->every);
  892. DIGEST_ALERT_CONFIG_VAL(cfg->green);
  893. DIGEST_ALERT_CONFIG_VAL(cfg->red);
  894. DIGEST_ALERT_CONFIG_VAL(cfg->warn);
  895. DIGEST_ALERT_CONFIG_VAL(cfg->crit);
  896. DIGEST_ALERT_CONFIG_VAL(cfg->exec);
  897. DIGEST_ALERT_CONFIG_VAL(cfg->to);
  898. DIGEST_ALERT_CONFIG_VAL(cfg->units);
  899. DIGEST_ALERT_CONFIG_VAL(cfg->info);
  900. DIGEST_ALERT_CONFIG_VAL(cfg->classification);
  901. DIGEST_ALERT_CONFIG_VAL(cfg->component);
  902. DIGEST_ALERT_CONFIG_VAL(cfg->type);
  903. DIGEST_ALERT_CONFIG_VAL(cfg->delay);
  904. DIGEST_ALERT_CONFIG_VAL(cfg->options);
  905. DIGEST_ALERT_CONFIG_VAL(cfg->repeat);
  906. DIGEST_ALERT_CONFIG_VAL(cfg->host_labels);
  907. EVP_DigestFinal_ex(evpctx, hash_value, &hash_len);
  908. EVP_MD_CTX_destroy(evpctx);
  909. fatal_assert(hash_len > sizeof(uuid_t));
  910. char uuid_str[GUID_LEN + 1];
  911. uuid_unparse_lower(*((uuid_t *)&hash_value), uuid_str);
  912. uuid_copy(hash_id, *((uuid_t *)&hash_value));
  913. /* store everything, so it can be recreated when not in memory or just a subset ? */
  914. if (store_hash)
  915. (void)sql_store_alert_config_hash( (uuid_t *)&hash_value, cfg);
  916. #else
  917. UNUSED(hash_id);
  918. UNUSED(cfg);
  919. UNUSED(store_hash);
  920. #endif
  921. return 1;
  922. }