糖果实验室杂货铺

Candy Lab

一个适用于Windows和类Unix的Syslog客户端的Python版

2 months ago 0

一个适用于Windows和类Unix的Syslog客户端的Python版

By Christian Stigen Larsen Posted 14 Nov 2008 — updated 03 Dec 2013

While the Python standard library offers a syslog module, it seem to be a wrapper around the POSIX syslog system calls. This means you cannot use it to send syslog messages over the network.

The code below implements a send function as described in RFC 3164. It has been used in production on Windows boxes sending messages to a Linux syslog server.

For this to work you must configure your syslog daemon to accept logs from the network.

""" Remote syslog client.

Works by sending UDP messages to a remote syslog server. The remote server must be configured to accept logs from the network.

License: PUBLIC DOMAIN Author: Christian Stigen Larsen

For more information, see RFC 3164. """

None
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import socket

class Facility:
  "Syslog facilities"
  KERN, USER, MAIL, DAEMON, AUTH, SYSLOG, \
  LPR, NEWS, UUCP, CRON, AUTHPRIV, FTP = range(12)

  LOCAL0, LOCAL1, LOCAL2, LOCAL3, \
  LOCAL4, LOCAL5, LOCAL6, LOCAL7 = range(16, 24)

class Level:
  "Syslog levels"
  EMERG, ALERT, CRIT, ERR, \
  WARNING, NOTICE, INFO, DEBUG = range(8)

class Syslog:
  """A syslog client that logs to a remote server.

  Example:
  >>> log = Syslog(host="foobar.example")
  >>> log.send("hello", Level.WARNING)
  """
  def __init__(self,
               host="localhost",
               port=514,
               facility=Facility.DAEMON):
    self.host = host
    self.port = port
    self.facility = facility
    self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

  def send(self, message, level):
    "Send a syslog message to remote host using UDP."
    data = "<%d>%s" % (level + self.facility*8, message)
    self.socket.sendto(data, (self.host, self.port))

  def warn(self, message):
    "Send a syslog warning message."
    self.send(message, Level.WARNING)

  def notice(self, message):
    "Send a syslog notice message."
    self.send(message, Level.NOTICE)

  def error(self, message):
    "Send a syslog error message."
    self.send(message, Level.ERR)

# ... add your own stuff here If you put it in a file syslog_client.py you can use it as a module.

None
1
2
3
>>> import syslog_client
>>> log = syslog_client.Syslog("remote-host-name")
>>> log.send("howdy", syslog_client.WARNING)

You can easily extend the class in several ways. E.g., you may want to add some convenience functions like warn(), etc.

None
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python
# -*- encoding: iso-8859-1 -*-
 
"""
Python syslog client.
 
This code is placed in the public domain bythe 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 pythoninstallation.
 
See RFC3164 for more info --http://tools.ietf.org/html/rfc3164
 
Note that if you intend to send messages toremote servers, their
syslogd must be started with -r to allow toreceive UDP from
the network.
"""
 
import socket
 
# I'm a python novice, so I don't know ofbetter ways to define enums
 
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):
 
       """
       Sendsyslog 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()

示例用法:

None
1
2
from syslog import syslog
syslog('There were zwei peanuts walking downder strasse...', host='brit.ain')

为了让它工作,你必须确保syslog服务接收远程消息。你能通过使用-r选项启动syslogd。 我也喜欢Pyton docstring有多么的好,例如:

$ python Python 2.5.1 (r251:54863, Apr 18 2007,08:51:08) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright","credits" or "license" for more information.

None
1
2
>>> import syslog
>>> help(syslog)

Help on module syslog:

NAME syslog - Python syslog client.

FILE c:\documents and settings\csl\jall\syslog.py

DESCRIPTION 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 getany syslog module in the default python installation.

SeeRFC3164 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 thenetwork.

FUNCTIONS syslog(message, level=5, facility=3, host='localhost', port=514) Send syslog UDP packet to given host and port.

DATA FACILITY = {'auth': 4, 'authpriv': 10, 'cron': 9, 'daemon': 3, 'ftp':... LEVEL = {'alert': 1, 'crit': 2, 'debug': 7, 'emerg': 0, 'err': 3,'inf...


糖果实验室

Openresty中文编程网
IKBC经典机械键盘
机械键盘领券优惠购买

Write a Comment