#!/usr/bin/env python3 # -*- encoding: utf-8 -*- ##################################################################### # (c) 2006-2011 by Sven Velt and team(ix) GmbH, Nuernberg, Germany # # sv@teamix.net # # (c) 2016- by Sven Velt, Germany # # sven-mymonplugins@velt.biz # # # # This file is part of "velt.biz - My Monitoring Plugins" # # a fork of "team(ix) Monitoring Plugins" in 2015 # # URL: https://gogs.velt.biz/velt.biz/MyMonPlugins/ # # # # This file is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published # # by the Free Software Foundation, either version 2 of the License, # # or (at your option) any later version. # # # # This file is distributed in the hope that it will be useful, but # # WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this file. If not, see . # ##################################################################### import fcntl import socket import struct import sys try: from MyMonPlugin import MonitoringPlugin except ImportError: print('==========================') print('AIKS! Python import error!') print('==========================\n') print('Could not find class "MonitoringPlugin"!\n') print('Did you download "%s"' % os.path.basename(sys.argv[0])) print('without "MyMonPlugin/"?\n') print('Please go back to') print('https://gogs.velt.biz/velt.biz/MyMonPlugins and:') print('- get a full archive at http://gogs.velt.biz/velt.biz/MyMonPlugins/releases') print('- or a master snapshot at http://gogs.velt.biz/velt.biz/MyMonPlugins/archive/master.tar.gz\n') sys.exit(127) def get_ipv4_address(iface): sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) return socket.inet_ntoa(fcntl.ioctl(sock.fileno(), 0x8915, struct.pack('256s', bytes(iface[:15], 'utf-8')))[20:24]) plugin = MonitoringPlugin(pluginname='check_iface-dns', tagforstatusline='IFACE-DNS', description='Check interface address vs. DNS', version='0.1') plugin.add_cmdlineoption('-i', '', 'iface', 'Interface to get IP from', default='lo') plugin.add_cmdlineoption('-d', '', 'dns', 'DNS object(s) to check, comma separated list', default='localhost') #plugin.add_cmdlineoption('-4', '', 'v4', 'Use IPv4', action='store_true') #plugin.add_cmdlineoption('-6', '', 'v6', 'Use IPv6', action='store_true') plugin.parse_cmdlineoptions() #if not plugin.options.v4 and not plugin.options.v6: # plugin.options.v4 = True # #if plugin.options.v4: # plugin.verbose(1, 'Using IPv4') # #if plugin.options.v6: # plugin.verbose(1, 'Using IPv6') # Get IP from interface try: ip_iface = get_ipv4_address(plugin.options.iface) except IOError as xxx_todo_changeme: (errno, strerror) = xxx_todo_changeme.args if errno == 19: plugin.back2nagios(2, 'Interface "%s" does not exist!' % plugin.options.iface) elif errno == 99: plugin.back2nagios(2, 'Interface "%s" has no IP!' % plugin.options.iface) plugin.back2nagios(2, 'I/O error(%s): %s, interface "%s"' % (errno, strerror, plugin.options.iface)) plugin.verbose(1, 'Found IP "%s" on interface "%s"' % (ip_iface, plugin.options.iface)) # Get IP(s) from DNS if not ',' in plugin.options.dns: # Only one DNS object try: ip_dns = socket.gethostbyname(plugin.options.dns) except socket.gaierror: ip_dns = None plugin.back2nagios(1, 'Could not find "%s" in DNS!' % plugin.options.dns) if ip_iface == ip_dns: plugin.back2nagios(0, 'Found %s for "%s" in DNS and on interface "%s"' % (ip_dns, plugin.options.dns, plugin.options.iface)) else: plugin.back2nagios(2, 'Found %s for "%s" in DNS, but %s for interface "%s"' % (ip_dns, plugin.options.dns, ip_iface, plugin.options.iface)) else: # Multiple DNS objects plugin.add_returncode(0) failed_dns = [] for dns in plugin.options.dns.split(','): try: ip_dns = socket.gethostbyname(dns) except socket.gaierror: ip_dns = None if ip_dns: plugin.verbose(1, 'Found IP "%s" as DNS object "%s"' % (ip_dns, dns)) else: plugin.verbose(1, 'Did not find IP for "%s"' % dns) if ip_iface != ip_dns: plugin.add_multilineoutput('CRITICAL - "%s" has unexpected IP "%s"' % (dns, ip_dns)) failed_dns.append(dns) else: plugin.add_multilineoutput('OK - "%s" resolves to "%s"' % (dns, ip_dns)) if len(failed_dns) == 0: plugin.add_returncode(0) plugin.add_output('All DNS objects have a correct IP') else: plugin.add_returncode(2) plugin.add_output('Following DNS objects did not resolve as expected: "%s"' % '", "'.join(failed_dns)) plugin.exit()