Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. """
  2. Remote syslog client.
  3. https://csl.name/post/python-syslog-client/
  4. Works by sending UDP messages to a remote syslog server. The remote server
  5. must be configured to accept logs from the network.
  6.  
  7. License: PUBLIC DOMAIN
  8. Author: Christian Stigen Larsen
  9.  
  10. For more information, see RFC 3164.
  11. """
  12.  
  13. import socket
  14.  
  15. class Facility:
  16. "Syslog facilities"
  17. KERN, USER, MAIL, DAEMON, AUTH, SYSLOG, \
  18. LPR, NEWS, UUCP, CRON, AUTHPRIV, FTP = range(12)
  19.  
  20. LOCAL0, LOCAL1, LOCAL2, LOCAL3, \
  21. LOCAL4, LOCAL5, LOCAL6, LOCAL7 = range(16, 24)
  22.  
  23. class Level:
  24. "Syslog levels"
  25. EMERG, ALERT, CRIT, ERR, \
  26. WARNING, NOTICE, INFO, DEBUG = range(8)
  27.  
  28. class Syslog:
  29. """A syslog client that logs to a remote server.
  30.  
  31. Example:
  32. >>> log = Syslog(host="foobar.example")
  33. >>> log.send("hello", Level.WARNING)
  34. """
  35. def __init__(self,
  36. host="localhost",
  37. port=514,
  38. facility=Facility.DAEMON):
  39. self.host = host
  40. self.port = port
  41. self.facility = facility
  42. self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  43.  
  44. def send(self, message, level):
  45. "Send a syslog message to remote host using UDP."
  46. data = "<%d>%s" % (level + self.facility*8, message)
  47. self.socket.sendto(data, (self.host, self.port))
  48.  
  49. def warn(self, message):
  50. "Send a syslog warning message."
  51. self.send(message, Level.WARNING)
  52.  
  53. def notice(self, message):
  54. "Send a syslog notice message."
  55. self.send(message, Level.NOTICE)
  56.  
  57. def error(self, message):
  58. "Send a syslog error message."
  59. self.send(message, Level.ERR)
  60.  
  61. # ... add your own stuff here
  62.  
  63. port = 514
  64. log = Syslog("172.24.106.1")
  65. log.port = 512
  66. log.send("howdy", Level.WARNING)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement