check_iface-dns.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/python
  2. from monitoringplugin import MonitoringPlugin
  3. import fcntl
  4. import socket
  5. import struct
  6. def get_ipv4_address(iface):
  7. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  8. return socket.inet_ntoa(fcntl.ioctl(sock.fileno(), 0x8915, struct.pack('256s', iface[:15]))[20:24])
  9. plugin = MonitoringPlugin(pluginname='check_iface-dns', tagforstatusline='IFACE-DNS', description='Check interface address vs. DNS', version='0.1')
  10. plugin.add_cmdlineoption('-i', '', 'iface', 'Interface to get IP from', default='lo')
  11. plugin.add_cmdlineoption('-d', '', 'dns', 'DNS object(s) to check, comma separated list', default='localhost')
  12. #plugin.add_cmdlineoption('-4', '', 'v4', 'Use IPv4', action='store_true')
  13. #plugin.add_cmdlineoption('-6', '', 'v6', 'Use IPv6', action='store_true')
  14. plugin.parse_cmdlineoptions()
  15. #if not plugin.options.v4 and not plugin.options.v6:
  16. # plugin.options.v4 = True
  17. #
  18. #if plugin.options.v4:
  19. # plugin.verbose(1, 'Using IPv4')
  20. #
  21. #if plugin.options.v6:
  22. # plugin.verbose(1, 'Using IPv6')
  23. # Get IP from interface
  24. try:
  25. ip_iface = get_ipv4_address(plugin.options.iface)
  26. except IOError, (errno, strerror):
  27. if errno == 19:
  28. plugin.back2nagios(2, 'Interface "%s" does not exist!' % plugin.options.iface)
  29. elif errno == 99:
  30. plugin.back2nagios(2, 'Interface "%s" has no IP!' % plugin.options.iface)
  31. plugin.back2nagios(2, 'I/O error(%s): %s, interface "%s"' % (errno, strerror, plugin.options.iface))
  32. plugin.verbose(1, 'Found IP "%s" on interface "%s"' % (ip_iface, plugin.options.iface))
  33. # Get IP(s) from DNS
  34. if not ',' in plugin.options.dns:
  35. # Only one DNS object
  36. try:
  37. ip_dns = socket.gethostbyname(plugin.options.dns)
  38. except socket.gaierror:
  39. ip_dns = None
  40. plugin.back2nagios(1, 'Could not find "%s" in DNS!' % plugin.options.dns)
  41. if ip_iface == ip_dns:
  42. plugin.back2nagios(0, 'Found %s for "%s" in DNS and on interface "%s"' % (ip_dns, plugin.options.dns, plugin.options.iface))
  43. else:
  44. plugin.back2nagios(2, 'Found %s for "%s" in DNS, but %s for interface "%s"' % (ip_dns, plugin.options.dns, ip_iface, plugin.options.iface))
  45. else:
  46. # Multiple DNS objects
  47. plugin.add_returncode(0)
  48. failed_dns = []
  49. for dns in plugin.options.dns.split(','):
  50. try:
  51. ip_dns = socket.gethostbyname(dns)
  52. except socket.gaierror:
  53. ip_dns = None
  54. if ip_dns:
  55. plugin.verbose(1, 'Found IP "%s" as DNS object "%s"' % (ip_dns, dns))
  56. else:
  57. plugin.verbose(1, 'Did not find IP for "%s"' % dns)
  58. if ip_iface != ip_dns:
  59. plugin.add_multilineoutput('CRITICAL - "%s" has unexpected IP "%s"' % (dns, ip_dns))
  60. failed_dns.append(dns)
  61. else:
  62. plugin.add_multilineoutput('OK - "%s" resolves to "%s"' % (dns, ip_dns))
  63. if len(failed_dns) == 0:
  64. plugin.add_returncode(0)
  65. plugin.add_output('All DNS objects have a correct IP')
  66. else:
  67. plugin.add_returncode(2)
  68. plugin.add_output('Following DNS objects did not resolve as expected: "%s"' % '", "'.join(failed_dns))
  69. plugin.exit()