Commit 91d241fc authored by Alexandre's avatar Alexandre
Browse files

[monitoring] Verify that module is run as main

parent 7c8f745f
......@@ -191,49 +191,50 @@ def run_default(name, connection, opts):
return print_result(connection, request)
# Main program
me = os.path.basename(sys.argv[0])
if __name__ == '__main__':
me = os.path.basename(sys.argv[0])
url, name = parse_opts_monitoring(me, opts)
url, name = parse_opts_monitoring(me, opts)
# The provided host is indeed a valid IP
# TODO catch ValueError exception if the host is an url as in :
# ./check_doh -H https://doh.bortzmeyer.fr -n afnic.fr
if homer.is_valid_ip_address(opts.host)[0]:
opts.connectTo = opts.host
# The provided host is indeed a valid IP
# TODO catch ValueError exception if the host is an url as in :
# ./check_doh -H https://doh.bortzmeyer.fr -n afnic.fr
if homer.is_valid_ip_address(opts.host)[0]:
opts.connectTo = opts.host
ok = True
if opts.dot and opts.vhostname is not None:
extracheck = opts.vhostname
else:
extracheck = None
try:
if opts.dot:
conn = homer.ConnectionDOT(url, servername=extracheck, connect_to=opts.connectTo,
forceIPv4=opts.forceIPv4, forceIPv6=opts.forceIPv6,
insecure=opts.insecure,
sni=opts.sni, key=opts.key)
ok = True
if opts.dot and opts.vhostname is not None:
extracheck = opts.vhostname
else:
conn = homer.ConnectionDOH(url, servername=extracheck, connect_to=opts.connectTo,
forceIPv4=opts.forceIPv4, forceIPv6=opts.forceIPv6,
insecure=opts.insecure)
except TimeoutError:
error("timeout")
except ConnectionRefusedError:
error("Connection to server refused")
except ValueError:
error("\"%s\" not a name or an IP address" % url)
except socket.gaierror:
error("Could not resolve \"%s\"" % url)
except (homer.ConnectionException, homer.DOHException) as e:
error(e)
if conn.dot and not conn.success:
ok = False
else:
ok = run_default(name, conn, opts)
conn.end()
if ok:
sys.exit(STATE_OK)
else:
sys.exit(STATE_CRITICAL)
extracheck = None
try:
if opts.dot:
conn = homer.ConnectionDOT(url, servername=extracheck, connect_to=opts.connectTo,
forceIPv4=opts.forceIPv4, forceIPv6=opts.forceIPv6,
insecure=opts.insecure,
sni=opts.sni, key=opts.key)
else:
conn = homer.ConnectionDOH(url, servername=extracheck, connect_to=opts.connectTo,
forceIPv4=opts.forceIPv4, forceIPv6=opts.forceIPv6,
insecure=opts.insecure)
except TimeoutError:
error("timeout")
except ConnectionRefusedError:
error("Connection to server refused")
except ValueError:
error("\"%s\" not a name or an IP address" % url)
except socket.gaierror:
error("Could not resolve \"%s\"" % url)
except (homer.ConnectionException, homer.DOHException) as e:
error(e)
if conn.dot and not conn.success:
ok = False
else:
ok = run_default(name, conn, opts)
conn.end()
if ok:
sys.exit(STATE_OK)
else:
sys.exit(STATE_CRITICAL)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment