diff options
author | Reyk Floeter <reyk@cvs.openbsd.org> | 2007-02-07 15:17:47 +0000 |
---|---|---|
committer | Reyk Floeter <reyk@cvs.openbsd.org> | 2007-02-07 15:17:47 +0000 |
commit | 3740f5b5e7e90adcd0be6513ddd3f8684cacde34 (patch) | |
tree | 7272f01760dc854689b773c8a27f5a0186cb8448 /usr.sbin/hoststated | |
parent | fe851fcc379376cc9a46baa630ec734083adbc3f (diff) |
add new "log (updates|all)" configuration option to log state
notifications after completed host checks. either only log the
"updates" to new states or log "all" state notifications, even if the
state didn't change. the log messages will be reported to syslog or to
stderr if the daemon is running in foreground mode.
ok claudio@ pyr@
Diffstat (limited to 'usr.sbin/hoststated')
-rw-r--r-- | usr.sbin/hoststated/hce.c | 30 | ||||
-rw-r--r-- | usr.sbin/hoststated/hoststated.conf.5 | 21 | ||||
-rw-r--r-- | usr.sbin/hoststated/hoststated.h | 33 | ||||
-rw-r--r-- | usr.sbin/hoststated/log.c | 62 | ||||
-rw-r--r-- | usr.sbin/hoststated/parse.y | 13 |
5 files changed, 132 insertions, 27 deletions
diff --git a/usr.sbin/hoststated/hce.c b/usr.sbin/hoststated/hce.c index 831be46bffb..4ee1a0135b4 100644 --- a/usr.sbin/hoststated/hce.c +++ b/usr.sbin/hoststated/hce.c @@ -1,4 +1,4 @@ -/* $OpenBSD: hce.c,v 1.14 2007/02/07 14:39:45 reyk Exp $ */ +/* $OpenBSD: hce.c,v 1.15 2007/02/07 15:17:46 reyk Exp $ */ /* * Copyright (c) 2006 Pierre-Yves Ritschard <pyr@spootnik.org> @@ -207,17 +207,43 @@ hce_launch_checks(int fd, short event, void *arg) void hce_notify_done(struct host *host, const char *msg) { + struct table *table; struct ctl_status st; + struct timeval tv_now, tv_dur; + u_long duration; + u_int logopt; st.id = host->id; st.up = host->up; host->flags |= (F_CHECK_SENT|F_CHECK_DONE); if (msg) log_debug("hce_notify_done: %s (%s)", host->name, msg); + if (host->up != host->last_up) { + logopt = HOSTSTATED_OPT_LOGUPDATE; imsg_compose(ibuf_pfe, IMSG_HOST_STATUS, 0, 0, &st, sizeof(st)); - host->last_up = host->up; + } else + logopt = HOSTSTATED_OPT_LOGNOTIFY; + + if ((table = table_find(env, host->tableid)) == NULL) + fatalx("hce_notify_done: invalid table id"); + + if (gettimeofday(&tv_now, NULL)) + fatal("hce_notify_done: gettimeofday"); + timersub(&tv_now, &host->cte.tv_start, &tv_dur); + if (timercmp(&host->cte.tv_start, &tv_dur, >)) + duration = (tv_dur.tv_sec * 1000) + (tv_dur.tv_usec / 1000.0); + else + duration = 0; + + if (env->opts & logopt) { + log_info("host %s, check %s%s (%lums), state %s -> %s", + host->name, table_check(table->check), + (table->flags & F_SSL) ? " use ssl" : "", duration, + host_status(host->last_up), host_status(host->up)); } + + host->last_up = host->up; } void diff --git a/usr.sbin/hoststated/hoststated.conf.5 b/usr.sbin/hoststated/hoststated.conf.5 index 0e5a1638c34..a2c61405d94 100644 --- a/usr.sbin/hoststated/hoststated.conf.5 +++ b/usr.sbin/hoststated/hoststated.conf.5 @@ -1,4 +1,4 @@ -.\" $OpenBSD: hoststated.conf.5,v 1.16 2007/01/29 18:38:15 pyr Exp $ +.\" $OpenBSD: hoststated.conf.5,v 1.17 2007/02/07 15:17:46 reyk Exp $ .\" .\" Copyright (c) 2006 Pierre-Yves Ritschard <pyr@spootnik.org> .\" @@ -91,6 +91,25 @@ Set the interval in seconds at which the hosts will be checked. The default interval is 10 seconds. .Pp .It Xo +.Ic log +.Pq Ic updates Ns \&| Ns Ic all +.Xc +Log state notifications after completed host checks. +Either only log the +.Ic updates +to new states or log +.Ic all +state notifications, even if the state didn't change. +The host state can be +.Ar up +(the health check completed successfully), +.Ar down +(the host is down or didn't match the check criteria), +or +.Ar unknown +(the host is disabled or has not been checked yet). +.Pp +.It Xo .Ic timeout Ar number .Xc Set the global timeout in milliseconds for checks. diff --git a/usr.sbin/hoststated/hoststated.h b/usr.sbin/hoststated/hoststated.h index 5599e1cf569..4d6b450e7d6 100644 --- a/usr.sbin/hoststated/hoststated.h +++ b/usr.sbin/hoststated/hoststated.h @@ -1,4 +1,4 @@ -/* $OpenBSD: hoststated.h,v 1.23 2007/02/07 13:39:58 reyk Exp $ */ +/* $OpenBSD: hoststated.h,v 1.24 2007/02/07 15:17:46 reyk Exp $ */ /* * Copyright (c) 2006 Pierre-Yves Ritschard <pyr@spootnik.org> @@ -185,9 +185,11 @@ struct host { }; TAILQ_HEAD(hostlist, host); -#define HOST_DOWN -1 -#define HOST_UNKNOWN 0 -#define HOST_UP 1 +enum host_status { + HOST_DOWN = -1, + HOST_UNKNOWN = 0, + HOST_UP = 1 +}; struct table { objid_t id; @@ -209,12 +211,14 @@ struct table { }; TAILQ_HEAD(tablelist, table); -#define CHECK_NOCHECK 0 -#define CHECK_ICMP 1 -#define CHECK_TCP 2 -#define CHECK_HTTP_CODE 3 -#define CHECK_HTTP_DIGEST 4 -#define CHECK_SEND_EXPECT 5 +enum table_check { + CHECK_NOCHECK = 0, + CHECK_ICMP = 1, + CHECK_TCP = 2, + CHECK_HTTP_CODE = 3, + CHECK_HTTP_DIGEST = 4, + CHECK_SEND_EXPECT = 5 +}; struct service { objid_t id; @@ -257,8 +261,11 @@ struct hoststated { struct ctl_icmp_event icmp6_recv; }; -#define HOSTSTATED_OPT_VERBOSE 0x01 -#define HOSTSTATED_OPT_NOACTION 0x04 +#define HOSTSTATED_OPT_VERBOSE 0x01 +#define HOSTSTATED_OPT_NOACTION 0x04 +#define HOSTSTATED_OPT_LOGUPDATE 0x08 +#define HOSTSTATED_OPT_LOGNOTIFY 0x10 +#define HOSTSTATED_OPT_LOGALL 0x18 /* initially control.h */ struct { @@ -304,6 +311,8 @@ void log_info(const char *, ...); void log_debug(const char *, ...); void fatal(const char *); void fatalx(const char *); +const char *host_status(enum host_status); +const char *table_check(enum table_check); /* buffer.c */ struct buf *buf_open(size_t); diff --git a/usr.sbin/hoststated/log.c b/usr.sbin/hoststated/log.c index 990f0fcde62..4fc3f35fb8c 100644 --- a/usr.sbin/hoststated/log.c +++ b/usr.sbin/hoststated/log.c @@ -1,4 +1,4 @@ -/* $OpenBSD: log.c,v 1.2 2006/12/16 17:48:27 deraadt Exp $ */ +/* $OpenBSD: log.c,v 1.3 2007/02/07 15:17:46 reyk Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -16,25 +16,31 @@ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include <sys/types.h> +#include <sys/param.h> +#include <sys/queue.h> +#include <sys/socket.h> + +#include <netinet/in_systm.h> +#include <netinet/in.h> +#include <netinet/ip.h> +#include <net/if.h> + #include <errno.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <syslog.h> +#include <event.h> + +#include <openssl/ssl.h> -/* prototypes */ -void log_init(int); -void vlog(int, const char *, va_list); -void log_warn(const char *, ...); -void log_warnx(const char *, ...); -void log_info(const char *, ...); -void log_debug(const char *, ...); -void fatal(const char *); -void fatalx(const char *); +#include "hoststated.h" int debug; +void vlog(int, const char *, va_list); void logit(int, const char *, ...); void @@ -157,3 +163,39 @@ fatalx(const char *emsg) errno = 0; fatal(emsg); } + +const char * +host_status(enum host_status status) +{ + switch (status) { + case HOST_DOWN: + return ("down"); + case HOST_UNKNOWN: + return ("unknown"); + case HOST_UP: + return ("up"); + }; + /* NOTREACHED */ + return ("invalid"); +} + +const char * +table_check(enum table_check check) +{ + switch (check) { + case CHECK_NOCHECK: + return ("none"); + case CHECK_ICMP: + return ("icmp"); + case CHECK_TCP: + return ("tcp"); + case CHECK_HTTP_CODE: + return ("http code"); + case CHECK_HTTP_DIGEST: + return ("http digest"); + case CHECK_SEND_EXPECT: + return ("send expect"); + }; + /* NOTREACHED */ + return ("invalid"); +} diff --git a/usr.sbin/hoststated/parse.y b/usr.sbin/hoststated/parse.y index af8c69d7d35..fe81c4569aa 100644 --- a/usr.sbin/hoststated/parse.y +++ b/usr.sbin/hoststated/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.22 2007/02/07 13:39:58 reyk Exp $ */ +/* $OpenBSD: parse.y,v 1.23 2007/02/07 15:17:46 reyk Exp $ */ /* * Copyright (c) 2006 Pierre-Yves Ritschard <pyr@spootnik.org> @@ -102,10 +102,11 @@ typedef struct { %token TIMEOUT CODE DIGEST PORT TAG INTERFACE %token VIRTUAL IP INTERVAL DISABLE STICKYADDR %token SEND EXPECT NOTHING USE SSL +%token LOG UPDATES ALL %token ERROR %token <v.string> STRING %type <v.string> interface -%type <v.number> number port http_type +%type <v.number> number port http_type loglevel %type <v.host> host %type <v.tv> timeout @@ -201,11 +202,16 @@ sendbuf : NOTHING { ; main : INTERVAL number { conf->interval.tv_sec = $2; } + | LOG loglevel { conf->opts |= $2; } | TIMEOUT timeout { bcopy(&$2, &conf->timeout, sizeof(struct timeval)); } ; +loglevel : UPDATES { $$ = HOSTSTATED_OPT_LOGUPDATE; } + | ALL { $$ = HOSTSTATED_OPT_LOGALL; } + ; + service : SERVICE STRING { struct service *srv; @@ -537,6 +543,7 @@ lookup(char *s) { /* this has to be sorted always */ static const struct keywords keywords[] = { + { "all", ALL }, { "backup", BACKUP }, { "check", CHECK }, { "code", CODE }, @@ -551,6 +558,7 @@ lookup(char *s) { "interface", INTERFACE }, { "interval", INTERVAL }, { "ip", IP }, + { "log", LOG }, { "nothing", NOTHING }, { "port", PORT }, { "real", REAL }, @@ -562,6 +570,7 @@ lookup(char *s) { "tag", TAG }, { "tcp", TCP }, { "timeout", TIMEOUT }, + { "updates", UPDATES }, { "use", USE }, { "virtual", VIRTUAL } }; |