sqlite_health.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298
  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. Updates an entry in the table
  9. */
  10. #define SQL_UPDATE_HEALTH_LOG "UPDATE health_log_detail set updated_by_id = ?, flags = ?, exec_run_timestamp = ?, exec_code = ? where unique_id = ? AND alarm_id = ? and transition_id = ?;"
  11. void sql_health_alarm_log_update(RRDHOST *host, ALARM_ENTRY *ae) {
  12. sqlite3_stmt *res = NULL;
  13. int rc;
  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;
  18. }
  19. rc = sqlite3_prepare_v2(db_meta, SQL_UPDATE_HEALTH_LOG, -1, &res, 0);
  20. if (unlikely(rc != SQLITE_OK)) {
  21. error_report("HEALTH [%s]: Failed to prepare statement for SQL_UPDATE_HEALTH_LOG", rrdhost_hostname(host));
  22. return;
  23. }
  24. rc = sqlite3_bind_int64(res, 1, (sqlite3_int64) ae->updated_by_id);
  25. if (unlikely(rc != SQLITE_OK)) {
  26. error_report("Failed to bind updated_by_id parameter for SQL_UPDATE_HEALTH_LOG");
  27. goto failed;
  28. }
  29. rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) ae->flags);
  30. if (unlikely(rc != SQLITE_OK)) {
  31. error_report("Failed to bind flags parameter for SQL_UPDATE_HEALTH_LOG");
  32. goto failed;
  33. }
  34. rc = sqlite3_bind_int64(res, 3, (sqlite3_int64) ae->exec_run_timestamp);
  35. if (unlikely(rc != SQLITE_OK)) {
  36. error_report("Failed to bind exec_run_timestamp parameter for SQL_UPDATE_HEALTH_LOG");
  37. goto failed;
  38. }
  39. rc = sqlite3_bind_int(res, 4, ae->exec_code);
  40. if (unlikely(rc != SQLITE_OK)) {
  41. error_report("Failed to bind exec_code parameter for SQL_UPDATE_HEALTH_LOG");
  42. goto failed;
  43. }
  44. rc = sqlite3_bind_int64(res, 5, (sqlite3_int64) ae->unique_id);
  45. if (unlikely(rc != SQLITE_OK)) {
  46. error_report("Failed to bind unique_id parameter for SQL_UPDATE_HEALTH_LOG");
  47. goto failed;
  48. }
  49. rc = sqlite3_bind_int64(res, 6, (sqlite3_int64) ae->alarm_id);
  50. if (unlikely(rc != SQLITE_OK)) {
  51. error_report("Failed to bind unique_id parameter for SQL_UPDATE_HEALTH_LOG");
  52. goto failed;
  53. }
  54. rc = sqlite3_bind_blob(res, 7, &ae->transition_id, sizeof(ae->transition_id), SQLITE_STATIC);
  55. if (unlikely(rc != SQLITE_OK)) {
  56. error_report("Failed to bind host_id for SQL_UPDATE_HEALTH_LOG.");
  57. goto failed;
  58. }
  59. rc = execute_insert(res);
  60. if (unlikely(rc != SQLITE_DONE)) {
  61. error_report("HEALTH [%s]: Failed to update health log, rc = %d", rrdhost_hostname(host), rc);
  62. }
  63. failed:
  64. if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
  65. error_report("HEALTH [%s]: Failed to finalize the prepared statement for updating health log.", rrdhost_hostname(host));
  66. }
  67. /* Health related SQL queries
  68. Inserts an entry in the table
  69. */
  70. #define SQL_INSERT_HEALTH_LOG "INSERT INTO health_log (host_id, alarm_id, " \
  71. "config_hash_id, name, chart, family, exec, recipient, units, chart_context, last_transition_id, chart_name) " \
  72. "VALUES (?,?,?,?,?,?,?,?,?,?,?,?) " \
  73. "ON CONFLICT (host_id, alarm_id) DO UPDATE SET last_transition_id = excluded.last_transition_id, " \
  74. "chart_name = excluded.chart_name RETURNING health_log_id; "
  75. #define SQL_INSERT_HEALTH_LOG_DETAIL "INSERT INTO health_log_detail (health_log_id, unique_id, alarm_id, alarm_event_id, " \
  76. "updated_by_id, updates_id, when_key, duration, non_clear_duration, flags, exec_run_timestamp, delay_up_to_timestamp, " \
  77. "info, exec_code, new_status, old_status, delay, new_value, old_value, last_repeat, transition_id, global_id) " \
  78. "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,@global_id); "
  79. void sql_health_alarm_log_insert(RRDHOST *host, ALARM_ENTRY *ae) {
  80. sqlite3_stmt *res = NULL;
  81. int rc;
  82. uint64_t health_log_id = 0;
  83. if (unlikely(!db_meta)) {
  84. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  85. error_report("HEALTH [%s]: Database has not been initialized", rrdhost_hostname(host));
  86. return;
  87. }
  88. rc = sqlite3_prepare_v2(db_meta, SQL_INSERT_HEALTH_LOG, -1, &res, 0);
  89. if (unlikely(rc != SQLITE_OK)) {
  90. error_report("HEALTH [%s]: Failed to prepare statement for SQL_INSERT_HEALTH_LOG", rrdhost_hostname(host));
  91. return;
  92. }
  93. rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
  94. if (unlikely(rc != SQLITE_OK)) {
  95. error_report("Failed to bind host_id for SQL_INSERT_HEALTH_LOG.");
  96. goto failed;
  97. }
  98. rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) ae->alarm_id);
  99. if (unlikely(rc != SQLITE_OK)) {
  100. error_report("Failed to bind alarm_id parameter for SQL_INSERT_HEALTH_LOG");
  101. goto failed;
  102. }
  103. rc = sqlite3_bind_blob(res, 3, &ae->config_hash_id, sizeof(ae->config_hash_id), SQLITE_STATIC);
  104. if (unlikely(rc != SQLITE_OK)) {
  105. error_report("Failed to bind config_hash_id parameter for SQL_INSERT_HEALTH_LOG");
  106. goto failed;
  107. }
  108. rc = sqlite3_bind_string_or_null(res, ae->name, 4);
  109. if (unlikely(rc != SQLITE_OK)) {
  110. error_report("Failed to bind name parameter for SQL_INSERT_HEALTH_LOG");
  111. goto failed;
  112. }
  113. rc = sqlite3_bind_string_or_null(res, ae->chart, 5);
  114. if (unlikely(rc != SQLITE_OK)) {
  115. error_report("Failed to bind chart parameter for SQL_INSERT_HEALTH_LOG");
  116. goto failed;
  117. }
  118. rc = sqlite3_bind_string_or_null(res, ae->family, 6);
  119. if (unlikely(rc != SQLITE_OK)) {
  120. error_report("Failed to bind family parameter for SQL_INSERT_HEALTH_LOG");
  121. goto failed;
  122. }
  123. rc = sqlite3_bind_string_or_null(res, ae->exec, 7);
  124. if (unlikely(rc != SQLITE_OK)) {
  125. error_report("Failed to bind exec parameter for SQL_INSERT_HEALTH_LOG");
  126. goto failed;
  127. }
  128. rc = sqlite3_bind_string_or_null(res, ae->recipient, 8);
  129. if (unlikely(rc != SQLITE_OK)) {
  130. error_report("Failed to bind recipient parameter for SQL_INSERT_HEALTH_LOG");
  131. goto failed;
  132. }
  133. rc = sqlite3_bind_string_or_null(res, ae->units, 9);
  134. if (unlikely(rc != SQLITE_OK)) {
  135. error_report("Failed to bind host_id parameter to store node instance information");
  136. goto failed;
  137. }
  138. rc = sqlite3_bind_string_or_null(res, ae->chart_context, 10);
  139. if (unlikely(rc != SQLITE_OK)) {
  140. error_report("Failed to bind chart_context parameter for SQL_INSERT_HEALTH_LOG");
  141. goto failed;
  142. }
  143. rc = sqlite3_bind_blob(res, 11, &ae->transition_id, sizeof(ae->transition_id), SQLITE_STATIC);
  144. if (unlikely(rc != SQLITE_OK)) {
  145. error_report("Failed to bind transition_id parameter for SQL_INSERT_HEALTH_LOG");
  146. goto failed;
  147. }
  148. rc = sqlite3_bind_string_or_null(res, ae->chart_name, 12);
  149. if (unlikely(rc != SQLITE_OK)) {
  150. error_report("Failed to bind chart_name parameter for SQL_INSERT_HEALTH_LOG");
  151. goto failed;
  152. }
  153. rc = sqlite3_step_monitored(res);
  154. if (likely(rc == SQLITE_ROW))
  155. health_log_id = (size_t) sqlite3_column_int64(res, 0);
  156. else {
  157. error_report("HEALTH [%s]: Failed to execute SQL_INSERT_HEALTH_LOG, rc = %d", rrdhost_hostname(host), rc);
  158. goto failed;
  159. }
  160. rc = sqlite3_finalize(res);
  161. if (unlikely(rc != SQLITE_OK))
  162. error_report("HEALTH [%s]: Failed to finalize the prepared statement for inserting to health log.", rrdhost_hostname(host));
  163. rc = sqlite3_prepare_v2(db_meta, SQL_INSERT_HEALTH_LOG_DETAIL, -1, &res, 0);
  164. if (unlikely(rc != SQLITE_OK)) {
  165. error_report("HEALTH [%s]: Failed to prepare statement for SQL_INSERT_HEALTH_LOG_DETAIL", rrdhost_hostname(host));
  166. return;
  167. }
  168. rc = sqlite3_bind_int64(res, 1, (sqlite3_int64) health_log_id);
  169. if (unlikely(rc != SQLITE_OK)) {
  170. error_report("Failed to bind unique_id parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
  171. goto failed;
  172. }
  173. rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) ae->unique_id);
  174. if (unlikely(rc != SQLITE_OK)) {
  175. error_report("Failed to bind unique_id parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
  176. goto failed;
  177. }
  178. rc = sqlite3_bind_int64(res, 3, (sqlite3_int64) ae->alarm_id);
  179. if (unlikely(rc != SQLITE_OK)) {
  180. error_report("Failed to bind unique_id parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
  181. goto failed;
  182. }
  183. rc = sqlite3_bind_int64(res, 4, (sqlite3_int64) ae->alarm_event_id);
  184. if (unlikely(rc != SQLITE_OK)) {
  185. error_report("Failed to bind alarm_event_id parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
  186. goto failed;
  187. }
  188. rc = sqlite3_bind_int64(res, 5, (sqlite3_int64) ae->updated_by_id);
  189. if (unlikely(rc != SQLITE_OK)) {
  190. error_report("Failed to bind updated_by_id parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
  191. goto failed;
  192. }
  193. rc = sqlite3_bind_int64(res, 6, (sqlite3_int64) ae->updates_id);
  194. if (unlikely(rc != SQLITE_OK)) {
  195. error_report("Failed to bind updates_id parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
  196. goto failed;
  197. }
  198. rc = sqlite3_bind_int64(res, 7, (sqlite3_int64) ae->when);
  199. if (unlikely(rc != SQLITE_OK)) {
  200. error_report("Failed to bind when parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
  201. goto failed;
  202. }
  203. rc = sqlite3_bind_int64(res, 8, (sqlite3_int64) ae->duration);
  204. if (unlikely(rc != SQLITE_OK)) {
  205. error_report("Failed to bind duration parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
  206. goto failed;
  207. }
  208. rc = sqlite3_bind_int64(res, 9, (sqlite3_int64) ae->non_clear_duration);
  209. if (unlikely(rc != SQLITE_OK)) {
  210. error_report("Failed to bind non_clear_duration parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
  211. goto failed;
  212. }
  213. rc = sqlite3_bind_int64(res, 10, (sqlite3_int64) ae->flags);
  214. if (unlikely(rc != SQLITE_OK)) {
  215. error_report("Failed to bind flags parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
  216. goto failed;
  217. }
  218. rc = sqlite3_bind_int64(res, 11, (sqlite3_int64) ae->exec_run_timestamp);
  219. if (unlikely(rc != SQLITE_OK)) {
  220. error_report("Failed to bind exec_run_timestamp parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
  221. goto failed;
  222. }
  223. rc = sqlite3_bind_int64(res, 12, (sqlite3_int64) ae->delay_up_to_timestamp);
  224. if (unlikely(rc != SQLITE_OK)) {
  225. error_report("Failed to bind delay_up_to_timestamp parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
  226. goto failed;
  227. }
  228. rc = sqlite3_bind_string_or_null(res, ae->info, 13);
  229. if (unlikely(rc != SQLITE_OK)) {
  230. error_report("Failed to bind info parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
  231. goto failed;
  232. }
  233. rc = sqlite3_bind_int(res, 14, ae->exec_code);
  234. if (unlikely(rc != SQLITE_OK)) {
  235. error_report("Failed to bind exec_code parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
  236. goto failed;
  237. }
  238. rc = sqlite3_bind_int(res, 15, ae->new_status);
  239. if (unlikely(rc != SQLITE_OK)) {
  240. error_report("Failed to bind new_status parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
  241. goto failed;
  242. }
  243. rc = sqlite3_bind_int(res, 16, ae->old_status);
  244. if (unlikely(rc != SQLITE_OK)) {
  245. error_report("Failed to bind old_status parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
  246. goto failed;
  247. }
  248. rc = sqlite3_bind_int(res, 17, ae->delay);
  249. if (unlikely(rc != SQLITE_OK)) {
  250. error_report("Failed to bind delay parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
  251. goto failed;
  252. }
  253. rc = sqlite3_bind_double(res, 18, ae->new_value);
  254. if (unlikely(rc != SQLITE_OK)) {
  255. error_report("Failed to bind new_value parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
  256. goto failed;
  257. }
  258. rc = sqlite3_bind_double(res, 19, ae->old_value);
  259. if (unlikely(rc != SQLITE_OK)) {
  260. error_report("Failed to bind old_value parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
  261. goto failed;
  262. }
  263. rc = sqlite3_bind_int64(res, 20, (sqlite3_int64) ae->last_repeat);
  264. if (unlikely(rc != SQLITE_OK)) {
  265. error_report("Failed to bind last_repeat parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
  266. goto failed;
  267. }
  268. rc = sqlite3_bind_blob(res, 21, &ae->transition_id, sizeof(ae->transition_id), SQLITE_STATIC);
  269. if (unlikely(rc != SQLITE_OK)) {
  270. error_report("Failed to bind transition_id parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
  271. goto failed;
  272. }
  273. rc = sqlite3_bind_int64(res, 22, (sqlite3_int64) ae->global_id);
  274. if (unlikely(rc != SQLITE_OK)) {
  275. error_report("Failed to bind global_id parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
  276. goto failed;
  277. }
  278. rc = execute_insert(res);
  279. if (unlikely(rc != SQLITE_DONE)) {
  280. error_report("HEALTH [%s]: Failed to execute SQL_INSERT_HEALTH_LOG_DETAIL, rc = %d", rrdhost_hostname(host), rc);
  281. goto failed;
  282. }
  283. ae->flags |= HEALTH_ENTRY_FLAG_SAVED;
  284. host->health.health_log_entries_written++;
  285. failed:
  286. if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
  287. error_report("HEALTH [%s]: Failed to finalize the prepared statement for inserting to health log.", rrdhost_hostname(host));
  288. }
  289. void sql_health_alarm_log_save(RRDHOST *host, ALARM_ENTRY *ae)
  290. {
  291. if (ae->flags & HEALTH_ENTRY_FLAG_SAVED)
  292. sql_health_alarm_log_update(host, ae);
  293. else {
  294. sql_health_alarm_log_insert(host, ae);
  295. #ifdef ENABLE_ACLK
  296. if (netdata_cloud_enabled) {
  297. sql_queue_alarm_to_aclk(host, ae, 0);
  298. }
  299. #endif
  300. }
  301. }
  302. /* Health related SQL queries
  303. Get a count of rows from health log table
  304. */
  305. #define SQL_COUNT_HEALTH_LOG_DETAIL "SELECT count(1) FROM health_log_detail hld, health_log hl where hl.host_id = @host_id and hl.health_log_id = hld.health_log_id;"
  306. void sql_health_alarm_log_count(RRDHOST *host) {
  307. sqlite3_stmt *res = NULL;
  308. int rc;
  309. if (unlikely(!db_meta)) {
  310. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  311. error_report("Database has not been initialized");
  312. return;
  313. }
  314. rc = sqlite3_prepare_v2(db_meta, SQL_COUNT_HEALTH_LOG_DETAIL, -1, &res, 0);
  315. if (unlikely(rc != SQLITE_OK)) {
  316. error_report("Failed to prepare statement to count health log entries from db");
  317. return;
  318. }
  319. rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
  320. if (unlikely(rc != SQLITE_OK)) {
  321. error_report("Failed to bind host_id for SQL_COUNT_HEALTH_LOG.");
  322. sqlite3_finalize(res);
  323. return;
  324. }
  325. rc = sqlite3_step_monitored(res);
  326. if (likely(rc == SQLITE_ROW))
  327. host->health.health_log_entries_written = (size_t) sqlite3_column_int64(res, 0);
  328. rc = sqlite3_finalize(res);
  329. if (unlikely(rc != SQLITE_OK))
  330. error_report("Failed to finalize the prepared statement to count health log entries from db");
  331. netdata_log_info("HEALTH [%s]: Table health_log_detail contains %lu entries.", rrdhost_hostname(host), (unsigned long int) host->health.health_log_entries_written);
  332. }
  333. /* Health related SQL queries
  334. Cleans up the health_log_detail table on a non-claimed host
  335. */
  336. #define SQL_CLEANUP_HEALTH_LOG_DETAIL_NOT_CLAIMED "DELETE FROM health_log_detail WHERE health_log_id IN (SELECT health_log_id FROM health_log WHERE host_id = ?1) AND when_key + ?2 < unixepoch() AND updated_by_id <> 0 AND transition_id NOT IN (SELECT last_transition_id FROM health_log hl WHERE hl.host_id = ?3);"
  337. void sql_health_alarm_log_cleanup_not_claimed(RRDHOST *host) {
  338. sqlite3_stmt *res = NULL;
  339. int rc;
  340. char command[MAX_HEALTH_SQL_SIZE + 1];
  341. if (unlikely(!db_meta)) {
  342. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  343. error_report("Database has not been initialized");
  344. return;
  345. }
  346. char uuid_str[UUID_STR_LEN];
  347. uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
  348. rc = sqlite3_prepare_v2(db_meta, SQL_CLEANUP_HEALTH_LOG_DETAIL_NOT_CLAIMED, -1, &res, 0);
  349. if (unlikely(rc != SQLITE_OK)) {
  350. error_report("Failed to prepare statement to cleanup health log detail table (un-claimed)");
  351. return;
  352. }
  353. rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
  354. if (unlikely(rc != SQLITE_OK)) {
  355. error_report("Failed to bind host_id for SQL_CLEANUP_HEALTH_LOG_NOT_CLAIMED.");
  356. sqlite3_finalize(res);
  357. return;
  358. }
  359. rc = sqlite3_bind_int64(res, 2, (sqlite3_int64)host->health_log.health_log_history);
  360. if (unlikely(rc != SQLITE_OK)) {
  361. error_report("Failed to bind health log history for SQL_CLEANUP_HEALTH_LOG_NOT_CLAIMED.");
  362. sqlite3_finalize(res);
  363. return;
  364. }
  365. rc = sqlite3_bind_blob(res, 3, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
  366. if (unlikely(rc != SQLITE_OK)) {
  367. error_report("Failed to bind host_id for SQL_CLEANUP_HEALTH_LOG_NOT_CLAIMED.");
  368. sqlite3_finalize(res);
  369. return;
  370. }
  371. rc = sqlite3_step_monitored(res);
  372. if (unlikely(rc != SQLITE_DONE))
  373. error_report("Failed to cleanup health log detail table, rc = %d", rc);
  374. rc = sqlite3_finalize(res);
  375. if (unlikely(rc != SQLITE_OK))
  376. error_report("Failed to finalize the prepared statement to cleanup health log detail table (un-claimed)");
  377. sql_health_alarm_log_count(host);
  378. snprintfz(command, MAX_HEALTH_SQL_SIZE, "aclk_alert_%s", uuid_str);
  379. if (unlikely(table_exists_in_database(command))) {
  380. sql_aclk_alert_clean_dead_entries(host);
  381. }
  382. }
  383. /* Health related SQL queries
  384. Cleans up the health_log_detail table on a claimed host
  385. */
  386. #define SQL_CLEANUP_HEALTH_LOG_DETAIL_CLAIMED(guid) "DELETE from health_log_detail WHERE unique_id NOT IN (SELECT filtered_alert_unique_id FROM aclk_alert_%s) AND unique_id IN (SELECT hld.unique_id FROM health_log hl, health_log_detail hld WHERE hl.host_id = ?1 AND hl.health_log_id = hld.health_log_id) AND health_log_id IN (SELECT health_log_id FROM health_log WHERE host_id = ?2) AND when_key + ?3 < unixepoch() AND updated_by_id <> 0 AND transition_id NOT IN (SELECT last_transition_id FROM health_log hl WHERE hl.host_id = ?4);", guid
  387. void sql_health_alarm_log_cleanup_claimed(RRDHOST *host) {
  388. sqlite3_stmt *res = NULL;
  389. int rc;
  390. char command[MAX_HEALTH_SQL_SIZE + 1];
  391. if (unlikely(!db_meta)) {
  392. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  393. error_report("Database has not been initialized");
  394. return;
  395. }
  396. char uuid_str[UUID_STR_LEN];
  397. uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
  398. snprintfz(command, MAX_HEALTH_SQL_SIZE, "aclk_alert_%s", uuid_str);
  399. if (!table_exists_in_database(command)) {
  400. sql_health_alarm_log_cleanup_not_claimed(host);
  401. return;
  402. }
  403. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_CLEANUP_HEALTH_LOG_DETAIL_CLAIMED(uuid_str));
  404. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  405. if (unlikely(rc != SQLITE_OK)) {
  406. error_report("Failed to prepare statement to cleanup health log detail table (claimed)");
  407. return;
  408. }
  409. rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
  410. if (unlikely(rc != SQLITE_OK)) {
  411. error_report("Failed to bind first host_id for SQL_CLEANUP_HEALTH_LOG_CLAIMED.");
  412. sqlite3_finalize(res);
  413. return;
  414. }
  415. rc = sqlite3_bind_blob(res, 2, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
  416. if (unlikely(rc != SQLITE_OK)) {
  417. error_report("Failed to bind second host_id for SQL_CLEANUP_HEALTH_LOG_CLAIMED.");
  418. sqlite3_finalize(res);
  419. return;
  420. }
  421. rc = sqlite3_bind_int64(res, 3, (sqlite3_int64)host->health_log.health_log_history);
  422. if (unlikely(rc != SQLITE_OK)) {
  423. error_report("Failed to bind health log history for SQL_CLEANUP_HEALTH_LOG_CLAIMED.");
  424. sqlite3_finalize(res);
  425. return;
  426. }
  427. rc = sqlite3_bind_blob(res, 4, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
  428. if (unlikely(rc != SQLITE_OK)) {
  429. error_report("Failed to bind second host_id for SQL_CLEANUP_HEALTH_LOG_CLAIMED.");
  430. sqlite3_finalize(res);
  431. return;
  432. }
  433. rc = sqlite3_step_monitored(res);
  434. if (unlikely(rc != SQLITE_DONE))
  435. error_report("Failed to cleanup health log detail table, rc = %d", rc);
  436. rc = sqlite3_finalize(res);
  437. if (unlikely(rc != SQLITE_OK))
  438. error_report("Failed to finalize the prepared statement to cleanup health log detail table (claimed)");
  439. sql_health_alarm_log_count(host);
  440. sql_aclk_alert_clean_dead_entries(host);
  441. }
  442. /* Health related SQL queries
  443. Cleans up the health_log table.
  444. */
  445. void sql_health_alarm_log_cleanup(RRDHOST *host) {
  446. if (!claimed()) {
  447. sql_health_alarm_log_cleanup_not_claimed(host);
  448. } else
  449. sql_health_alarm_log_cleanup_claimed(host);
  450. }
  451. #define SQL_INJECT_REMOVED "insert into health_log_detail (health_log_id, unique_id, alarm_id, alarm_event_id, updated_by_id, updates_id, when_key, duration, non_clear_duration, flags, exec_run_timestamp, delay_up_to_timestamp, info, exec_code, new_status, old_status, delay, new_value, old_value, last_repeat, transition_id, global_id) select health_log_id, ?1, ?2, ?3, 0, ?4, unixepoch(), 0, 0, flags, exec_run_timestamp, unixepoch(), info, exec_code, -2, new_status, delay, NULL, new_value, 0, ?5, now_usec(0) from health_log_detail where unique_id = ?6 and transition_id = ?7;"
  452. #define SQL_INJECT_REMOVED_UPDATE_DETAIL "update health_log_detail set flags = flags | ?1, updated_by_id = ?2 where unique_id = ?3 and transition_id = ?4;"
  453. #define SQL_INJECT_REMOVED_UPDATE_LOG "update health_log set last_transition_id = ?1 where alarm_id = ?2 and last_transition_id = ?3 and host_id = ?4;"
  454. void sql_inject_removed_status(RRDHOST *host, uint32_t alarm_id, uint32_t alarm_event_id, uint32_t unique_id, uint32_t max_unique_id, uuid_t *prev_transition_id)
  455. {
  456. int rc;
  457. if (!alarm_id || !alarm_event_id || !unique_id || !max_unique_id)
  458. return;
  459. sqlite3_stmt *res = NULL;
  460. rc = sqlite3_prepare_v2(db_meta, SQL_INJECT_REMOVED, -1, &res, 0);
  461. if (rc != SQLITE_OK) {
  462. error_report("Failed to prepare statement when trying to inject removed event");
  463. return;
  464. }
  465. rc = sqlite3_bind_int64(res, 1, (sqlite3_int64) max_unique_id);
  466. if (unlikely(rc != SQLITE_OK)) {
  467. error_report("Failed to bind max_unique_id parameter for SQL_INJECT_REMOVED");
  468. goto failed;
  469. }
  470. rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) alarm_id);
  471. if (unlikely(rc != SQLITE_OK)) {
  472. error_report("Failed to bind alarm_id parameter for SQL_INJECT_REMOVED");
  473. goto failed;
  474. }
  475. rc = sqlite3_bind_int64(res, 3, (sqlite3_int64) alarm_event_id + 1);
  476. if (unlikely(rc != SQLITE_OK)) {
  477. error_report("Failed to bind alarm_event_id parameter for SQL_INJECT_REMOVED");
  478. goto failed;
  479. }
  480. rc = sqlite3_bind_int64(res, 4, (sqlite3_int64) unique_id);
  481. if (unlikely(rc != SQLITE_OK)) {
  482. error_report("Failed to bind unique_id parameter for SQL_INJECT_REMOVED");
  483. goto failed;
  484. }
  485. uuid_t transition_id;
  486. uuid_generate_random(transition_id);
  487. rc = sqlite3_bind_blob(res, 5, &transition_id, sizeof(transition_id), SQLITE_STATIC);
  488. if (unlikely(rc != SQLITE_OK)) {
  489. error_report("Failed to bind config_hash_id parameter for SQL_INJECT_REMOVED");
  490. goto failed;
  491. }
  492. rc = sqlite3_bind_int64(res, 6, (sqlite3_int64) unique_id);
  493. if (unlikely(rc != SQLITE_OK)) {
  494. error_report("Failed to bind unique_id parameter for SQL_INJECT_REMOVED");
  495. goto failed;
  496. }
  497. rc = sqlite3_bind_blob(res, 7, prev_transition_id, sizeof(*prev_transition_id), SQLITE_STATIC);
  498. if (unlikely(rc != SQLITE_OK)) {
  499. error_report("Failed to bind host_id parameter for SQL_INJECT_REMOVED.");
  500. goto failed;
  501. }
  502. rc = execute_insert(res);
  503. if (unlikely(rc != SQLITE_DONE)) {
  504. error_report("HEALTH [N/A]: Failed to execute SQL_INJECT_REMOVED, rc = %d", rc);
  505. goto failed;
  506. }
  507. if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
  508. error_report("HEALTH [N/A]: Failed to finalize the prepared statement for injecting removed event.");
  509. //update the old entry in health_log_detail
  510. rc = sqlite3_prepare_v2(db_meta, SQL_INJECT_REMOVED_UPDATE_DETAIL, -1, &res, 0);
  511. if (rc != SQLITE_OK) {
  512. error_report("Failed to prepare statement when trying to update health_log_detail during inject removed event");
  513. return;
  514. }
  515. rc = sqlite3_bind_int64(res, 1, (sqlite3_int64) HEALTH_ENTRY_FLAG_UPDATED);
  516. if (unlikely(rc != SQLITE_OK)) {
  517. error_report("Failed to bind flags parameter for SQL_INJECT_REMOVED_UPDATE_DETAIL");
  518. goto failed;
  519. }
  520. rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) max_unique_id);
  521. if (unlikely(rc != SQLITE_OK)) {
  522. error_report("Failed to bind max_unique_id parameter for SQL_INJECT_REMOVED_UPDATE_DETAIL");
  523. goto failed;
  524. }
  525. rc = sqlite3_bind_int64(res, 3, (sqlite3_int64) unique_id);
  526. if (unlikely(rc != SQLITE_OK)) {
  527. error_report("Failed to bind unique_id parameter for SQL_INJECT_REMOVED_UPDATE_DETAIL");
  528. goto failed;
  529. }
  530. rc = sqlite3_bind_blob(res, 4, prev_transition_id, sizeof(*prev_transition_id), SQLITE_STATIC);
  531. if (unlikely(rc != SQLITE_OK)) {
  532. error_report("Failed to bind host_id parameter for SQL_INJECT_REMOVED_UPDATE_DETAIL");
  533. goto failed;
  534. }
  535. rc = execute_insert(res);
  536. if (unlikely(rc != SQLITE_DONE)) {
  537. error_report("HEALTH [N/A]: Failed to execute SQL_INJECT_REMOVED_UPDATE_DETAIL, rc = %d", rc);
  538. goto failed;
  539. }
  540. //update the health_log_table
  541. rc = sqlite3_prepare_v2(db_meta, SQL_INJECT_REMOVED_UPDATE_LOG, -1, &res, 0);
  542. if (rc != SQLITE_OK) {
  543. error_report("Failed to prepare statement when trying to update health_log during inject removed event");
  544. return;
  545. }
  546. rc = sqlite3_bind_blob(res, 1, &transition_id, sizeof(transition_id), SQLITE_STATIC);
  547. if (unlikely(rc != SQLITE_OK)) {
  548. error_report("Failed to bind host_id parameter for SQL_INJECT_REMOVED_UPDATE_LOG");
  549. goto failed;
  550. }
  551. rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) alarm_id);
  552. if (unlikely(rc != SQLITE_OK)) {
  553. error_report("Failed to bind unique_id parameter for SQL_INJECT_REMOVED_UPDATE_DETAIL");
  554. goto failed;
  555. }
  556. rc = sqlite3_bind_blob(res, 3, prev_transition_id, sizeof(*prev_transition_id), SQLITE_STATIC);
  557. if (unlikely(rc != SQLITE_OK)) {
  558. error_report("Failed to bind host_id parameter for SQL_INJECT_REMOVED_UPDATE_LOG");
  559. goto failed;
  560. }
  561. rc = sqlite3_bind_blob(res, 4, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
  562. if (unlikely(rc != SQLITE_OK)) {
  563. error_report("Failed to bind host_id parameter for SQL_INJECT_REMOVED_UPDATE_DETAIL");
  564. goto failed;
  565. }
  566. rc = execute_insert(res);
  567. if (unlikely(rc != SQLITE_DONE)) {
  568. error_report("HEALTH [N/A]: Failed to execute SQL_INJECT_REMOVED_UPDATE_DETAIL, rc = %d", rc);
  569. goto failed;
  570. }
  571. failed:
  572. if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
  573. error_report("HEALTH [N/A]: Failed to finalize the prepared statement for injecting removed event.");
  574. }
  575. #define SQL_SELECT_MAX_UNIQUE_ID \
  576. "SELECT MAX(hld.unique_id) FROM health_log_detail hld, health_log hl " \
  577. "WHERE hl.host_id = @host_id AND hl.health_log_id = hld.health_log_id"
  578. uint32_t sql_get_max_unique_id (RRDHOST *host)
  579. {
  580. int rc;
  581. uint32_t max_unique_id = 0;
  582. sqlite3_stmt *res = NULL;
  583. rc = sqlite3_prepare_v2(db_meta, SQL_SELECT_MAX_UNIQUE_ID, -1, &res, 0);
  584. if (rc != SQLITE_OK) {
  585. error_report("Failed to prepare statement when trying to get max unique id");
  586. return 0;
  587. }
  588. rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
  589. if (unlikely(rc != SQLITE_OK)) {
  590. error_report("Failed to bind host_id parameter for SQL_SELECT_MAX_UNIQUE_ID.");
  591. sqlite3_finalize(res);
  592. return 0;
  593. }
  594. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  595. max_unique_id = (uint32_t) sqlite3_column_int64(res, 0);
  596. }
  597. rc = sqlite3_finalize(res);
  598. if (unlikely(rc != SQLITE_OK))
  599. error_report("Failed to finalize the statement");
  600. return max_unique_id;
  601. }
  602. #define SQL_SELECT_LAST_STATUSES "SELECT hld.new_status, hld.unique_id, hld.alarm_id, hld.alarm_event_id, hld.transition_id from health_log hl, health_log_detail hld where hl.host_id = @host_id and hl.last_transition_id = hld.transition_id;"
  603. void sql_check_removed_alerts_state(RRDHOST *host)
  604. {
  605. int rc;
  606. uint32_t max_unique_id = 0;
  607. sqlite3_stmt *res = NULL;
  608. uuid_t transition_id;
  609. rc = sqlite3_prepare_v2(db_meta, SQL_SELECT_LAST_STATUSES, -1, &res, 0);
  610. if (rc != SQLITE_OK) {
  611. error_report("Failed to prepare statement when trying to check removed statuses");
  612. return;
  613. }
  614. rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
  615. if (unlikely(rc != SQLITE_OK)) {
  616. error_report("Failed to bind host_id parameter for SQL_SELECT_LAST_STATUSES.");
  617. sqlite3_finalize(res);
  618. return;
  619. }
  620. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  621. uint32_t alarm_id, alarm_event_id, unique_id;
  622. RRDCALC_STATUS status;
  623. status = (RRDCALC_STATUS) sqlite3_column_int(res, 0);
  624. unique_id = (uint32_t) sqlite3_column_int64(res, 1);
  625. alarm_id = (uint32_t) sqlite3_column_int64(res, 2);
  626. alarm_event_id = (uint32_t) sqlite3_column_int64(res, 3);
  627. uuid_copy(transition_id, *((uuid_t *) sqlite3_column_blob(res, 4)));
  628. if (unlikely(status != RRDCALC_STATUS_REMOVED)) {
  629. if (unlikely(!max_unique_id))
  630. max_unique_id = sql_get_max_unique_id (host);
  631. sql_inject_removed_status (host, alarm_id, alarm_event_id, unique_id, ++max_unique_id, &transition_id);
  632. }
  633. }
  634. rc = sqlite3_finalize(res);
  635. if (unlikely(rc != SQLITE_OK))
  636. error_report("Failed to finalize the statement");
  637. }
  638. /* Health related SQL queries
  639. Load from the health log table
  640. */
  641. #define SQL_LOAD_HEALTH_LOG "SELECT hld.unique_id, hld.alarm_id, hld.alarm_event_id, hl.config_hash_id, hld.updated_by_id, " \
  642. "hld.updates_id, hld.when_key, hld.duration, hld.non_clear_duration, hld.flags, hld.exec_run_timestamp, " \
  643. "hld.delay_up_to_timestamp, hl.name, hl.chart, hl.family, hl.exec, hl.recipient, ah.source, hl.units, " \
  644. "hld.info, hld.exec_code, hld.new_status, hld.old_status, hld.delay, hld.new_value, hld.old_value, " \
  645. "hld.last_repeat, ah.class, ah.component, ah.type, hl.chart_context, hld.transition_id, hld.global_id, hl.chart_name " \
  646. "FROM health_log hl, alert_hash ah, health_log_detail hld " \
  647. "WHERE hl.config_hash_id = ah.hash_id and hl.host_id = @host_id and hl.last_transition_id = hld.transition_id;"
  648. void sql_health_alarm_log_load(RRDHOST *host) {
  649. sqlite3_stmt *res = NULL;
  650. int ret;
  651. ssize_t errored = 0, loaded = 0;
  652. host->health.health_log_entries_written = 0;
  653. if (unlikely(!db_meta)) {
  654. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  655. error_report("HEALTH [%s]: Database has not been initialized", rrdhost_hostname(host));
  656. return;
  657. }
  658. sql_check_removed_alerts_state(host);
  659. ret = sqlite3_prepare_v2(db_meta, SQL_LOAD_HEALTH_LOG, -1, &res, 0);
  660. if (unlikely(ret != SQLITE_OK)) {
  661. error_report("HEALTH [%s]: Failed to prepare sql statement to load health log.", rrdhost_hostname(host));
  662. return;
  663. }
  664. ret = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
  665. if (unlikely(ret != SQLITE_OK)) {
  666. error_report("Failed to bind host_id parameter for SQL_LOAD_HEALTH_LOG.");
  667. sqlite3_finalize(res);
  668. return;
  669. }
  670. DICTIONARY *all_rrdcalcs = dictionary_create(
  671. DICT_OPTION_NAME_LINK_DONT_CLONE | DICT_OPTION_VALUE_LINK_DONT_CLONE | DICT_OPTION_DONT_OVERWRITE_VALUE);
  672. RRDCALC *rc;
  673. foreach_rrdcalc_in_rrdhost_read(host, rc) {
  674. dictionary_set(all_rrdcalcs, rrdcalc_name(rc), rc, sizeof(*rc));
  675. }
  676. foreach_rrdcalc_in_rrdhost_done(rc);
  677. rw_spinlock_read_lock(&host->health_log.spinlock);
  678. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  679. ALARM_ENTRY *ae = NULL;
  680. // check that we have valid ids
  681. uint32_t unique_id = (uint32_t) sqlite3_column_int64(res, 0);
  682. if(!unique_id) {
  683. error_report("HEALTH [%s]: Got invalid unique id. Ignoring it.", rrdhost_hostname(host));
  684. errored++;
  685. continue;
  686. }
  687. uint32_t alarm_id = (uint32_t) sqlite3_column_int64(res, 1);
  688. if(!alarm_id) {
  689. error_report("HEALTH [%s]: Got invalid alarm id. Ignoring it.", rrdhost_hostname(host));
  690. errored++;
  691. continue;
  692. }
  693. //need name, chart and family
  694. if (sqlite3_column_type(res, 12) == SQLITE_NULL) {
  695. error_report("HEALTH [%s]: Got null name field. Ignoring it.", rrdhost_hostname(host));
  696. errored++;
  697. continue;
  698. }
  699. if (sqlite3_column_type(res, 13) == SQLITE_NULL) {
  700. error_report("HEALTH [%s]: Got null chart field. Ignoring it.", rrdhost_hostname(host));
  701. errored++;
  702. continue;
  703. }
  704. if (sqlite3_column_type(res, 14) == SQLITE_NULL) {
  705. error_report("HEALTH [%s]: Got null family field. Ignoring it.", rrdhost_hostname(host));
  706. errored++;
  707. continue;
  708. }
  709. // Check if we got last_repeat field
  710. time_t last_repeat = (time_t)sqlite3_column_int64(res, 26);
  711. rc = dictionary_get(all_rrdcalcs, (char *) sqlite3_column_text(res, 13));
  712. if(unlikely(rc)) {
  713. if (rrdcalc_isrepeating(rc)) {
  714. rc->last_repeat = last_repeat;
  715. // We iterate through repeating alarm entries only to
  716. // find the latest last_repeat timestamp. Otherwise,
  717. // there is no need to keep them in memory.
  718. continue;
  719. }
  720. }
  721. ae = callocz(1, sizeof(ALARM_ENTRY));
  722. ae->unique_id = unique_id;
  723. ae->alarm_id = alarm_id;
  724. if (sqlite3_column_type(res, 3) != SQLITE_NULL)
  725. uuid_copy(ae->config_hash_id, *((uuid_t *) sqlite3_column_blob(res, 3)));
  726. ae->alarm_event_id = (uint32_t) sqlite3_column_int64(res, 2);
  727. ae->updated_by_id = (uint32_t) sqlite3_column_int64(res, 4);
  728. ae->updates_id = (uint32_t) sqlite3_column_int64(res, 5);
  729. ae->when = (time_t) sqlite3_column_int64(res, 6);
  730. ae->duration = (time_t) sqlite3_column_int64(res, 7);
  731. ae->non_clear_duration = (time_t) sqlite3_column_int64(res, 8);
  732. ae->flags = (uint32_t) sqlite3_column_int64(res, 9);
  733. ae->flags |= HEALTH_ENTRY_FLAG_SAVED;
  734. ae->exec_run_timestamp = (time_t) sqlite3_column_int64(res, 10);
  735. ae->delay_up_to_timestamp = (time_t) sqlite3_column_int64(res, 11);
  736. ae->name = string_strdupz((char *) sqlite3_column_text(res, 12));
  737. ae->chart = string_strdupz((char *) sqlite3_column_text(res, 13));
  738. ae->family = string_strdupz((char *) sqlite3_column_text(res, 14));
  739. if (sqlite3_column_type(res, 15) != SQLITE_NULL)
  740. ae->exec = string_strdupz((char *) sqlite3_column_text(res, 15));
  741. else
  742. ae->exec = NULL;
  743. if (sqlite3_column_type(res, 16) != SQLITE_NULL)
  744. ae->recipient = string_strdupz((char *) sqlite3_column_text(res, 16));
  745. else
  746. ae->recipient = NULL;
  747. if (sqlite3_column_type(res, 17) != SQLITE_NULL)
  748. ae->source = string_strdupz((char *) sqlite3_column_text(res, 17));
  749. else
  750. ae->source = NULL;
  751. if (sqlite3_column_type(res, 18) != SQLITE_NULL)
  752. ae->units = string_strdupz((char *) sqlite3_column_text(res, 18));
  753. else
  754. ae->units = NULL;
  755. if (sqlite3_column_type(res, 19) != SQLITE_NULL)
  756. ae->info = string_strdupz((char *) sqlite3_column_text(res, 19));
  757. else
  758. ae->info = NULL;
  759. ae->exec_code = (int) sqlite3_column_int(res, 20);
  760. ae->new_status = (RRDCALC_STATUS) sqlite3_column_int(res, 21);
  761. ae->old_status = (RRDCALC_STATUS)sqlite3_column_int(res, 22);
  762. ae->delay = (int) sqlite3_column_int(res, 23);
  763. ae->new_value = (NETDATA_DOUBLE) sqlite3_column_double(res, 24);
  764. ae->old_value = (NETDATA_DOUBLE) sqlite3_column_double(res, 25);
  765. ae->last_repeat = last_repeat;
  766. if (sqlite3_column_type(res, 27) != SQLITE_NULL)
  767. ae->classification = string_strdupz((char *) sqlite3_column_text(res, 27));
  768. else
  769. ae->classification = NULL;
  770. if (sqlite3_column_type(res, 28) != SQLITE_NULL)
  771. ae->component = string_strdupz((char *) sqlite3_column_text(res, 28));
  772. else
  773. ae->component = NULL;
  774. if (sqlite3_column_type(res, 29) != SQLITE_NULL)
  775. ae->type = string_strdupz((char *) sqlite3_column_text(res, 29));
  776. else
  777. ae->type = NULL;
  778. if (sqlite3_column_type(res, 30) != SQLITE_NULL)
  779. ae->chart_context = string_strdupz((char *) sqlite3_column_text(res, 30));
  780. else
  781. ae->chart_context = NULL;
  782. if (sqlite3_column_type(res, 31) != SQLITE_NULL)
  783. uuid_copy(ae->transition_id, *((uuid_t *)sqlite3_column_blob(res, 31)));
  784. if (sqlite3_column_type(res, 32) != SQLITE_NULL)
  785. ae->global_id = sqlite3_column_int64(res, 32);
  786. if (sqlite3_column_type(res, 33) != SQLITE_NULL)
  787. ae->chart_name = string_strdupz((char *) sqlite3_column_text(res, 33));
  788. else
  789. ae->chart_name = NULL;
  790. char value_string[100 + 1];
  791. string_freez(ae->old_value_string);
  792. string_freez(ae->new_value_string);
  793. ae->old_value_string = string_strdupz(format_value_and_unit(value_string, 100, ae->old_value, ae_units(ae), -1));
  794. ae->new_value_string = string_strdupz(format_value_and_unit(value_string, 100, ae->new_value, ae_units(ae), -1));
  795. ae->next = host->health_log.alarms;
  796. host->health_log.alarms = ae;
  797. if(unlikely(ae->unique_id > host->health_max_unique_id))
  798. host->health_max_unique_id = ae->unique_id;
  799. if(unlikely(ae->alarm_id >= host->health_max_alarm_id))
  800. host->health_max_alarm_id = ae->alarm_id;
  801. loaded++;
  802. }
  803. rw_spinlock_read_unlock(&host->health_log.spinlock);
  804. dictionary_destroy(all_rrdcalcs);
  805. all_rrdcalcs = NULL;
  806. if(!host->health_max_unique_id) host->health_max_unique_id = (uint32_t)now_realtime_sec();
  807. if(!host->health_max_alarm_id) host->health_max_alarm_id = (uint32_t)now_realtime_sec();
  808. host->health_log.next_log_id = host->health_max_unique_id + 1;
  809. if (unlikely(!host->health_log.next_alarm_id || host->health_log.next_alarm_id <= host->health_max_alarm_id))
  810. host->health_log.next_alarm_id = host->health_max_alarm_id + 1;
  811. netdata_log_health("[%s]: Table health_log, loaded %zd alarm entries, errors in %zd entries.", rrdhost_hostname(host), loaded, errored);
  812. ret = sqlite3_finalize(res);
  813. if (unlikely(ret != SQLITE_OK))
  814. error_report("Failed to finalize the health log read statement");
  815. sql_health_alarm_log_count(host);
  816. }
  817. /*
  818. * Store an alert config hash in the database
  819. */
  820. #define SQL_STORE_ALERT_CONFIG_HASH "insert or replace into alert_hash (hash_id, date_updated, alarm, template, " \
  821. "on_key, class, component, type, os, hosts, lookup, every, units, calc, families, plugin, module, " \
  822. "charts, green, red, warn, crit, exec, to_key, info, delay, options, repeat, host_labels, " \
  823. "p_db_lookup_dimensions, p_db_lookup_method, p_db_lookup_options, p_db_lookup_after, " \
  824. "p_db_lookup_before, p_update_every, source, chart_labels) values (?1,unixepoch(),?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12," \
  825. "?13,?14,?15,?16,?17,?18,?19,?20,?21,?22,?23,?24,?25,?26,?27,?28,?29,?30,?31,?32,?33,?34,?35,?36);"
  826. int sql_store_alert_config_hash(uuid_t *hash_id, struct alert_config *cfg)
  827. {
  828. static __thread sqlite3_stmt *res = NULL;
  829. int rc, param = 0;
  830. if (unlikely(!db_meta)) {
  831. if (default_rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE)
  832. return 0;
  833. error_report("Database has not been initialized");
  834. return 1;
  835. }
  836. if (unlikely(!res)) {
  837. rc = prepare_statement(db_meta, SQL_STORE_ALERT_CONFIG_HASH, &res);
  838. if (unlikely(rc != SQLITE_OK)) {
  839. error_report("Failed to prepare statement to store alert configuration, rc = %d", rc);
  840. return 1;
  841. }
  842. }
  843. rc = sqlite3_bind_blob(res, ++param, hash_id, sizeof(*hash_id), SQLITE_STATIC);
  844. if (unlikely(rc != SQLITE_OK))
  845. goto bind_fail;
  846. rc = sqlite3_bind_string_or_null(res, cfg->alarm, ++param);
  847. if (unlikely(rc != SQLITE_OK))
  848. goto bind_fail;
  849. rc = sqlite3_bind_string_or_null(res, cfg->template_key, ++param);
  850. if (unlikely(rc != SQLITE_OK))
  851. goto bind_fail;
  852. rc = sqlite3_bind_string_or_null(res, cfg->on, ++param);
  853. if (unlikely(rc != SQLITE_OK))
  854. goto bind_fail;
  855. rc = sqlite3_bind_string_or_null(res, cfg->classification, ++param);
  856. if (unlikely(rc != SQLITE_OK))
  857. goto bind_fail;
  858. rc = sqlite3_bind_string_or_null(res, cfg->component, ++param);
  859. if (unlikely(rc != SQLITE_OK))
  860. goto bind_fail;
  861. rc = sqlite3_bind_string_or_null(res, cfg->type, ++param);
  862. if (unlikely(rc != SQLITE_OK))
  863. goto bind_fail;
  864. rc = sqlite3_bind_string_or_null(res, cfg->os, ++param);
  865. if (unlikely(rc != SQLITE_OK))
  866. goto bind_fail;
  867. rc = sqlite3_bind_string_or_null(res, cfg->host, ++param);
  868. if (unlikely(rc != SQLITE_OK))
  869. goto bind_fail;
  870. rc = sqlite3_bind_string_or_null(res, cfg->lookup, ++param);
  871. if (unlikely(rc != SQLITE_OK))
  872. goto bind_fail;
  873. rc = sqlite3_bind_string_or_null(res, cfg->every, ++param);
  874. if (unlikely(rc != SQLITE_OK))
  875. goto bind_fail;
  876. rc = sqlite3_bind_string_or_null(res, cfg->units, ++param);
  877. if (unlikely(rc != SQLITE_OK))
  878. goto bind_fail;
  879. rc = sqlite3_bind_string_or_null(res, cfg->calc, ++param);
  880. if (unlikely(rc != SQLITE_OK))
  881. goto bind_fail;
  882. rc = sqlite3_bind_string_or_null(res, cfg->families, ++param);
  883. if (unlikely(rc != SQLITE_OK))
  884. goto bind_fail;
  885. rc = sqlite3_bind_string_or_null(res, cfg->plugin, ++param);
  886. if (unlikely(rc != SQLITE_OK))
  887. goto bind_fail;
  888. rc = sqlite3_bind_string_or_null(res, cfg->module, ++param);
  889. if (unlikely(rc != SQLITE_OK))
  890. goto bind_fail;
  891. rc = sqlite3_bind_string_or_null(res, cfg->charts, ++param);
  892. if (unlikely(rc != SQLITE_OK))
  893. goto bind_fail;
  894. rc = sqlite3_bind_string_or_null(res, cfg->green, ++param);
  895. if (unlikely(rc != SQLITE_OK))
  896. goto bind_fail;
  897. rc = sqlite3_bind_string_or_null(res, cfg->red, ++param);
  898. if (unlikely(rc != SQLITE_OK))
  899. goto bind_fail;
  900. rc = sqlite3_bind_string_or_null(res, cfg->warn, ++param);
  901. if (unlikely(rc != SQLITE_OK))
  902. goto bind_fail;
  903. rc = sqlite3_bind_string_or_null(res, cfg->crit, ++param);
  904. if (unlikely(rc != SQLITE_OK))
  905. goto bind_fail;
  906. rc = sqlite3_bind_string_or_null(res, cfg->exec, ++param);
  907. if (unlikely(rc != SQLITE_OK))
  908. goto bind_fail;
  909. rc = sqlite3_bind_string_or_null(res, cfg->to, ++param);
  910. if (unlikely(rc != SQLITE_OK))
  911. goto bind_fail;
  912. rc = sqlite3_bind_string_or_null(res, cfg->info, ++param);
  913. if (unlikely(rc != SQLITE_OK))
  914. goto bind_fail;
  915. rc = sqlite3_bind_string_or_null(res, cfg->delay, ++param);
  916. if (unlikely(rc != SQLITE_OK))
  917. goto bind_fail;
  918. rc = sqlite3_bind_string_or_null(res, cfg->options, ++param);
  919. if (unlikely(rc != SQLITE_OK))
  920. goto bind_fail;
  921. rc = sqlite3_bind_string_or_null(res, cfg->repeat, ++param);
  922. if (unlikely(rc != SQLITE_OK))
  923. goto bind_fail;
  924. rc = sqlite3_bind_string_or_null(res, cfg->host_labels, ++param);
  925. if (unlikely(rc != SQLITE_OK))
  926. goto bind_fail;
  927. if (cfg->p_db_lookup_after) {
  928. rc = sqlite3_bind_string_or_null(res, cfg->p_db_lookup_dimensions, ++param);
  929. if (unlikely(rc != SQLITE_OK))
  930. goto bind_fail;
  931. rc = sqlite3_bind_string_or_null(res, cfg->p_db_lookup_method, ++param);
  932. if (unlikely(rc != SQLITE_OK))
  933. goto bind_fail;
  934. rc = sqlite3_bind_int(res, ++param, (int) cfg->p_db_lookup_options);
  935. if (unlikely(rc != SQLITE_OK))
  936. goto bind_fail;
  937. rc = sqlite3_bind_int(res, ++param, (int) cfg->p_db_lookup_after);
  938. if (unlikely(rc != SQLITE_OK))
  939. goto bind_fail;
  940. rc = sqlite3_bind_int(res, ++param, (int) cfg->p_db_lookup_before);
  941. if (unlikely(rc != SQLITE_OK))
  942. goto bind_fail;
  943. } else {
  944. rc = sqlite3_bind_null(res, ++param);
  945. if (unlikely(rc != SQLITE_OK))
  946. goto bind_fail;
  947. rc = sqlite3_bind_null(res, ++param);
  948. if (unlikely(rc != SQLITE_OK))
  949. goto bind_fail;
  950. rc = sqlite3_bind_null(res, ++param);
  951. if (unlikely(rc != SQLITE_OK))
  952. goto bind_fail;
  953. rc = sqlite3_bind_null(res, ++param);
  954. if (unlikely(rc != SQLITE_OK))
  955. goto bind_fail;
  956. rc = sqlite3_bind_null(res, ++param);
  957. if (unlikely(rc != SQLITE_OK))
  958. goto bind_fail;
  959. }
  960. rc = sqlite3_bind_int(res, ++param, cfg->p_update_every);
  961. if (unlikely(rc != SQLITE_OK))
  962. goto bind_fail;
  963. rc = sqlite3_bind_string_or_null(res, cfg->source, ++param);
  964. if (unlikely(rc != SQLITE_OK))
  965. goto bind_fail;
  966. rc = sqlite3_bind_string_or_null(res, cfg->chart_labels, ++param);
  967. if (unlikely(rc != SQLITE_OK))
  968. goto bind_fail;
  969. rc = execute_insert(res);
  970. if (unlikely(rc != SQLITE_DONE))
  971. error_report("Failed to store alert config, rc = %d", rc);
  972. rc = sqlite3_reset(res);
  973. if (unlikely(rc != SQLITE_OK))
  974. error_report("Failed to reset statement in alert hash_id store function, rc = %d", rc);
  975. return 0;
  976. bind_fail:
  977. error_report("Failed to bind parameter %d to store alert hash_id, rc = %d", param, rc);
  978. rc = sqlite3_reset(res);
  979. if (unlikely(rc != SQLITE_OK))
  980. error_report("Failed to reset statement in alert hash_id store function, rc = %d", rc);
  981. return 1;
  982. }
  983. /*
  984. alert hashes are used for cloud communication.
  985. if cloud is disabled or openssl is not available (which will prevent cloud connectivity)
  986. skip hash calculations
  987. */
  988. #if defined ENABLE_HTTPS
  989. #define DIGEST_ALERT_CONFIG_VAL(v) ((v) ? EVP_DigestUpdate(evpctx, (string2str(v)), string_strlen((v))) : EVP_DigestUpdate(evpctx, "", 1))
  990. #endif
  991. int alert_hash_and_store_config(
  992. uuid_t hash_id,
  993. struct alert_config *cfg,
  994. int store_hash)
  995. {
  996. #if defined ENABLE_HTTPS
  997. EVP_MD_CTX *evpctx;
  998. unsigned char hash_value[EVP_MAX_MD_SIZE];
  999. unsigned int hash_len;
  1000. evpctx = EVP_MD_CTX_create();
  1001. EVP_DigestInit_ex(evpctx, EVP_sha256(), NULL);
  1002. DIGEST_ALERT_CONFIG_VAL(cfg->alarm);
  1003. DIGEST_ALERT_CONFIG_VAL(cfg->template_key);
  1004. DIGEST_ALERT_CONFIG_VAL(cfg->os);
  1005. DIGEST_ALERT_CONFIG_VAL(cfg->host);
  1006. DIGEST_ALERT_CONFIG_VAL(cfg->on);
  1007. DIGEST_ALERT_CONFIG_VAL(cfg->families);
  1008. DIGEST_ALERT_CONFIG_VAL(cfg->plugin);
  1009. DIGEST_ALERT_CONFIG_VAL(cfg->module);
  1010. DIGEST_ALERT_CONFIG_VAL(cfg->charts);
  1011. DIGEST_ALERT_CONFIG_VAL(cfg->lookup);
  1012. DIGEST_ALERT_CONFIG_VAL(cfg->calc);
  1013. DIGEST_ALERT_CONFIG_VAL(cfg->every);
  1014. DIGEST_ALERT_CONFIG_VAL(cfg->green);
  1015. DIGEST_ALERT_CONFIG_VAL(cfg->red);
  1016. DIGEST_ALERT_CONFIG_VAL(cfg->warn);
  1017. DIGEST_ALERT_CONFIG_VAL(cfg->crit);
  1018. DIGEST_ALERT_CONFIG_VAL(cfg->exec);
  1019. DIGEST_ALERT_CONFIG_VAL(cfg->to);
  1020. DIGEST_ALERT_CONFIG_VAL(cfg->units);
  1021. DIGEST_ALERT_CONFIG_VAL(cfg->info);
  1022. DIGEST_ALERT_CONFIG_VAL(cfg->classification);
  1023. DIGEST_ALERT_CONFIG_VAL(cfg->component);
  1024. DIGEST_ALERT_CONFIG_VAL(cfg->type);
  1025. DIGEST_ALERT_CONFIG_VAL(cfg->delay);
  1026. DIGEST_ALERT_CONFIG_VAL(cfg->options);
  1027. DIGEST_ALERT_CONFIG_VAL(cfg->repeat);
  1028. DIGEST_ALERT_CONFIG_VAL(cfg->host_labels);
  1029. DIGEST_ALERT_CONFIG_VAL(cfg->chart_labels);
  1030. EVP_DigestFinal_ex(evpctx, hash_value, &hash_len);
  1031. EVP_MD_CTX_destroy(evpctx);
  1032. fatal_assert(hash_len > sizeof(uuid_t));
  1033. char uuid_str[UUID_STR_LEN];
  1034. uuid_unparse_lower(*((uuid_t *)&hash_value), uuid_str);
  1035. uuid_copy(hash_id, *((uuid_t *)&hash_value));
  1036. /* store everything, so it can be recreated when not in memory or just a subset ? */
  1037. if (store_hash)
  1038. (void)sql_store_alert_config_hash( (uuid_t *)&hash_value, cfg);
  1039. #else
  1040. UNUSED(hash_id);
  1041. UNUSED(cfg);
  1042. UNUSED(store_hash);
  1043. #endif
  1044. return 1;
  1045. }
  1046. #define SQL_SELECT_HEALTH_LAST_EXECUTED_EVENT "SELECT hld.new_status FROM health_log hl, health_log_detail hld WHERE hl.alarm_id = %u AND hld.unique_id != %u AND hld.flags & %u AND hl.host_id = @host_id and hl.health_log_id = hld.health_log_id ORDER BY hld.unique_id DESC LIMIT 1;"
  1047. int sql_health_get_last_executed_event(RRDHOST *host, ALARM_ENTRY *ae, RRDCALC_STATUS *last_executed_status)
  1048. {
  1049. int rc = 0, ret = -1;
  1050. char command[MAX_HEALTH_SQL_SIZE + 1];
  1051. sqlite3_stmt *res = NULL;
  1052. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_SELECT_HEALTH_LAST_EXECUTED_EVENT, ae->alarm_id, ae->unique_id, (uint32_t) HEALTH_ENTRY_FLAG_EXEC_RUN);
  1053. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  1054. if (rc != SQLITE_OK) {
  1055. error_report("Failed to prepare statement when trying to get last executed status");
  1056. return ret;
  1057. }
  1058. rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
  1059. if (unlikely(rc != SQLITE_OK)) {
  1060. error_report("Failed to bind host_id parameter for SQL_SELECT_HEALTH_LAST_EXECUTED_EVENT.");
  1061. sqlite3_finalize(res);
  1062. return ret;
  1063. }
  1064. ret = 0;
  1065. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  1066. *last_executed_status = (RRDCALC_STATUS) sqlite3_column_int(res, 0);
  1067. ret = 1;
  1068. }
  1069. rc = sqlite3_finalize(res);
  1070. if (unlikely(rc != SQLITE_OK))
  1071. error_report("Failed to finalize the statement.");
  1072. return ret;
  1073. }
  1074. #define SQL_SELECT_HEALTH_LOG "SELECT hld.unique_id, hld.alarm_id, hld.alarm_event_id, hl.config_hash_id, hld.updated_by_id, hld.updates_id, hld.when_key, hld.duration, hld.non_clear_duration, hld.flags, hld.exec_run_timestamp, hld.delay_up_to_timestamp, hl.name, hl.chart, hl.family, hl.exec, hl.recipient, ah.source, hl.units, hld.info, hld.exec_code, hld.new_status, hld.old_status, hld.delay, hld.new_value, hld.old_value, hld.last_repeat, ah.class, ah.component, ah.type, hl.chart_context, hld.transition_id FROM health_log hl, alert_hash ah, health_log_detail hld WHERE hl.config_hash_id = ah.hash_id and hl.health_log_id = hld.health_log_id and hl.host_id = @host_id "
  1075. void sql_health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after, char *chart) {
  1076. buffer_strcat(wb, "[");
  1077. unsigned int max = host->health_log.max;
  1078. unsigned int count = 0;
  1079. sqlite3_stmt *res = NULL;
  1080. int rc;
  1081. BUFFER *command = buffer_create(MAX_HEALTH_SQL_SIZE, NULL);
  1082. buffer_sprintf(command, SQL_SELECT_HEALTH_LOG);
  1083. if (chart) {
  1084. char chart_sql[MAX_HEALTH_SQL_SIZE + 1];
  1085. snprintfz(chart_sql, MAX_HEALTH_SQL_SIZE, "AND hl.chart = '%s' ", chart);
  1086. buffer_strcat(command, chart_sql);
  1087. }
  1088. if (after) {
  1089. char after_sql[MAX_HEALTH_SQL_SIZE + 1];
  1090. snprintfz(after_sql, MAX_HEALTH_SQL_SIZE, "AND hld.unique_id > %u ", after);
  1091. buffer_strcat(command, after_sql);
  1092. }
  1093. {
  1094. char limit_sql[MAX_HEALTH_SQL_SIZE + 1];
  1095. snprintfz(limit_sql, MAX_HEALTH_SQL_SIZE, "ORDER BY hld.unique_id DESC LIMIT %u ", max);
  1096. buffer_strcat(command, limit_sql);
  1097. }
  1098. rc = sqlite3_prepare_v2(db_meta, buffer_tostring(command), -1, &res, 0);
  1099. if (unlikely(rc != SQLITE_OK)) {
  1100. error_report("Failed to prepare statement SQL_SELECT_HEALTH_LOG");
  1101. buffer_free(command);
  1102. return;
  1103. }
  1104. rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
  1105. if (unlikely(rc != SQLITE_OK)) {
  1106. error_report("Failed to bind host_id for SQL_SELECT_HEALTH_LOG.");
  1107. sqlite3_finalize(res);
  1108. buffer_free(command);
  1109. return;
  1110. }
  1111. while (sqlite3_step(res) == SQLITE_ROW) {
  1112. char old_value_string[100 + 1];
  1113. char new_value_string[100 + 1];
  1114. char config_hash_id[UUID_STR_LEN];
  1115. uuid_unparse_lower(*((uuid_t *) sqlite3_column_blob(res, 3)), config_hash_id);
  1116. char transition_id[UUID_STR_LEN] = {0};
  1117. if (sqlite3_column_type(res, 31) != SQLITE_NULL)
  1118. uuid_unparse_lower(*((uuid_t *) sqlite3_column_blob(res, 31)), transition_id);
  1119. char *edit_command = sqlite3_column_bytes(res, 17) > 0 ? health_edit_command_from_source((char *)sqlite3_column_text(res, 17)) : strdupz("UNKNOWN=0=UNKNOWN");
  1120. if (count)
  1121. buffer_sprintf(wb, ",");
  1122. count++;
  1123. buffer_sprintf(
  1124. wb,
  1125. "\n\t{\n"
  1126. "\t\t\"hostname\": \"%s\",\n"
  1127. "\t\t\"utc_offset\": %d,\n"
  1128. "\t\t\"timezone\": \"%s\",\n"
  1129. "\t\t\"unique_id\": %u,\n"
  1130. "\t\t\"alarm_id\": %u,\n"
  1131. "\t\t\"alarm_event_id\": %u,\n"
  1132. "\t\t\"config_hash_id\": \"%s\",\n"
  1133. "\t\t\"transition_id\": \"%s\",\n"
  1134. "\t\t\"name\": \"%s\",\n"
  1135. "\t\t\"chart\": \"%s\",\n"
  1136. "\t\t\"context\": \"%s\",\n"
  1137. "\t\t\"family\": \"%s\",\n"
  1138. "\t\t\"class\": \"%s\",\n"
  1139. "\t\t\"component\": \"%s\",\n"
  1140. "\t\t\"type\": \"%s\",\n"
  1141. "\t\t\"processed\": %s,\n"
  1142. "\t\t\"updated\": %s,\n"
  1143. "\t\t\"exec_run\": %lu,\n"
  1144. "\t\t\"exec_failed\": %s,\n"
  1145. "\t\t\"exec\": \"%s\",\n"
  1146. "\t\t\"recipient\": \"%s\",\n"
  1147. "\t\t\"exec_code\": %d,\n"
  1148. "\t\t\"source\": \"%s\",\n"
  1149. "\t\t\"command\": \"%s\",\n"
  1150. "\t\t\"units\": \"%s\",\n"
  1151. "\t\t\"when\": %lu,\n"
  1152. "\t\t\"duration\": %lu,\n"
  1153. "\t\t\"non_clear_duration\": %lu,\n"
  1154. "\t\t\"status\": \"%s\",\n"
  1155. "\t\t\"old_status\": \"%s\",\n"
  1156. "\t\t\"delay\": %d,\n"
  1157. "\t\t\"delay_up_to_timestamp\": %lu,\n"
  1158. "\t\t\"updated_by_id\": %u,\n"
  1159. "\t\t\"updates_id\": %u,\n"
  1160. "\t\t\"value_string\": \"%s\",\n"
  1161. "\t\t\"old_value_string\": \"%s\",\n"
  1162. "\t\t\"last_repeat\": \"%lu\",\n"
  1163. "\t\t\"silenced\": \"%s\",\n",
  1164. rrdhost_hostname(host),
  1165. host->utc_offset,
  1166. rrdhost_abbrev_timezone(host),
  1167. (unsigned int) sqlite3_column_int64(res, 0),
  1168. (unsigned int) sqlite3_column_int64(res, 1),
  1169. (unsigned int) sqlite3_column_int64(res, 2),
  1170. config_hash_id,
  1171. transition_id,
  1172. sqlite3_column_text(res, 12),
  1173. sqlite3_column_text(res, 13),
  1174. sqlite3_column_text(res, 30),
  1175. sqlite3_column_text(res, 14),
  1176. sqlite3_column_text(res, 27) ? (const char *) sqlite3_column_text(res, 27) : (char *) "Unknown",
  1177. sqlite3_column_text(res, 28) ? (const char *) sqlite3_column_text(res, 28) : (char *) "Unknown",
  1178. sqlite3_column_text(res, 29) ? (const char *) sqlite3_column_text(res, 29) : (char *) "Unknown",
  1179. (sqlite3_column_int64(res, 9) & HEALTH_ENTRY_FLAG_PROCESSED)?"true":"false",
  1180. (sqlite3_column_int64(res, 9) & HEALTH_ENTRY_FLAG_UPDATED)?"true":"false",
  1181. (long unsigned int)sqlite3_column_int64(res, 10),
  1182. (sqlite3_column_int64(res, 9) & HEALTH_ENTRY_FLAG_EXEC_FAILED)?"true":"false",
  1183. sqlite3_column_text(res, 15) ? (const char *) sqlite3_column_text(res, 15) : string2str(host->health.health_default_exec),
  1184. sqlite3_column_text(res, 16) ? (const char *) sqlite3_column_text(res, 16) : string2str(host->health.health_default_recipient),
  1185. sqlite3_column_int(res, 20),
  1186. sqlite3_column_text(res, 17) ? (const char *) sqlite3_column_text(res, 17) : (char *) "Unknown",
  1187. edit_command,
  1188. sqlite3_column_text(res, 18),
  1189. (long unsigned int)sqlite3_column_int64(res, 6),
  1190. (long unsigned int)sqlite3_column_int64(res, 7),
  1191. (long unsigned int)sqlite3_column_int64(res, 8),
  1192. rrdcalc_status2string(sqlite3_column_int(res, 21)),
  1193. rrdcalc_status2string(sqlite3_column_int(res, 22)),
  1194. sqlite3_column_int(res, 23),
  1195. (long unsigned int)sqlite3_column_int64(res, 11),
  1196. (unsigned int)sqlite3_column_int64(res, 4),
  1197. (unsigned int)sqlite3_column_int64(res, 5),
  1198. sqlite3_column_type(res, 24) == SQLITE_NULL ? "-" : format_value_and_unit(new_value_string, 100, sqlite3_column_double(res, 24), (char *) sqlite3_column_text(res, 18), -1),
  1199. sqlite3_column_type(res, 25) == SQLITE_NULL ? "-" : format_value_and_unit(old_value_string, 100, sqlite3_column_double(res, 25), (char *) sqlite3_column_text(res, 18), -1),
  1200. (long unsigned int)sqlite3_column_int64(res, 26),
  1201. (sqlite3_column_int64(res, 9) & HEALTH_ENTRY_FLAG_SILENCED)?"true":"false");
  1202. health_string2json(wb, "\t\t", "info", (char *) sqlite3_column_text(res, 19), ",\n");
  1203. if(unlikely(sqlite3_column_int64(res, 9) & HEALTH_ENTRY_FLAG_NO_CLEAR_NOTIFICATION)) {
  1204. buffer_strcat(wb, "\t\t\"no_clear_notification\": true,\n");
  1205. }
  1206. buffer_strcat(wb, "\t\t\"value\":");
  1207. if (sqlite3_column_type(res, 24) == SQLITE_NULL)
  1208. buffer_strcat(wb, "null");
  1209. else
  1210. buffer_print_netdata_double(wb, sqlite3_column_double(res, 24));
  1211. buffer_strcat(wb, ",\n");
  1212. buffer_strcat(wb, "\t\t\"old_value\":");
  1213. if (sqlite3_column_type(res, 25) == SQLITE_NULL)
  1214. buffer_strcat(wb, "null");
  1215. else
  1216. buffer_print_netdata_double(wb, sqlite3_column_double(res, 25));
  1217. buffer_strcat(wb, "\n");
  1218. buffer_strcat(wb, "\t}");
  1219. freez(edit_command);
  1220. }
  1221. buffer_strcat(wb, "\n]");
  1222. rc = sqlite3_finalize(res);
  1223. if (unlikely(rc != SQLITE_OK))
  1224. error_report("Failed to finalize statement for SQL_SELECT_HEALTH_LOG");
  1225. buffer_free(command);
  1226. }
  1227. #define SQL_COPY_HEALTH_LOG(table) "INSERT OR IGNORE INTO health_log (host_id, alarm_id, config_hash_id, name, chart, family, exec, recipient, units, chart_context) SELECT ?1, alarm_id, config_hash_id, name, chart, family, exec, recipient, units, chart_context from %s;", table
  1228. #define SQL_COPY_HEALTH_LOG_DETAIL(table) "INSERT INTO health_log_detail (unique_id, alarm_id, alarm_event_id, updated_by_id, updates_id, when_key, duration, non_clear_duration, flags, exec_run_timestamp, delay_up_to_timestamp, info, exec_code, new_status, old_status, delay, new_value, old_value, last_repeat, transition_id, global_id, host_id) SELECT unique_id, alarm_id, alarm_event_id, updated_by_id, updates_id, when_key, duration, non_clear_duration, flags, exec_run_timestamp, delay_up_to_timestamp, info, exec_code, new_status, old_status, delay, new_value, old_value, last_repeat, transition_id, now_usec(1), ?1 from %s;", table
  1229. #define SQL_UPDATE_HEALTH_LOG_DETAIL_TRANSITION_ID "update health_log_detail set transition_id = uuid_random() where transition_id is null;"
  1230. #define SQL_UPDATE_HEALTH_LOG_DETAIL_HEALTH_LOG_ID "update health_log_detail set health_log_id = (select health_log_id from health_log where host_id = ?1 and alarm_id = health_log_detail.alarm_id) where health_log_id is null and host_id = ?2;"
  1231. #define SQL_UPDATE_HEALTH_LOG_LAST_TRANSITION_ID "update health_log set last_transition_id = (select transition_id from health_log_detail where health_log_id = health_log.health_log_id and alarm_id = health_log.alarm_id group by (alarm_id) having max(alarm_event_id)) where host_id = ?1;"
  1232. int health_migrate_old_health_log_table(char *table) {
  1233. if (!table)
  1234. return 0;
  1235. //table should contain guid. We need to
  1236. //keep it in the new table along with it's data
  1237. //health_log_XXXXXXXX_XXXX_XXXX_XXXX_XXXXXXXXXXXX
  1238. if (strnlen(table, 46) != 46) {
  1239. return 0;
  1240. }
  1241. char *uuid_from_table = strdupz(table + 11);
  1242. uuid_t uuid;
  1243. if (uuid_parse_fix(uuid_from_table, uuid)) {
  1244. freez(uuid_from_table);
  1245. return 0;
  1246. }
  1247. int rc;
  1248. char command[MAX_HEALTH_SQL_SIZE + 1];
  1249. sqlite3_stmt *res = NULL;
  1250. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_COPY_HEALTH_LOG(table));
  1251. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  1252. if (unlikely(rc != SQLITE_OK)) {
  1253. error_report("Failed to prepare statement to copy health log, rc = %d", rc);
  1254. freez(uuid_from_table);
  1255. return 0;
  1256. }
  1257. rc = sqlite3_bind_blob(res, 1, &uuid, sizeof(uuid), SQLITE_STATIC);
  1258. if (unlikely(rc != SQLITE_OK)) {
  1259. rc = sqlite3_finalize(res);
  1260. if (unlikely(rc != SQLITE_OK))
  1261. error_report("Failed to reset statement to copy health log table, rc = %d", rc);
  1262. freez(uuid_from_table);
  1263. return 0;
  1264. }
  1265. rc = execute_insert(res);
  1266. if (unlikely(rc != SQLITE_DONE)) {
  1267. error_report("Failed to execute SQL_COPY_HEALTH_LOG, rc = %d", rc);
  1268. rc = sqlite3_finalize(res);
  1269. if (unlikely(rc != SQLITE_OK))
  1270. error_report("Failed to reset statement to copy health log table, rc = %d", rc);
  1271. freez(uuid_from_table);
  1272. }
  1273. //detail
  1274. snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_COPY_HEALTH_LOG_DETAIL(table));
  1275. rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
  1276. if (unlikely(rc != SQLITE_OK)) {
  1277. error_report("Failed to prepare statement to copy health log detail, rc = %d", rc);
  1278. return 0;
  1279. }
  1280. rc = sqlite3_bind_blob(res, 1, &uuid, sizeof(uuid), SQLITE_STATIC);
  1281. if (unlikely(rc != SQLITE_OK)) {
  1282. rc = sqlite3_finalize(res);
  1283. if (unlikely(rc != SQLITE_OK))
  1284. error_report("Failed to reset statement to copy health log detail, rc = %d", rc);
  1285. return 0;
  1286. }
  1287. rc = execute_insert(res);
  1288. if (unlikely(rc != SQLITE_DONE)) {
  1289. error_report("Failed to execute SQL_COPY_HEALTH_LOG_DETAIL, rc = %d", rc);
  1290. rc = sqlite3_finalize(res);
  1291. if (unlikely(rc != SQLITE_OK))
  1292. error_report("Failed to reset statement to copy health log detail table, rc = %d", rc);
  1293. return 0;
  1294. }
  1295. //update transition ids
  1296. rc = sqlite3_prepare_v2(db_meta, SQL_UPDATE_HEALTH_LOG_DETAIL_TRANSITION_ID, -1, &res, 0);
  1297. if (unlikely(rc != SQLITE_OK)) {
  1298. error_report("Failed to prepare statement to update health log detail with transition ids, rc = %d", rc);
  1299. return 0;
  1300. }
  1301. rc = execute_insert(res);
  1302. if (unlikely(rc != SQLITE_DONE)) {
  1303. error_report("Failed to execute SQL_UPDATE_HEALTH_LOG_DETAIL_TRANSITION_ID, rc = %d", rc);
  1304. rc = sqlite3_finalize(res);
  1305. if (unlikely(rc != SQLITE_OK))
  1306. error_report("Failed to reset statement to update health log detail table with transition ids, rc = %d", rc);
  1307. return 0;
  1308. }
  1309. //update health_log_id
  1310. rc = sqlite3_prepare_v2(db_meta, SQL_UPDATE_HEALTH_LOG_DETAIL_HEALTH_LOG_ID, -1, &res, 0);
  1311. if (unlikely(rc != SQLITE_OK)) {
  1312. error_report("Failed to prepare statement to update health log detail with health log ids, rc = %d", rc);
  1313. return 0;
  1314. }
  1315. rc = sqlite3_bind_blob(res, 1, &uuid, sizeof(uuid), SQLITE_STATIC);
  1316. if (unlikely(rc != SQLITE_OK)) {
  1317. rc = sqlite3_finalize(res);
  1318. if (unlikely(rc != SQLITE_OK))
  1319. error_report("Failed to reset statement to update health log detail with health log ids, rc = %d", rc);
  1320. return 0;
  1321. }
  1322. rc = sqlite3_bind_blob(res, 2, &uuid, sizeof(uuid), SQLITE_STATIC);
  1323. if (unlikely(rc != SQLITE_OK)) {
  1324. rc = sqlite3_finalize(res);
  1325. if (unlikely(rc != SQLITE_OK))
  1326. error_report("Failed to reset statement to update health log detail with health log ids, rc = %d", rc);
  1327. return 0;
  1328. }
  1329. rc = execute_insert(res);
  1330. if (unlikely(rc != SQLITE_DONE)) {
  1331. error_report("Failed to execute SQL_UPDATE_HEALTH_LOG_DETAIL_HEALTH_LOG_ID, rc = %d", rc);
  1332. rc = sqlite3_finalize(res);
  1333. if (unlikely(rc != SQLITE_OK))
  1334. error_report("Failed to reset statement to update health log detail table with health log ids, rc = %d", rc);
  1335. }
  1336. //update last transition id
  1337. rc = sqlite3_prepare_v2(db_meta, SQL_UPDATE_HEALTH_LOG_LAST_TRANSITION_ID, -1, &res, 0);
  1338. if (unlikely(rc != SQLITE_OK)) {
  1339. error_report("Failed to prepare statement to update health log with last transition id, rc = %d", rc);
  1340. return 0;
  1341. }
  1342. rc = sqlite3_bind_blob(res, 1, &uuid, sizeof(uuid), SQLITE_STATIC);
  1343. if (unlikely(rc != SQLITE_OK)) {
  1344. rc = sqlite3_finalize(res);
  1345. if (unlikely(rc != SQLITE_OK))
  1346. error_report("Failed to reset statement to update health log with last transition id, rc = %d", rc);
  1347. return 0;
  1348. }
  1349. rc = execute_insert(res);
  1350. if (unlikely(rc != SQLITE_DONE)) {
  1351. error_report("Failed to execute SQL_UPDATE_HEALTH_LOG_LAST_TRANSITION_ID, rc = %d", rc);
  1352. rc = sqlite3_finalize(res);
  1353. if (unlikely(rc != SQLITE_OK))
  1354. error_report("Failed to reset statement to update health log table with last transition id, rc = %d", rc);
  1355. }
  1356. return 1;
  1357. }
  1358. #define SQL_GET_ALARM_ID "select alarm_id, health_log_id from health_log where host_id = @host_id and chart = @chart and name = @name and config_hash_id = @config_hash_id"
  1359. #define SQL_GET_EVENT_ID "select max(alarm_event_id) + 1 from health_log_detail where health_log_id = @health_log_id and alarm_id = @alarm_id"
  1360. uint32_t sql_get_alarm_id(RRDHOST *host, STRING *chart, STRING *name, uint32_t *next_event_id, uuid_t *config_hash_id)
  1361. {
  1362. int rc = 0;
  1363. sqlite3_stmt *res = NULL;
  1364. uint32_t alarm_id = 0;
  1365. uint64_t health_log_id = 0;
  1366. rc = sqlite3_prepare_v2(db_meta, SQL_GET_ALARM_ID, -1, &res, 0);
  1367. if (rc != SQLITE_OK) {
  1368. error_report("Failed to prepare statement when trying to get an alarm id");
  1369. return alarm_id;
  1370. }
  1371. rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
  1372. if (unlikely(rc != SQLITE_OK)) {
  1373. error_report("Failed to bind host_id parameter for SQL_GET_ALARM_ID.");
  1374. sqlite3_finalize(res);
  1375. return alarm_id;
  1376. }
  1377. rc = sqlite3_bind_string_or_null(res, chart, 2);
  1378. if (unlikely(rc != SQLITE_OK)) {
  1379. error_report("Failed to bind char parameter for SQL_GET_ALARM_ID.");
  1380. sqlite3_finalize(res);
  1381. return alarm_id;
  1382. }
  1383. rc = sqlite3_bind_string_or_null(res, name, 3);
  1384. if (unlikely(rc != SQLITE_OK)) {
  1385. error_report("Failed to bind name parameter for SQL_GET_ALARM_ID.");
  1386. sqlite3_finalize(res);
  1387. return alarm_id;
  1388. }
  1389. rc = sqlite3_bind_blob(res, 4, config_hash_id, sizeof(*config_hash_id), SQLITE_STATIC);
  1390. if (unlikely(rc != SQLITE_OK)) {
  1391. error_report("Failed to bind config_hash_id parameter for SQL_GET_ALARM_ID.");
  1392. sqlite3_finalize(res);
  1393. return alarm_id;
  1394. }
  1395. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  1396. alarm_id = (uint32_t) sqlite3_column_int64(res, 0);
  1397. health_log_id = (uint64_t) sqlite3_column_int64(res, 1);
  1398. }
  1399. rc = sqlite3_finalize(res);
  1400. if (unlikely(rc != SQLITE_OK))
  1401. error_report("Failed to finalize the statement while getting an alarm id.");
  1402. if (alarm_id) {
  1403. rc = sqlite3_prepare_v2(db_meta, SQL_GET_EVENT_ID, -1, &res, 0);
  1404. if (rc != SQLITE_OK) {
  1405. error_report("Failed to prepare statement when trying to get an event id");
  1406. return alarm_id;
  1407. }
  1408. rc = sqlite3_bind_int64(res, 1, (sqlite3_int64) health_log_id);
  1409. if (unlikely(rc != SQLITE_OK)) {
  1410. error_report("Failed to bind host_id parameter for SQL_GET_EVENT_ID.");
  1411. sqlite3_finalize(res);
  1412. return alarm_id;
  1413. }
  1414. rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) alarm_id);
  1415. if (unlikely(rc != SQLITE_OK)) {
  1416. error_report("Failed to bind char parameter for SQL_GET_EVENT_ID.");
  1417. sqlite3_finalize(res);
  1418. return alarm_id;
  1419. }
  1420. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  1421. *next_event_id = (uint32_t) sqlite3_column_int64(res, 0);
  1422. }
  1423. rc = sqlite3_finalize(res);
  1424. if (unlikely(rc != SQLITE_OK))
  1425. error_report("Failed to finalize the statement while getting an alarm id.");
  1426. }
  1427. return alarm_id;
  1428. }
  1429. #define SQL_UPDATE_ALARM_ID_WITH_CONFIG_HASH "update health_log set config_hash_id = @config_hash_id where host_id = @host_id and alarm_id = @alarm_id and health_log_id = @health_log_id"
  1430. void sql_update_alarm_with_config_hash(RRDHOST *host, uint32_t alarm_id, uint64_t health_log_id, uuid_t *config_hash_id)
  1431. {
  1432. int rc = 0;
  1433. sqlite3_stmt *res = NULL;
  1434. rc = sqlite3_prepare_v2(db_meta, SQL_UPDATE_ALARM_ID_WITH_CONFIG_HASH, -1, &res, 0);
  1435. if (rc != SQLITE_OK) {
  1436. error_report("Failed to prepare statement when trying to update an alarm id with a config hash.");
  1437. return;
  1438. }
  1439. rc = sqlite3_bind_blob(res, 1, config_hash_id, sizeof(*config_hash_id), SQLITE_STATIC);
  1440. if (unlikely(rc != SQLITE_OK)) {
  1441. error_report("Failed to bind config_hash_id parameter for SQL_UPDATE_ALARM_ID_WITH_CONFIG_HASH.");
  1442. sqlite3_finalize(res);
  1443. return;
  1444. }
  1445. rc = sqlite3_bind_blob(res, 2, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
  1446. if (unlikely(rc != SQLITE_OK)) {
  1447. error_report("Failed to bind host_id parameter for SQL_UPDATE_ALARM_ID_WITH_CONFIG_HASH.");
  1448. sqlite3_finalize(res);
  1449. return;
  1450. }
  1451. rc = sqlite3_bind_int64(res, 3, (sqlite3_int64) alarm_id);
  1452. if (unlikely(rc != SQLITE_OK)) {
  1453. error_report("Failed to bind alarm_id parameter for SQL_GET_ALARM_ID.");
  1454. sqlite3_finalize(res);
  1455. return;
  1456. }
  1457. rc = sqlite3_bind_int64(res, 4, (sqlite3_int64) health_log_id);
  1458. if (unlikely(rc != SQLITE_OK)) {
  1459. error_report("Failed to bind alarm_id parameter for SQL_GET_ALARM_ID.");
  1460. sqlite3_finalize(res);
  1461. return;
  1462. }
  1463. rc = execute_insert(res);
  1464. if (unlikely(rc != SQLITE_DONE)) {
  1465. error_report("Failed to execute SQL_UPDATE_ALARM_ID_WITH_CONFIG_HASH, rc = %d", rc);
  1466. rc = sqlite3_finalize(res);
  1467. if (unlikely(rc != SQLITE_OK))
  1468. error_report("Failed to reset statement to update health log detail table with config hash ids, rc = %d", rc);
  1469. return;
  1470. }
  1471. }
  1472. #define SQL_GET_ALARM_ID_CHECK_ZERO_HASH "select alarm_id, health_log_id from health_log where host_id = @host_id and chart = @chart and name = @name and (config_hash_id is null or config_hash_id = zeroblob(16))"
  1473. uint32_t sql_get_alarm_id_check_zero_hash(RRDHOST *host, STRING *chart, STRING *name, uint32_t *next_event_id, uuid_t *config_hash_id)
  1474. {
  1475. int rc = 0;
  1476. sqlite3_stmt *res = NULL;
  1477. uint32_t alarm_id = 0;
  1478. uint64_t health_log_id = 0;
  1479. rc = sqlite3_prepare_v2(db_meta, SQL_GET_ALARM_ID_CHECK_ZERO_HASH, -1, &res, 0);
  1480. if (rc != SQLITE_OK) {
  1481. error_report("Failed to prepare statement when trying to get an alarm id with zero hash");
  1482. return alarm_id;
  1483. }
  1484. rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
  1485. if (unlikely(rc != SQLITE_OK)) {
  1486. error_report("Failed to bind host_id parameter for SQL_GET_ALARM_ID_CHECK_ZERO_HASH.");
  1487. sqlite3_finalize(res);
  1488. return alarm_id;
  1489. }
  1490. rc = sqlite3_bind_string_or_null(res, chart, 2);
  1491. if (unlikely(rc != SQLITE_OK)) {
  1492. error_report("Failed to bind char parameter for SQL_GET_ALARM_ID_CHECK_ZERO_HASH.");
  1493. sqlite3_finalize(res);
  1494. return alarm_id;
  1495. }
  1496. rc = sqlite3_bind_string_or_null(res, name, 3);
  1497. if (unlikely(rc != SQLITE_OK)) {
  1498. error_report("Failed to bind name parameter for SQL_GET_ALARM_ID_CHECK_ZERO_HASH.");
  1499. sqlite3_finalize(res);
  1500. return alarm_id;
  1501. }
  1502. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  1503. alarm_id = (uint32_t) sqlite3_column_int64(res, 0);
  1504. health_log_id = (uint64_t) sqlite3_column_int64(res, 1);
  1505. }
  1506. rc = sqlite3_finalize(res);
  1507. if (unlikely(rc != SQLITE_OK))
  1508. error_report("Failed to finalize the statement while getting an alarm id.");
  1509. if (alarm_id) {
  1510. sql_update_alarm_with_config_hash(host, alarm_id, health_log_id, config_hash_id);
  1511. rc = sqlite3_prepare_v2(db_meta, SQL_GET_EVENT_ID, -1, &res, 0);
  1512. if (rc != SQLITE_OK) {
  1513. error_report("Failed to prepare statement when trying to get an event id");
  1514. return alarm_id;
  1515. }
  1516. rc = sqlite3_bind_int64(res, 1, (sqlite3_int64) health_log_id);
  1517. if (unlikely(rc != SQLITE_OK)) {
  1518. error_report("Failed to bind host_id parameter for SQL_GET_EVENT_ID.");
  1519. sqlite3_finalize(res);
  1520. return alarm_id;
  1521. }
  1522. rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) alarm_id);
  1523. if (unlikely(rc != SQLITE_OK)) {
  1524. error_report("Failed to bind char parameter for SQL_GET_EVENT_ID.");
  1525. sqlite3_finalize(res);
  1526. return alarm_id;
  1527. }
  1528. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  1529. *next_event_id = (uint32_t) sqlite3_column_int64(res, 0);
  1530. }
  1531. rc = sqlite3_finalize(res);
  1532. if (unlikely(rc != SQLITE_OK))
  1533. error_report("Failed to finalize the statement while getting an alarm id.");
  1534. }
  1535. return alarm_id;
  1536. }
  1537. #define SQL_GET_ALARM_ID_FROM_TRANSITION_ID "SELECT hld.alarm_id, hl.host_id, hl.chart_context FROM " \
  1538. "health_log_detail hld, health_log hl WHERE hld.transition_id = @transition_id " \
  1539. "and hld.health_log_id = hl.health_log_id"
  1540. bool sql_find_alert_transition(const char *transition, void (*cb)(const char *machine_guid, const char *context, time_t alert_id, void *data), void *data)
  1541. {
  1542. static __thread sqlite3_stmt *res = NULL;
  1543. char machine_guid[UUID_STR_LEN];
  1544. int rc;
  1545. uuid_t transition_uuid;
  1546. if (uuid_parse(transition, transition_uuid))
  1547. return false;
  1548. if (unlikely(!res)) {
  1549. rc = prepare_statement(db_meta, SQL_GET_ALARM_ID_FROM_TRANSITION_ID, &res);
  1550. if (unlikely(rc != SQLITE_OK)) {
  1551. error_report("Failed to prepare statement when trying to get transition id");
  1552. return false;
  1553. }
  1554. }
  1555. bool ok = false;
  1556. rc = sqlite3_bind_blob(res, 1, &transition_uuid, sizeof(transition_uuid), SQLITE_STATIC);
  1557. if (unlikely(rc != SQLITE_OK)) {
  1558. error_report("Failed to bind transition");
  1559. goto fail;
  1560. }
  1561. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  1562. ok = true;
  1563. uuid_unparse_lower(*(uuid_t *) sqlite3_column_blob(res, 1), machine_guid);
  1564. cb(machine_guid, (const char *) sqlite3_column_text(res, 2), sqlite3_column_int(res, 0), data);
  1565. }
  1566. fail:
  1567. rc = sqlite3_reset(res);
  1568. if (unlikely(rc != SQLITE_OK))
  1569. error_report("Failed to reset the statement when trying to find transition");
  1570. return ok;
  1571. }
  1572. #define SQL_BUILD_ALERT_TRANSITION "CREATE TEMP TABLE IF NOT EXISTS v_%p (host_id blob)"
  1573. #define SQL_POPULATE_TEMP_ALERT_TRANSITION_TABLE "INSERT INTO v_%p (host_id) VALUES (@host_id)"
  1574. #define SQL_SEARCH_ALERT_TRANSITION_SELECT "SELECT " \
  1575. "h.host_id, h.alarm_id, h.config_hash_id, h.name, h.chart, h.chart_name, h.family, h.recipient, h.units, h.exec, " \
  1576. "h.chart_context, d.when_key, d.duration, d.non_clear_duration, d.flags, d.delay_up_to_timestamp, " \
  1577. "d.info, d.exec_code, d.new_status, d.old_status, d.delay, d.new_value, d.old_value, d.last_repeat, " \
  1578. "d.transition_id, d.global_id, ah.class, ah.type, ah.component, d.exec_run_timestamp"
  1579. #define SQL_SEARCH_ALERT_TRANSITION_COMMON_WHERE \
  1580. "h.config_hash_id = ah.hash_id AND h.health_log_id = d.health_log_id"
  1581. #define SQL_SEARCH_ALERT_TRANSITION SQL_SEARCH_ALERT_TRANSITION_SELECT " FROM health_log h, health_log_detail d, v_%p t, alert_hash ah " \
  1582. " WHERE h.host_id = t.host_id AND " SQL_SEARCH_ALERT_TRANSITION_COMMON_WHERE " AND ( d.new_status > 2 OR d.old_status > 2 ) AND d.global_id BETWEEN @after AND @before "
  1583. #define SQL_SEARCH_ALERT_TRANSITION_DIRECT SQL_SEARCH_ALERT_TRANSITION_SELECT " FROM health_log h, health_log_detail d, alert_hash ah " \
  1584. " WHERE " SQL_SEARCH_ALERT_TRANSITION_COMMON_WHERE " AND transition_id = @transition "
  1585. void sql_alert_transitions(
  1586. DICTIONARY *nodes,
  1587. time_t after,
  1588. time_t before,
  1589. const char *context,
  1590. const char *alert_name,
  1591. const char *transition,
  1592. void (*cb)(struct sql_alert_transition_data *, void *),
  1593. void *data,
  1594. bool debug __maybe_unused)
  1595. {
  1596. uuid_t transition_uuid;
  1597. char sql[512];
  1598. int rc;
  1599. sqlite3_stmt *res = NULL;
  1600. BUFFER *command = NULL;
  1601. if (unlikely(!nodes))
  1602. return;
  1603. if (transition) {
  1604. if (uuid_parse(transition, transition_uuid)) {
  1605. error_report("Invalid transition given %s", transition);
  1606. return;
  1607. }
  1608. rc = sqlite3_prepare_v2(db_meta, SQL_SEARCH_ALERT_TRANSITION_DIRECT, -1, &res, 0);
  1609. rc = sqlite3_bind_blob(res, 1, &transition_uuid, sizeof(transition_uuid), SQLITE_STATIC);
  1610. if (unlikely(rc != SQLITE_OK)) {
  1611. error_report("Failed to bind transition_id parameter");
  1612. goto fail;
  1613. }
  1614. goto run_query;
  1615. }
  1616. snprintfz(sql, 511, SQL_BUILD_ALERT_TRANSITION, nodes);
  1617. rc = db_execute(db_meta, sql);
  1618. if (rc)
  1619. return;
  1620. snprintfz(sql, 511, SQL_POPULATE_TEMP_ALERT_TRANSITION_TABLE, nodes);
  1621. // Prepare statement to add things
  1622. rc = sqlite3_prepare_v2(db_meta, sql, -1, &res, 0);
  1623. if (unlikely(rc != SQLITE_OK)) {
  1624. error_report("Failed to prepare statement to INSERT into v_%p", nodes);
  1625. goto fail_only_drop;
  1626. }
  1627. void *t;
  1628. dfe_start_read(nodes, t) {
  1629. uuid_t host_uuid;
  1630. uuid_parse( t_dfe.name, host_uuid);
  1631. rc = sqlite3_bind_blob(res, 1, &host_uuid, sizeof(host_uuid), SQLITE_STATIC);
  1632. if (unlikely(rc != SQLITE_OK))
  1633. error_report("Failed to bind host_id parameter.");
  1634. rc = sqlite3_step_monitored(res);
  1635. if (rc != SQLITE_DONE)
  1636. error_report("Error while populating temp table");
  1637. rc = sqlite3_reset(res);
  1638. if (rc != SQLITE_OK)
  1639. error_report("Error while resetting parameters");
  1640. }
  1641. dfe_done(t);
  1642. rc = sqlite3_finalize(res);
  1643. if (unlikely(rc != SQLITE_OK)) {
  1644. // log error but continue
  1645. error_report("Failed to finalize statement for sql_alert_transitions temp table population");
  1646. }
  1647. command = buffer_create(MAX_HEALTH_SQL_SIZE, NULL);
  1648. buffer_sprintf(command, SQL_SEARCH_ALERT_TRANSITION, nodes);
  1649. if (context)
  1650. buffer_sprintf(command, " AND h.chart_context = @context");
  1651. if (alert_name)
  1652. buffer_sprintf(command, " AND h.name = @alert_name");
  1653. buffer_strcat(command, " ORDER BY d.global_id DESC");
  1654. rc = sqlite3_prepare_v2(db_meta, buffer_tostring(command), -1, &res, 0);
  1655. if (unlikely(rc != SQLITE_OK)) {
  1656. error_report("Failed to prepare statement sql_alert_transitions");
  1657. goto fail_only_drop;
  1658. }
  1659. int param = 1;
  1660. rc = sqlite3_bind_int64(res, param++, (sqlite3_int64)(after * USEC_PER_SEC));
  1661. if (unlikely(rc != SQLITE_OK)) {
  1662. error_report("Failed to bind after parameter");
  1663. goto fail;
  1664. }
  1665. rc = sqlite3_bind_int64(res, param++, (sqlite3_int64)(before * USEC_PER_SEC));
  1666. if (unlikely(rc != SQLITE_OK)) {
  1667. error_report("Failed to bind before parameter");
  1668. goto fail;
  1669. }
  1670. if (context) {
  1671. rc = sqlite3_bind_text(res, param++, context, -1, SQLITE_STATIC);
  1672. if (unlikely(rc != SQLITE_OK)) {
  1673. error_report("Failed to bind context parameter");
  1674. goto fail;
  1675. }
  1676. }
  1677. if (alert_name) {
  1678. rc = sqlite3_bind_text(res, param++, alert_name, -1, SQLITE_STATIC);
  1679. if (unlikely(rc != SQLITE_OK)) {
  1680. error_report("Failed to bind alert_name parameter");
  1681. goto fail;
  1682. }
  1683. }
  1684. run_query:;
  1685. struct sql_alert_transition_data atd = {0 };
  1686. while (sqlite3_step(res) == SQLITE_ROW) {
  1687. atd.host_id = (uuid_t *) sqlite3_column_blob(res, 0);
  1688. atd.alarm_id = sqlite3_column_int64(res, 1);
  1689. atd.config_hash_id = (uuid_t *)sqlite3_column_blob(res, 2);
  1690. atd.alert_name = (const char *) sqlite3_column_text(res, 3);
  1691. atd.chart = (const char *) sqlite3_column_text(res, 4);
  1692. atd.chart_name = (const char *) sqlite3_column_text(res, 5);
  1693. atd.family = (const char *) sqlite3_column_text(res, 6);
  1694. atd.recipient = (const char *) sqlite3_column_text(res, 7);
  1695. atd.units = (const char *) sqlite3_column_text(res, 8);
  1696. atd.exec = (const char *) sqlite3_column_text(res, 9);
  1697. atd.chart_context = (const char *) sqlite3_column_text(res, 10);
  1698. atd.when_key = sqlite3_column_int64(res, 11);
  1699. atd.duration = sqlite3_column_int64(res, 12);
  1700. atd.non_clear_duration = sqlite3_column_int64(res, 13);
  1701. atd.flags = sqlite3_column_int64(res, 14);
  1702. atd.delay_up_to_timestamp = sqlite3_column_int64(res, 15);
  1703. atd.info = (const char *) sqlite3_column_text(res, 16);
  1704. atd.exec_code = sqlite3_column_int(res, 17);
  1705. atd.new_status = sqlite3_column_int(res, 18);
  1706. atd.old_status = sqlite3_column_int(res, 19);
  1707. atd.delay = (int) sqlite3_column_int(res, 20);
  1708. atd.new_value = (NETDATA_DOUBLE) sqlite3_column_double(res, 21);
  1709. atd.old_value = (NETDATA_DOUBLE) sqlite3_column_double(res, 22);
  1710. atd.last_repeat = sqlite3_column_int64(res, 23);
  1711. atd.transition_id = (uuid_t *) sqlite3_column_blob(res, 24);
  1712. atd.global_id = sqlite3_column_int64(res, 25);
  1713. atd.classification = (const char *) sqlite3_column_text(res, 26);
  1714. atd.type = (const char *) sqlite3_column_text(res, 27);
  1715. atd.component = (const char *) sqlite3_column_text(res, 28);
  1716. atd.exec_run_timestamp = sqlite3_column_int64(res, 29);
  1717. cb(&atd, data);
  1718. }
  1719. fail:
  1720. rc = sqlite3_finalize(res);
  1721. if (unlikely(rc != SQLITE_OK))
  1722. error_report("Failed to finalize statement for sql_alert_transitions");
  1723. fail_only_drop:
  1724. if (likely(!transition)) {
  1725. (void)snprintfz(sql, 511, "DROP TABLE IF EXISTS v_%p", nodes);
  1726. (void)db_execute(db_meta, sql);
  1727. buffer_free(command);
  1728. }
  1729. }
  1730. #define SQL_BUILD_CONFIG_TARGET_LIST "CREATE TEMP TABLE IF NOT EXISTS c_%p (hash_id blob)"
  1731. #define SQL_POPULATE_TEMP_CONFIG_TARGET_TABLE "INSERT INTO c_%p (hash_id) VALUES (@hash_id)"
  1732. #define SQL_SEARCH_CONFIG_LIST "SELECT ah.hash_id, alarm, template, on_key, class, component, type, os, hosts, lookup, every, " \
  1733. " units, calc, families, plugin, module, charts, green, red, warn, crit, " \
  1734. " exec, to_key, info, delay, options, repeat, host_labels, p_db_lookup_dimensions, p_db_lookup_method, " \
  1735. " p_db_lookup_options, p_db_lookup_after, p_db_lookup_before, p_update_every, source, chart_labels " \
  1736. " FROM alert_hash ah, c_%p t where ah.hash_id = t.hash_id"
  1737. int sql_get_alert_configuration(
  1738. DICTIONARY *configs,
  1739. void (*cb)(struct sql_alert_config_data *, void *),
  1740. void *data,
  1741. bool debug __maybe_unused)
  1742. {
  1743. int added = -1;
  1744. char sql[512];
  1745. int rc;
  1746. sqlite3_stmt *res = NULL;
  1747. BUFFER *command = NULL;
  1748. if (unlikely(!configs))
  1749. return added;
  1750. snprintfz(sql, 511, SQL_BUILD_CONFIG_TARGET_LIST, configs);
  1751. rc = db_execute(db_meta, sql);
  1752. if (rc)
  1753. return added;
  1754. snprintfz(sql, 511, SQL_POPULATE_TEMP_CONFIG_TARGET_TABLE, configs);
  1755. // Prepare statement to add things
  1756. rc = sqlite3_prepare_v2(db_meta, sql, -1, &res, 0);
  1757. if (unlikely(rc != SQLITE_OK)) {
  1758. error_report("Failed to prepare statement to INSERT into c_%p", configs);
  1759. goto fail_only_drop;
  1760. }
  1761. void *t;
  1762. dfe_start_read(configs, t) {
  1763. uuid_t hash_id;
  1764. uuid_parse( t_dfe.name, hash_id);
  1765. rc = sqlite3_bind_blob(res, 1, &hash_id, sizeof(hash_id), SQLITE_STATIC);
  1766. if (unlikely(rc != SQLITE_OK))
  1767. error_report("Failed to bind host_id parameter.");
  1768. rc = sqlite3_step_monitored(res);
  1769. if (rc != SQLITE_DONE)
  1770. error_report("Error while populating temp table");
  1771. rc = sqlite3_reset(res);
  1772. if (rc != SQLITE_OK)
  1773. error_report("Error while resetting parameters");
  1774. }
  1775. dfe_done(t);
  1776. rc = sqlite3_finalize(res);
  1777. if (unlikely(rc != SQLITE_OK)) {
  1778. // log error but continue
  1779. error_report("Failed to finalize statement for sql_get_alert_configuration temp table population");
  1780. }
  1781. command = buffer_create(MAX_HEALTH_SQL_SIZE, NULL);
  1782. buffer_sprintf(command, SQL_SEARCH_CONFIG_LIST, configs);
  1783. rc = sqlite3_prepare_v2(db_meta, buffer_tostring(command), -1, &res, 0);
  1784. if (unlikely(rc != SQLITE_OK)) {
  1785. error_report("Failed to prepare statement sql_get_alert_configuration");
  1786. goto fail_only_drop;
  1787. }
  1788. struct sql_alert_config_data acd = {0 };
  1789. added = 0;
  1790. int param;
  1791. while (sqlite3_step(res) == SQLITE_ROW) {
  1792. param = 0;
  1793. acd.config_hash_id = (uuid_t *) sqlite3_column_blob(res, param++);
  1794. acd.name = (const char *) sqlite3_column_text(res, param++);
  1795. acd.selectors.on_template = (const char *) sqlite3_column_text(res, param++);
  1796. acd.selectors.on_key = (const char *) sqlite3_column_text(res, param++);
  1797. acd.classification = (const char *) sqlite3_column_text(res, param++);
  1798. acd.component = (const char *) sqlite3_column_text(res, param++);
  1799. acd.type = (const char *) sqlite3_column_text(res, param++);
  1800. acd.selectors.os = (const char *) sqlite3_column_text(res, param++);
  1801. acd.selectors.hosts = (const char *) sqlite3_column_text(res, param++);
  1802. acd.value.db.lookup = (const char *) sqlite3_column_text(res, param++);
  1803. acd.value.every = (const char *) sqlite3_column_text(res, param++);
  1804. acd.value.units = (const char *) sqlite3_column_text(res, param++);
  1805. acd.value.calc = (const char *) sqlite3_column_text(res, param++);
  1806. acd.selectors.families = (const char *) sqlite3_column_text(res, param++);
  1807. acd.selectors.plugin = (const char *) sqlite3_column_text(res, param++);
  1808. acd.selectors.module = (const char *) sqlite3_column_text(res, param++);
  1809. acd.selectors.charts = (const char *) sqlite3_column_text(res, param++);
  1810. acd.status.green = (const char *) sqlite3_column_text(res, param++);
  1811. acd.status.red = (const char *) sqlite3_column_text(res, param++);
  1812. acd.status.warn = (const char *) sqlite3_column_text(res, param++);
  1813. acd.status.crit = (const char *) sqlite3_column_text(res, param++);
  1814. acd.notification.exec = (const char *) sqlite3_column_text(res, param++);
  1815. acd.notification.to_key = (const char *) sqlite3_column_text(res, param++);
  1816. acd.info = (const char *) sqlite3_column_text(res, param++);
  1817. acd.notification.delay = (const char *) sqlite3_column_text(res, param++);
  1818. acd.notification.options = (const char *) sqlite3_column_text(res, param++);
  1819. acd.notification.repeat = (const char *) sqlite3_column_text(res, param++);
  1820. acd.selectors.host_labels = (const char *) sqlite3_column_text(res, param++);
  1821. acd.value.db.dimensions = (const char *) sqlite3_column_text(res, param++);
  1822. acd.value.db.method = (const char *) sqlite3_column_text(res, param++);
  1823. acd.value.db.options = (uint32_t) sqlite3_column_int(res, param++);
  1824. acd.value.db.after = (int32_t) sqlite3_column_int(res, param++);
  1825. acd.value.db.before = (int32_t) sqlite3_column_int(res, param++);
  1826. acd.value.update_every = (int32_t) sqlite3_column_int(res, param++);
  1827. acd.source = (const char *) sqlite3_column_text(res, param++);
  1828. acd.selectors.chart_labels = (const char *) sqlite3_column_text(res, param++);
  1829. cb(&acd, data);
  1830. added++;
  1831. }
  1832. rc = sqlite3_finalize(res);
  1833. if (unlikely(rc != SQLITE_OK))
  1834. error_report("Failed to finalize statement for sql_get_alert_configuration");
  1835. fail_only_drop:
  1836. (void)snprintfz(sql, 511, "DROP TABLE IF EXISTS c_%p", configs);
  1837. (void)db_execute(db_meta, sql);
  1838. buffer_free(command);
  1839. return added;
  1840. }
  1841. #define SQL_FETCH_CHART_NAME "SELECT chart_name FROM health_log where host_id = @host_id LIMIT 1;"
  1842. bool is_chart_name_populated(uuid_t *host_uuid)
  1843. {
  1844. sqlite3_stmt *res = NULL;
  1845. int rc;
  1846. bool status = true;
  1847. rc = sqlite3_prepare_v2(db_meta, SQL_FETCH_CHART_NAME, -1, &res, 0);
  1848. if (unlikely(rc != SQLITE_OK)) {
  1849. error_report("Failed to prepare statement to check health_log chart_name");
  1850. return true;
  1851. }
  1852. rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC);
  1853. if (unlikely(rc != SQLITE_OK)) {
  1854. error_report("Failed to bind host_id for health_log chart_name check");
  1855. goto fail;
  1856. }
  1857. rc = sqlite3_step_monitored(res);
  1858. if (likely(rc == SQLITE_ROW))
  1859. status = sqlite3_column_type(res, 0) != SQLITE_NULL;
  1860. fail:
  1861. rc = sqlite3_finalize(res);
  1862. if (unlikely(rc != SQLITE_OK))
  1863. error_report("Failed to finalize the prepared statement for health_log chart_name check");
  1864. return status;
  1865. }
  1866. #define SQL_POPULATE_CHART_NAME " UPDATE health_log SET chart_name = upd.chart_name FROM " \
  1867. "(SELECT c.type || '.' || IFNULL(c.name, c.id) AS chart_name, hl.host_id, hl.health_log_id FROM " \
  1868. "chart c, health_log hl WHERE (c.type || '.' || c.id) = hl.chart AND c.host_id = hl.host_id " \
  1869. "AND hl.host_id = @host_id) AS upd WHERE health_log.host_id = upd.host_id " \
  1870. "AND health_log.health_log_id = upd.health_log_id"
  1871. void chart_name_populate(uuid_t *host_uuid)
  1872. {
  1873. sqlite3_stmt *res = NULL;
  1874. int rc;
  1875. rc = sqlite3_prepare_v2(db_meta, SQL_POPULATE_CHART_NAME, -1, &res, 0);
  1876. if (unlikely(rc != SQLITE_OK)) {
  1877. error_report("Failed to prepare statement to update health_log chart_name");
  1878. return;
  1879. }
  1880. rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC);
  1881. if (unlikely(rc != SQLITE_OK)) {
  1882. error_report("Failed to bind host_id for health_log chart_name update");
  1883. goto fail;
  1884. }
  1885. rc = execute_insert(res);
  1886. if (unlikely(rc != SQLITE_DONE))
  1887. error_report("Failed to update chart name in health_log, rc = %d", rc);
  1888. fail:
  1889. rc = sqlite3_finalize(res);
  1890. if (unlikely(rc != SQLITE_OK))
  1891. error_report("Failed to finalize the prepared statement for health_log chart_name update");
  1892. }