[PATCH] logger: support for logging to UDP socket / remote syslog server

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I hope you will find the following patch useful.

It adds the ability to logger to log a message to a udp socket.
The -n option followed by the hostname of the remote host is mandatory
to do this.
The optional -P option can be used to change the UDP destination port
(default 514).
The function udpopenlog is used to open the udp socket. After that
everything works in almost the same way like it does when logging to a
UNIX socket.

Signed-off-by: Josef Wuebbels <josef.wuebbels@xxxxxx>

--- a/misc-utils/logger.c	2010-11-30 11:26:31.000000000 +0100
+++ b/misc-utils/logger.c	2011-01-17 07:56:43.194540015 +0100
@@ -47,6 +47,8 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/un.h>
+#include <arpa/inet.h>
+#include <netdb.h>
 #include "nls.h"
 
 #define	SYSLOG_NAMES
@@ -57,6 +59,7 @@
 void	usage __P((void));
 
 static int optd = 0;
+static int udpport = 514;
 
 static int
 myopenlog(const char *sock) {
@@ -83,6 +86,32 @@
        return fd;
 }
 
+static int
+udpopenlog(const char *servername,int port) {
+	int fd;
+	struct sockaddr_in s_addr;
+	struct hostent *serverhost;
+
+	if ((serverhost = gethostbyname(servername)) == NULL ){
+        	printf (_("unable to resolve '%s'\n"), servername);
+                exit(1);
+	}
+
+	if ((fd = socket(AF_INET, SOCK_DGRAM , 0)) == -1) {
+        	printf (_("socket: %s.\n"), strerror(errno));
+        	exit (1);
+	}
+
+	bcopy(serverhost->h_addr,&s_addr.sin_addr,serverhost->h_length);
+        s_addr.sin_family=AF_INET;
+        s_addr.sin_port=htons(port);
+
+        if (connect(fd, (struct sockaddr *) &s_addr, sizeof(s_addr)) ==
-1) {
+        	printf (_("connect: %s.\n"), strerror(errno));
+                exit(1);
+        }
+	return fd;
+}
 static void
 mysyslog(int fd, int logflags, int pri, char *tag, char *msg) {
        char buf[1000], pid[30], *cp, *tp;
@@ -122,6 +151,7 @@
 	int ch, logflags, pri;
 	char *tag, buf[1024];
 	char *usock = NULL;
+	char *udpserver = NULL;
 	int LogSock = -1;
 
 	setlocale(LC_ALL, "");
@@ -131,7 +161,7 @@
 	tag = NULL;
 	pri = LOG_NOTICE;
 	logflags = 0;
-	while ((ch = getopt(argc, argv, "f:ip:st:u:d")) != -1)
+	while ((ch = getopt(argc, argv, "f:ip:st:u:dn:P:")) != -1)
 		switch((char)ch) {
 		case 'f':		/* file to log */
 			if (freopen(optarg, "r", stdin) == NULL) {
@@ -159,6 +189,13 @@
 		case 'd':
 			optd = 1;	/* use datagrams */
 			break;
+		case 'n':		/* udp socket */
+			optd = 1;	/* use datagrams because udp */
+			udpserver = optarg;
+			break;
+		case 'P':		/* change udp port */
+			udpport = atoi(optarg);
+			break;
 		case '?':
 		default:
 			usage();
@@ -167,8 +204,10 @@
 	argv += optind;
 
 	/* setup for logging */
-	if (!usock)
+	if (!usock && !udpserver)
 		openlog(tag ? tag : getlogin(), logflags, 0);
+	else if (udpserver)
+		LogSock = udpopenlog(udpserver,udpport);
 	else
 		LogSock = myopenlog(usock);
 
@@ -182,14 +221,14 @@
 		for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
 			len = strlen(*argv);
 			if (p + len > endp && p > buf) {
-			    if (!usock)
+			    if (!usock && !udpserver)
 				syslog(pri, "%s", buf);
 			    else
 				mysyslog(LogSock, logflags, pri, tag,
buf);
 				p = buf;
 			}
 			if (len > sizeof(buf) - 1) {
-			    if (!usock)
+			    if (!usock && !udpserver)
 				syslog(pri, "%s", *argv++);
 			    else
 				mysyslog(LogSock, logflags, pri, tag,
*argv++);
@@ -201,7 +240,7 @@
 			}
 		}
 		if (p != buf) {
-		    if (!usock)
+		    if (!usock && !udpserver)
 			syslog(pri, "%s", buf);
 		    else
 			mysyslog(LogSock, logflags, pri, tag, buf);
@@ -215,12 +254,12 @@
 		    if (len > 0 && buf[len - 1] == '\n')
 			    buf[len - 1] = '\0';
 
-		    if (!usock)
+		    if (!usock && !udpserver)
 			syslog(pri, "%s", buf);
 		    else
 			mysyslog(LogSock, logflags, pri, tag, buf);
 		}
-	if (!usock)
+	if (!usock && !udpserver)
 		closelog();
 	else
 		close(LogSock);
@@ -282,6 +321,6 @@
 usage()
 {
 	(void)fprintf(stderr,
-	    _("usage: logger [-is] [-f file] [-p pri] [-t tag] [-u
socket] [ message ... ]\n"));
+	    _("usage: logger [-is] [-f file] [-p pri] [-t tag] [-u
socket] [-n udpserver] [-P udpport] [ message ... ]\n"));
 	exit(1);
 }
---

Best regards

Josef Wuebbels

MTU Aero Engines GmbH
Dachauer Str. 665
80995 Muenchen
Germany

http://www.mtu.de

-- 
MTU Aero Engines GmbH
Geschaeftsfuehrung/Board of Management: Egon W. Behle, Vorsitzender/CEO; Dr. Rainer Martens, Dr. Stefan Weingartner, Reiner Winkler
Vorsitzender des Aufsichtsrats/Chairman of the Supervisory Board: Klaus Eberhardt
Sitz der Gesellschaft/Registered Office: Muenchen
Handelsregister/Commercial Register: Muenchen HRB 154230

Diese E-Mail sowie ihre Anhaenge enthalten MTU-eigene vertrauliche oder rechtlich geschuetzte Informationen.
Wenn Sie nicht der beabsichtigte Empfaenger sind, informieren Sie bitte den Absender und loeschen Sie diese E-Mail 
sowie die Anhaenge. Das unbefugte Speichern, Kopieren oder Weiterleiten ist nicht gestattet.
 
This e-mail and any attached documents are proprietary to MTU, confidential or protected by law.
If you are not the intended recipient, please advise the sender and delete this message and its attachments.
Any unauthorised storing, copying or distribution is prohibited.

--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux