-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from ypsun/develop
test merge
- Loading branch information
Showing
2 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |