access.log improvements
Costa Tsaousis (ktsaou) committed
Sep 17, 2017 at 15:52 UTC
dc502a86d9c1e9881cae0d40a8cd9cd97f23c892
2 files changed
+49
-16
src/rrdpush.c
+18
@@ -560,6 +560,10 @@ cleanup:
560
// ----------------------------------------------------------------------------
561
// rrdpush receiver thread
562
563
+static void log_stream_connection(const char *client_ip, const char *client_port, const char *api_key, const char *host, const char *msg) {
564
+ log_access("STREAM: %d '[%s]:%s' '%s' host '%s' api key '%s'", gettid(), client_ip, client_port, msg, host, api_key);
565
+}
566
+
567
static int rrdpush_receive(int fd, const char *key, const char *hostname, const char *registry_hostname, const char *machine_guid, const char *os, const char *tags, int update_every, char *client_ip, char *client_port) {
568
RRDHOST *host;
569
int history = default_rrd_history_entries;
@@ -618,6 +622,7 @@ static int rrdpush_receive(int fd, const char *key, const char *hostname, const
622
623
if(!host) {
624
close(fd);
625
+ log_stream_connection(client_ip, client_port, key, hostname, "FAILED - CANNOT ACQUIRE HOST");
626
error("STREAM %s [receive from [%s]:%s]: failed to find/create host structure.", hostname, client_ip, client_port);
627
return 1;
628
}
@@ -656,6 +661,7 @@ static int rrdpush_receive(int fd, const char *key, const char *hostname, const
661
662
info("STREAM %s [receive from [%s]:%s]: initializing communication...", host->hostname, client_ip, client_port);
663
if(send_timeout(fd, START_STREAMING_PROMPT, strlen(START_STREAMING_PROMPT), 0, 60) != strlen(START_STREAMING_PROMPT)) {
664
+ log_stream_connection(client_ip, client_port, key, host->hostname, "FAILED - CANNOT REPLY");
665
error("STREAM %s [receive from [%s]:%s]: cannot send ready command.", host->hostname, client_ip, client_port);
666
close(fd);
667
return 0;
@@ -668,6 +674,7 @@ static int rrdpush_receive(int fd, const char *key, const char *hostname, const
674
// convert the socket to a FILE *
675
FILE *fp = fdopen(fd, "r");
676
if(!fp) {
677
+ log_stream_connection(client_ip, client_port, key, host->hostname, "FAILED - SOCKET ERROR");
678
error("STREAM %s [receive from [%s]:%s]: failed to get a FILE for FD %d.", host->hostname, client_ip, client_port, fd);
679
close(fd);
680
return 0;
@@ -692,7 +699,11 @@ static int rrdpush_receive(int fd, const char *key, const char *hostname, const
699
700
// call the plugins.d processor to receive the metrics
701
info("STREAM %s [receive from [%s]:%s]: receiving metrics...", host->hostname, client_ip, client_port);
702
+ log_stream_connection(client_ip, client_port, key, host->hostname, "ACCESS GRANDED");
703
+
704
size_t count = pluginsd_process(host, &cd, fp, 1);
705
+
706
+ log_stream_connection(client_ip, client_port, key, host->hostname, "DISCONNECTED");
707
error("STREAM %s [receive from [%s]:%s]: disconnected (completed updates %zu).", host->hostname, client_ip, client_port, count);
708
709
rrdhost_wrlock(host);
@@ -806,6 +817,7 @@ int rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url
817
}
818
819
if(!key || !*key) {
820
+ log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - NO KEY");
821
error("STREAM [receive from [%s]:%s]: request without an API key. Forbidding access.", w->client_ip, w->client_port);
822
buffer_flush(w->response.data);
823
buffer_sprintf(w->response.data, "You need an API key for this request.");
@@ -813,6 +825,7 @@ int rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url
825
}
826
827
if(!hostname || !*hostname) {
828
+ log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - NO HOSTNAME");
829
error("STREAM [receive from [%s]:%s]: request without a hostname. Forbidding access.", w->client_ip, w->client_port);
830
buffer_flush(w->response.data);
831
buffer_sprintf(w->response.data, "You need to send a hostname too.");
@@ -820,6 +833,7 @@ int rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url
833
}
834
835
if(!machine_guid || !*machine_guid) {
836
+ log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - NO MACHINE GUID");
837
error("STREAM [receive from [%s]:%s]: request without a machine GUID. Forbidding access.", w->client_ip, w->client_port);
838
buffer_flush(w->response.data);
839
buffer_sprintf(w->response.data, "You need to send a machine GUID too.");
@@ -827,6 +841,7 @@ int rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url
841
}
842
843
if(regenerate_guid(key, buf) == -1) {
844
+ log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - INVALID KEY");
845
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);
846
buffer_flush(w->response.data);
847
buffer_sprintf(w->response.data, "Your API key is invalid.");
@@ -834,6 +849,7 @@ int rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url
849
}
850
851
if(regenerate_guid(machine_guid, buf) == -1) {
852
+ log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - INVALID MACHINE GUID");
853
error("STREAM [receive from [%s]:%s]: machine GUID '%s' is not GUID. Forbidding access.", w->client_ip, w->client_port, machine_guid);
854
buffer_flush(w->response.data);
855
buffer_sprintf(w->response.data, "Your machine GUID is invalid.");
@@ -841,6 +857,7 @@ int rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url
857
}
858
859
if(!appconfig_get_boolean(&stream_config, key, "enabled", 0)) {
860
+ log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - KEY NOT ENABLED");
861
error("STREAM [receive from [%s]:%s]: API key '%s' is not allowed. Forbidding access.", w->client_ip, w->client_port, key);
862
buffer_flush(w->response.data);
863
buffer_sprintf(w->response.data, "Your API key is not permitted access.");
@@ -848,6 +865,7 @@ int rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url
865
}
866
867
if(!appconfig_get_boolean(&stream_config, machine_guid, "enabled", 1)) {
868
+ log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - MACHINE GUID NOT ENABLED");
869
error("STREAM [receive from [%s]:%s]: machine GUID '%s' is not allowed. Forbidding access.", w->client_ip, w->client_port, machine_guid);
870
buffer_flush(w->response.data);
871
buffer_sprintf(w->response.data, "Your machine guide is not permitted access.");
src/web_client.c
+31
-16
@@ -52,6 +52,10 @@ static inline int web_client_uncrock_socket(struct web_client *w) {
52
return 0;
53
}
54
55
+static void log_connection(struct web_client *w, const char *msg) {
56
+ log_access("%llu: %d '[%s]:%s' '%s'", w->id, gettid(), w->client_ip, w->client_port, msg);
57
+}
58
+
59
struct web_client *web_client_create(int listener) {
60
struct web_client *w;
61
@@ -61,14 +65,24 @@ struct web_client *web_client_create(int listener) {
65
66
{
67
w->ifd = accept_socket(listener, SOCK_NONBLOCK, w->client_ip, sizeof(w->client_ip), w->client_port, sizeof(w->client_port), web_client_access_list);
64
- if (w->ifd == -1) {
68
66
- if(errno != EPERM)
69
+ if(unlikely(!*w->client_ip)) strcpy(w->client_ip, "-");
70
+ if(unlikely(!*w->client_port)) strcpy(w->client_port, "-");
71
+
72
+ if (w->ifd == -1) {
73
+ if(errno == EPERM)
74
+ log_connection(w, "ACCESS DENIED");
75
+ else {
76
+ log_connection(w, "CONNECTION FAILED");
77
error("%llu: Failed to accept new incoming connection.", w->id);
78
+ }
79
80
freez(w);
81
return NULL;
82
}
83
+ else
84
+ log_connection(w, "CONNECTED");
85
+
86
w->ofd = w->ifd;
87
88
int flag = 1;
@@ -125,17 +139,20 @@ void web_client_reset(struct web_client *w) {
139
140
// --------------------------------------------------------------------
141
// access log
128
-
129
- log_access("%llu: (sent/all = %zu/%zu bytes %0.0f%%, prep/sent/total = %0.2f/%0.2f/%0.2f ms) %s: %d '%s'",
130
- w->id,
131
- sent, size, -((size > 0) ? ((size - sent) / (double) size * 100.0) : 0.0),
132
- dt_usec(&w->tv_ready, &w->tv_in) / 1000.0,
133
- dt_usec(&tv, &w->tv_ready) / 1000.0,
134
- dt_usec(&tv, &w->tv_in) / 1000.0,
135
- (w->mode == WEB_CLIENT_MODE_FILECOPY) ? "filecopy" : ((w->mode == WEB_CLIENT_MODE_OPTIONS)
136
- ? "options" : "data"),
137
- w->response.code,
138
- w->last_url
142
+ log_access("%llu: %d '[%s]:%s' '%s' (sent/all = %zu/%zu bytes %0.0f%%, prep/sent/total = %0.2f/%0.2f/%0.2f ms) %d '%s'",
143
+ w->id
144
+ , gettid()
145
+ , w->client_ip
146
+ , w->client_port
147
+ , (w->mode == WEB_CLIENT_MODE_FILECOPY) ? "FILECOPY" : ((w->mode == WEB_CLIENT_MODE_OPTIONS) ? "OPTIONS" : "DATA")
148
+ , sent
149
+ , size
150
+ , -((size > 0) ? ((size - sent) / (double) size * 100.0) : 0.0)
151
+ , dt_usec(&w->tv_ready, &w->tv_in) / 1000.0
152
+ , dt_usec(&tv, &w->tv_ready) / 1000.0
153
+ , dt_usec(&tv, &w->tv_in) / 1000.0
154
+ , w->response.code
155
+ , w->last_url
156
);
157
}
158
@@ -1612,8 +1629,6 @@ void *web_client_main(void *ptr)
1629
int retval, timeout;
1630
nfds_t fdmax = 0;
1631
1615
- log_access("%llu: %s port %s connected on thread task id %d", w->id, w->client_ip, w->client_port, gettid());
1616
-
1632
for(;;) {
1633
if(unlikely(netdata_exit)) break;
1634
@@ -1723,7 +1738,7 @@ void *web_client_main(void *ptr)
1738
1739
web_client_reset(w);
1740
1726
- log_access("%llu: %s port %s disconnected from thread task id %d", w->id, w->client_ip, w->client_port, gettid());
1741
+ log_connection(w, "DISCONNECTED");
1742
debug(D_WEB_CLIENT, "%llu: done...", w->id);
1743
1744
// close the sockets/files now