Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. #
  5. # Parse arpwatch log file and store into MySQL database
  6. # Copyright (C) 2013 Stjepan Groš <stjepan.gros@gmail.com>
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. #
  22. # Version 20130312
  23. # - Initial version
  24.  
  25. import sys
  26. from datetime import datetime^
  27. import MySQLdb
  28. from socket import inet_aton
  29. import struct
  30. import ConfigParser
  31.  
  32. CONFIGFILE="arpwatchlog2sql.conf"
  33.  
  34. def normalizemac(mac):
  35. digitsStr = mac.split(':')
  36. digits = [ int(x, 16) for x in digitsStr ]
  37. digitsStr = "%02x:%02x:%02x:%02x:%02x:%02x" % tuple(digits)
  38. return digitsStr
  39.  
  40. def ip2int(ip):
  41. nums = ip.split('.')
  42. return int(nums[0])*256**3 + int(nums[1])*256**2 + int(nums[2])*256 + int(nums[3])
  43.  
  44. def main(args):
  45.  
  46. config = ConfigParser.RawConfigParser()
  47. config.read(CONFIGFILE)
  48.  
  49. LOGFILE = config.get("Main", "LOGFILE")
  50. STATEFILE = config.get("Main", "STATEFILE")
  51.  
  52. dbHost = config.get("Main", "dbHost")
  53. dbUser = config.get("Main", "dbUser")
  54. dbPass = config.get("Main", "dbPass")
  55. dbName = config.get("Main", "dbName")
  56.  
  57. lastline = 0
  58. try:
  59. # Read state file
  60. statefile = open(STATEFILE, 'r')
  61. lastline = int(statefile.readline())
  62. statefile.close()
  63. except IOError as e:
  64. if e.errno != 2:
  65. raise
  66.  
  67. try:
  68. logfile = open(LOGFILE, 'r')
  69. except IOError as e:
  70. if e.errno != 2:
  71. raise
  72.  
  73. print "Log file", LOGFILE, "not found. Exiting!"
  74. sys.exit(1)
  75.  
  76. # Check if the log file was rotated, if so, reset counter
  77. logfile.seek(0, 2)
  78. size = logfile.tell()
  79.  
  80. if size < lastline:
  81. lastline = 0
  82.  
  83. # Connect to the database...
  84. conn = MySQLdb.connect (dbHost, dbUser, dbPass, dbName)
  85. cursor = conn.cursor ()
  86.  
  87. logfile.seek(lastline)
  88.  
  89. for log in logfile:
  90. timestamp = datetime.strptime(log[:15], "%b %d %H:%M:%S")
  91. timestamp = timestamp.replace(year = datetime.now().year)
  92.  
  93. fields = log.split()
  94.  
  95. if fields[5] == 'bogon' and len(fields) == 8:
  96. # This is bogon
  97. cursor.execute("INSERT INTO arpwatch VALUES('%s', %d, '%s', '%s', NULL)" % (normalizemac(fields[7]), ip2int(fields[6]), "bogon", timestamp))
  98. elif fields[5] == 'new' and len(fields) == 9:
  99. cursor.execute("INSERT INTO arpwatch VALUES('%s', %d, '%s', '%s', NULL)" % (normalizemac(fields[8]), ip2int(fields[7]), "new", timestamp))
  100. elif fields[5] == 'flip' and len(fields) == 10:
  101. cursor.execute("INSERT INTO arpwatch VALUES('%s', %d, '%s', '%s', '%s')" % (normalizemac(fields[8]), ip2int(fields[7]), "flipflop", timestamp, normalizemac(fields[9][1:-1])))
  102. elif fields[5] == 'Running' and len(fields) == 9:
  103. pass
  104. elif fields[5] == 'listening' and len(fields) == 8:
  105. pass
  106. elif fields[5] == 'report:' and len(fields) == 9:
  107. pass
  108. elif fields[5] == 'short' and len(fields) == 8:
  109. print "WARNING: ARP request or response received was too short at " + str(timestamp)
  110. elif fields[5] == 'changed' and len(fields) == 11:
  111. cursor.execute("INSERT INTO arpwatch VALUES('%s', %d, '%s', '%s', '%s')" % (normalizemac(fields[9]), ip2int(fields[8]), "changed", timestamp, normalizemac(fields[10][1:-1])))
  112. else:
  113. print "ERROR: Unrecognized log line: ", log
  114.  
  115. lastline = logfile.tell()
  116. logfile.close()
  117.  
  118. cursor.close ()
  119. conn.close ()
  120.  
  121. # Write new state file
  122. statefile = open(STATEFILE, 'w')
  123. statefile.write(str(lastline))
  124. statefile.close()
  125.  
  126. if __name__ == '__main__':
  127. main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement