Skip to content

Commit

Permalink
Merge pull request #9 from ypsun/develop
Browse files Browse the repository at this point in the history
test merge
  • Loading branch information
ypsun committed Aug 29, 2014
2 parents 42d5e6c + c39d424 commit 275a443
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
syslogger
logsend
=========

syslog sender based on logger shell command
the stupid syslog sender
63 changes: 63 additions & 0 deletions logsend
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import argparse
import itertools
import logging, logging.handlers
import time


# local logger
mylogger = logging.getLogger("local")
myformatter = logging.Formatter("%(filename)s: %(levelname)s: %(message)s")
myhandler = logging.StreamHandler()
myhandler.setFormatter(myformatter)
mylogger.addHandler(myhandler)

# argparse
def pri_type(priority):
facility = ["auth", "authpriv", "cron", "daemon", "ftp", "kern", "lpr", "mail", "news", "syslog", "user", "uucp", \
"local0", "local1", "local2", "local3", "local4", "local5", "local6", "local7"]
severity = ["debug", "info", "warning", "error", "critical"]
f, s = priority.split(".")
if f not in facility:
raise argparse.ArgumentTypeError("unknown facility name: %s\nsupported are %s" % (f, facility))
if s not in severity:
raise argparse.ArgumentTypeError("unknown severity name: %s\nsupported are %s" % (s, severity))
return f, s

parser = argparse.ArgumentParser(
description="the stupid syslog sender",
epilog="https://github.com/ypsun/logsend"
)
parser.add_argument("-f", "--eps", type=int, default=1, help="events per second (eps) ")
parser.add_argument("-i", "--file", type=argparse.FileType("r"), help="message by file content")
parser.add_argument("-n", "--number", type=int, metavar="N", default=1, help="send %(metavar)s events, -1 for endless")
parser.add_argument("-p", "--priority", type=pri_type, default="syslog.info", help="syslog priority (facility.severity)")
parser.add_argument("-s", "--server", default="localhost", metavar="SERVER", help="send to syslog server %(metavar)s")
parser.add_argument("-v", "--version", action="version", version="1.0")
parser.add_argument("message", help="message by text")
args = parser.parse_args()


# [ initialization ]
period = 1.0 / args.eps
msg = (line.rstrip() for line in args.file) if args.file else (args.message for i in itertools.count())
runs = itertools.count() if args.file or args.number < 0 else xrange(args.number)
facility, severity = args.priority

# logger to work
logger = logging.getLogger("logsend")
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())
logger.addHandler(logging.handlers.SysLogHandler(address=(args.server, 514), facility=facility))
exec("logsend = logger.%s" % severity)


# [ main ]
try:
for run in runs:
logsend(msg.next())
time.sleep(period)
except StopIteration:
pass

0 comments on commit 275a443

Please sign in to comment.