rrdhost.c 61 KB

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