diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2020-02-11 18:48:29 +0100 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2020-02-11 18:48:29 +0100 |
commit | f9991b5a09024a7b7bd3ee52338f0cda5ddde794 (patch) | |
tree | 0b7712e0423c4cddada0e1e0a1ca97b368035d5f | |
parent | 38d71b4e92d60b751ada2ee326acc22707aa5900 (diff) | |
download | ping903-f9991b5a09024a7b7bd3ee52338f0cda5ddde794.tar.gz ping903-f9991b5a09024a7b7bd3ee52338f0cda5ddde794.tar.bz2 |
Improve logging and configuration.
-rw-r--r-- | src/config.c | 26 | ||||
-rw-r--r-- | src/logger.c | 29 | ||||
-rw-r--r-- | src/main.c | 2 | ||||
-rw-r--r-- | src/ping903.c | 10 | ||||
-rw-r--r-- | src/ping903.conf | 60 | ||||
-rw-r--r-- | src/ping903.h | 3 | ||||
-rw-r--r-- | src/pinger.c | 26 |
7 files changed, 146 insertions, 10 deletions
diff --git a/src/config.c b/src/config.c index 26d858d..5c72a28 100644 --- a/src/config.c +++ b/src/config.c @@ -141,6 +141,8 @@ struct cf_stmt statements[] = { { "tolerance", STMT_T_ULONG, &ping_tolerance }, { "ip-list", STMT_T_CALLBACK, NULL, cf_ip_list }, { "data-length", STMT_T_ULONG, &data_length }, + { "syslog-facility", STMT_T_CALLBACK, NULL, cf_syslog_facility }, + { "access-log", STMT_T_BOOL, &httpd_access_log }, { NULL } }; @@ -174,8 +176,25 @@ stmt_parse(char const *kw, char const *val, char const *file, unsigned line) case STMT_T_ULONG: return get_num(val, cf->data, file, line); - case STMT_T_BOOL: - abort(); + case STMT_T_BOOL: { + static char *t_val[] = { "1", "t", "true", "yes", "on", NULL }; + static char *f_val[] = { "0", "f", "nil", "false", "no", "off", NULL }; + int i; + for (i = 0; t_val[i]; i++) { + if (strcasecmp(t_val[i], val) == 0) { + *(int*)cf->data = 1; + return 0; + } + } + for (i = 0; f_val[i]; i++) { + if (strcasecmp(f_val[i], val) == 0) { + *(int*)cf->data = 0; + return 0; + } + } + error("%s:%d: not a valid boolean value", file, line); + return -1; + } case STMT_T_CALLBACK: arg.input.val = val; @@ -286,7 +305,8 @@ config_to_json(void) break; case STMT_T_BOOL: - abort(); + vn = json_new_bool(*(int*)cf->data); + break; case STMT_T_CALLBACK: arg.output = NULL; diff --git a/src/logger.c b/src/logger.c index fe8de2f..a2b21cb 100644 --- a/src/logger.c +++ b/src/logger.c @@ -18,6 +18,7 @@ #include <string.h> #include <syslog.h> #include "ping903.h" +#include "json.h" char *progname; @@ -122,6 +123,34 @@ set_log_facility(char const *arg) return 0; } +int +cf_syslog_facility(int mode, union cf_callback_arg *arg, void *data) +{ + if (mode == CF_PARSE) { + if (set_log_facility(arg->input.val)) { + error("%s:%d: unknown syslog facility", + arg->input.file, arg->input.line); + return CF_RET_FAIL; + } + } else { + struct facility *f; + for (f = facilities; f->s; f++) { + if (f->n == facility) { + struct json_value *val = json_new_string(f->s); + if (!val) { + fatal("out of memory"); + return CF_RET_FAIL; + } + arg->output = val; + return CF_RET_OK; + } + } + fatal("can't decode the current facility; please report"); + return CF_RET_FAIL; + } + return CF_RET_OK; +} + void set_progname(char const *arg) { @@ -27,7 +27,7 @@ #include "json.h" static char *config_file = "/etc/ping903.conf"; -static int verbose; +int verbose; void diff --git a/src/ping903.c b/src/ping903.c index f98983b..c3c0a46 100644 --- a/src/ping903.c +++ b/src/ping903.c @@ -37,6 +37,7 @@ #endif char *httpd_addr; +int httpd_access_log = 0; static int open_node(char const *node, char const *serv, struct sockaddr **saddr) @@ -144,6 +145,9 @@ http_log(struct MHD_Connection *connection, struct tm *tm; char tbuf[30]; + if (!httpd_access_log) + return; + ipstr = get_remote_ip(connection); host = MHD_lookup_connection_value(connection, @@ -173,9 +177,9 @@ http_error(struct MHD_Connection *conn, { int ret; struct MHD_Response *resp = - MHD_create_response_from_buffer (0, - NULL, - MHD_RESPMEM_PERSISTENT); + MHD_create_response_from_buffer(0, + NULL, + MHD_RESPMEM_PERSISTENT); http_log(conn, method, url, status, NULL); ret = MHD_queue_response(conn, status, resp); MHD_destroy_response(resp); diff --git a/src/ping903.conf b/src/ping903.conf new file mode 100644 index 0000000..864ed85 --- /dev/null +++ b/src/ping903.conf @@ -0,0 +1,60 @@ +# Name of the file with IP addresses (or hostnames) to monitor. Each IP +# must be listed on a separate line. Empty lines, leading and trailing +# whitespace is ignored. Comments are introduced by a hash sign as the first +# non-whitespace character on the line. +# Multiple statements accumulate. At least one must be present. +#ip-list FILENAME + +# Write PID of the main process to the specified file. By default no pidfile +# is created. +# +# Example usage: +# +#pidfile /var/run/ping903.pid + +# Ping interval in seconds. The program will poll the registered IPs once +# in the given number of seconds. +# +#interval 60 + +# Number of ICMP echo requests to send to each host. +# +#ping-count 10 + +# Failure tolerance. Mark hosts as being down if that many requests from +# ping-count miss replies. +# +#tolerance 3 + +# Number of additional bytes to send in each ICMP echo requests (apart from +# the timestamp). +# +#data-length 40 + +# Log using this syslog facility. Ignored if running in foreground mode (-f) +#syslog-facility daemon + +# Listen on this interface for incoming HTTP requests. Argument can be any of: +# IPADDR +# IPADDR:PORT +# :PORT +# where IPADDR stands for a IPv4 address and PORT for the port number or +# symbolic name from /etc/services. +# +#listen :8080 + +# Enable apache-style HTTPD access logging. Valid values are: +# 1, t, true, yes, on - enable logging, +# 0, f, nil, false, no, off - disable logging. +#access-log off + +# Register trusted IP (or network) for the purpose of HTTP logging. For +# requests coming from that IP the value of the X-Forwarded-For header will +# be trusted. Default is empty. Argument is IP[/MASK], where IP stands for +# a IPv4 IP and MASK is a netmask (dotted-quad) or netmask length (decimal). +# Multiple statements accumulate. +# +# Example usage: +# +#trusted-ip 127.0.0.1 + diff --git a/src/ping903.h b/src/ping903.h index e93bf0a..ea72810 100644 --- a/src/ping903.h +++ b/src/ping903.h @@ -64,6 +64,7 @@ enum { int readconfig(char const *file); int cf_trusted_ip(int mode, union cf_callback_arg *arg, void *data); +int cf_syslog_facility(int mode, union cf_callback_arg *arg, void *data); void emalloc_die(void); void *emalloc(size_t s); @@ -108,8 +109,10 @@ typedef struct hostaddr { } HOSTADDR; extern char *progname; +extern int verbose; extern int fatal_signals[]; extern char *httpd_addr; +extern int httpd_access_log; extern char *pidfile; extern unsigned long ping_interval; extern unsigned long ping_count; diff --git a/src/pinger.c b/src/pinger.c index c050f81..145ab19 100644 --- a/src/pinger.c +++ b/src/pinger.c @@ -272,8 +272,8 @@ send_echo(HOSTADDR *host, unsigned char *ping_buffer) else { host->xmit_count++; if (n != buflen) - error ("ping: wrote %s %d chars, ret=%d\n", - host->name, buflen, n); + error("ping: wrote %s %d chars, ret=%d\n", + host->name, buflen, n); } } @@ -466,6 +466,23 @@ get_all_host_stat(struct json_value **retval) return -1; } +static void +log_echo(struct sockaddr *addr, socklen_t addrlen, + struct icmp *icmp, struct ip *ip, + size_t datalen, double rtt) +{ + char hbuf[NI_MAXHOST]; + + datalen -= ip->ip_hl << 2; + if (getnameinfo(addr, addrlen, hbuf, sizeof(hbuf), + NULL, 0, NI_NUMERICHOST)) { + strcpy(hbuf, "UNKNOWN"); + } + + info("%d bytes from %s: icmp_seq=%u ttl=%d time=%.3f ms", datalen, + hbuf, icmp->icmp_seq, ip->ip_ttl, rtt); +} + void * p903_receiver(void *p) { @@ -526,7 +543,6 @@ p903_receiver(void *p) struct timeval tv_now, tv_orig, tv_diff, *tp; double rtt; -// info("got reply for %s", host->name); gettimeofday (&tv_now, NULL); tp = (struct timeval *) icmp->icmp_data; @@ -545,6 +561,10 @@ p903_receiver(void *p) host->tmax = rtt; host->recv_count++; + + if (verbose) + log_echo((struct sockaddr *)&addr, addrlen, icmp, ip, n, rtt); + if (host->recv_count < ping_count) { sendq_enqueue(host); } else { |