slaves propagate registry_hostname to master, which is now used for alarm notifications, so that clicking an alarm generated by the master can properly show the slave dashboard
Costa Tsaousis (ktsaou) committed
May 13, 2017 at 23:47 UTC
793dcded1f97a5493375bb241c8903587426ce39
10 files changed
+53
-34
plugins.d/alarm-notify.sh
+1
-5
@@ -115,8 +115,6 @@ debug() {
115
NETDATA_CONFIG_DIR="${NETDATA_CONFIG_DIR-/etc/netdata}"
116
NETDATA_CACHE_DIR="${NETDATA_CACHE_DIR-/var/cache/netdata}"
117
[ -z "${NETDATA_REGISTRY_URL}" ] && NETDATA_REGISTRY_URL="https://registry.my-netdata.io"
118
-[ -z "${NETDATA_HOSTNAME}" ] && NETDATA_HOSTNAME="$(hostname)"
119
-[ -z "${NETDATA_REGISTRY_HOSTNAME}" ] && NETDATA_REGISTRY_HOSTNAME="${NETDATA_HOSTNAME}"
118
119
# -----------------------------------------------------------------------------
120
# parse command line parameters
@@ -145,8 +143,6 @@ old_value_string="${20}" # friendly old value (with units)
143
# -----------------------------------------------------------------------------
144
# find a suitable hostname to use, if netdata did not supply a hostname
145
148
-[ -z "${host}" ] && host="${NETDATA_HOSTNAME}"
149
-[ -z "${host}" ] && host="${NETDATA_REGISTRY_HOSTNAME}"
146
[ -z "${host}" ] && host="$(hostname 2>/dev/null)"
147
148
# -----------------------------------------------------------------------------
@@ -1108,7 +1104,7 @@ EOF
1104
# prepare the content of the notification
1105
1106
# the url to send the user on click
1111
-urlencode "${NETDATA_REGISTRY_HOSTNAME}" >/dev/null; url_host="${REPLY}"
1107
+urlencode "${host}" >/dev/null; url_host="${REPLY}"
1108
urlencode "${chart}" >/dev/null; url_chart="${REPLY}"
1109
urlencode "${family}" >/dev/null; url_family="${REPLY}"
1110
urlencode "${name}" >/dev/null; url_name="${REPLY}"
src/health.c
+1
-1
@@ -148,7 +148,7 @@ static inline void health_alarm_execute(RRDHOST *host, ALARM_ENTRY *ae) {
148
snprintfz(command_to_run, ALARM_EXEC_COMMAND_LENGTH, "exec %s '%s' '%s' '%u' '%u' '%u' '%lu' '%s' '%s' '%s' '%s' '%s' '%0.0Lf' '%0.0Lf' '%s' '%u' '%u' '%s' '%s' '%s' '%s'",
149
exec,
150
recipient,
151
- host->hostname,
151
+ host->registry_hostname,
152
ae->unique_id,
153
ae->alarm_id,
154
ae->alarm_event_id,
src/main.c
-2
@@ -425,8 +425,6 @@ static void get_netdata_configured_variables() {
425
netdata_configured_hostname = config_get(CONFIG_SECTION_GLOBAL, "hostname", buf);
426
debug(D_OPTIONS, "hostname set to '%s'", netdata_configured_hostname);
427
428
- netdata_configured_hostname = config_get(CONFIG_SECTION_GLOBAL, "hostname", CONFIG_DIR);
429
-
428
// ------------------------------------------------------------------------
429
// get default database size
430
src/registry.c
+1
-1
@@ -45,7 +45,7 @@ static inline void registry_json_header(RRDHOST *host, struct web_client *w, con
45
buffer_flush(w->response.data);
46
w->response.data->contenttype = CT_APPLICATION_JSON;
47
buffer_sprintf(w->response.data, "{\n\t\"action\": \"%s\",\n\t\"status\": \"%s\",\n\t\"hostname\": \"%s\",\n\t\"machine_guid\": \"%s\"",
48
- action, status, (host == localhost)?registry.hostname:host->hostname, host->machine_guid);
48
+ action, status, host->registry_hostname, host->machine_guid);
49
}
50
51
static inline void registry_json_footer(struct web_client *w) {
src/registry.h
+2
@@ -70,6 +70,8 @@ extern int registry_request_hello_json(RRDHOST *host, struct web_client *w);
70
extern void registry_statistics(void);
71
72
extern char *registry_get_this_machine_guid(void);
73
+extern char *registry_get_this_machine_hostname(void);
74
+
75
extern int regenerate_guid(const char *guid, char *result);
76
77
#endif /* NETDATA_REGISTRY_H */
src/registry_init.c
+1
-1
@@ -34,7 +34,7 @@ int registry_init(void) {
34
registry.persons_expiration = config_get_number(CONFIG_SECTION_REGISTRY, "registry expire idle persons days", 365) * 86400;
35
registry.registry_domain = config_get(CONFIG_SECTION_REGISTRY, "registry domain", "");
36
registry.registry_to_announce = config_get(CONFIG_SECTION_REGISTRY, "registry to announce", "https://registry.my-netdata.io");
37
- registry.hostname = config_get(CONFIG_SECTION_REGISTRY, "registry hostname", config_get(CONFIG_SECTION_GLOBAL, "hostname", "localhost"));
37
+ registry.hostname = config_get(CONFIG_SECTION_REGISTRY, "registry hostname", netdata_configured_hostname);
38
registry.verify_cookies_redirects = config_get_boolean(CONFIG_SECTION_REGISTRY, "verify browser cookies support", 1);
39
40
setenv("NETDATA_REGISTRY_HOSTNAME", registry.hostname, 1);
src/registry_internals.c
+4
@@ -274,6 +274,10 @@ static inline int is_machine_guid_blacklisted(const char *guid) {
274
return 0;
275
}
276
277
+char *registry_get_this_machine_hostname(void) {
278
+ return registry.hostname;
279
+}
280
+
281
char *registry_get_this_machine_guid(void) {
282
static char guid[GUID_LEN + 1] = "";
283
src/rrd.h
+3
@@ -368,6 +368,8 @@ struct rrdhost {
368
char *hostname; // the hostname of this host
369
uint32_t hash_hostname; // the hostname hash
370
371
+ char *registry_hostname; // the registry hostname for this host
372
+
373
char machine_guid[GUID_LEN + 1]; // the unique ID of this host
374
uint32_t hash_machine_guid; // the hash of the unique ID
375
@@ -488,6 +490,7 @@ extern RRDHOST *rrdhost_find_by_guid(const char *guid, uint32_t hash);
490
491
extern RRDHOST *rrdhost_find_or_create(
492
const char *hostname
493
+ , const char *registry_hostname
494
, const char *guid
495
, const char *os
496
, int update_every
src/rrdhost.c
+18
-11
@@ -80,16 +80,17 @@ static inline void rrdhost_init_machine_guid(RRDHOST *host, const char *machine_
80
// RRDHOST - add a host
81
82
RRDHOST *rrdhost_create(const char *hostname,
83
- const char *guid,
84
- const char *os,
85
- int update_every,
86
- long entries,
87
- RRD_MEMORY_MODE memory_mode,
88
- int health_enabled,
89
- int rrdpush_enabled,
90
- char *rrdpush_destination,
91
- char *rrdpush_api_key,
92
- int is_localhost
83
+ const char *registry_hostname,
84
+ const char *guid,
85
+ const char *os,
86
+ int update_every,
87
+ long entries,
88
+ RRD_MEMORY_MODE memory_mode,
89
+ int health_enabled,
90
+ int rrdpush_enabled,
91
+ char *rrdpush_destination,
92
+ char *rrdpush_api_key,
93
+ int is_localhost
94
) {
95
debug(D_RRDHOST, "Host '%s': adding with guid '%s'", hostname, guid);
96
@@ -115,6 +116,7 @@ RRDHOST *rrdhost_create(const char *hostname,
116
rrdhost_init_hostname(host, hostname);
117
rrdhost_init_machine_guid(host, guid);
118
rrdhost_init_os(host, os);
119
+ host->registry_hostname = strdupz((registry_hostname && *registry_hostname)?registry_hostname:hostname);
120
121
avl_init_lock(&(host->rrdset_root_index), rrdset_compare);
122
avl_init_lock(&(host->rrdset_root_index_name), rrdset_compare_name);
@@ -229,7 +231,7 @@ RRDHOST *rrdhost_create(const char *hostname,
231
host = NULL;
232
}
233
else {
232
- info("Host '%s' with guid '%s' initialized"
234
+ info("Host '%s' (at registry as '%s') with guid '%s' initialized"
235
", os %s"
236
", update every %d"
237
", memory mode %s"
@@ -243,6 +245,7 @@ RRDHOST *rrdhost_create(const char *hostname,
245
", alarms default handler '%s'"
246
", alarms default recipient '%s'"
247
, host->hostname
248
+ , host->registry_hostname
249
, host->machine_guid
250
, host->os
251
, host->rrd_update_every
@@ -267,6 +270,7 @@ RRDHOST *rrdhost_create(const char *hostname,
270
271
RRDHOST *rrdhost_find_or_create(
272
const char *hostname
273
+ , const char *registry_hostname
274
, const char *guid
275
, const char *os
276
, int update_every
@@ -284,6 +288,7 @@ RRDHOST *rrdhost_find_or_create(
288
if(!host) {
289
host = rrdhost_create(
290
hostname
291
+ , registry_hostname
292
, guid
293
, os
294
, update_every
@@ -372,6 +377,7 @@ void rrd_init(char *hostname) {
377
rrd_wrlock();
378
localhost = rrdhost_create(
379
hostname
380
+ , registry_get_this_machine_hostname()
381
, registry_get_this_machine_guid()
382
, os_type
383
, default_rrd_update_every
@@ -482,6 +488,7 @@ void rrdhost_free(RRDHOST *host) {
488
freez(host->health_default_recipient);
489
freez(host->health_log_filename);
490
freez(host->hostname);
491
+ freez(host->registry_hostname);
492
rrdhost_unlock(host);
493
netdata_rwlock_destroy(&host->health_log.alarm_log_rwlock);
494
netdata_rwlock_destroy(&host->rrdhost_rwlock);
src/rrdpush.c
+22
-13
@@ -309,11 +309,12 @@ void *rrdpush_sender_thread(void *ptr) {
309
310
char http[1000 + 1];
311
snprintfz(http, 1000,
312
- "STREAM key=%s&hostname=%s&machine_guid=%s&os=%s&update_every=%d HTTP/1.1\r\n"
312
+ "STREAM key=%s&hostname=%s®istry_hostname=%s&machine_guid=%s&os=%s&update_every=%d HTTP/1.1\r\n"
313
"User-Agent: netdata-push-service/%s\r\n"
314
"Accept: */*\r\n\r\n"
315
, host->rrdpush_api_key
316
, host->hostname
317
+ , host->registry_hostname
318
, host->machine_guid
319
, host->os
320
, default_rrd_update_every
@@ -510,7 +511,7 @@ cleanup:
511
// ----------------------------------------------------------------------------
512
// rrdpush receiver thread
513
513
-int rrdpush_receive(int fd, const char *key, const char *hostname, const char *machine_guid, const char *os, int update_every, char *client_ip, char *client_port) {
514
+int rrdpush_receive(int fd, const char *key, const char *hostname, const char *registry_hostname, const char *machine_guid, const char *os, int update_every, char *client_ip, char *client_port) {
515
RRDHOST *host;
516
int history = default_rrd_history_entries;
517
RRD_MEMORY_MODE mode = default_rrd_memory_mode;
@@ -550,6 +551,7 @@ int rrdpush_receive(int fd, const char *key, const char *hostname, const char *m
551
else
552
host = rrdhost_find_or_create(
553
hostname
554
+ , registry_hostname
555
, machine_guid
556
, os
557
, update_every
@@ -649,6 +651,7 @@ struct rrdpush_thread {
651
int fd;
652
char *key;
653
char *hostname;
654
+ char *registry_hostname;
655
char *machine_guid;
656
char *os;
657
char *client_ip;
@@ -667,11 +670,12 @@ void *rrdpush_receiver_thread(void *ptr) {
670
671
672
info("STREAM %s [%s]:%s: receive thread created (task id %d)", rpt->hostname, rpt->client_ip, rpt->client_port, gettid());
670
- rrdpush_receive(rpt->fd, rpt->key, rpt->hostname, rpt->machine_guid, rpt->os, rpt->update_every, rpt->client_ip, rpt->client_port);
673
+ rrdpush_receive(rpt->fd, rpt->key, rpt->hostname, rpt->registry_hostname, rpt->machine_guid, rpt->os, rpt->update_every, rpt->client_ip, rpt->client_port);
674
info("STREAM %s [receive from [%s]:%s]: receive thread ended (task id %d)", rpt->hostname, rpt->client_ip, rpt->client_port, gettid());
675
676
freez(rpt->key);
677
freez(rpt->hostname);
678
+ freez(rpt->registry_hostname);
679
freez(rpt->machine_guid);
680
freez(rpt->os);
681
freez(rpt->client_ip);
@@ -704,7 +708,7 @@ int rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url
708
709
info("STREAM [receive from [%s]:%s]: new client connection.", w->client_ip, w->client_port);
710
707
- char *key = NULL, *hostname = NULL, *machine_guid = NULL, *os = "unknown";
711
+ char *key = NULL, *hostname = NULL, *registry_hostname = NULL, *machine_guid = NULL, *os = "unknown";
712
int update_every = default_rrd_update_every;
713
char buf[GUID_LEN + 1];
714
@@ -720,12 +724,16 @@ int rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url
724
key = value;
725
else if(!strcmp(name, "hostname"))
726
hostname = value;
727
+ else if(!strcmp(name, "registry_hostname"))
728
+ registry_hostname = value;
729
else if(!strcmp(name, "machine_guid"))
730
machine_guid = value;
731
else if(!strcmp(name, "update_every"))
732
update_every = (int)strtoul(value, NULL, 0);
733
else if(!strcmp(name, "os"))
734
os = value;
735
+ else
736
+ info("STREAM [receive from [%s]:%s]: request has parameter '%s' = '%s', which is not used.", w->client_ip, w->client_port, key, value);
737
}
738
739
if(!key || !*key) {
@@ -750,7 +758,7 @@ int rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url
758
}
759
760
if(regenerate_guid(key, buf) == -1) {
753
- error("STREAM [receive from [%s]:%s]: API key '%s' is not valid GUID. Forbidding access.", w->client_ip, w->client_port, key);
761
+ error("STREAM [receive from [%s]:%s]: API key '%s' is not valid GUID (use the command uuidgen to generate one). Forbidding access.", w->client_ip, w->client_port, key);
762
buffer_flush(w->response.data);
763
buffer_sprintf(w->response.data, "Your API key is invalid.");
764
return 401;
@@ -778,14 +786,15 @@ int rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url
786
}
787
788
struct rrdpush_thread *rpt = mallocz(sizeof(struct rrdpush_thread));
781
- rpt->fd = w->ifd;
782
- rpt->key = strdupz(key);
783
- rpt->hostname = strdupz(hostname);
784
- rpt->machine_guid = strdupz(machine_guid);
785
- rpt->os = strdupz(os);
786
- rpt->client_ip = strdupz(w->client_ip);
787
- rpt->client_port = strdupz(w->client_port);
788
- rpt->update_every = update_every;
789
+ rpt->fd = w->ifd;
790
+ rpt->key = strdupz(key);
791
+ rpt->hostname = strdupz(hostname);
792
+ rpt->registry_hostname = strdupz((registry_hostname && *registry_hostname)?registry_hostname:hostname);
793
+ rpt->machine_guid = strdupz(machine_guid);
794
+ rpt->os = strdupz(os);
795
+ rpt->client_ip = strdupz(w->client_ip);
796
+ rpt->client_port = strdupz(w->client_port);
797
+ rpt->update_every = update_every;
798
pthread_t thread;
799
800
debug(D_SYSTEM, "STREAM [receive from [%s]:%s]: starting receiving thread.", w->client_ip, w->client_port);