rrdhost.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #define NETDATA_RRD_INTERNALS
  3. #include "rrd.h"
  4. RRDHOST *localhost = NULL;
  5. size_t rrd_hosts_available = 0;
  6. netdata_rwlock_t rrd_rwlock = NETDATA_RWLOCK_INITIALIZER;
  7. time_t rrdset_free_obsolete_time = 3600;
  8. time_t rrdhost_free_orphan_time = 3600;
  9. // ----------------------------------------------------------------------------
  10. // RRDHOST index
  11. int rrdhost_compare(void* a, void* b) {
  12. if(((RRDHOST *)a)->hash_machine_guid < ((RRDHOST *)b)->hash_machine_guid) return -1;
  13. else if(((RRDHOST *)a)->hash_machine_guid > ((RRDHOST *)b)->hash_machine_guid) return 1;
  14. else return strcmp(((RRDHOST *)a)->machine_guid, ((RRDHOST *)b)->machine_guid);
  15. }
  16. avl_tree_lock rrdhost_root_index = {
  17. .avl_tree = { NULL, rrdhost_compare },
  18. .rwlock = AVL_LOCK_INITIALIZER
  19. };
  20. RRDHOST *rrdhost_find_by_guid(const char *guid, uint32_t hash) {
  21. debug(D_RRDHOST, "Searching in index for host with guid '%s'", guid);
  22. RRDHOST tmp;
  23. strncpyz(tmp.machine_guid, guid, GUID_LEN);
  24. tmp.hash_machine_guid = (hash)?hash:simple_hash(tmp.machine_guid);
  25. return (RRDHOST *)avl_search_lock(&(rrdhost_root_index), (avl_t *) &tmp);
  26. }
  27. RRDHOST *rrdhost_find_by_hostname(const char *hostname, uint32_t hash) {
  28. if(unlikely(!strcmp(hostname, "localhost")))
  29. return localhost;
  30. if(unlikely(!hash)) hash = simple_hash(hostname);
  31. rrd_rdlock();
  32. RRDHOST *host;
  33. rrdhost_foreach_read(host) {
  34. if(unlikely((hash == host->hash_hostname && !strcmp(hostname, host->hostname)))) {
  35. rrd_unlock();
  36. return host;
  37. }
  38. }
  39. rrd_unlock();
  40. return NULL;
  41. }
  42. #define rrdhost_index_add(rrdhost) (RRDHOST *)avl_insert_lock(&(rrdhost_root_index), (avl_t *)(rrdhost))
  43. #define rrdhost_index_del(rrdhost) (RRDHOST *)avl_remove_lock(&(rrdhost_root_index), (avl_t *)(rrdhost))
  44. // ----------------------------------------------------------------------------
  45. // RRDHOST - internal helpers
  46. static inline void rrdhost_init_tags(RRDHOST *host, const char *tags) {
  47. if(host->tags && tags && !strcmp(host->tags, tags))
  48. return;
  49. void *old = (void *)host->tags;
  50. host->tags = (tags && *tags)?strdupz(tags):NULL;
  51. freez(old);
  52. }
  53. static inline void rrdhost_init_hostname(RRDHOST *host, const char *hostname) {
  54. if(host->hostname && hostname && !strcmp(host->hostname, hostname))
  55. return;
  56. void *old = host->hostname;
  57. host->hostname = strdupz(hostname?hostname:"localhost");
  58. host->hash_hostname = simple_hash(host->hostname);
  59. freez(old);
  60. }
  61. static inline void rrdhost_init_os(RRDHOST *host, const char *os) {
  62. if(host->os && os && !strcmp(host->os, os))
  63. return;
  64. void *old = (void *)host->os;
  65. host->os = strdupz(os?os:"unknown");
  66. freez(old);
  67. }
  68. static inline void rrdhost_init_timezone(RRDHOST *host, const char *timezone, const char *abbrev_timezone, int32_t utc_offset) {
  69. if (host->timezone && timezone && !strcmp(host->timezone, timezone) && host->abbrev_timezone && abbrev_timezone &&
  70. !strcmp(host->abbrev_timezone, abbrev_timezone) && host->utc_offset == utc_offset)
  71. return;
  72. void *old = (void *)host->timezone;
  73. host->timezone = strdupz((timezone && *timezone)?timezone:"unknown");
  74. freez(old);
  75. old = (void *)host->abbrev_timezone;
  76. host->abbrev_timezone = strdupz((abbrev_timezone && *abbrev_timezone) ? abbrev_timezone : "UTC");
  77. freez(old);
  78. host->utc_offset = utc_offset;
  79. }
  80. static inline void rrdhost_init_machine_guid(RRDHOST *host, const char *machine_guid) {
  81. strncpy(host->machine_guid, machine_guid, GUID_LEN);
  82. host->machine_guid[GUID_LEN] = '\0';
  83. host->hash_machine_guid = simple_hash(host->machine_guid);
  84. }
  85. void set_host_properties(RRDHOST *host, int update_every, RRD_MEMORY_MODE memory_mode, const char *hostname,
  86. const char *registry_hostname, const char *guid, const char *os, const char *tags,
  87. const char *tzone, const char *abbrev_tzone, int32_t utc_offset, const char *program_name,
  88. const char *program_version)
  89. {
  90. host->rrd_update_every = update_every;
  91. host->rrd_memory_mode = memory_mode;
  92. rrdhost_init_hostname(host, hostname);
  93. rrdhost_init_machine_guid(host, guid);
  94. rrdhost_init_os(host, os);
  95. rrdhost_init_timezone(host, tzone, abbrev_tzone, utc_offset);
  96. rrdhost_init_tags(host, tags);
  97. host->program_name = strdupz((program_name && *program_name) ? program_name : "unknown");
  98. host->program_version = strdupz((program_version && *program_version) ? program_version : "unknown");
  99. host->registry_hostname = strdupz((registry_hostname && *registry_hostname) ? registry_hostname : host->hostname);
  100. }
  101. // ----------------------------------------------------------------------------
  102. // RRDHOST - add a host
  103. RRDHOST *rrdhost_create(const char *hostname,
  104. const char *registry_hostname,
  105. const char *guid,
  106. const char *os,
  107. const char *timezone,
  108. const char *abbrev_timezone,
  109. int32_t utc_offset,
  110. const char *tags,
  111. const char *program_name,
  112. const char *program_version,
  113. int update_every,
  114. long entries,
  115. RRD_MEMORY_MODE memory_mode,
  116. unsigned int health_enabled,
  117. unsigned int rrdpush_enabled,
  118. char *rrdpush_destination,
  119. char *rrdpush_api_key,
  120. char *rrdpush_send_charts_matching,
  121. struct rrdhost_system_info *system_info,
  122. int is_localhost
  123. ) {
  124. debug(D_RRDHOST, "Host '%s': adding with guid '%s'", hostname, guid);
  125. #ifdef ENABLE_DBENGINE
  126. int is_legacy = (memory_mode == RRD_MEMORY_MODE_DBENGINE) && is_legacy_child(guid);
  127. #else
  128. int is_legacy = 1;
  129. #endif
  130. rrd_check_wrlock();
  131. int is_in_multihost = (memory_mode == RRD_MEMORY_MODE_DBENGINE && !is_legacy);
  132. RRDHOST *host = callocz(1, sizeof(RRDHOST));
  133. set_host_properties(host, (update_every > 0)?update_every:1, memory_mode, hostname, registry_hostname, guid, os,
  134. tags, timezone, abbrev_timezone, utc_offset, program_name, program_version);
  135. host->rrd_history_entries = align_entries_to_pagesize(memory_mode, entries);
  136. host->health_enabled = ((memory_mode == RRD_MEMORY_MODE_NONE)) ? 0 : health_enabled;
  137. host->sender = mallocz(sizeof(*host->sender));
  138. sender_init(host->sender, host);
  139. netdata_mutex_init(&host->receiver_lock);
  140. host->rrdpush_send_enabled = (rrdpush_enabled && rrdpush_destination && *rrdpush_destination && rrdpush_api_key && *rrdpush_api_key) ? 1 : 0;
  141. host->rrdpush_send_destination = (host->rrdpush_send_enabled)?strdupz(rrdpush_destination):NULL;
  142. host->rrdpush_send_api_key = (host->rrdpush_send_enabled)?strdupz(rrdpush_api_key):NULL;
  143. host->rrdpush_send_charts_matching = simple_pattern_create(rrdpush_send_charts_matching, NULL, SIMPLE_PATTERN_EXACT);
  144. host->rrdpush_sender_pipe[0] = -1;
  145. host->rrdpush_sender_pipe[1] = -1;
  146. host->rrdpush_sender_socket = -1;
  147. //host->stream_version = STREAMING_PROTOCOL_CURRENT_VERSION; Unused?
  148. #ifdef ENABLE_HTTPS
  149. host->ssl.conn = NULL;
  150. host->ssl.flags = NETDATA_SSL_START;
  151. host->stream_ssl.conn = NULL;
  152. host->stream_ssl.flags = NETDATA_SSL_START;
  153. #endif
  154. netdata_rwlock_init(&host->rrdhost_rwlock);
  155. netdata_rwlock_init(&host->labels.labels_rwlock);
  156. netdata_mutex_init(&host->aclk_state_lock);
  157. host->system_info = system_info;
  158. avl_init_lock(&(host->rrdset_root_index), rrdset_compare);
  159. avl_init_lock(&(host->rrdset_root_index_name), rrdset_compare_name);
  160. avl_init_lock(&(host->rrdfamily_root_index), rrdfamily_compare);
  161. avl_init_lock(&(host->rrdvar_root_index), rrdvar_compare);
  162. if(config_get_boolean(CONFIG_SECTION_GLOBAL, "delete obsolete charts files", 1))
  163. rrdhost_flag_set(host, RRDHOST_FLAG_DELETE_OBSOLETE_CHARTS);
  164. if(config_get_boolean(CONFIG_SECTION_GLOBAL, "delete orphan hosts files", 1) && !is_localhost)
  165. rrdhost_flag_set(host, RRDHOST_FLAG_DELETE_ORPHAN_HOST);
  166. host->health_default_warn_repeat_every = config_get_duration(CONFIG_SECTION_HEALTH, "default repeat warning", "never");
  167. host->health_default_crit_repeat_every = config_get_duration(CONFIG_SECTION_HEALTH, "default repeat critical", "never");
  168. avl_init_lock(&(host->alarms_idx_health_log), alarm_compare_id);
  169. avl_init_lock(&(host->alarms_idx_name), alarm_compare_name);
  170. // ------------------------------------------------------------------------
  171. // initialize health variables
  172. host->health_log.next_log_id = 1;
  173. host->health_log.next_alarm_id = 1;
  174. host->health_log.max = 1000;
  175. host->health_log.next_log_id = (uint32_t)now_realtime_sec();
  176. host->health_log.next_alarm_id = 0;
  177. long n = config_get_number(CONFIG_SECTION_HEALTH, "in memory max health log entries", host->health_log.max);
  178. if(n < 10) {
  179. error("Host '%s': health configuration has invalid max log entries %ld. Using default %u", host->hostname, n, host->health_log.max);
  180. config_set_number(CONFIG_SECTION_HEALTH, "in memory max health log entries", (long)host->health_log.max);
  181. }
  182. else
  183. host->health_log.max = (unsigned int)n;
  184. netdata_rwlock_init(&host->health_log.alarm_log_rwlock);
  185. char filename[FILENAME_MAX + 1];
  186. if(is_localhost) {
  187. host->cache_dir = strdupz(netdata_configured_cache_dir);
  188. host->varlib_dir = strdupz(netdata_configured_varlib_dir);
  189. }
  190. else {
  191. // this is not localhost - append our GUID to localhost path
  192. if (is_in_multihost) { // don't append to cache dir in multihost
  193. host->cache_dir = strdupz(netdata_configured_cache_dir);
  194. } else {
  195. snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_cache_dir, host->machine_guid);
  196. host->cache_dir = strdupz(filename);
  197. }
  198. if((host->rrd_memory_mode == RRD_MEMORY_MODE_MAP || host->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || (
  199. host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE && is_legacy))) {
  200. int r = mkdir(host->cache_dir, 0775);
  201. if(r != 0 && errno != EEXIST)
  202. error("Host '%s': cannot create directory '%s'", host->hostname, host->cache_dir);
  203. }
  204. snprintfz(filename, FILENAME_MAX, "%s/%s", netdata_configured_varlib_dir, host->machine_guid);
  205. host->varlib_dir = strdupz(filename);
  206. if(host->health_enabled) {
  207. int r = mkdir(host->varlib_dir, 0775);
  208. if(r != 0 && errno != EEXIST)
  209. error("Host '%s': cannot create directory '%s'", host->hostname, host->varlib_dir);
  210. }
  211. }
  212. if(host->health_enabled) {
  213. snprintfz(filename, FILENAME_MAX, "%s/health", host->varlib_dir);
  214. int r = mkdir(filename, 0775);
  215. if(r != 0 && errno != EEXIST)
  216. error("Host '%s': cannot create directory '%s'", host->hostname, filename);
  217. }
  218. snprintfz(filename, FILENAME_MAX, "%s/health/health-log.db", host->varlib_dir);
  219. host->health_log_filename = strdupz(filename);
  220. snprintfz(filename, FILENAME_MAX, "%s/alarm-notify.sh", netdata_configured_primary_plugins_dir);
  221. host->health_default_exec = strdupz(config_get(CONFIG_SECTION_HEALTH, "script to execute on alarm", filename));
  222. host->health_default_recipient = strdupz("root");
  223. // ------------------------------------------------------------------------
  224. // load health configuration
  225. if(host->health_enabled) {
  226. rrdhost_wrlock(host);
  227. health_readdir(host, health_user_config_dir(), health_stock_config_dir(), NULL);
  228. rrdhost_unlock(host);
  229. }
  230. RRDHOST *t = rrdhost_index_add(host);
  231. if(t != host) {
  232. error("Host '%s': cannot add host with machine guid '%s' to index. It already exists as host '%s' with machine guid '%s'.", host->hostname, host->machine_guid, t->hostname, t->machine_guid);
  233. rrdhost_free(host);
  234. return NULL;
  235. }
  236. if (likely(!uuid_parse(host->machine_guid, host->host_uuid))) {
  237. int rc = sql_store_host(&host->host_uuid, hostname, registry_hostname, update_every, os, timezone, tags);
  238. if (unlikely(rc))
  239. error_report("Failed to store machine GUID to the database");
  240. sql_load_node_id(host);
  241. if (host->health_enabled) {
  242. if (!file_is_migrated(host->health_log_filename)) {
  243. int rc = sql_create_health_log_table(host);
  244. if (unlikely(rc)) {
  245. error_report("Failed to create health log table in the database");
  246. health_alarm_log_load(host);
  247. health_alarm_log_open(host);
  248. }
  249. else {
  250. health_alarm_log_load(host);
  251. add_migrated_file(host->health_log_filename, 0);
  252. }
  253. } else {
  254. sql_create_health_log_table(host);
  255. sql_health_alarm_log_load(host);
  256. }
  257. }
  258. }
  259. else
  260. error_report("Host machine GUID %s is not valid", host->machine_guid);
  261. if (host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
  262. #ifdef ENABLE_DBENGINE
  263. char dbenginepath[FILENAME_MAX + 1];
  264. int ret;
  265. snprintfz(dbenginepath, FILENAME_MAX, "%s/dbengine", host->cache_dir);
  266. ret = mkdir(dbenginepath, 0775);
  267. if (ret != 0 && errno != EEXIST)
  268. error("Host '%s': cannot create directory '%s'", host->hostname, dbenginepath);
  269. else ret = 0; // succeed
  270. if (is_legacy) // initialize legacy dbengine instance as needed
  271. ret = rrdeng_init(host, &host->rrdeng_ctx, dbenginepath, default_rrdeng_page_cache_mb,
  272. default_rrdeng_disk_quota_mb); // may fail here for legacy dbengine initialization
  273. else
  274. host->rrdeng_ctx = &multidb_ctx;
  275. if (ret) { // check legacy or multihost initialization success
  276. error(
  277. "Host '%s': cannot initialize host with machine guid '%s'. Failed to initialize DB engine at '%s'.",
  278. host->hostname, host->machine_guid, host->cache_dir);
  279. rrdhost_free(host);
  280. host = NULL;
  281. //rrd_hosts_available++; //TODO: maybe we want this?
  282. return host;
  283. }
  284. #else
  285. fatal("RRD_MEMORY_MODE_DBENGINE is not supported in this platform.");
  286. #endif
  287. }
  288. else {
  289. #ifdef ENABLE_DBENGINE
  290. host->rrdeng_ctx = &multidb_ctx;
  291. #endif
  292. }
  293. // ------------------------------------------------------------------------
  294. // link it and add it to the index
  295. if(is_localhost) {
  296. host->next = localhost;
  297. localhost = host;
  298. }
  299. else {
  300. if(localhost) {
  301. host->next = localhost->next;
  302. localhost->next = host;
  303. }
  304. else localhost = host;
  305. }
  306. // ------------------------------------------------------------------------
  307. // init new ML host and update system_info to let upstreams know
  308. // about ML functionality
  309. //
  310. if (is_localhost && host->system_info) {
  311. host->system_info->ml_capable = ml_capable();
  312. host->system_info->ml_enabled = ml_enabled(host);
  313. }
  314. ml_new_host(host);
  315. info("Host '%s' (at registry as '%s') with guid '%s' initialized"
  316. ", os '%s'"
  317. ", timezone '%s'"
  318. ", tags '%s'"
  319. ", program_name '%s'"
  320. ", program_version '%s'"
  321. ", update every %d"
  322. ", memory mode %s"
  323. ", history entries %ld"
  324. ", streaming %s"
  325. " (to '%s' with api key '%s')"
  326. ", health %s"
  327. ", cache_dir '%s'"
  328. ", varlib_dir '%s'"
  329. ", health_log '%s'"
  330. ", alarms default handler '%s'"
  331. ", alarms default recipient '%s'"
  332. , host->hostname
  333. , host->registry_hostname
  334. , host->machine_guid
  335. , host->os
  336. , host->timezone
  337. , (host->tags)?host->tags:""
  338. , host->program_name
  339. , host->program_version
  340. , host->rrd_update_every
  341. , rrd_memory_mode_name(host->rrd_memory_mode)
  342. , host->rrd_history_entries
  343. , host->rrdpush_send_enabled?"enabled":"disabled"
  344. , host->rrdpush_send_destination?host->rrdpush_send_destination:""
  345. , host->rrdpush_send_api_key?host->rrdpush_send_api_key:""
  346. , host->health_enabled?"enabled":"disabled"
  347. , host->cache_dir
  348. , host->varlib_dir
  349. , host->health_log_filename
  350. , host->health_default_exec
  351. , host->health_default_recipient
  352. );
  353. rrd_hosts_available++;
  354. return host;
  355. }
  356. void rrdhost_update(RRDHOST *host
  357. , const char *hostname
  358. , const char *registry_hostname
  359. , const char *guid
  360. , const char *os
  361. , const char *timezone
  362. , const char *abbrev_timezone
  363. , int32_t utc_offset
  364. , const char *tags
  365. , const char *program_name
  366. , const char *program_version
  367. , int update_every
  368. , long history
  369. , RRD_MEMORY_MODE mode
  370. , unsigned int health_enabled
  371. , unsigned int rrdpush_enabled
  372. , char *rrdpush_destination
  373. , char *rrdpush_api_key
  374. , char *rrdpush_send_charts_matching
  375. , struct rrdhost_system_info *system_info
  376. )
  377. {
  378. UNUSED(guid);
  379. UNUSED(rrdpush_enabled);
  380. UNUSED(rrdpush_destination);
  381. UNUSED(rrdpush_api_key);
  382. UNUSED(rrdpush_send_charts_matching);
  383. host->health_enabled = (mode == RRD_MEMORY_MODE_NONE) ? 0 : health_enabled;
  384. //host->stream_version = STREAMING_PROTOCOL_CURRENT_VERSION; Unused?
  385. rrdhost_system_info_free(host->system_info);
  386. host->system_info = system_info;
  387. rrdhost_init_os(host, os);
  388. rrdhost_init_timezone(host, timezone, abbrev_timezone, utc_offset);
  389. freez(host->registry_hostname);
  390. host->registry_hostname = strdupz((registry_hostname && *registry_hostname)?registry_hostname:hostname);
  391. if(strcmp(host->hostname, hostname) != 0) {
  392. info("Host '%s' has been renamed to '%s'. If this is not intentional it may mean multiple hosts are using the same machine_guid.", host->hostname, hostname);
  393. char *t = host->hostname;
  394. host->hostname = strdupz(hostname);
  395. host->hash_hostname = simple_hash(host->hostname);
  396. freez(t);
  397. }
  398. if(strcmp(host->program_name, program_name) != 0) {
  399. info("Host '%s' switched program name from '%s' to '%s'", host->hostname, host->program_name, program_name);
  400. char *t = host->program_name;
  401. host->program_name = strdupz(program_name);
  402. freez(t);
  403. }
  404. if(strcmp(host->program_version, program_version) != 0) {
  405. info("Host '%s' switched program version from '%s' to '%s'", host->hostname, host->program_version, program_version);
  406. char *t = host->program_version;
  407. host->program_version = strdupz(program_version);
  408. freez(t);
  409. }
  410. if(host->rrd_update_every != update_every)
  411. error("Host '%s' has an update frequency of %d seconds, but the wanted one is %d seconds. Restart netdata here to apply the new settings.", host->hostname, host->rrd_update_every, update_every);
  412. if(host->rrd_history_entries < history)
  413. error("Host '%s' has history of %ld entries, but the wanted one is %ld entries. Restart netdata here to apply the new settings.", host->hostname, host->rrd_history_entries, history);
  414. if(host->rrd_memory_mode != mode)
  415. error("Host '%s' has memory mode '%s', but the wanted one is '%s'. Restart netdata here to apply the new settings.", host->hostname, rrd_memory_mode_name(host->rrd_memory_mode), rrd_memory_mode_name(mode));
  416. // update host tags
  417. rrdhost_init_tags(host, tags);
  418. if (rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED)) {
  419. rrdhost_flag_clear(host, RRDHOST_FLAG_ARCHIVED);
  420. if(host->health_enabled) {
  421. int r;
  422. char filename[FILENAME_MAX + 1];
  423. if (host != localhost) {
  424. r = mkdir(host->varlib_dir, 0775);
  425. if (r != 0 && errno != EEXIST)
  426. error("Host '%s': cannot create directory '%s'", host->hostname, host->varlib_dir);
  427. }
  428. snprintfz(filename, FILENAME_MAX, "%s/health", host->varlib_dir);
  429. r = mkdir(filename, 0775);
  430. if(r != 0 && errno != EEXIST)
  431. error("Host '%s': cannot create directory '%s'", host->hostname, filename);
  432. rrdhost_wrlock(host);
  433. health_readdir(host, health_user_config_dir(), health_stock_config_dir(), NULL);
  434. rrdhost_unlock(host);
  435. if (!file_is_migrated(host->health_log_filename)) {
  436. int rc = sql_create_health_log_table(host);
  437. if (unlikely(rc)) {
  438. error_report("Failed to create health log table in the database");
  439. health_alarm_log_load(host);
  440. health_alarm_log_open(host);
  441. } else {
  442. health_alarm_log_load(host);
  443. add_migrated_file(host->health_log_filename, 0);
  444. }
  445. } else {
  446. sql_create_health_log_table(host);
  447. sql_health_alarm_log_load(host);
  448. }
  449. }
  450. rrd_hosts_available++;
  451. info("Host %s is not in archived mode anymore", host->hostname);
  452. }
  453. return;
  454. }
  455. RRDHOST *rrdhost_find_or_create(
  456. const char *hostname
  457. , const char *registry_hostname
  458. , const char *guid
  459. , const char *os
  460. , const char *timezone
  461. , const char *abbrev_timezone
  462. , int32_t utc_offset
  463. , const char *tags
  464. , const char *program_name
  465. , const char *program_version
  466. , int update_every
  467. , long history
  468. , RRD_MEMORY_MODE mode
  469. , unsigned int health_enabled
  470. , unsigned int rrdpush_enabled
  471. , char *rrdpush_destination
  472. , char *rrdpush_api_key
  473. , char *rrdpush_send_charts_matching
  474. , struct rrdhost_system_info *system_info
  475. ) {
  476. debug(D_RRDHOST, "Searching for host '%s' with guid '%s'", hostname, guid);
  477. rrd_wrlock();
  478. RRDHOST *host = rrdhost_find_by_guid(guid, 0);
  479. if (unlikely(host && RRD_MEMORY_MODE_DBENGINE != mode && rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED))) {
  480. /* If a legacy memory mode instantiates all dbengine state must be discarded to avoid inconsistencies */
  481. error("Archived host '%s' has memory mode '%s', but the wanted one is '%s'. Discarding archived state.",
  482. host->hostname, rrd_memory_mode_name(host->rrd_memory_mode), rrd_memory_mode_name(mode));
  483. rrdhost_free(host);
  484. host = NULL;
  485. }
  486. if(!host) {
  487. host = rrdhost_create(
  488. hostname
  489. , registry_hostname
  490. , guid
  491. , os
  492. , timezone
  493. , abbrev_timezone
  494. , utc_offset
  495. , tags
  496. , program_name
  497. , program_version
  498. , update_every
  499. , history
  500. , mode
  501. , health_enabled
  502. , rrdpush_enabled
  503. , rrdpush_destination
  504. , rrdpush_api_key
  505. , rrdpush_send_charts_matching
  506. , system_info
  507. , 0
  508. );
  509. }
  510. else {
  511. rrdhost_update(host
  512. , hostname
  513. , registry_hostname
  514. , guid
  515. , os
  516. , timezone
  517. , abbrev_timezone
  518. , utc_offset
  519. , tags
  520. , program_name
  521. , program_version
  522. , update_every
  523. , history
  524. , mode
  525. , health_enabled
  526. , rrdpush_enabled
  527. , rrdpush_destination
  528. , rrdpush_api_key
  529. , rrdpush_send_charts_matching
  530. , system_info);
  531. }
  532. if (host) {
  533. rrdhost_wrlock(host);
  534. rrdhost_flag_clear(host, RRDHOST_FLAG_ORPHAN);
  535. host->senders_disconnected_time = 0;
  536. rrdhost_unlock(host);
  537. }
  538. rrd_unlock();
  539. return host;
  540. }
  541. inline int rrdhost_should_be_removed(RRDHOST *host, RRDHOST *protected_host, time_t now) {
  542. if(host != protected_host
  543. && host != localhost
  544. && rrdhost_flag_check(host, RRDHOST_FLAG_ORPHAN)
  545. && !host->receiver
  546. && host->senders_disconnected_time
  547. && host->senders_disconnected_time + rrdhost_free_orphan_time < now)
  548. return 1;
  549. return 0;
  550. }
  551. void rrdhost_cleanup_orphan_hosts_nolock(RRDHOST *protected_host) {
  552. time_t now = now_realtime_sec();
  553. RRDHOST *host;
  554. restart_after_removal:
  555. rrdhost_foreach_write(host) {
  556. if(rrdhost_should_be_removed(host, protected_host, now)) {
  557. info("Host '%s' with machine guid '%s' is obsolete - cleaning up.", host->hostname, host->machine_guid);
  558. if (rrdhost_flag_check(host, RRDHOST_FLAG_DELETE_ORPHAN_HOST)
  559. #ifdef ENABLE_DBENGINE
  560. /* don't delete multi-host DB host files */
  561. && !(host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE && host->rrdeng_ctx == &multidb_ctx)
  562. #endif
  563. )
  564. rrdhost_delete_charts(host);
  565. else
  566. rrdhost_save_charts(host);
  567. rrdhost_free(host);
  568. goto restart_after_removal;
  569. }
  570. }
  571. }
  572. // ----------------------------------------------------------------------------
  573. // RRDHOST global / startup initialization
  574. int rrd_init(char *hostname, struct rrdhost_system_info *system_info) {
  575. rrdset_free_obsolete_time = config_get_number(CONFIG_SECTION_GLOBAL, "cleanup obsolete charts after seconds", rrdset_free_obsolete_time);
  576. // Current chart locking and invalidation scheme doesn't prevent Netdata from segmentation faults if a short
  577. // cleanup delay is set. Extensive stress tests showed that 10 seconds is quite a safe delay. Look at
  578. // https://github.com/netdata/netdata/pull/11222#issuecomment-868367920 for more information.
  579. if (rrdset_free_obsolete_time < 10) {
  580. rrdset_free_obsolete_time = 10;
  581. info("The \"cleanup obsolete charts after seconds\" option was set to 10 seconds. A lower delay can potentially cause a segmentation fault.");
  582. }
  583. gap_when_lost_iterations_above = (int)config_get_number(CONFIG_SECTION_GLOBAL, "gap when lost iterations above", gap_when_lost_iterations_above);
  584. if (gap_when_lost_iterations_above < 1)
  585. gap_when_lost_iterations_above = 1;
  586. if (unlikely(sql_init_database(DB_CHECK_NONE))) {
  587. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  588. fatal("Failed to initialize SQLite");
  589. info("Skipping SQLITE metadata initialization since memory mode is not db engine");
  590. }
  591. health_init();
  592. rrdpush_init();
  593. debug(D_RRDHOST, "Initializing localhost with hostname '%s'", hostname);
  594. rrd_wrlock();
  595. localhost = rrdhost_create(
  596. hostname
  597. , registry_get_this_machine_hostname()
  598. , registry_get_this_machine_guid()
  599. , os_type
  600. , netdata_configured_timezone
  601. , netdata_configured_abbrev_timezone
  602. , netdata_configured_utc_offset
  603. , ""
  604. , program_name
  605. , program_version
  606. , default_rrd_update_every
  607. , default_rrd_history_entries
  608. , default_rrd_memory_mode
  609. , default_health_enabled
  610. , default_rrdpush_enabled
  611. , default_rrdpush_destination
  612. , default_rrdpush_api_key
  613. , default_rrdpush_send_charts_matching
  614. , system_info
  615. , 1
  616. );
  617. if (unlikely(!localhost)) {
  618. rrd_unlock();
  619. return 1;
  620. }
  621. #ifdef ENABLE_DBENGINE
  622. char dbenginepath[FILENAME_MAX + 1];
  623. int ret;
  624. snprintfz(dbenginepath, FILENAME_MAX, "%s/dbengine", localhost->cache_dir);
  625. ret = mkdir(dbenginepath, 0775);
  626. if (ret != 0 && errno != EEXIST)
  627. error("Host '%s': cannot create directory '%s'", localhost->hostname, dbenginepath);
  628. else // Unconditionally create multihost db to support on demand host creation
  629. ret = rrdeng_init(NULL, NULL, dbenginepath, default_rrdeng_page_cache_mb, default_multidb_disk_quota_mb);
  630. if (ret) {
  631. error(
  632. "Host '%s' with machine guid '%s' failed to initialize multi-host DB engine instance at '%s'.",
  633. localhost->hostname, localhost->machine_guid, localhost->cache_dir);
  634. rrdhost_free(localhost);
  635. localhost = NULL;
  636. rrd_unlock();
  637. fatal("Failed to initialize dbengine");
  638. }
  639. #endif
  640. sql_aclk_sync_init();
  641. rrd_unlock();
  642. web_client_api_v1_management_init();
  643. return localhost==NULL;
  644. }
  645. // ----------------------------------------------------------------------------
  646. // RRDHOST - lock validations
  647. // there are only used when NETDATA_INTERNAL_CHECKS is set
  648. void __rrdhost_check_rdlock(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
  649. debug(D_RRDHOST, "Checking read lock on host '%s'", host->hostname);
  650. int ret = netdata_rwlock_trywrlock(&host->rrdhost_rwlock);
  651. if(ret == 0)
  652. fatal("RRDHOST '%s' should be read-locked, but it is not, at function %s() at line %lu of file '%s'", host->hostname, function, line, file);
  653. }
  654. void __rrdhost_check_wrlock(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
  655. debug(D_RRDHOST, "Checking write lock on host '%s'", host->hostname);
  656. int ret = netdata_rwlock_tryrdlock(&host->rrdhost_rwlock);
  657. if(ret == 0)
  658. fatal("RRDHOST '%s' should be write-locked, but it is not, at function %s() at line %lu of file '%s'", host->hostname, function, line, file);
  659. }
  660. void __rrd_check_rdlock(const char *file, const char *function, const unsigned long line) {
  661. debug(D_RRDHOST, "Checking read lock on all RRDs");
  662. int ret = netdata_rwlock_trywrlock(&rrd_rwlock);
  663. if(ret == 0)
  664. fatal("RRDs should be read-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
  665. }
  666. void __rrd_check_wrlock(const char *file, const char *function, const unsigned long line) {
  667. debug(D_RRDHOST, "Checking write lock on all RRDs");
  668. int ret = netdata_rwlock_tryrdlock(&rrd_rwlock);
  669. if(ret == 0)
  670. fatal("RRDs should be write-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
  671. }
  672. // ----------------------------------------------------------------------------
  673. // RRDHOST - free
  674. void rrdhost_system_info_free(struct rrdhost_system_info *system_info) {
  675. info("SYSTEM_INFO: free %p", system_info);
  676. if(likely(system_info)) {
  677. freez(system_info->cloud_provider_type);
  678. freez(system_info->cloud_instance_type);
  679. freez(system_info->cloud_instance_region);
  680. freez(system_info->host_os_name);
  681. freez(system_info->host_os_id);
  682. freez(system_info->host_os_id_like);
  683. freez(system_info->host_os_version);
  684. freez(system_info->host_os_version_id);
  685. freez(system_info->host_os_detection);
  686. freez(system_info->host_cores);
  687. freez(system_info->host_cpu_freq);
  688. freez(system_info->host_ram_total);
  689. freez(system_info->host_disk_space);
  690. freez(system_info->container_os_name);
  691. freez(system_info->container_os_id);
  692. freez(system_info->container_os_id_like);
  693. freez(system_info->container_os_version);
  694. freez(system_info->container_os_version_id);
  695. freez(system_info->container_os_detection);
  696. freez(system_info->kernel_name);
  697. freez(system_info->kernel_version);
  698. freez(system_info->architecture);
  699. freez(system_info->virtualization);
  700. freez(system_info->virt_detection);
  701. freez(system_info->container);
  702. freez(system_info->container_detection);
  703. freez(system_info->is_k8s_node);
  704. freez(system_info->install_type);
  705. freez(system_info->prebuilt_arch);
  706. freez(system_info->prebuilt_dist);
  707. freez(system_info);
  708. }
  709. }
  710. void destroy_receiver_state(struct receiver_state *rpt);
  711. void rrdhost_free(RRDHOST *host) {
  712. if(!host) return;
  713. info("Freeing all memory for host '%s'...", host->hostname);
  714. rrd_check_wrlock(); // make sure the RRDs are write locked
  715. rrdhost_wrlock(host);
  716. ml_delete_host(host);
  717. rrdhost_unlock(host);
  718. // ------------------------------------------------------------------------
  719. // clean up streaming
  720. rrdpush_sender_thread_stop(host); // stop a possibly running thread
  721. cbuffer_free(host->sender->buffer);
  722. buffer_free(host->sender->build);
  723. #ifdef ENABLE_COMPRESSION
  724. if (host->sender->compressor)
  725. host->sender->compressor->destroy(&host->sender->compressor);
  726. #endif
  727. freez(host->sender);
  728. host->sender = NULL;
  729. if (netdata_exit) {
  730. netdata_mutex_lock(&host->receiver_lock);
  731. if (host->receiver) {
  732. if (!host->receiver->exited)
  733. netdata_thread_cancel(host->receiver->thread);
  734. netdata_mutex_unlock(&host->receiver_lock);
  735. struct receiver_state *rpt = host->receiver;
  736. while (host->receiver && !rpt->exited)
  737. sleep_usec(50 * USEC_PER_MS);
  738. // If the receiver detached from the host then its thread will destroy the state
  739. if (host->receiver == rpt)
  740. destroy_receiver_state(host->receiver);
  741. }
  742. else
  743. netdata_mutex_unlock(&host->receiver_lock);
  744. }
  745. rrdhost_wrlock(host); // lock this RRDHOST
  746. #if defined(ENABLE_ACLK) && defined(ENABLE_NEW_CLOUD_PROTOCOL)
  747. struct aclk_database_worker_config *wc = host->dbsync_worker;
  748. if (wc && !netdata_exit) {
  749. struct aclk_database_cmd cmd;
  750. memset(&cmd, 0, sizeof(cmd));
  751. cmd.opcode = ACLK_DATABASE_ORPHAN_HOST;
  752. struct aclk_completion compl ;
  753. init_aclk_completion(&compl );
  754. cmd.completion = &compl ;
  755. aclk_database_enq_cmd(wc, &cmd);
  756. wait_for_aclk_completion(&compl );
  757. destroy_aclk_completion(&compl );
  758. }
  759. #endif
  760. // ------------------------------------------------------------------------
  761. // release its children resources
  762. #ifdef ENABLE_DBENGINE
  763. if (host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
  764. if (host->rrdeng_ctx != &multidb_ctx)
  765. rrdeng_prepare_exit(host->rrdeng_ctx);
  766. }
  767. #endif
  768. while(host->rrdset_root)
  769. rrdset_free(host->rrdset_root);
  770. freez(host->exporting_flags);
  771. while(host->alarms)
  772. rrdcalc_unlink_and_free(host, host->alarms);
  773. RRDCALC *rc,*nc;
  774. for(rc = host->alarms_with_foreach; rc ; rc = nc) {
  775. nc = rc->next;
  776. rrdcalc_free(rc);
  777. }
  778. host->alarms_with_foreach = NULL;
  779. while(host->templates)
  780. rrdcalctemplate_unlink_and_free(host, host->templates);
  781. RRDCALCTEMPLATE *rt,*next;
  782. for(rt = host->alarms_template_with_foreach; rt ; rt = next) {
  783. next = rt->next;
  784. rrdcalctemplate_free(rt);
  785. }
  786. host->alarms_template_with_foreach = NULL;
  787. debug(D_RRD_CALLS, "RRDHOST: Cleaning up remaining host variables for host '%s'", host->hostname);
  788. rrdvar_free_remaining_variables(host, &host->rrdvar_root_index);
  789. health_alarm_log_free(host);
  790. #ifdef ENABLE_DBENGINE
  791. if (host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE && host->rrdeng_ctx != &multidb_ctx)
  792. rrdeng_exit(host->rrdeng_ctx);
  793. #endif
  794. // ------------------------------------------------------------------------
  795. // remove it from the indexes
  796. if(rrdhost_index_del(host) != host)
  797. error("RRDHOST '%s' removed from index, deleted the wrong entry.", host->hostname);
  798. // ------------------------------------------------------------------------
  799. // unlink it from the host
  800. if(host == localhost) {
  801. localhost = host->next;
  802. }
  803. else {
  804. // find the previous one
  805. RRDHOST *h;
  806. for(h = localhost; h && h->next != host ; h = h->next) ;
  807. // bypass it
  808. if(h) h->next = host->next;
  809. else error("Request to free RRDHOST '%s': cannot find it", host->hostname);
  810. }
  811. // ------------------------------------------------------------------------
  812. // free it
  813. pthread_mutex_destroy(&host->aclk_state_lock);
  814. freez(host->aclk_state.claimed_id);
  815. freez(host->aclk_state.prev_claimed_id);
  816. freez((void *)host->tags);
  817. free_label_list(host->labels.head);
  818. freez((void *)host->os);
  819. freez((void *)host->timezone);
  820. freez((void *)host->abbrev_timezone);
  821. freez(host->program_version);
  822. freez(host->program_name);
  823. rrdhost_system_info_free(host->system_info);
  824. freez(host->cache_dir);
  825. freez(host->varlib_dir);
  826. freez(host->rrdpush_send_api_key);
  827. freez(host->rrdpush_send_destination);
  828. freez(host->health_default_exec);
  829. freez(host->health_default_recipient);
  830. freez(host->health_log_filename);
  831. freez(host->hostname);
  832. freez(host->registry_hostname);
  833. simple_pattern_free(host->rrdpush_send_charts_matching);
  834. rrdhost_unlock(host);
  835. netdata_rwlock_destroy(&host->labels.labels_rwlock);
  836. netdata_rwlock_destroy(&host->health_log.alarm_log_rwlock);
  837. netdata_rwlock_destroy(&host->rrdhost_rwlock);
  838. freez(host->node_id);
  839. freez(host);
  840. #if defined(ENABLE_ACLK) && defined(ENABLE_NEW_CLOUD_PROTOCOL)
  841. if (wc)
  842. wc->is_orphan = 0;
  843. #endif
  844. rrd_hosts_available--;
  845. }
  846. void rrdhost_free_all(void) {
  847. rrd_wrlock();
  848. /* Make sure child-hosts are released before the localhost. */
  849. while(localhost->next) rrdhost_free(localhost->next);
  850. rrdhost_free(localhost);
  851. rrd_unlock();
  852. }
  853. // ----------------------------------------------------------------------------
  854. // RRDHOST - save host files
  855. void rrdhost_save_charts(RRDHOST *host) {
  856. if(!host) return;
  857. info("Saving/Closing database of host '%s'...", host->hostname);
  858. RRDSET *st;
  859. // we get a write lock
  860. // to ensure only one thread is saving the database
  861. rrdhost_wrlock(host);
  862. rrdset_foreach_write(st, host) {
  863. rrdset_rdlock(st);
  864. rrdset_save(st);
  865. rrdset_unlock(st);
  866. }
  867. rrdhost_unlock(host);
  868. }
  869. static struct label *rrdhost_load_auto_labels(void)
  870. {
  871. struct label *label_list = NULL;
  872. if (localhost->system_info->cloud_provider_type)
  873. label_list =
  874. add_label_to_list(label_list, "_cloud_provider_type", localhost->system_info->cloud_provider_type, LABEL_SOURCE_AUTO);
  875. if (localhost->system_info->cloud_instance_type)
  876. label_list =
  877. add_label_to_list(label_list, "_cloud_instance_type", localhost->system_info->cloud_instance_type, LABEL_SOURCE_AUTO);
  878. if (localhost->system_info->cloud_instance_region)
  879. label_list =
  880. add_label_to_list(label_list, "_cloud_instance_region", localhost->system_info->cloud_instance_region, LABEL_SOURCE_AUTO);
  881. if (localhost->system_info->host_os_name)
  882. label_list =
  883. add_label_to_list(label_list, "_os_name", localhost->system_info->host_os_name, LABEL_SOURCE_AUTO);
  884. if (localhost->system_info->host_os_version)
  885. label_list =
  886. add_label_to_list(label_list, "_os_version", localhost->system_info->host_os_version, LABEL_SOURCE_AUTO);
  887. if (localhost->system_info->kernel_version)
  888. label_list =
  889. add_label_to_list(label_list, "_kernel_version", localhost->system_info->kernel_version, LABEL_SOURCE_AUTO);
  890. if (localhost->system_info->host_cores)
  891. label_list =
  892. add_label_to_list(label_list, "_system_cores", localhost->system_info->host_cores, LABEL_SOURCE_AUTO);
  893. if (localhost->system_info->host_cpu_freq)
  894. label_list =
  895. add_label_to_list(label_list, "_system_cpu_freq", localhost->system_info->host_cpu_freq, LABEL_SOURCE_AUTO);
  896. if (localhost->system_info->host_ram_total)
  897. label_list =
  898. add_label_to_list(label_list, "_system_ram_total", localhost->system_info->host_ram_total, LABEL_SOURCE_AUTO);
  899. if (localhost->system_info->host_disk_space)
  900. label_list =
  901. add_label_to_list(label_list, "_system_disk_space", localhost->system_info->host_disk_space, LABEL_SOURCE_AUTO);
  902. if (localhost->system_info->architecture)
  903. label_list =
  904. add_label_to_list(label_list, "_architecture", localhost->system_info->architecture, LABEL_SOURCE_AUTO);
  905. if (localhost->system_info->virtualization)
  906. label_list =
  907. add_label_to_list(label_list, "_virtualization", localhost->system_info->virtualization, LABEL_SOURCE_AUTO);
  908. if (localhost->system_info->container)
  909. label_list =
  910. add_label_to_list(label_list, "_container", localhost->system_info->container, LABEL_SOURCE_AUTO);
  911. if (localhost->system_info->container_detection)
  912. label_list =
  913. add_label_to_list(label_list, "_container_detection", localhost->system_info->container_detection, LABEL_SOURCE_AUTO);
  914. if (localhost->system_info->virt_detection)
  915. label_list =
  916. add_label_to_list(label_list, "_virt_detection", localhost->system_info->virt_detection, LABEL_SOURCE_AUTO);
  917. if (localhost->system_info->is_k8s_node)
  918. label_list =
  919. add_label_to_list(label_list, "_is_k8s_node", localhost->system_info->is_k8s_node, LABEL_SOURCE_AUTO);
  920. if (localhost->system_info->install_type)
  921. label_list =
  922. add_label_to_list(label_list, "_install_type", localhost->system_info->install_type, LABEL_SOURCE_AUTO);
  923. if (localhost->system_info->prebuilt_arch)
  924. label_list =
  925. add_label_to_list(label_list, "_prebuilt_arch", localhost->system_info->prebuilt_arch, LABEL_SOURCE_AUTO);
  926. if (localhost->system_info->prebuilt_dist)
  927. label_list =
  928. add_label_to_list(label_list, "_prebuilt_dist", localhost->system_info->prebuilt_dist, LABEL_SOURCE_AUTO);
  929. label_list = add_aclk_host_labels(label_list);
  930. label_list = add_label_to_list(
  931. label_list, "_is_parent", (localhost->next || configured_as_parent()) ? "true" : "false", LABEL_SOURCE_AUTO);
  932. if (localhost->rrdpush_send_destination)
  933. label_list =
  934. add_label_to_list(label_list, "_streams_to", localhost->rrdpush_send_destination, LABEL_SOURCE_AUTO);
  935. return label_list;
  936. }
  937. static inline int rrdhost_is_valid_label_config_option(char *name, char *value)
  938. {
  939. return (is_valid_label_key(name) && is_valid_label_value(value) && strcmp(name, "from environment") &&
  940. strcmp(name, "from kubernetes pods"));
  941. }
  942. static struct label *rrdhost_load_config_labels()
  943. {
  944. int status = config_load(NULL, 1, CONFIG_SECTION_HOST_LABEL);
  945. if(!status) {
  946. char *filename = CONFIG_DIR "/" CONFIG_FILENAME;
  947. error("LABEL: Cannot reload the configuration file '%s', using labels in memory", filename);
  948. }
  949. struct label *l = NULL;
  950. struct section *co = appconfig_get_section(&netdata_config, CONFIG_SECTION_HOST_LABEL);
  951. if(co) {
  952. config_section_wrlock(co);
  953. struct config_option *cv;
  954. for(cv = co->values; cv ; cv = cv->next) {
  955. if(rrdhost_is_valid_label_config_option(cv->name, cv->value)) {
  956. l = add_label_to_list(l, cv->name, cv->value, LABEL_SOURCE_NETDATA_CONF);
  957. cv->flags |= CONFIG_VALUE_USED;
  958. } else {
  959. error("LABELS: It was not possible to create the label '%s' because it contains invalid character(s) or values."
  960. , cv->name);
  961. }
  962. }
  963. config_section_unlock(co);
  964. }
  965. return l;
  966. }
  967. struct label *parse_simple_tags(
  968. struct label *label_list,
  969. const char *tags,
  970. char key_value_separator,
  971. char label_separator,
  972. STRIP_QUOTES_OPTION strip_quotes_from_key,
  973. STRIP_QUOTES_OPTION strip_quotes_from_value,
  974. SKIP_ESCAPED_CHARACTERS_OPTION skip_escaped_characters)
  975. {
  976. const char *end = tags;
  977. while (*end) {
  978. const char *start = end;
  979. char key[CONFIG_MAX_VALUE + 1];
  980. char value[CONFIG_MAX_VALUE + 1];
  981. while (*end && *end != key_value_separator)
  982. end++;
  983. strncpyz(key, start, end - start);
  984. if (*end)
  985. start = ++end;
  986. while (*end && *end != label_separator)
  987. end++;
  988. strncpyz(value, start, end - start);
  989. label_list = add_label_to_list(
  990. label_list,
  991. strip_quotes_from_key ? strip_double_quotes(trim(key), skip_escaped_characters) : trim(key),
  992. strip_quotes_from_value ? strip_double_quotes(trim(value), skip_escaped_characters) : trim(value),
  993. LABEL_SOURCE_NETDATA_CONF);
  994. if (*end)
  995. end++;
  996. }
  997. return label_list;
  998. }
  999. struct label *parse_json_tags(struct label *label_list, const char *tags)
  1000. {
  1001. char tags_buf[CONFIG_MAX_VALUE + 1];
  1002. strncpy(tags_buf, tags, CONFIG_MAX_VALUE);
  1003. char *str = tags_buf;
  1004. switch (*str) {
  1005. case '{':
  1006. str++;
  1007. strip_last_symbol(str, '}', SKIP_ESCAPED_CHARACTERS);
  1008. label_list = parse_simple_tags(label_list, str, ':', ',', STRIP_QUOTES, STRIP_QUOTES, SKIP_ESCAPED_CHARACTERS);
  1009. break;
  1010. case '[':
  1011. str++;
  1012. strip_last_symbol(str, ']', SKIP_ESCAPED_CHARACTERS);
  1013. char *end = str + strlen(str);
  1014. size_t i = 0;
  1015. while (str < end) {
  1016. char key[CONFIG_MAX_VALUE + 1];
  1017. snprintfz(key, CONFIG_MAX_VALUE, "host_tag%zu", i);
  1018. str = strip_double_quotes(trim(str), SKIP_ESCAPED_CHARACTERS);
  1019. label_list = add_label_to_list(label_list, key, str, LABEL_SOURCE_NETDATA_CONF);
  1020. // skip to the next element in the array
  1021. str += strlen(str) + 1;
  1022. while (*str && *str != ',')
  1023. str++;
  1024. str++;
  1025. i++;
  1026. }
  1027. break;
  1028. case '"':
  1029. label_list = add_label_to_list(
  1030. label_list, "host_tag", strip_double_quotes(str, SKIP_ESCAPED_CHARACTERS), LABEL_SOURCE_NETDATA_CONF);
  1031. break;
  1032. default:
  1033. label_list = add_label_to_list(label_list, "host_tag", str, LABEL_SOURCE_NETDATA_CONF);
  1034. break;
  1035. }
  1036. return label_list;
  1037. }
  1038. static struct label *rrdhost_load_kubernetes_labels(void)
  1039. {
  1040. struct label *l=NULL;
  1041. char *label_script = mallocz(sizeof(char) * (strlen(netdata_configured_primary_plugins_dir) + strlen("get-kubernetes-labels.sh") + 2));
  1042. sprintf(label_script, "%s/%s", netdata_configured_primary_plugins_dir, "get-kubernetes-labels.sh");
  1043. if (unlikely(access(label_script, R_OK) != 0)) {
  1044. error("Kubernetes pod label fetching script %s not found.",label_script);
  1045. freez(label_script);
  1046. } else {
  1047. pid_t command_pid;
  1048. debug(D_RRDHOST, "Attempting to fetch external labels via %s", label_script);
  1049. FILE *fp = mypopen(label_script, &command_pid);
  1050. if(fp) {
  1051. int MAX_LINE_SIZE=300;
  1052. char buffer[MAX_LINE_SIZE + 1];
  1053. while (fgets(buffer, MAX_LINE_SIZE, fp) != NULL) {
  1054. char *name=buffer;
  1055. char *value=buffer;
  1056. while (*value && *value != ':') value++;
  1057. if (*value == ':') {
  1058. *value = '\0';
  1059. value++;
  1060. }
  1061. char *eos=value;
  1062. while (*eos && *eos != '\n') eos++;
  1063. if (*eos == '\n') *eos = '\0';
  1064. if (strlen(value)>0) {
  1065. if (is_valid_label_key(name)){
  1066. l = add_label_to_list(l, name, value, LABEL_SOURCE_KUBERNETES);
  1067. } else {
  1068. info("Ignoring invalid label name '%s'", name);
  1069. }
  1070. } else {
  1071. error("%s outputted unexpected result: '%s'", label_script, name);
  1072. }
  1073. };
  1074. // Non-zero exit code means that all the script output is error messages. We've shown already any message that didn't include a ':'
  1075. // Here we'll inform with an ERROR that the script failed, show whatever (if anything) was added to the list of labels, free the memory and set the return to null
  1076. int retcode=mypclose(fp, command_pid);
  1077. if (retcode) {
  1078. error("%s exited abnormally. No kubernetes labels will be added to the host.", label_script);
  1079. struct label *ll=l;
  1080. while (ll != NULL) {
  1081. info("Ignoring Label [source id=%s]: \"%s\" -> \"%s\"\n", translate_label_source(ll->label_source), ll->key, ll->value);
  1082. ll = ll->next;
  1083. freez(l);
  1084. l=ll;
  1085. }
  1086. }
  1087. }
  1088. freez(label_script);
  1089. }
  1090. return l;
  1091. }
  1092. void reload_host_labels(void)
  1093. {
  1094. struct label *from_auto = rrdhost_load_auto_labels();
  1095. struct label *from_k8s = rrdhost_load_kubernetes_labels();
  1096. struct label *from_config = rrdhost_load_config_labels();
  1097. struct label *new_labels = merge_label_lists(from_auto, from_k8s);
  1098. new_labels = merge_label_lists(new_labels, from_config);
  1099. rrdhost_rdlock(localhost);
  1100. replace_label_list(&localhost->labels, new_labels);
  1101. health_label_log_save(localhost);
  1102. rrdhost_unlock(localhost);
  1103. /* TODO-GAPS - fix this so that it looks properly at the state and version of the sender
  1104. if(localhost->rrdpush_send_enabled && localhost->rrdpush_sender_buffer){
  1105. localhost->labels.labels_flag |= LABEL_FLAG_UPDATE_STREAM;
  1106. rrdpush_send_labels(localhost);
  1107. }
  1108. */
  1109. health_reload();
  1110. }
  1111. // ----------------------------------------------------------------------------
  1112. // RRDHOST - delete host files
  1113. void rrdhost_delete_charts(RRDHOST *host) {
  1114. if(!host) return;
  1115. info("Deleting database of host '%s'...", host->hostname);
  1116. RRDSET *st;
  1117. // we get a write lock
  1118. // to ensure only one thread is saving the database
  1119. rrdhost_wrlock(host);
  1120. rrdset_foreach_write(st, host) {
  1121. rrdset_rdlock(st);
  1122. rrdset_delete(st);
  1123. rrdset_unlock(st);
  1124. }
  1125. recursively_delete_dir(host->cache_dir, "left over host");
  1126. rrdhost_unlock(host);
  1127. }
  1128. // ----------------------------------------------------------------------------
  1129. // RRDHOST - cleanup host files
  1130. void rrdhost_cleanup_charts(RRDHOST *host) {
  1131. if(!host) return;
  1132. info("Cleaning up database of host '%s'...", host->hostname);
  1133. RRDSET *st;
  1134. uint32_t rrdhost_delete_obsolete_charts = rrdhost_flag_check(host, RRDHOST_FLAG_DELETE_OBSOLETE_CHARTS);
  1135. // we get a write lock
  1136. // to ensure only one thread is saving the database
  1137. rrdhost_wrlock(host);
  1138. rrdset_foreach_write(st, host) {
  1139. rrdset_rdlock(st);
  1140. if(rrdhost_delete_obsolete_charts && rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE))
  1141. rrdset_delete(st);
  1142. else if(rrdhost_delete_obsolete_charts && rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE_DIMENSIONS))
  1143. rrdset_delete_obsolete_dimensions(st);
  1144. else
  1145. rrdset_save(st);
  1146. rrdset_unlock(st);
  1147. }
  1148. rrdhost_unlock(host);
  1149. }
  1150. // ----------------------------------------------------------------------------
  1151. // RRDHOST - save all hosts to disk
  1152. void rrdhost_save_all(void) {
  1153. info("Saving database [%zu hosts(s)]...", rrd_hosts_available);
  1154. rrd_rdlock();
  1155. RRDHOST *host;
  1156. rrdhost_foreach_read(host)
  1157. rrdhost_save_charts(host);
  1158. rrd_unlock();
  1159. }
  1160. // ----------------------------------------------------------------------------
  1161. // RRDHOST - save or delete all hosts from disk
  1162. void rrdhost_cleanup_all(void) {
  1163. info("Cleaning up database [%zu hosts(s)]...", rrd_hosts_available);
  1164. rrd_rdlock();
  1165. RRDHOST *host;
  1166. rrdhost_foreach_read(host) {
  1167. if (host != localhost && rrdhost_flag_check(host, RRDHOST_FLAG_DELETE_ORPHAN_HOST) && !host->receiver
  1168. #ifdef ENABLE_DBENGINE
  1169. /* don't delete multi-host DB host files */
  1170. && !(host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE && host->rrdeng_ctx == &multidb_ctx)
  1171. #endif
  1172. )
  1173. rrdhost_delete_charts(host);
  1174. else
  1175. rrdhost_cleanup_charts(host);
  1176. }
  1177. rrd_unlock();
  1178. }
  1179. // ----------------------------------------------------------------------------
  1180. // RRDHOST - save or delete all the host charts from disk
  1181. void rrdhost_cleanup_obsolete_charts(RRDHOST *host) {
  1182. time_t now = now_realtime_sec();
  1183. RRDSET *st;
  1184. uint32_t rrdhost_delete_obsolete_charts = rrdhost_flag_check(host, RRDHOST_FLAG_DELETE_OBSOLETE_CHARTS);
  1185. restart_after_removal:
  1186. rrdset_foreach_write(st, host) {
  1187. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE)
  1188. && st->last_accessed_time + rrdset_free_obsolete_time < now
  1189. && st->last_updated.tv_sec + rrdset_free_obsolete_time < now
  1190. && st->last_collected_time.tv_sec + rrdset_free_obsolete_time < now
  1191. )) {
  1192. st->rrdhost->obsolete_charts_count--;
  1193. #ifdef ENABLE_DBENGINE
  1194. if(st->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
  1195. RRDDIM *rd, *last;
  1196. rrdset_flag_set(st, RRDSET_FLAG_ARCHIVED);
  1197. while (st->variables) rrdsetvar_free(st->variables);
  1198. while (st->alarms) rrdsetcalc_unlink(st->alarms);
  1199. rrdset_wrlock(st);
  1200. for (rd = st->dimensions, last = NULL ; likely(rd) ; ) {
  1201. if (rrddim_flag_check(rd, RRDDIM_FLAG_ARCHIVED)) {
  1202. last = rd;
  1203. rd = rd->next;
  1204. continue;
  1205. }
  1206. if (rrddim_flag_check(rd, RRDDIM_FLAG_ACLK)) {
  1207. last = rd;
  1208. rd = rd->next;
  1209. continue;
  1210. }
  1211. rrddim_flag_set(rd, RRDDIM_FLAG_ARCHIVED);
  1212. while (rd->variables)
  1213. rrddimvar_free(rd->variables);
  1214. if (rrddim_flag_check(rd, RRDDIM_FLAG_OBSOLETE)) {
  1215. rrddim_flag_clear(rd, RRDDIM_FLAG_OBSOLETE);
  1216. /* only a collector can mark a chart as obsolete, so we must remove the reference */
  1217. uint8_t can_delete_metric = rd->state->collect_ops.finalize(rd);
  1218. if (can_delete_metric) {
  1219. /* This metric has no data and no references */
  1220. delete_dimension_uuid(&rd->state->metric_uuid);
  1221. rrddim_free(st, rd);
  1222. if (unlikely(!last)) {
  1223. rd = st->dimensions;
  1224. }
  1225. else {
  1226. rd = last->next;
  1227. }
  1228. continue;
  1229. }
  1230. #if defined(ENABLE_ACLK) && defined(ENABLE_NEW_CLOUD_PROTOCOL)
  1231. else {
  1232. aclk_send_dimension_update(rd);
  1233. }
  1234. #endif
  1235. }
  1236. last = rd;
  1237. rd = rd->next;
  1238. }
  1239. rrdset_unlock(st);
  1240. debug(D_RRD_CALLS, "RRDSET: Cleaning up remaining chart variables for host '%s', chart '%s'", host->hostname, st->id);
  1241. rrdvar_free_remaining_variables(host, &st->rrdvar_root_index);
  1242. rrdset_flag_clear(st, RRDSET_FLAG_OBSOLETE);
  1243. if (st->dimensions) {
  1244. /* If the chart still has dimensions don't delete it from the metadata log */
  1245. continue;
  1246. }
  1247. }
  1248. #endif
  1249. rrdset_rdlock(st);
  1250. if(rrdhost_delete_obsolete_charts)
  1251. rrdset_delete(st);
  1252. else
  1253. rrdset_save(st);
  1254. rrdset_unlock(st);
  1255. rrdset_free(st);
  1256. goto restart_after_removal;
  1257. }
  1258. #if defined(ENABLE_ACLK) && defined(ENABLE_NEW_CLOUD_PROTOCOL)
  1259. else
  1260. sql_check_chart_liveness(st);
  1261. #endif
  1262. }
  1263. }
  1264. void rrd_cleanup_obsolete_charts()
  1265. {
  1266. rrd_rdlock();
  1267. RRDHOST *host;
  1268. rrdhost_foreach_read(host)
  1269. {
  1270. if (host->obsolete_charts_count) {
  1271. rrdhost_wrlock(host);
  1272. #ifdef ENABLE_ACLK
  1273. host->deleted_charts_count = 0;
  1274. #endif
  1275. rrdhost_cleanup_obsolete_charts(host);
  1276. #ifdef ENABLE_ACLK
  1277. if (host->deleted_charts_count)
  1278. aclk_update_chart(host, "dummy-chart", 0);
  1279. #endif
  1280. rrdhost_unlock(host);
  1281. }
  1282. }
  1283. rrd_unlock();
  1284. }
  1285. // ----------------------------------------------------------------------------
  1286. // RRDHOST - set system info from environment variables
  1287. // system_info fields must be heap allocated or NULL
  1288. int rrdhost_set_system_info_variable(struct rrdhost_system_info *system_info, char *name, char *value) {
  1289. int res = 0;
  1290. if (!strcmp(name, "NETDATA_PROTOCOL_VERSION"))
  1291. return res;
  1292. else if(!strcmp(name, "NETDATA_INSTANCE_CLOUD_TYPE")){
  1293. freez(system_info->cloud_provider_type);
  1294. system_info->cloud_provider_type = strdupz(value);
  1295. }
  1296. else if(!strcmp(name, "NETDATA_INSTANCE_CLOUD_INSTANCE_TYPE")){
  1297. freez(system_info->cloud_instance_type);
  1298. system_info->cloud_instance_type = strdupz(value);
  1299. }
  1300. else if(!strcmp(name, "NETDATA_INSTANCE_CLOUD_INSTANCE_REGION")){
  1301. freez(system_info->cloud_instance_region);
  1302. system_info->cloud_instance_region = strdupz(value);
  1303. }
  1304. else if(!strcmp(name, "NETDATA_CONTAINER_OS_NAME")){
  1305. freez(system_info->container_os_name);
  1306. system_info->container_os_name = strdupz(value);
  1307. }
  1308. else if(!strcmp(name, "NETDATA_CONTAINER_OS_ID")){
  1309. freez(system_info->container_os_id);
  1310. system_info->container_os_id = strdupz(value);
  1311. }
  1312. else if(!strcmp(name, "NETDATA_CONTAINER_OS_ID_LIKE")){
  1313. freez(system_info->container_os_id_like);
  1314. system_info->container_os_id_like = strdupz(value);
  1315. }
  1316. else if(!strcmp(name, "NETDATA_CONTAINER_OS_VERSION")){
  1317. freez(system_info->container_os_version);
  1318. system_info->container_os_version = strdupz(value);
  1319. }
  1320. else if(!strcmp(name, "NETDATA_CONTAINER_OS_VERSION_ID")){
  1321. freez(system_info->container_os_version_id);
  1322. system_info->container_os_version_id = strdupz(value);
  1323. }
  1324. else if(!strcmp(name, "NETDATA_CONTAINER_OS_DETECTION")){
  1325. freez(system_info->container_os_detection);
  1326. system_info->container_os_detection = strdupz(value);
  1327. }
  1328. else if(!strcmp(name, "NETDATA_HOST_OS_NAME")){
  1329. freez(system_info->host_os_name);
  1330. system_info->host_os_name = strdupz(value);
  1331. json_fix_string(system_info->host_os_name);
  1332. }
  1333. else if(!strcmp(name, "NETDATA_HOST_OS_ID")){
  1334. freez(system_info->host_os_id);
  1335. system_info->host_os_id = strdupz(value);
  1336. }
  1337. else if(!strcmp(name, "NETDATA_HOST_OS_ID_LIKE")){
  1338. freez(system_info->host_os_id_like);
  1339. system_info->host_os_id_like = strdupz(value);
  1340. }
  1341. else if(!strcmp(name, "NETDATA_HOST_OS_VERSION")){
  1342. freez(system_info->host_os_version);
  1343. system_info->host_os_version = strdupz(value);
  1344. }
  1345. else if(!strcmp(name, "NETDATA_HOST_OS_VERSION_ID")){
  1346. freez(system_info->host_os_version_id);
  1347. system_info->host_os_version_id = strdupz(value);
  1348. }
  1349. else if(!strcmp(name, "NETDATA_HOST_OS_DETECTION")){
  1350. freez(system_info->host_os_detection);
  1351. system_info->host_os_detection = strdupz(value);
  1352. }
  1353. else if(!strcmp(name, "NETDATA_SYSTEM_KERNEL_NAME")){
  1354. freez(system_info->kernel_name);
  1355. system_info->kernel_name = strdupz(value);
  1356. }
  1357. else if(!strcmp(name, "NETDATA_SYSTEM_CPU_LOGICAL_CPU_COUNT")){
  1358. freez(system_info->host_cores);
  1359. system_info->host_cores = strdupz(value);
  1360. }
  1361. else if(!strcmp(name, "NETDATA_SYSTEM_CPU_FREQ")){
  1362. freez(system_info->host_cpu_freq);
  1363. system_info->host_cpu_freq = strdupz(value);
  1364. }
  1365. else if(!strcmp(name, "NETDATA_SYSTEM_TOTAL_RAM")){
  1366. freez(system_info->host_ram_total);
  1367. system_info->host_ram_total = strdupz(value);
  1368. }
  1369. else if(!strcmp(name, "NETDATA_SYSTEM_TOTAL_DISK_SIZE")){
  1370. freez(system_info->host_disk_space);
  1371. system_info->host_disk_space = strdupz(value);
  1372. }
  1373. else if(!strcmp(name, "NETDATA_SYSTEM_KERNEL_VERSION")){
  1374. freez(system_info->kernel_version);
  1375. system_info->kernel_version = strdupz(value);
  1376. }
  1377. else if(!strcmp(name, "NETDATA_SYSTEM_ARCHITECTURE")){
  1378. freez(system_info->architecture);
  1379. system_info->architecture = strdupz(value);
  1380. }
  1381. else if(!strcmp(name, "NETDATA_SYSTEM_VIRTUALIZATION")){
  1382. freez(system_info->virtualization);
  1383. system_info->virtualization = strdupz(value);
  1384. }
  1385. else if(!strcmp(name, "NETDATA_SYSTEM_VIRT_DETECTION")){
  1386. freez(system_info->virt_detection);
  1387. system_info->virt_detection = strdupz(value);
  1388. }
  1389. else if(!strcmp(name, "NETDATA_SYSTEM_CONTAINER")){
  1390. freez(system_info->container);
  1391. system_info->container = strdupz(value);
  1392. }
  1393. else if(!strcmp(name, "NETDATA_SYSTEM_CONTAINER_DETECTION")){
  1394. freez(system_info->container_detection);
  1395. system_info->container_detection = strdupz(value);
  1396. }
  1397. else if(!strcmp(name, "NETDATA_HOST_IS_K8S_NODE")){
  1398. freez(system_info->is_k8s_node);
  1399. system_info->is_k8s_node = strdupz(value);
  1400. }
  1401. else if (!strcmp(name, "NETDATA_SYSTEM_CPU_VENDOR"))
  1402. return res;
  1403. else if (!strcmp(name, "NETDATA_SYSTEM_CPU_MODEL"))
  1404. return res;
  1405. else if (!strcmp(name, "NETDATA_SYSTEM_CPU_DETECTION"))
  1406. return res;
  1407. else if (!strcmp(name, "NETDATA_SYSTEM_RAM_DETECTION"))
  1408. return res;
  1409. else if (!strcmp(name, "NETDATA_SYSTEM_DISK_DETECTION"))
  1410. return res;
  1411. else if (!strcmp(name, "NETDATA_CONTAINER_IS_OFFICIAL_IMAGE"))
  1412. return res;
  1413. else {
  1414. res = 1;
  1415. }
  1416. return res;
  1417. }
  1418. /**
  1419. * Alarm Compare ID
  1420. *
  1421. * Callback function used with the binary trees to compare the id of RRDCALC
  1422. *
  1423. * @param a a pointer to the RRDCAL item to insert,compare or update the binary tree
  1424. * @param b the pointer to the binary tree.
  1425. *
  1426. * @return It returns 0 case the values are equal, 1 case a is bigger than b and -1 case a is smaller than b.
  1427. */
  1428. int alarm_compare_id(void *a, void *b) {
  1429. register uint32_t hash1 = ((RRDCALC *)a)->id;
  1430. register uint32_t hash2 = ((RRDCALC *)b)->id;
  1431. if(hash1 < hash2) return -1;
  1432. else if(hash1 > hash2) return 1;
  1433. return 0;
  1434. }
  1435. /**
  1436. * Alarm Compare NAME
  1437. *
  1438. * Callback function used with the binary trees to compare the name of RRDCALC
  1439. *
  1440. * @param a a pointer to the RRDCAL item to insert,compare or update the binary tree
  1441. * @param b the pointer to the binary tree.
  1442. *
  1443. * @return It returns 0 case the values are equal, 1 case a is bigger than b and -1 case a is smaller than b.
  1444. */
  1445. int alarm_compare_name(void *a, void *b) {
  1446. RRDCALC *in1 = (RRDCALC *)a;
  1447. RRDCALC *in2 = (RRDCALC *)b;
  1448. if(in1->hash < in2->hash) return -1;
  1449. else if(in1->hash > in2->hash) return 1;
  1450. return strcmp(in1->name,in2->name);
  1451. }
  1452. // Added for gap-filling, if this proves to be a bottleneck in large-scale systems then we will need to cache
  1453. // the last entry times as the metric updates, but let's see if it is a problem first.
  1454. time_t rrdhost_last_entry_t(RRDHOST *h) {
  1455. rrdhost_rdlock(h);
  1456. RRDSET *st;
  1457. time_t result = 0;
  1458. rrdset_foreach_read(st, h) {
  1459. time_t st_last = rrdset_last_entry_t(st);
  1460. if (st_last > result)
  1461. result = st_last;
  1462. }
  1463. rrdhost_unlock(h);
  1464. return result;
  1465. }