Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. # Syslog server in Python
  2.  
  3. Very basic Syslog server in Python that accepts messages in RFC3164 and RFC5424 format. This has only been tested
  4. with Rsyslog on Linux and Cisco IOS syslog. Other implementations may send mesages in slightly different format than
  5. the two regular expressions at the top of the script will match. In particular there are apparently many implementations
  6. that deviate from strict RFC3164.
  7.  
  8. ```python
  9. from __future__ import print_function
  10. import re
  11. import socket
  12. import arrow
  13.  
  14. RFC_3164 = re.compile('<(?P<pri>\d+)>\s*(?P<timestamp>\S{3}\s\d{2}\s+\d{2}:\d{2}:\d{2})\s+(?P<host>\S+)\s+(?P<tag>[^\[\s\:]+)\[*(?P<procid>\d*)\]*:*\s+(?P<msg>.*)')
  15. RFC_5424 = re.compile('<(?P<pri>\d+)>(?P<version>\d+)\s+(?P<timestamp>\S+)\s+(?P<host>\S+)\s+(?P<tag>\S+)\s+(?P<procid>\S+)\s+(?P<msgid>\S+)\s+-\s+(?P<msg>.*)')
  16. CISCO_IOS = re.compile('<(?P<pri>\d+)>\d*:\s*\.*(?P<timestamp>\S{3}\s\d{2}\s+\d{2}:\d{2}:\d{2}\.\d{3}):\s+(?P<tag>\S+)\s+(?P<msg>.*)')
  17.  
  18. RFC_3164_TIMEFMT = 'MMM DD HH:mm:ss'
  19. RFC_5424_TIMEFMT = None
  20. CISCO_IOS_TIMEFMT = 'MMM DD HH:mm:ss.SSS'
  21.  
  22.  
  23. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  24. sock.bind(('', 514))
  25.  
  26. SEVERITY = {0: 'emergency',
  27. 1: 'alert',
  28. 2: 'critical',
  29. 3: 'error',
  30. 4: 'warning',
  31. 5: 'notice',
  32. 6: 'info',
  33. 7: 'debug'}
  34.  
  35. FACILITY = {0: 'kernel',
  36. 1: 'user',
  37. 2: 'mail',
  38. 3: 'daemon',
  39. 4: 'auth',
  40. 5: 'syslog',
  41. 6: 'lpr',
  42. 7: 'news',
  43. 8: 'uucp',
  44. 9: 'cron',
  45. 10: 'authpriv',
  46. 11: 'ftp',
  47. 12: 'ntp',
  48. 13: 'security',
  49. 14: 'console',
  50. 15: 'solaris-cron',
  51. 16: 'local0',
  52. 17: 'local1',
  53. 18: 'local2',
  54. 19: 'local3',
  55. 20: 'local4',
  56. 21: 'local5',
  57. 22: 'local6',
  58. 23: 'local7'}
  59.  
  60. while True:
  61.  
  62. data, addr = sock.recvfrom(1500)
  63. NOW = arrow.now()
  64. srcip,sport = addr
  65.  
  66. m = None
  67. for regex,timefmt in ( (RFC_3164,RFC_3164_TIMEFMT),
  68. (RFC_5424, RFC_5424_TIMEFMT),
  69. (CISCO_IOS, CISCO_IOS_TIMEFMT) ):
  70. m = regex.search(data)
  71. if m:
  72. break
  73.  
  74. if not m:
  75. raise ValueError(data)
  76.  
  77. groups = m.groupdict()
  78. pri = int(groups.get('pri', 0))
  79. facility = int(pri/8)
  80. severity = pri - (facility * 8)
  81. facility_name = FACILITY.get(facility, None)
  82. severity_name = SEVERITY.get(severity, None)
  83.  
  84. version = int(groups.get('version', 0))
  85. timestamp = groups.get('timestamp', None)
  86. host = groups.get('host', srcip)
  87. tag = groups.get('tag', None)
  88. procid = groups.get('procid', None)
  89. msgid = groups.get('msgid', None)
  90. msg = groups.get('msg', None)
  91.  
  92. if timefmt:
  93. timestamp = arrow.get(timestamp, timefmt).replace(year=NOW.year)
  94. else:
  95. timestamp = arrow.get(timestamp)
  96.  
  97. # Correct timezone
  98. if timestamp.hour == NOW.hour and timestamp.minute == NOW.minute:
  99. timestamp = timestamp.replace(tzinfo=NOW.tzinfo)
  100. else:
  101. timestamp = timestamp.to(NOW.tzinfo)
  102.  
  103.  
  104. print(facility_name, severity_name, version, timestamp, host, tag, procid, msgid, msg)
  105. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement