Created
December 11, 2014 17:04
-
-
Save haukurk/5ef80fa47ee60e815ce7 to your computer and use it in GitHub Desktop.
Logger that sends to syslog servers.
This file contains hidden or 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
#!/usr/bin/python | |
# -*- encoding: iso-8859-1 -*- | |
""" | |
Python syslog client. | |
This code is placed in the public domain by the author. | |
Written by Christian Stigen Larsen. | |
This is especially neat for Windows users, who (I think) don't | |
get any syslog module in the default python installation. | |
See RFC3164 for more info -- http://tools.ietf.org/html/rfc3164 | |
Note that if you intend to send messages to remote servers, their | |
syslogd must be started with -r to allow to receive UDP from | |
the network. | |
--------------------------------------------------------------------- | |
usage: logger [-h] [-m MESSAGE] [-l LEVEL] [-H HOST] [-p PORT] | |
optional arguments: | |
-h, --help show this help message and exit | |
-m MESSAGE, --message MESSAGE | |
Body of the log message. | |
-l LEVEL, --level LEVEL | |
Syslog level, info: 6, alert: 1, warning: 4 | |
-H HOST, --host HOST remote syslog server ip. | |
-p PORT, --port PORT remove syslog server port. | |
""" | |
import socket | |
import argparse | |
FACILITY = { | |
'kern': 0, 'user': 1, 'mail': 2, 'daemon': 3, | |
'auth': 4, 'syslog': 5, 'lpr': 6, 'news': 7, | |
'uucp': 8, 'cron': 9, 'authpriv': 10, 'ftp': 11, | |
'local0': 16, 'local1': 17, 'local2': 18, 'local3': 19, | |
'local4': 20, 'local5': 21, 'local6': 22, 'local7': 23, | |
} | |
LEVEL = { | |
'emerg': 0, 'alert':1, 'crit': 2, 'err': 3, | |
'warning': 4, 'notice': 5, 'info': 6, 'debug': 7 | |
} | |
def syslog(message, level=LEVEL['notice'], facility=FACILITY['daemon'], | |
host='localhost', port=514): | |
""" | |
Send syslog UDP packet to given host and port. | |
""" | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
data = '<%d>%s' % (level + facility*8, message) | |
sock.sendto(data, (host, port)) | |
sock.close() | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-m', "--message", help="Body of the log message.") | |
parser.add_argument('-l', "--level", help="Syslog level, info: 6, alert: 1, warning: 4", type=int, default=6) | |
parser.add_argument('-H', "--host", help="remote syslog server ip.", default="localhost") | |
parser.add_argument('-p', "--port", help="remove syslog server port.", type=int, default=514) | |
args = parser.parse_args() | |
if not (args.message): | |
parser.error('No message specified, add --message or -m or go home.') | |
syslog(message=args.message,level=args.level,host=args.host,port=args.port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment