[vhffs-dev] [2167] Added VHFFS syslogger!, a tool to redirect standard input to syslog

[ Thread Index | Date Index | More vhffs.org/vhffs-dev Archives ]


Revision: 2167
Author:   gradator
Date:     2012-05-01 02:16:25 +0200 (Tue, 01 May 2012)
Log Message:
-----------
Added VHFFS syslogger!, a tool to redirect standard input to syslog

Modified Paths:
--------------
    trunk/Makefile.am
    trunk/configure.ac

Added Paths:
-----------
    trunk/vhffs-syslogger/
    trunk/vhffs-syslogger/Makefile.am
    trunk/vhffs-syslogger/syslogger.c

Modified: trunk/Makefile.am
===================================================================
--- trunk/Makefile.am	2012-04-30 17:30:44 UTC (rev 2166)
+++ trunk/Makefile.am	2012-05-01 00:16:25 UTC (rev 2167)
@@ -13,7 +13,7 @@
 
 SUBDIRS = $(SUB_VHFFS_FS) vhffs-api vhffs-patches vhffs-backend vhffs-doc vhffs-compat vhffs-intl vhffs-jabber vhffs-listengine \
 	vhffs-cron vhffs-panel vhffs-public vhffs-robots vhffs-shells vhffs-themes vhffs-tools vhffs-irc vhffs-autokill \
-	vhffs-forum vhffs-fssync vhffs-mw vhffs-stsmon
+	vhffs-syslogger vhffs-forum vhffs-fssync vhffs-mw vhffs-stsmon
 
 EXTRA_DIST = config.rpath m4/ChangeLog
 

Modified: trunk/configure.ac
===================================================================
--- trunk/configure.ac	2012-04-30 17:30:44 UTC (rev 2166)
+++ trunk/configure.ac	2012-05-01 00:16:25 UTC (rev 2167)
@@ -301,7 +301,7 @@
 dnl Install extra tools or not ?
 AC_ARG_ENABLE(extra,
     AC_HELP_STRING([--enable-extra],
-        [Enable extra tools (autokill, stsmon, etc.)
+        [Enable extra tools (autokill, syslogger, stsmon, etc.)
             [default=no]]),
         enable_extra=$enableval, enable_extra=no)
 
@@ -388,6 +388,7 @@
 	vhffs-robots/Makefile
 	vhffs-shells/Makefile
 	vhffs-stsmon/Makefile
+	vhffs-syslogger/Makefile
 	vhffs-themes/Makefile
 	vhffs-tools/Makefile
 	vhffs-tools/src/Makefile

Added: trunk/vhffs-syslogger/Makefile.am
===================================================================
--- trunk/vhffs-syslogger/Makefile.am	                        (rev 0)
+++ trunk/vhffs-syslogger/Makefile.am	2012-05-01 00:16:25 UTC (rev 2167)
@@ -0,0 +1,7 @@
+if INSTALL_EXTRA
+
+sysloggerdir=@EXTRADIR@
+syslogger_SOURCES=syslogger.c
+syslogger_PROGRAMS=syslogger
+
+endif

Added: trunk/vhffs-syslogger/syslogger.c
===================================================================
--- trunk/vhffs-syslogger/syslogger.c	                        (rev 0)
+++ trunk/vhffs-syslogger/syslogger.c	2012-05-01 00:16:25 UTC (rev 2167)
@@ -0,0 +1,242 @@
+/*
+ *     syslogger - A program which redirects its standard input to syslog
+ *
+ *  Copyright (C) 2012 Sylvain Rochet
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#define DEFAULT_FACILITY LOG_LOCAL4
+#define DEFAULT_PRIORITY LOG_NOTICE
+#define DEFAULT_OPTION 0
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <getopt.h>
+#include <syslog.h>
+
+#define FALSE 0
+#define TRUE !FALSE
+
+struct syslogger_name_to_int {
+	const char *name;
+	int val;
+};
+
+static void usage_exit(int ret_code, char *progname);
+int syslog_facility_from_str(const char *facility);
+int syslog_priority_from_str(const char *priority);
+int syslog_option_from_str(const char *priority);
+
+int main(int argc, char *argv[])  {
+	char *buffer, *ident = "httpd";
+	int facility = DEFAULT_FACILITY, priority = DEFAULT_PRIORITY, option = DEFAULT_OPTION;
+	size_t bufsiz = 32768;
+
+	struct option long_options[] = {
+		{ "ident", required_argument, NULL, 'i' },
+		{ "size", required_argument, NULL,'s' },
+		{ "facility", required_argument, NULL, 'f' },
+		{ "priority", required_argument, NULL, 'p' },
+		{ "option", required_argument, NULL, 'o' },
+		{ "help", no_argument, NULL, 'h' },
+		{ "version", no_argument, NULL, 'v' },
+		{ 0, 0, 0, 0 }
+	};
+
+	while(1) {
+		int option_index = 0, c;
+		c = getopt_long(argc, argv, "i:s:f:p:o:hv", long_options, &option_index);
+		if(c == -1)
+			break;
+
+		switch(c) {
+			case 'i':
+				ident = strdup(optarg);
+				break;
+
+			case 's':
+				bufsiz = (size_t)(atoi(optarg));
+				break;
+
+			case 'f':
+				facility = syslog_facility_from_str(optarg);
+				if(facility < 0) {
+					fprintf(stderr, "%s is not a valid facility\n\n", optarg);
+					usage_exit(1, argv[0]);
+				}
+				break;
+
+			case 'p':
+				priority = syslog_priority_from_str(optarg);
+				if(priority < 0) {
+					fprintf(stderr, "%s is not a valid priority\n\n", optarg);
+					usage_exit(1, argv[0]);
+				}
+				break;
+
+			case 'o': {
+					char *cur = optarg, *ptr;
+					while(1) {
+						int opt;
+						ptr = strchr(cur, ',');
+						if(ptr) *ptr = '\0';
+						opt = syslog_option_from_str(cur);
+						if(opt < 0) {
+							fprintf(stderr, "%s is not a valid option\n\n", cur);
+							usage_exit(1, argv[0]);
+						}
+						option |= opt;
+						if(!ptr) break;
+						cur = ++ptr;
+					}
+				}
+				break;
+
+			case 'h':
+				usage_exit(0, argv[0]);
+
+			case 'v':
+#ifdef VERSION
+				fputs("syslogger " VERSION "\n", stdout);
+#else
+				fputs("syslogger\n", stdout);
+#endif
+				exit(0);
+
+			case '?':
+				/* `getopt_long' already printed an error message. */
+				fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]);
+				exit(1);
+
+			default:
+				abort();
+		}
+	}
+
+	if(optind != argc)
+		usage_exit(1, argv[0]);
+
+	if(bufsiz < 1024)
+		bufsiz = 1024;
+	buffer = malloc(bufsiz);
+	if(!buffer) {
+		fprintf(stderr, "Failed to allocated buffer, exiting.\n");
+		return -1;
+	}
+
+	openlog(ident, option, facility);
+	while( fgets(buffer, bufsiz, stdin) ) {
+		syslog(priority, buffer);
+	}
+	closelog();
+
+	return 0;
+}
+
+int syslog_facility_from_str(const char *facility) {
+	int i = 0;
+	struct syslogger_name_to_int assoc[] = {
+		{ "authpriv", LOG_AUTHPRIV },
+		{ "cron", LOG_CRON },
+		{ "daemon", LOG_DAEMON },
+		{ "ftp", LOG_FTP },
+		{ "kern", LOG_KERN },
+		{ "local0", LOG_LOCAL0 },
+		{ "local1", LOG_LOCAL1 },
+		{ "local2", LOG_LOCAL2 },
+		{ "local3", LOG_LOCAL3 },
+		{ "local4", LOG_LOCAL4 },
+		{ "local5", LOG_LOCAL5 },
+		{ "local6", LOG_LOCAL6 },
+		{ "local7", LOG_LOCAL7 },
+		{ "lpr", LOG_LPR },
+		{ "mail", LOG_MAIL },
+		{ "news", LOG_NEWS },
+		{ "user", LOG_USER },
+		{ "uucp", LOG_UUCP },
+		{ 0, 0 }
+	};
+
+	while(assoc[i].name) {
+		if(!strcmp(facility, assoc[i].name))
+			return assoc[i].val;
+		i++;
+	}
+	return -1;
+}
+
+int syslog_priority_from_str(const char *priority) {
+	int i = 0;
+	struct syslogger_name_to_int assoc[] = {
+		{ "emerg", LOG_EMERG },
+		{ "alert", LOG_ALERT },
+		{ "crit", LOG_CRIT },
+		{ "err", LOG_ERR },
+		{ "warning", LOG_WARNING },
+		{ "notice", LOG_NOTICE },
+		{ "info", LOG_INFO },
+		{ "debug", LOG_DEBUG },
+		{ 0, 0 }
+	};
+
+	while(assoc[i].name) {
+		if(!strcmp(priority, assoc[i].name))
+			return assoc[i].val;
+		i++;
+	}
+	return -1;
+}
+
+int syslog_option_from_str(const char *priority) {
+	int i = 0;
+	struct syslogger_name_to_int assoc[] = {
+		{ "cons", LOG_CONS },
+		{ "ndelay", LOG_NDELAY },
+		{ "nowait", LOG_NOWAIT },
+		{ "odelay", LOG_ODELAY },
+		{ "perror", LOG_PERROR },
+		{ "pid", LOG_PID },
+		{ 0, 0 }
+	};
+
+	while(assoc[i].name) {
+		if(!strcmp(priority, assoc[i].name))
+			return assoc[i].val;
+		i++;
+	}
+	return -1;
+}
+
+static void usage_exit(int ret_code, char *progname) {
+	printf ("Usage: %s [OPTION]...\n"
+		"A program which redirects its standard input to syslog\n\n"
+		"  -i, --ident=STRING\t\tIdent string to use, defaults to httpd\n"
+		"  -s, --size=BYTES\t\tMaximum size of a message, defaults to 32768 bytes, minimum is 1024\n"
+		"  -f, --facility=STRING\t\tSyslog facility to use, defaults to local4\n"
+		"\t\t\t\tPossible facility are: authpriv, cron, daemon, ftp, kern, local0 through local7, lpr, mail, news, user, uucp\n"
+		"  -p, --priority=STRING\t\tSyslog priority to use, defaults to notice\n"
+		"\t\t\t\tPossible priority are: emerg, alert, crit, err, warning, notice, info, debug\n"
+		"  -o, --option=STRING\t\tA comma separated list of syslog options, defaults to none\n"
+		"\t\t\t\tPossible options are: cons, ndelay, nowait, odelay, perror, pid\n"
+		"  -h, --help\t\t\tDisplay this help and exit\n"
+		"  -v, --version\t\t\tOutput version information and exit\n"
+		"\n"
+		"See syslog(3) for details\n",
+		progname);
+	exit(ret_code);
+}


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/