m_syslog.py 8.38 KB
Newer Older
1 2
# -*- Mode: Python; tab-width: 4 -*-

3 4 5
# From: "Samual M. Rushing" <rushing@nightmare.com>
# Date: Mon, 21 Apr 1997 16:10:42 -0500

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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
# ======================================================================
# Copyright 1997 by Sam Rushing
# 
#                         All Rights Reserved
# 
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby
# granted, provided that the above copyright notice appear in all
# copies and that both that copyright notice and this permission
# notice appear in supporting documentation, and that the name of Sam
# Rushing not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission.
# 
# SAM RUSHING DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
# NO EVENT SHALL SAM RUSHING BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# ======================================================================

"""socket interface to unix syslog.
On Unix, there are usually two ways of getting to syslog: via a
local unix-domain socket, or via the TCP service.

Usually "/dev/log" is the unix domain socket.  This may be different
for other systems.

>>> my_client = syslog_client ('/dev/log')

Otherwise, just use the UDP version, port 514.

>>> my_client = syslog_client (('my_log_host', 514))

On win32, you will have to use the UDP version.  Note that
you can use this to log to other hosts (and indeed, multiple
hosts).

This module is not a drop-in replacement for the python
<syslog> extension module - the interface is different.

Usage:

>>> c = syslog_client()
>>> c = syslog_client ('/strange/non_standard_log_location')
>>> c = syslog_client (('other_host.com', 514))
>>> c.log ('testing', facility='local0', priority='debug')

"""

# TODO: support named-pipe syslog.
# [see ftp://sunsite.unc.edu/pub/Linux/system/Daemons/syslog-fifo.tar.z]

# from <linux/sys/syslog.h>:
# ===========================================================================
# priorities/facilities are encoded into a single 32-bit quantity, where the
# bottom 3 bits are the priority (0-7) and the top 28 bits are the facility
# (0-big number).  Both the priorities and the facilities map roughly
# one-to-one to strings in the syslogd(8) source code.  This mapping is
# included in this file.
#
# priorities (these are ordered)

's avatar
committed
71 72 73 74 75 76 77 78
LOG_EMERG       = 0     #  system is unusable 
LOG_ALERT       = 1     #  action must be taken immediately 
LOG_CRIT        = 2     #  critical conditions 
LOG_ERR         = 3     #  error conditions 
LOG_WARNING     = 4     #  warning conditions 
LOG_NOTICE      = 5     #  normal but significant condition 
LOG_INFO        = 6     #  informational 
LOG_DEBUG       = 7     #  debug-level messages 
79 80

#  facility codes 
's avatar
committed
81 82 83 84 85 86 87 88 89 90 91
LOG_KERN        = 0     #  kernel messages 
LOG_USER        = 1     #  random user-level messages 
LOG_MAIL        = 2     #  mail system 
LOG_DAEMON      = 3     #  system daemons 
LOG_AUTH        = 4     #  security/authorization messages 
LOG_SYSLOG      = 5     #  messages generated internally by syslogd 
LOG_LPR         = 6     #  line printer subsystem 
LOG_NEWS        = 7     #  network news subsystem 
LOG_UUCP        = 8     #  UUCP subsystem 
LOG_CRON        = 9     #  clock daemon 
LOG_AUTHPRIV    = 10    #  security/authorization messages (private) 
92 93

#  other codes through 15 reserved for system use 
's avatar
committed
94 95 96 97 98 99 100 101
LOG_LOCAL0      = 16        #  reserved for local use 
LOG_LOCAL1      = 17        #  reserved for local use 
LOG_LOCAL2      = 18        #  reserved for local use 
LOG_LOCAL3      = 19        #  reserved for local use 
LOG_LOCAL4      = 20        #  reserved for local use 
LOG_LOCAL5      = 21        #  reserved for local use 
LOG_LOCAL6      = 22        #  reserved for local use 
LOG_LOCAL7      = 23        #  reserved for local use 
102 103

priority_names = {
's avatar
committed
104 105 106 107 108 109 110 111 112 113 114 115
    "alert":    LOG_ALERT,
    "crit":     LOG_CRIT,
    "debug":    LOG_DEBUG,
    "emerg":    LOG_EMERG,
    "err":      LOG_ERR,
    "error":    LOG_ERR,        #  DEPRECATED 
    "info":     LOG_INFO,
    "notice":   LOG_NOTICE,
    "panic":    LOG_EMERG,      #  DEPRECATED 
    "warn":     LOG_WARNING,        #  DEPRECATED 
    "warning":  LOG_WARNING,
    }
116 117

facility_names = {
's avatar
committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    "auth":     LOG_AUTH,
    "authpriv": LOG_AUTHPRIV,
    "cron":     LOG_CRON,
    "daemon":   LOG_DAEMON,
    "kern":     LOG_KERN,
    "lpr":      LOG_LPR,
    "mail":     LOG_MAIL,
    "news":     LOG_NEWS,
    "security": LOG_AUTH,       #  DEPRECATED 
    "syslog":   LOG_SYSLOG,
    "user":     LOG_USER,
    "uucp":     LOG_UUCP,
    "local0":   LOG_LOCAL0,
    "local1":   LOG_LOCAL1,
    "local2":   LOG_LOCAL2,
    "local3":   LOG_LOCAL3,
    "local4":   LOG_LOCAL4,
    "local5":   LOG_LOCAL5,
    "local6":   LOG_LOCAL6,
    "local7":   LOG_LOCAL7,
    }
139 140 141 142

import socket

class syslog_client:
's avatar
committed
143 144 145 146 147
    def __init__ (self, address='/dev/log'):
        self.address = address
        if type (address) == type(''):
            try: # APUE 13.4.2 specifes /dev/log as datagram socket
                self.socket = socket.socket( socket.AF_UNIX
148
                                                       , socket.SOCK_DGRAM)
's avatar
committed
149 150 151
                self.socket.connect (address)
            except: # older linux may create as stream socket
                self.socket = socket.socket( socket.AF_UNIX
152
                                                       , socket.SOCK_STREAM)
's avatar
committed
153 154 155 156
                self.socket.connect (address)
            self.unix = 1
        else:
            self.socket = socket.socket( socket.AF_INET
157
                                                   , socket.SOCK_DGRAM)
's avatar
committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
            self.unix = 0

    # curious: when talking to the unix-domain '/dev/log' socket, a
    #   zero-terminator seems to be required.  this string is placed
    #   into a class variable so that it can be overridden if
    #   necessary.

    log_format_string = '<%d>%s\000'

    def log (self, message, facility=LOG_USER, priority=LOG_INFO):
        message = self.log_format_string % (
            self.encode_priority (facility, priority),
            message
            )
        if self.unix:
            self.socket.send (message)
        else:
            self.socket.sendto (message, self.address)

    def encode_priority (self, facility, priority):
        if type(facility) == type(''):
            facility = facility_names[facility]
        if type(priority) == type(''):
            priority = priority_names[priority]         
        return (facility<<3) | priority

    def close (self):
        if self.unix:
            self.socket.close()
187

188 189 190
if __name__ == '__main__':
    """
       Unit test for syslog_client.  Set up for the test by:
's avatar
committed
191 192
    
    * tail -f /var/log/allstuf (to see the "normal" log messages).
193 194

        * Running the test_logger.py script with a junk file name (which
's avatar
committed
195 196
      will be opened as a Unix-domain socket). "Custom" log messages
      will go here.
197

's avatar
committed
198
    * Run this script, passing the same junk file name.
199

's avatar
committed
200
    * Check that the "bogus" test throws, and that none of the rest do.
201

's avatar
committed
202
    * Check that the 'default' and 'UDP' messages show up in the tail.
203

's avatar
committed
204 205
    * Check that the 'non-std' message shows up in the test_logger
      console.
206

's avatar
committed
207 208
    * Finally, kill off the tail and test_logger, and clean up the
      socket file.
209 210 211 212 213 214 215 216 217
    """
    import sys, traceback

    if len( sys.argv ) != 2:
       print "Usage: syslog.py localSocketFilename"
       sys.exit()

    def test_client( desc, address=None ):
        try:
's avatar
committed
218 219 220 221
            if address:
                client = syslog_client( address )
            else:
                client = syslog_client()
222
        except:
's avatar
committed
223
            print 'syslog_client() [%s] ctor threw' % desc
224
            traceback.print_exc()
's avatar
committed
225
        return
226 227 228 229 230 231 232 233 234 235 236 237 238

        try:
            client.log( 'testing syslog_client() [%s]' % desc
                      , facility='local0', priority='debug' )
            print 'syslog_client.log() [%s] did not throw' % desc
        except:
            print 'syslog_client.log() [%s] threw' % desc
            traceback.print_exc()

    test_client( 'default' )
    test_client( 'bogus file', '/some/bogus/logsocket' )
    test_client( 'nonstd file', sys.argv[1] )
    test_client( 'UDP',  ('localhost', 514) )