rrdhost.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723
  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. ml_new_host(host);
  310. if (is_localhost && host->system_info) {
  311. #ifndef ENABLE_ML
  312. host->system_info->ml_capable = 0;
  313. #else
  314. host->system_info->ml_capable = 1;
  315. #endif
  316. host->system_info->ml_enabled = host->ml_host != NULL;
  317. }
  318. info("Host '%s' (at registry as '%s') with guid '%s' initialized"
  319. ", os '%s'"
  320. ", timezone '%s'"
  321. ", tags '%s'"
  322. ", program_name '%s'"
  323. ", program_version '%s'"
  324. ", update every %d"
  325. ", memory mode %s"
  326. ", history entries %ld"
  327. ", streaming %s"
  328. " (to '%s' with api key '%s')"
  329. ", health %s"
  330. ", cache_dir '%s'"
  331. ", varlib_dir '%s'"
  332. ", health_log '%s'"
  333. ", alarms default handler '%s'"
  334. ", alarms default recipient '%s'"
  335. , host->hostname
  336. , host->registry_hostname
  337. , host->machine_guid
  338. , host->os
  339. , host->timezone
  340. , (host->tags)?host->tags:""
  341. , host->program_name
  342. , host->program_version
  343. , host->rrd_update_every
  344. , rrd_memory_mode_name(host->rrd_memory_mode)
  345. , host->rrd_history_entries
  346. , host->rrdpush_send_enabled?"enabled":"disabled"
  347. , host->rrdpush_send_destination?host->rrdpush_send_destination:""
  348. , host->rrdpush_send_api_key?host->rrdpush_send_api_key:""
  349. , host->health_enabled?"enabled":"disabled"
  350. , host->cache_dir
  351. , host->varlib_dir
  352. , host->health_log_filename
  353. , host->health_default_exec
  354. , host->health_default_recipient
  355. );
  356. rrd_hosts_available++;
  357. return host;
  358. }
  359. void rrdhost_update(RRDHOST *host
  360. , const char *hostname
  361. , const char *registry_hostname
  362. , const char *guid
  363. , const char *os
  364. , const char *timezone
  365. , const char *abbrev_timezone
  366. , int32_t utc_offset
  367. , const char *tags
  368. , const char *program_name
  369. , const char *program_version
  370. , int update_every
  371. , long history
  372. , RRD_MEMORY_MODE mode
  373. , unsigned int health_enabled
  374. , unsigned int rrdpush_enabled
  375. , char *rrdpush_destination
  376. , char *rrdpush_api_key
  377. , char *rrdpush_send_charts_matching
  378. , struct rrdhost_system_info *system_info
  379. )
  380. {
  381. UNUSED(guid);
  382. UNUSED(rrdpush_enabled);
  383. UNUSED(rrdpush_destination);
  384. UNUSED(rrdpush_api_key);
  385. UNUSED(rrdpush_send_charts_matching);
  386. host->health_enabled = (mode == RRD_MEMORY_MODE_NONE) ? 0 : health_enabled;
  387. //host->stream_version = STREAMING_PROTOCOL_CURRENT_VERSION; Unused?
  388. rrdhost_system_info_free(host->system_info);
  389. host->system_info = system_info;
  390. rrdhost_init_os(host, os);
  391. rrdhost_init_timezone(host, timezone, abbrev_timezone, utc_offset);
  392. freez(host->registry_hostname);
  393. host->registry_hostname = strdupz((registry_hostname && *registry_hostname)?registry_hostname:hostname);
  394. if(strcmp(host->hostname, hostname) != 0) {
  395. 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);
  396. char *t = host->hostname;
  397. host->hostname = strdupz(hostname);
  398. host->hash_hostname = simple_hash(host->hostname);
  399. freez(t);
  400. }
  401. if(strcmp(host->program_name, program_name) != 0) {
  402. info("Host '%s' switched program name from '%s' to '%s'", host->hostname, host->program_name, program_name);
  403. char *t = host->program_name;
  404. host->program_name = strdupz(program_name);
  405. freez(t);
  406. }
  407. if(strcmp(host->program_version, program_version) != 0) {
  408. info("Host '%s' switched program version from '%s' to '%s'", host->hostname, host->program_version, program_version);
  409. char *t = host->program_version;
  410. host->program_version = strdupz(program_version);
  411. freez(t);
  412. }
  413. if(host->rrd_update_every != update_every)
  414. 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);
  415. if(host->rrd_history_entries < history)
  416. 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);
  417. if(host->rrd_memory_mode != mode)
  418. 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));
  419. // update host tags
  420. rrdhost_init_tags(host, tags);
  421. if (rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED)) {
  422. rrdhost_flag_clear(host, RRDHOST_FLAG_ARCHIVED);
  423. if(host->health_enabled) {
  424. int r;
  425. char filename[FILENAME_MAX + 1];
  426. if (host != localhost) {
  427. r = mkdir(host->varlib_dir, 0775);
  428. if (r != 0 && errno != EEXIST)
  429. error("Host '%s': cannot create directory '%s'", host->hostname, host->varlib_dir);
  430. }
  431. snprintfz(filename, FILENAME_MAX, "%s/health", host->varlib_dir);
  432. r = mkdir(filename, 0775);
  433. if(r != 0 && errno != EEXIST)
  434. error("Host '%s': cannot create directory '%s'", host->hostname, filename);
  435. rrdhost_wrlock(host);
  436. health_readdir(host, health_user_config_dir(), health_stock_config_dir(), NULL);
  437. rrdhost_unlock(host);
  438. if (!file_is_migrated(host->health_log_filename)) {
  439. int rc = sql_create_health_log_table(host);
  440. if (unlikely(rc)) {
  441. error_report("Failed to create health log table in the database");
  442. health_alarm_log_load(host);
  443. health_alarm_log_open(host);
  444. } else {
  445. health_alarm_log_load(host);
  446. add_migrated_file(host->health_log_filename, 0);
  447. }
  448. } else {
  449. sql_create_health_log_table(host);
  450. sql_health_alarm_log_load(host);
  451. }
  452. }
  453. rrd_hosts_available++;
  454. info("Host %s is not in archived mode anymore", host->hostname);
  455. }
  456. return;
  457. }
  458. RRDHOST *rrdhost_find_or_create(
  459. const char *hostname
  460. , const char *registry_hostname
  461. , const char *guid
  462. , const char *os
  463. , const char *timezone
  464. , const char *abbrev_timezone
  465. , int32_t utc_offset
  466. , const char *tags
  467. , const char *program_name
  468. , const char *program_version
  469. , int update_every
  470. , long history
  471. , RRD_MEMORY_MODE mode
  472. , unsigned int health_enabled
  473. , unsigned int rrdpush_enabled
  474. , char *rrdpush_destination
  475. , char *rrdpush_api_key
  476. , char *rrdpush_send_charts_matching
  477. , struct rrdhost_system_info *system_info
  478. ) {
  479. debug(D_RRDHOST, "Searching for host '%s' with guid '%s'", hostname, guid);
  480. rrd_wrlock();
  481. RRDHOST *host = rrdhost_find_by_guid(guid, 0);
  482. if (unlikely(host && RRD_MEMORY_MODE_DBENGINE != mode && rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED))) {
  483. /* If a legacy memory mode instantiates all dbengine state must be discarded to avoid inconsistencies */
  484. error("Archived host '%s' has memory mode '%s', but the wanted one is '%s'. Discarding archived state.",
  485. host->hostname, rrd_memory_mode_name(host->rrd_memory_mode), rrd_memory_mode_name(mode));
  486. rrdhost_free(host);
  487. host = NULL;
  488. }
  489. if(!host) {
  490. host = rrdhost_create(
  491. hostname
  492. , registry_hostname
  493. , guid
  494. , os
  495. , timezone
  496. , abbrev_timezone
  497. , utc_offset
  498. , tags
  499. , program_name
  500. , program_version
  501. , update_every
  502. , history
  503. , mode
  504. , health_enabled
  505. , rrdpush_enabled
  506. , rrdpush_destination
  507. , rrdpush_api_key
  508. , rrdpush_send_charts_matching
  509. , system_info
  510. , 0
  511. );
  512. }
  513. else {
  514. rrdhost_update(host
  515. , hostname
  516. , registry_hostname
  517. , guid
  518. , os
  519. , timezone
  520. , abbrev_timezone
  521. , utc_offset
  522. , tags
  523. , program_name
  524. , program_version
  525. , update_every
  526. , history
  527. , mode
  528. , health_enabled
  529. , rrdpush_enabled
  530. , rrdpush_destination
  531. , rrdpush_api_key
  532. , rrdpush_send_charts_matching
  533. , system_info);
  534. }
  535. if (host) {
  536. rrdhost_wrlock(host);
  537. rrdhost_flag_clear(host, RRDHOST_FLAG_ORPHAN);
  538. host->senders_disconnected_time = 0;
  539. rrdhost_unlock(host);
  540. }
  541. rrdhost_cleanup_orphan_hosts_nolock(host);
  542. rrd_unlock();
  543. return host;
  544. }
  545. inline int rrdhost_should_be_removed(RRDHOST *host, RRDHOST *protected_host, time_t now) {
  546. if(host != protected_host
  547. && host != localhost
  548. && rrdhost_flag_check(host, RRDHOST_FLAG_ORPHAN)
  549. && host->receiver
  550. && host->senders_disconnected_time
  551. && host->senders_disconnected_time + rrdhost_free_orphan_time < now)
  552. return 1;
  553. return 0;
  554. }
  555. void rrdhost_cleanup_orphan_hosts_nolock(RRDHOST *protected_host) {
  556. time_t now = now_realtime_sec();
  557. RRDHOST *host;
  558. restart_after_removal:
  559. rrdhost_foreach_write(host) {
  560. if(rrdhost_should_be_removed(host, protected_host, now)) {
  561. info("Host '%s' with machine guid '%s' is obsolete - cleaning up.", host->hostname, host->machine_guid);
  562. if (rrdhost_flag_check(host, RRDHOST_FLAG_DELETE_ORPHAN_HOST)
  563. #ifdef ENABLE_DBENGINE
  564. /* don't delete multi-host DB host files */
  565. && !(host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE && host->rrdeng_ctx == &multidb_ctx)
  566. #endif
  567. )
  568. rrdhost_delete_charts(host);
  569. else
  570. rrdhost_save_charts(host);
  571. rrdhost_free(host);
  572. goto restart_after_removal;
  573. }
  574. }
  575. }
  576. // ----------------------------------------------------------------------------
  577. // RRDHOST global / startup initialization
  578. int rrd_init(char *hostname, struct rrdhost_system_info *system_info) {
  579. rrdset_free_obsolete_time = config_get_number(CONFIG_SECTION_GLOBAL, "cleanup obsolete charts after seconds", rrdset_free_obsolete_time);
  580. // Current chart locking and invalidation scheme doesn't prevent Netdata from segmentation faults if a short
  581. // cleanup delay is set. Extensive stress tests showed that 10 seconds is quite a safe delay. Look at
  582. // https://github.com/netdata/netdata/pull/11222#issuecomment-868367920 for more information.
  583. if (rrdset_free_obsolete_time < 10) {
  584. rrdset_free_obsolete_time = 10;
  585. info("The \"cleanup obsolete charts after seconds\" option was set to 10 seconds. A lower delay can potentially cause a segmentation fault.");
  586. }
  587. gap_when_lost_iterations_above = (int)config_get_number(CONFIG_SECTION_GLOBAL, "gap when lost iterations above", gap_when_lost_iterations_above);
  588. if (gap_when_lost_iterations_above < 1)
  589. gap_when_lost_iterations_above = 1;
  590. if (unlikely(sql_init_database(DB_CHECK_NONE))) {
  591. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
  592. fatal("Failed to initialize SQLite");
  593. info("Skipping SQLITE metadata initialization since memory mode is not db engine");
  594. }
  595. health_init();
  596. rrdpush_init();
  597. debug(D_RRDHOST, "Initializing localhost with hostname '%s'", hostname);
  598. rrd_wrlock();
  599. localhost = rrdhost_create(
  600. hostname
  601. , registry_get_this_machine_hostname()
  602. , registry_get_this_machine_guid()
  603. , os_type
  604. , netdata_configured_timezone
  605. , netdata_configured_abbrev_timezone
  606. , netdata_configured_utc_offset
  607. , config_get(CONFIG_SECTION_BACKEND, "host tags", "")
  608. , program_name
  609. , program_version
  610. , default_rrd_update_every
  611. , default_rrd_history_entries
  612. , default_rrd_memory_mode
  613. , default_health_enabled
  614. , default_rrdpush_enabled
  615. , default_rrdpush_destination
  616. , default_rrdpush_api_key
  617. , default_rrdpush_send_charts_matching
  618. , system_info
  619. , 1
  620. );
  621. if (unlikely(!localhost)) {
  622. rrd_unlock();
  623. return 1;
  624. }
  625. #ifdef ENABLE_DBENGINE
  626. char dbenginepath[FILENAME_MAX + 1];
  627. int ret;
  628. snprintfz(dbenginepath, FILENAME_MAX, "%s/dbengine", localhost->cache_dir);
  629. ret = mkdir(dbenginepath, 0775);
  630. if (ret != 0 && errno != EEXIST)
  631. error("Host '%s': cannot create directory '%s'", localhost->hostname, dbenginepath);
  632. else // Unconditionally create multihost db to support on demand host creation
  633. ret = rrdeng_init(NULL, NULL, dbenginepath, default_rrdeng_page_cache_mb, default_multidb_disk_quota_mb);
  634. if (ret) {
  635. error(
  636. "Host '%s' with machine guid '%s' failed to initialize multi-host DB engine instance at '%s'.",
  637. localhost->hostname, localhost->machine_guid, localhost->cache_dir);
  638. rrdhost_free(localhost);
  639. localhost = NULL;
  640. rrd_unlock();
  641. fatal("Failed to initialize dbengine");
  642. }
  643. #endif
  644. sql_aclk_sync_init();
  645. rrd_unlock();
  646. web_client_api_v1_management_init();
  647. return localhost==NULL;
  648. }
  649. // ----------------------------------------------------------------------------
  650. // RRDHOST - lock validations
  651. // there are only used when NETDATA_INTERNAL_CHECKS is set
  652. void __rrdhost_check_rdlock(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
  653. debug(D_RRDHOST, "Checking read lock on host '%s'", host->hostname);
  654. int ret = netdata_rwlock_trywrlock(&host->rrdhost_rwlock);
  655. if(ret == 0)
  656. 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);
  657. }
  658. void __rrdhost_check_wrlock(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
  659. debug(D_RRDHOST, "Checking write lock on host '%s'", host->hostname);
  660. int ret = netdata_rwlock_tryrdlock(&host->rrdhost_rwlock);
  661. if(ret == 0)
  662. 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);
  663. }
  664. void __rrd_check_rdlock(const char *file, const char *function, const unsigned long line) {
  665. debug(D_RRDHOST, "Checking read lock on all RRDs");
  666. int ret = netdata_rwlock_trywrlock(&rrd_rwlock);
  667. if(ret == 0)
  668. fatal("RRDs should be read-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
  669. }
  670. void __rrd_check_wrlock(const char *file, const char *function, const unsigned long line) {
  671. debug(D_RRDHOST, "Checking write lock on all RRDs");
  672. int ret = netdata_rwlock_tryrdlock(&rrd_rwlock);
  673. if(ret == 0)
  674. fatal("RRDs should be write-locked, but it are not, at function %s() at line %lu of file '%s'", function, line, file);
  675. }
  676. // ----------------------------------------------------------------------------
  677. // RRDHOST - free
  678. void rrdhost_system_info_free(struct rrdhost_system_info *system_info) {
  679. info("SYSTEM_INFO: free %p", system_info);
  680. if(likely(system_info)) {
  681. freez(system_info->host_os_name);
  682. freez(system_info->host_os_id);
  683. freez(system_info->host_os_id_like);
  684. freez(system_info->host_os_version);
  685. freez(system_info->host_os_version_id);
  686. freez(system_info->host_os_detection);
  687. freez(system_info->host_cores);
  688. freez(system_info->host_cpu_freq);
  689. freez(system_info->host_ram_total);
  690. freez(system_info->host_disk_space);
  691. freez(system_info->container_os_name);
  692. freez(system_info->container_os_id);
  693. freez(system_info->container_os_id_like);
  694. freez(system_info->container_os_version);
  695. freez(system_info->container_os_version_id);
  696. freez(system_info->container_os_detection);
  697. freez(system_info->kernel_name);
  698. freez(system_info->kernel_version);
  699. freez(system_info->architecture);
  700. freez(system_info->virtualization);
  701. freez(system_info->virt_detection);
  702. freez(system_info->container);
  703. freez(system_info->container_detection);
  704. freez(system_info->is_k8s_node);
  705. freez(system_info);
  706. }
  707. }
  708. void destroy_receiver_state(struct receiver_state *rpt);
  709. void rrdhost_free(RRDHOST *host) {
  710. if(!host) return;
  711. info("Freeing all memory for host '%s'...", host->hostname);
  712. rrd_check_wrlock(); // make sure the RRDs are write locked
  713. // ------------------------------------------------------------------------
  714. // clean up streaming
  715. rrdpush_sender_thread_stop(host); // stop a possibly running thread
  716. cbuffer_free(host->sender->buffer);
  717. buffer_free(host->sender->build);
  718. #ifdef ENABLE_COMPRESSION
  719. if (host->sender->compressor)
  720. host->sender->compressor->destroy(&host->sender->compressor);
  721. #endif
  722. freez(host->sender);
  723. host->sender = NULL;
  724. if (netdata_exit) {
  725. netdata_mutex_lock(&host->receiver_lock);
  726. if (host->receiver) {
  727. if (!host->receiver->exited)
  728. netdata_thread_cancel(host->receiver->thread);
  729. netdata_mutex_unlock(&host->receiver_lock);
  730. struct receiver_state *rpt = host->receiver;
  731. while (host->receiver && !rpt->exited)
  732. sleep_usec(50 * USEC_PER_MS);
  733. // If the receiver detached from the host then its thread will destroy the state
  734. if (host->receiver == rpt)
  735. destroy_receiver_state(host->receiver);
  736. }
  737. else
  738. netdata_mutex_unlock(&host->receiver_lock);
  739. }
  740. rrdhost_wrlock(host); // lock this RRDHOST
  741. // ------------------------------------------------------------------------
  742. // release its children resources
  743. #ifdef ENABLE_DBENGINE
  744. if (host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
  745. if (host->rrdeng_ctx != &multidb_ctx)
  746. rrdeng_prepare_exit(host->rrdeng_ctx);
  747. }
  748. #endif
  749. while(host->rrdset_root)
  750. rrdset_free(host->rrdset_root);
  751. freez(host->exporting_flags);
  752. while(host->alarms)
  753. rrdcalc_unlink_and_free(host, host->alarms);
  754. RRDCALC *rc,*nc;
  755. for(rc = host->alarms_with_foreach; rc ; rc = nc) {
  756. nc = rc->next;
  757. rrdcalc_free(rc);
  758. }
  759. host->alarms_with_foreach = NULL;
  760. while(host->templates)
  761. rrdcalctemplate_unlink_and_free(host, host->templates);
  762. RRDCALCTEMPLATE *rt,*next;
  763. for(rt = host->alarms_template_with_foreach; rt ; rt = next) {
  764. next = rt->next;
  765. rrdcalctemplate_free(rt);
  766. }
  767. host->alarms_template_with_foreach = NULL;
  768. debug(D_RRD_CALLS, "RRDHOST: Cleaning up remaining host variables for host '%s'", host->hostname);
  769. rrdvar_free_remaining_variables(host, &host->rrdvar_root_index);
  770. health_alarm_log_free(host);
  771. #ifdef ENABLE_DBENGINE
  772. if (host->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE && host->rrdeng_ctx != &multidb_ctx)
  773. rrdeng_exit(host->rrdeng_ctx);
  774. #endif
  775. ml_delete_host(host);
  776. // ------------------------------------------------------------------------
  777. // remove it from the indexes
  778. if(rrdhost_index_del(host) != host)
  779. error("RRDHOST '%s' removed from index, deleted the wrong entry.", host->hostname);
  780. // ------------------------------------------------------------------------
  781. // unlink it from the host
  782. if(host == localhost) {
  783. localhost = host->next;
  784. }
  785. else {
  786. // find the previous one
  787. RRDHOST *h;
  788. for(h = localhost; h && h->next != host ; h = h->next) ;
  789. // bypass it
  790. if(h) h->next = host->next;
  791. else error("Request to free RRDHOST '%s': cannot find it", host->hostname);
  792. }
  793. // ------------------------------------------------------------------------
  794. // free it
  795. pthread_mutex_destroy(&host->aclk_state_lock);
  796. freez(host->aclk_state.claimed_id);
  797. freez((void *)host->tags);
  798. free_label_list(host->labels.head);
  799. freez((void *)host->os);
  800. freez((void *)host->timezone);
  801. freez((void *)host->abbrev_timezone);
  802. freez(host->program_version);
  803. freez(host->program_name);
  804. rrdhost_system_info_free(host->system_info);
  805. freez(host->cache_dir);
  806. freez(host->varlib_dir);
  807. freez(host->rrdpush_send_api_key);
  808. freez(host->rrdpush_send_destination);
  809. freez(host->health_default_exec);
  810. freez(host->health_default_recipient);
  811. freez(host->health_log_filename);
  812. freez(host->hostname);
  813. freez(host->registry_hostname);
  814. simple_pattern_free(host->rrdpush_send_charts_matching);
  815. rrdhost_unlock(host);
  816. netdata_rwlock_destroy(&host->labels.labels_rwlock);
  817. netdata_rwlock_destroy(&host->health_log.alarm_log_rwlock);
  818. netdata_rwlock_destroy(&host->rrdhost_rwlock);
  819. freez(host->node_id);
  820. freez(host);
  821. rrd_hosts_available--;
  822. }
  823. void rrdhost_free_all(void) {
  824. rrd_wrlock();
  825. /* Make sure child-hosts are released before the localhost. */
  826. while(localhost->next) rrdhost_free(localhost->next);
  827. rrdhost_free(localhost);
  828. rrd_unlock();
  829. }
  830. // ----------------------------------------------------------------------------
  831. // RRDHOST - save host files
  832. void rrdhost_save_charts(RRDHOST *host) {
  833. if(!host) return;
  834. info("Saving/Closing database of host '%s'...", host->hostname);
  835. RRDSET *st;
  836. // we get a write lock
  837. // to ensure only one thread is saving the database
  838. rrdhost_wrlock(host);
  839. rrdset_foreach_write(st, host) {
  840. rrdset_rdlock(st);
  841. rrdset_save(st);
  842. rrdset_unlock(st);
  843. }
  844. rrdhost_unlock(host);
  845. }
  846. static struct label *rrdhost_load_auto_labels(void)
  847. {
  848. struct label *label_list = NULL;
  849. if (localhost->system_info->host_os_name)
  850. label_list =
  851. add_label_to_list(label_list, "_os_name", localhost->system_info->host_os_name, LABEL_SOURCE_AUTO);
  852. if (localhost->system_info->host_os_version)
  853. label_list =
  854. add_label_to_list(label_list, "_os_version", localhost->system_info->host_os_version, LABEL_SOURCE_AUTO);
  855. if (localhost->system_info->kernel_version)
  856. label_list =
  857. add_label_to_list(label_list, "_kernel_version", localhost->system_info->kernel_version, LABEL_SOURCE_AUTO);
  858. if (localhost->system_info->host_cores)
  859. label_list =
  860. add_label_to_list(label_list, "_system_cores", localhost->system_info->host_cores, LABEL_SOURCE_AUTO);
  861. if (localhost->system_info->host_cpu_freq)
  862. label_list =
  863. add_label_to_list(label_list, "_system_cpu_freq", localhost->system_info->host_cpu_freq, LABEL_SOURCE_AUTO);
  864. if (localhost->system_info->host_ram_total)
  865. label_list =
  866. add_label_to_list(label_list, "_system_ram_total", localhost->system_info->host_ram_total, LABEL_SOURCE_AUTO);
  867. if (localhost->system_info->host_disk_space)
  868. label_list =
  869. add_label_to_list(label_list, "_system_disk_space", localhost->system_info->host_disk_space, LABEL_SOURCE_AUTO);
  870. if (localhost->system_info->architecture)
  871. label_list =
  872. add_label_to_list(label_list, "_architecture", localhost->system_info->architecture, LABEL_SOURCE_AUTO);
  873. if (localhost->system_info->virtualization)
  874. label_list =
  875. add_label_to_list(label_list, "_virtualization", localhost->system_info->virtualization, LABEL_SOURCE_AUTO);
  876. if (localhost->system_info->container)
  877. label_list =
  878. add_label_to_list(label_list, "_container", localhost->system_info->container, LABEL_SOURCE_AUTO);
  879. if (localhost->system_info->container_detection)
  880. label_list =
  881. add_label_to_list(label_list, "_container_detection", localhost->system_info->container_detection, LABEL_SOURCE_AUTO);
  882. if (localhost->system_info->virt_detection)
  883. label_list =
  884. add_label_to_list(label_list, "_virt_detection", localhost->system_info->virt_detection, LABEL_SOURCE_AUTO);
  885. if (localhost->system_info->is_k8s_node)
  886. label_list =
  887. add_label_to_list(label_list, "_is_k8s_node", localhost->system_info->is_k8s_node, LABEL_SOURCE_AUTO);
  888. label_list = add_aclk_host_labels(label_list);
  889. label_list = add_label_to_list(
  890. label_list, "_is_parent", (localhost->next || configured_as_parent()) ? "true" : "false", LABEL_SOURCE_AUTO);
  891. if (localhost->rrdpush_send_destination)
  892. label_list =
  893. add_label_to_list(label_list, "_streams_to", localhost->rrdpush_send_destination, LABEL_SOURCE_AUTO);
  894. return label_list;
  895. }
  896. static inline int rrdhost_is_valid_label_config_option(char *name, char *value)
  897. {
  898. return (is_valid_label_key(name) && is_valid_label_value(value) && strcmp(name, "from environment") &&
  899. strcmp(name, "from kubernetes pods"));
  900. }
  901. static struct label *rrdhost_load_config_labels()
  902. {
  903. int status = config_load(NULL, 1, CONFIG_SECTION_HOST_LABEL);
  904. if(!status) {
  905. char *filename = CONFIG_DIR "/" CONFIG_FILENAME;
  906. error("LABEL: Cannot reload the configuration file '%s', using labels in memory", filename);
  907. }
  908. struct label *l = NULL;
  909. struct section *co = appconfig_get_section(&netdata_config, CONFIG_SECTION_HOST_LABEL);
  910. if(co) {
  911. config_section_wrlock(co);
  912. struct config_option *cv;
  913. for(cv = co->values; cv ; cv = cv->next) {
  914. if(rrdhost_is_valid_label_config_option(cv->name, cv->value)) {
  915. l = add_label_to_list(l, cv->name, cv->value, LABEL_SOURCE_NETDATA_CONF);
  916. cv->flags |= CONFIG_VALUE_USED;
  917. } else {
  918. error("LABELS: It was not possible to create the label '%s' because it contains invalid character(s) or values."
  919. , cv->name);
  920. }
  921. }
  922. config_section_unlock(co);
  923. }
  924. return l;
  925. }
  926. struct label *parse_simple_tags(
  927. struct label *label_list,
  928. const char *tags,
  929. char key_value_separator,
  930. char label_separator,
  931. STRIP_QUOTES_OPTION strip_quotes_from_key,
  932. STRIP_QUOTES_OPTION strip_quotes_from_value,
  933. SKIP_ESCAPED_CHARACTERS_OPTION skip_escaped_characters)
  934. {
  935. const char *end = tags;
  936. while (*end) {
  937. const char *start = end;
  938. char key[CONFIG_MAX_VALUE + 1];
  939. char value[CONFIG_MAX_VALUE + 1];
  940. while (*end && *end != key_value_separator)
  941. end++;
  942. strncpyz(key, start, end - start);
  943. if (*end)
  944. start = ++end;
  945. while (*end && *end != label_separator)
  946. end++;
  947. strncpyz(value, start, end - start);
  948. label_list = add_label_to_list(
  949. label_list,
  950. strip_quotes_from_key ? strip_double_quotes(trim(key), skip_escaped_characters) : trim(key),
  951. strip_quotes_from_value ? strip_double_quotes(trim(value), skip_escaped_characters) : trim(value),
  952. LABEL_SOURCE_NETDATA_CONF);
  953. if (*end)
  954. end++;
  955. }
  956. return label_list;
  957. }
  958. struct label *parse_json_tags(struct label *label_list, const char *tags)
  959. {
  960. char tags_buf[CONFIG_MAX_VALUE + 1];
  961. strncpy(tags_buf, tags, CONFIG_MAX_VALUE);
  962. char *str = tags_buf;
  963. switch (*str) {
  964. case '{':
  965. str++;
  966. strip_last_symbol(str, '}', SKIP_ESCAPED_CHARACTERS);
  967. label_list = parse_simple_tags(label_list, str, ':', ',', STRIP_QUOTES, STRIP_QUOTES, SKIP_ESCAPED_CHARACTERS);
  968. break;
  969. case '[':
  970. str++;
  971. strip_last_symbol(str, ']', SKIP_ESCAPED_CHARACTERS);
  972. char *end = str + strlen(str);
  973. size_t i = 0;
  974. while (str < end) {
  975. char key[CONFIG_MAX_VALUE + 1];
  976. snprintfz(key, CONFIG_MAX_VALUE, "host_tag%zu", i);
  977. str = strip_double_quotes(trim(str), SKIP_ESCAPED_CHARACTERS);
  978. label_list = add_label_to_list(label_list, key, str, LABEL_SOURCE_NETDATA_CONF);
  979. // skip to the next element in the array
  980. str += strlen(str) + 1;
  981. while (*str && *str != ',')
  982. str++;
  983. str++;
  984. i++;
  985. }
  986. break;
  987. case '"':
  988. label_list = add_label_to_list(
  989. label_list, "host_tag", strip_double_quotes(str, SKIP_ESCAPED_CHARACTERS), LABEL_SOURCE_NETDATA_CONF);
  990. break;
  991. default:
  992. label_list = add_label_to_list(label_list, "host_tag", str, LABEL_SOURCE_NETDATA_CONF);
  993. break;
  994. }
  995. return label_list;
  996. }
  997. static struct label *rrdhost_load_labels_from_tags(void)
  998. {
  999. if (!localhost->tags)
  1000. return NULL;
  1001. struct label *label_list = NULL;
  1002. BACKEND_TYPE type = BACKEND_TYPE_UNKNOWN;
  1003. if (config_exists(CONFIG_SECTION_BACKEND, "enabled")) {
  1004. if (config_get_boolean(CONFIG_SECTION_BACKEND, "enabled", CONFIG_BOOLEAN_NO) != CONFIG_BOOLEAN_NO) {
  1005. const char *type_name = config_get(CONFIG_SECTION_BACKEND, "type", "graphite");
  1006. type = backend_select_type(type_name);
  1007. }
  1008. }
  1009. switch (type) {
  1010. case BACKEND_TYPE_GRAPHITE:
  1011. label_list = parse_simple_tags(
  1012. label_list, localhost->tags, '=', ';', DO_NOT_STRIP_QUOTES, DO_NOT_STRIP_QUOTES,
  1013. DO_NOT_SKIP_ESCAPED_CHARACTERS);
  1014. break;
  1015. case BACKEND_TYPE_OPENTSDB_USING_TELNET:
  1016. label_list = parse_simple_tags(
  1017. label_list, localhost->tags, '=', ' ', DO_NOT_STRIP_QUOTES, DO_NOT_STRIP_QUOTES,
  1018. DO_NOT_SKIP_ESCAPED_CHARACTERS);
  1019. break;
  1020. case BACKEND_TYPE_OPENTSDB_USING_HTTP:
  1021. label_list = parse_simple_tags(
  1022. label_list, localhost->tags, ':', ',', STRIP_QUOTES, STRIP_QUOTES,
  1023. DO_NOT_SKIP_ESCAPED_CHARACTERS);
  1024. break;
  1025. case BACKEND_TYPE_JSON:
  1026. label_list = parse_json_tags(label_list, localhost->tags);
  1027. break;
  1028. default:
  1029. label_list = parse_simple_tags(
  1030. label_list, localhost->tags, '=', ',', DO_NOT_STRIP_QUOTES, STRIP_QUOTES,
  1031. DO_NOT_SKIP_ESCAPED_CHARACTERS);
  1032. break;
  1033. }
  1034. return label_list;
  1035. }
  1036. static struct label *rrdhost_load_kubernetes_labels(void)
  1037. {
  1038. struct label *l=NULL;
  1039. char *label_script = mallocz(sizeof(char) * (strlen(netdata_configured_primary_plugins_dir) + strlen("get-kubernetes-labels.sh") + 2));
  1040. sprintf(label_script, "%s/%s", netdata_configured_primary_plugins_dir, "get-kubernetes-labels.sh");
  1041. if (unlikely(access(label_script, R_OK) != 0)) {
  1042. error("Kubernetes pod label fetching script %s not found.",label_script);
  1043. freez(label_script);
  1044. } else {
  1045. pid_t command_pid;
  1046. debug(D_RRDHOST, "Attempting to fetch external labels via %s", label_script);
  1047. FILE *fp = mypopen(label_script, &command_pid);
  1048. if(fp) {
  1049. int MAX_LINE_SIZE=300;
  1050. char buffer[MAX_LINE_SIZE + 1];
  1051. while (fgets(buffer, MAX_LINE_SIZE, fp) != NULL) {
  1052. char *name=buffer;
  1053. char *value=buffer;
  1054. while (*value && *value != ':') value++;
  1055. if (*value == ':') {
  1056. *value = '\0';
  1057. value++;
  1058. }
  1059. char *eos=value;
  1060. while (*eos && *eos != '\n') eos++;
  1061. if (*eos == '\n') *eos = '\0';
  1062. if (strlen(value)>0) {
  1063. if (is_valid_label_key(name)){
  1064. l = add_label_to_list(l, name, value, LABEL_SOURCE_KUBERNETES);
  1065. } else {
  1066. info("Ignoring invalid label name '%s'", name);
  1067. }
  1068. } else {
  1069. error("%s outputted unexpected result: '%s'", label_script, name);
  1070. }
  1071. };
  1072. // Non-zero exit code means that all the script output is error messages. We've shown already any message that didn't include a ':'
  1073. // 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
  1074. int retcode=mypclose(fp, command_pid);
  1075. if (retcode) {
  1076. error("%s exited abnormally. No kubernetes labels will be added to the host.", label_script);
  1077. struct label *ll=l;
  1078. while (ll != NULL) {
  1079. info("Ignoring Label [source id=%s]: \"%s\" -> \"%s\"\n", translate_label_source(ll->label_source), ll->key, ll->value);
  1080. ll = ll->next;
  1081. freez(l);
  1082. l=ll;
  1083. }
  1084. }
  1085. }
  1086. freez(label_script);
  1087. }
  1088. return l;
  1089. }
  1090. void reload_host_labels(void)
  1091. {
  1092. struct label *from_auto = rrdhost_load_auto_labels();
  1093. struct label *from_k8s = rrdhost_load_kubernetes_labels();
  1094. struct label *from_config = rrdhost_load_config_labels();
  1095. struct label *from_tags = rrdhost_load_labels_from_tags();
  1096. struct label *new_labels = merge_label_lists(from_auto, from_k8s);
  1097. new_labels = merge_label_lists(new_labels, from_tags);
  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. }
  1231. last = rd;
  1232. rd = rd->next;
  1233. }
  1234. rrdset_unlock(st);
  1235. debug(D_RRD_CALLS, "RRDSET: Cleaning up remaining chart variables for host '%s', chart '%s'", host->hostname, st->id);
  1236. rrdvar_free_remaining_variables(host, &st->rrdvar_root_index);
  1237. rrdset_flag_clear(st, RRDSET_FLAG_OBSOLETE);
  1238. if (st->dimensions) {
  1239. /* If the chart still has dimensions don't delete it from the metadata log */
  1240. continue;
  1241. }
  1242. }
  1243. #endif
  1244. rrdset_rdlock(st);
  1245. if(rrdhost_delete_obsolete_charts)
  1246. rrdset_delete(st);
  1247. else
  1248. rrdset_save(st);
  1249. rrdset_unlock(st);
  1250. rrdset_free(st);
  1251. goto restart_after_removal;
  1252. }
  1253. }
  1254. }
  1255. void rrd_cleanup_obsolete_charts()
  1256. {
  1257. rrd_rdlock();
  1258. RRDHOST *host;
  1259. rrdhost_foreach_read(host)
  1260. {
  1261. if (host->obsolete_charts_count) {
  1262. rrdhost_wrlock(host);
  1263. #ifdef ENABLE_ACLK
  1264. host->deleted_charts_count = 0;
  1265. #endif
  1266. rrdhost_cleanup_obsolete_charts(host);
  1267. #ifdef ENABLE_ACLK
  1268. if (host->deleted_charts_count)
  1269. aclk_update_chart(host, "dummy-chart", 0);
  1270. #endif
  1271. rrdhost_unlock(host);
  1272. }
  1273. }
  1274. rrd_unlock();
  1275. }
  1276. // ----------------------------------------------------------------------------
  1277. // RRDHOST - set system info from environment variables
  1278. // system_info fields must be heap allocated or NULL
  1279. int rrdhost_set_system_info_variable(struct rrdhost_system_info *system_info, char *name, char *value) {
  1280. int res = 0;
  1281. if (!strcmp(name, "NETDATA_PROTOCOL_VERSION"))
  1282. return res;
  1283. else if(!strcmp(name, "NETDATA_CONTAINER_OS_NAME")){
  1284. freez(system_info->container_os_name);
  1285. system_info->container_os_name = strdupz(value);
  1286. }
  1287. else if(!strcmp(name, "NETDATA_CONTAINER_OS_ID")){
  1288. freez(system_info->container_os_id);
  1289. system_info->container_os_id = strdupz(value);
  1290. }
  1291. else if(!strcmp(name, "NETDATA_CONTAINER_OS_ID_LIKE")){
  1292. freez(system_info->container_os_id_like);
  1293. system_info->container_os_id_like = strdupz(value);
  1294. }
  1295. else if(!strcmp(name, "NETDATA_CONTAINER_OS_VERSION")){
  1296. freez(system_info->container_os_version);
  1297. system_info->container_os_version = strdupz(value);
  1298. }
  1299. else if(!strcmp(name, "NETDATA_CONTAINER_OS_VERSION_ID")){
  1300. freez(system_info->container_os_version_id);
  1301. system_info->container_os_version_id = strdupz(value);
  1302. }
  1303. else if(!strcmp(name, "NETDATA_CONTAINER_OS_DETECTION")){
  1304. freez(system_info->container_os_detection);
  1305. system_info->container_os_detection = strdupz(value);
  1306. }
  1307. else if(!strcmp(name, "NETDATA_HOST_OS_NAME")){
  1308. freez(system_info->host_os_name);
  1309. system_info->host_os_name = strdupz(value);
  1310. }
  1311. else if(!strcmp(name, "NETDATA_HOST_OS_ID")){
  1312. freez(system_info->host_os_id);
  1313. system_info->host_os_id = strdupz(value);
  1314. }
  1315. else if(!strcmp(name, "NETDATA_HOST_OS_ID_LIKE")){
  1316. freez(system_info->host_os_id_like);
  1317. system_info->host_os_id_like = strdupz(value);
  1318. }
  1319. else if(!strcmp(name, "NETDATA_HOST_OS_VERSION")){
  1320. freez(system_info->host_os_version);
  1321. system_info->host_os_version = strdupz(value);
  1322. }
  1323. else if(!strcmp(name, "NETDATA_HOST_OS_VERSION_ID")){
  1324. freez(system_info->host_os_version_id);
  1325. system_info->host_os_version_id = strdupz(value);
  1326. }
  1327. else if(!strcmp(name, "NETDATA_HOST_OS_DETECTION")){
  1328. freez(system_info->host_os_detection);
  1329. system_info->host_os_detection = strdupz(value);
  1330. }
  1331. else if(!strcmp(name, "NETDATA_SYSTEM_KERNEL_NAME")){
  1332. freez(system_info->kernel_name);
  1333. system_info->kernel_name = strdupz(value);
  1334. }
  1335. else if(!strcmp(name, "NETDATA_SYSTEM_CPU_LOGICAL_CPU_COUNT")){
  1336. freez(system_info->host_cores);
  1337. system_info->host_cores = strdupz(value);
  1338. }
  1339. else if(!strcmp(name, "NETDATA_SYSTEM_CPU_FREQ")){
  1340. freez(system_info->host_cpu_freq);
  1341. system_info->host_cpu_freq = strdupz(value);
  1342. }
  1343. else if(!strcmp(name, "NETDATA_SYSTEM_TOTAL_RAM")){
  1344. freez(system_info->host_ram_total);
  1345. system_info->host_ram_total = strdupz(value);
  1346. }
  1347. else if(!strcmp(name, "NETDATA_SYSTEM_TOTAL_DISK_SIZE")){
  1348. freez(system_info->host_disk_space);
  1349. system_info->host_disk_space = strdupz(value);
  1350. }
  1351. else if(!strcmp(name, "NETDATA_SYSTEM_KERNEL_VERSION")){
  1352. freez(system_info->kernel_version);
  1353. system_info->kernel_version = strdupz(value);
  1354. }
  1355. else if(!strcmp(name, "NETDATA_SYSTEM_ARCHITECTURE")){
  1356. freez(system_info->architecture);
  1357. system_info->architecture = strdupz(value);
  1358. }
  1359. else if(!strcmp(name, "NETDATA_SYSTEM_VIRTUALIZATION")){
  1360. freez(system_info->virtualization);
  1361. system_info->virtualization = strdupz(value);
  1362. }
  1363. else if(!strcmp(name, "NETDATA_SYSTEM_VIRT_DETECTION")){
  1364. freez(system_info->virt_detection);
  1365. system_info->virt_detection = strdupz(value);
  1366. }
  1367. else if(!strcmp(name, "NETDATA_SYSTEM_CONTAINER")){
  1368. freez(system_info->container);
  1369. system_info->container = strdupz(value);
  1370. }
  1371. else if(!strcmp(name, "NETDATA_SYSTEM_CONTAINER_DETECTION")){
  1372. freez(system_info->container_detection);
  1373. system_info->container_detection = strdupz(value);
  1374. }
  1375. else if(!strcmp(name, "NETDATA_HOST_IS_K8S_NODE")){
  1376. freez(system_info->is_k8s_node);
  1377. system_info->is_k8s_node = strdupz(value);
  1378. }
  1379. else if (!strcmp(name, "NETDATA_SYSTEM_CPU_VENDOR"))
  1380. return res;
  1381. else if (!strcmp(name, "NETDATA_SYSTEM_CPU_MODEL"))
  1382. return res;
  1383. else if (!strcmp(name, "NETDATA_SYSTEM_CPU_DETECTION"))
  1384. return res;
  1385. else if (!strcmp(name, "NETDATA_SYSTEM_RAM_DETECTION"))
  1386. return res;
  1387. else if (!strcmp(name, "NETDATA_SYSTEM_DISK_DETECTION"))
  1388. return res;
  1389. else if (!strcmp(name, "NETDATA_CONTAINER_IS_OFFICIAL_IMAGE"))
  1390. return res;
  1391. else {
  1392. res = 1;
  1393. }
  1394. return res;
  1395. }
  1396. /**
  1397. * Alarm Compare ID
  1398. *
  1399. * Callback function used with the binary trees to compare the id of RRDCALC
  1400. *
  1401. * @param a a pointer to the RRDCAL item to insert,compare or update the binary tree
  1402. * @param b the pointer to the binary tree.
  1403. *
  1404. * @return It returns 0 case the values are equal, 1 case a is bigger than b and -1 case a is smaller than b.
  1405. */
  1406. int alarm_compare_id(void *a, void *b) {
  1407. register uint32_t hash1 = ((RRDCALC *)a)->id;
  1408. register uint32_t hash2 = ((RRDCALC *)b)->id;
  1409. if(hash1 < hash2) return -1;
  1410. else if(hash1 > hash2) return 1;
  1411. return 0;
  1412. }
  1413. /**
  1414. * Alarm Compare NAME
  1415. *
  1416. * Callback function used with the binary trees to compare the name of RRDCALC
  1417. *
  1418. * @param a a pointer to the RRDCAL item to insert,compare or update the binary tree
  1419. * @param b the pointer to the binary tree.
  1420. *
  1421. * @return It returns 0 case the values are equal, 1 case a is bigger than b and -1 case a is smaller than b.
  1422. */
  1423. int alarm_compare_name(void *a, void *b) {
  1424. RRDCALC *in1 = (RRDCALC *)a;
  1425. RRDCALC *in2 = (RRDCALC *)b;
  1426. if(in1->hash < in2->hash) return -1;
  1427. else if(in1->hash > in2->hash) return 1;
  1428. return strcmp(in1->name,in2->name);
  1429. }
  1430. // Added for gap-filling, if this proves to be a bottleneck in large-scale systems then we will need to cache
  1431. // the last entry times as the metric updates, but let's see if it is a problem first.
  1432. time_t rrdhost_last_entry_t(RRDHOST *h) {
  1433. rrdhost_rdlock(h);
  1434. RRDSET *st;
  1435. time_t result = 0;
  1436. rrdset_foreach_read(st, h) {
  1437. time_t st_last = rrdset_last_entry_t(st);
  1438. if (st_last > result)
  1439. result = st_last;
  1440. }
  1441. rrdhost_unlock(h);
  1442. return result;
  1443. }