From cc48e301037a7473b6f5da1ca019ad6ac61a1ebe Mon Sep 17 00:00:00 2001 From: dwmalone Date: Tue, 18 Jul 2000 08:56:54 +0000 Subject: Allow logger to send messages directly to a remote syslog. (This only does IPv4 as our syslogd only does IPv4. I dunno if the KAME people have any plans for syslogd). PR: 19821 Submitted by: Nick Hilliard Reviewed by: sheldonh --- usr.bin/logger/logger.1 | 5 ++++ usr.bin/logger/logger.c | 75 +++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 72 insertions(+), 8 deletions(-) (limited to 'usr.bin/logger') diff --git a/usr.bin/logger/logger.1 b/usr.bin/logger/logger.1 index 9062fb3..a5f52a2 100644 --- a/usr.bin/logger/logger.1 +++ b/usr.bin/logger/logger.1 @@ -42,6 +42,7 @@ .Nm .Op Fl is .Op Fl f Ar file +.Op Fl h Ar host .Op Fl p Ar pri .Op Fl t Ar tag .Op Ar message ... @@ -60,6 +61,10 @@ with each line. Log the message to standard error, as well as the system log. .It Fl f Ar file Log the specified file. +.It Fl h Ar host +Send the message to the remote system +.Ar host +instead of logging it locally. .It Fl p Ar pri Enter the message with the specified priority. The priority may be specified numerically or as a ``facility.level'' diff --git a/usr.bin/logger/logger.c b/usr.bin/logger/logger.c index e7be771..6cbba4b 100644 --- a/usr.bin/logger/logger.c +++ b/usr.bin/logger/logger.c @@ -45,8 +45,13 @@ static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ +#include +#include +#include + #include #include +#include #include #include #include @@ -57,6 +62,7 @@ static const char rcsid[] = int decode __P((char *, CODE *)); int pencode __P((char *)); +static void logmessage __P((int, char *, char *)); static void usage __P((void)); /* @@ -71,18 +77,22 @@ main(argc, argv) char *argv[]; { int ch, logflags, pri; - char *tag, buf[1024]; + char *tag, *host, buf[1024]; tag = NULL; + host = NULL; pri = LOG_NOTICE; logflags = 0; unsetenv("TZ"); - while ((ch = getopt(argc, argv, "f:ip:st:")) != -1) + while ((ch = getopt(argc, argv, "f:h:ip:st:")) != -1) switch((char)ch) { case 'f': /* file to log */ if (freopen(optarg, "r", stdin) == NULL) err(1, "%s", optarg); break; + case 'h': /* hostname to deliver to */ + host = optarg; + break; case 'i': /* log process id also */ logflags |= LOG_PID; break; @@ -114,11 +124,11 @@ main(argc, argv) for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) { len = strlen(*argv); if (p + len > endp && p > buf) { - syslog(pri, "%s", buf); + logmessage(pri, host, buf); p = buf; } if (len > sizeof(buf) - 1) - syslog(pri, "%s", *argv++); + logmessage(pri, host, *argv++); else { if (p != buf) *p++ = ' '; @@ -127,14 +137,62 @@ main(argc, argv) } } if (p != buf) - syslog(pri, "%s", buf); + logmessage(pri, host, buf); } else while (fgets(buf, sizeof(buf), stdin) != NULL) - syslog(pri, "%s", buf); + logmessage(pri, host, buf); exit(0); } /* + * Send the message to syslog, either on the local host, or on a remote host + */ +void +logmessage(int pri, char *host, char *buf) +{ + static int sock = -1; + static struct sockaddr_in sin; + char *line; + int len; + + if (host == NULL) { + syslog(pri, "%s", buf); + return; + } + + if (sock == -1) { /* set up socket stuff */ + struct servent *sp; + struct hostent *hp = NULL; + + sin.sin_family = AF_INET; + + if ((sp = getservbyname("syslog", "udp")) == NULL) + warnx ("syslog/udp: unknown service"); /* not fatal */ + sin.sin_port = (sp == NULL ? htons(514) : sp->s_port); + + /* resolve hostname */ + if (!(inet_aton(host, &sin.sin_addr)) && + (hp = gethostbyname(host)) == NULL) + errx(1, "unknown host: %s", host); + if (hp != NULL) + memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr)); + + sock = socket(PF_INET, SOCK_DGRAM, 0); + if (sock < 0) + errx(1, "socket"); + } + + if ((len = asprintf(&line, "<%d>%s", pri, buf)) == -1) + errx(1, "asprintf"); + + if (sendto(sock, line, len, 0, (struct sockaddr *)&sin, sizeof(sin)) + < len) + warnx ("sendmsg"); + + free(line); +} + +/* * Decode a symbolic name to a numeric value */ int @@ -182,7 +240,8 @@ decode(name, codetab) static void usage() { - (void)fprintf(stderr, - "usage: logger [-is] [-f file] [-p pri] [-t tag] [message ...]\n"); + (void)fprintf(stderr, "usage: %s\n", + "logger [-is] [-f file] [-h host] [-p pri] [-t tag] [message ...]" + ); exit(1); } -- cgit v1.1