Page MenuHomeGRNET

No OneTemporary

File Metadata

Created
Mon, Nov 25, 7:52 PM
diff --git a/ici/__main__.py b/ici/__main__.py
index 11fb5ed..09ed2cc 100644
--- a/ici/__main__.py
+++ b/ici/__main__.py
@@ -1,154 +1,154 @@
import argparse
import socket
import sys
from ici import host_status, downtime, ack
# Hack to set a default subcommand (needed for status).
def set_default_subparser(self, name, args=None):
"""default subparser selection. Call after setup, just before parse_args()
name: is the name of the subparser to call by default
args: if set is the argument list handed to parse_args()
, tested with 2.7, 3.2, 3.3, 3.4
it works with 2.6 assuming argparse is installed
"""
subparser_found = False
for arg in sys.argv[1:]:
if arg in ['-h', '--help']: # global help if no subparser
break
else:
for x in self._subparsers._actions:
if not isinstance(x, argparse._SubParsersAction):
continue
for sp_name in x._name_parser_map.keys():
if sp_name in sys.argv[1:]:
subparser_found = True
if not subparser_found:
# insert default in first position, this implies no
# global options without a sub_parsers specified
if args is None:
sys.argv.insert(1, name)
else:
args.insert(0, name)
argparse.ArgumentParser.set_default_subparser = set_default_subparser
def main():
parser = argparse.ArgumentParser()
# top-level parser.
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help='')
# parser for status subcommand.
parser_a = subparsers.add_parser(
'status',
help='Returns the status of a host'
)
parser_a.set_defaults(function=host_status)
parser_a.add_argument(
"hostname",
help="The fqdn of the host to see it's status",
default=socket.getfqdn().strip(),
nargs='?'
)
parser_a.add_argument(
"-a", "--all",
help="Show the status of OK services too",
action="store_true"
)
# parser for downtime subcommand.
parser_b = subparsers.add_parser(
'downtime',
help='schedules downtime for a host or a group of services'
)
parser_b.set_defaults(function=downtime)
# if a service group is defined then no hostname is needed.
group = parser_b.add_mutually_exclusive_group()
group.add_argument(
"hostname",
help="The fqdn of the host you want to schedule downtime",
default="",
nargs='?'
)
group.add_argument(
"-g", "--service-group",
help="The group of service to be scheduled for downtime",
nargs=1
)
parser_b.add_argument(
"-t", "--downtime",
help="Downtime in seconds",
nargs='?',
default=3600,
type=int
)
parser_b.add_argument(
"-s", "--service",
help="Service Name",
nargs=1
)
# parser for ack subcommand.
parser_c = subparsers.add_parser(
'ack',
help='Acknowledge a problem'
)
parser_c.set_defaults(function=ack)
parser_c.add_argument(
"hostname",
help="The fqdn of the host of the problem to acknowledge",
default=socket.getfqdn().strip(),
nargs='?'
)
parser_c.add_argument(
"-s", "--service",
help="Service Name",
nargs=1
)
parser_c.add_argument(
"-n", "--notify",
help="Notify",
action='store_true'
)
parser_c.add_argument(
"-c", "--comment",
help="Comment to accompany the ack",
nargs=1
)
parser.set_default_subparser('status')
args = parser.parse_args()
# If servicegroup is specified then hostname is irrelevant,
# otherwise if the hostname is not specified get the system hostname
if args.service_group:
hostname = ""
else:
- if not hostname:
+ if not args.hostname:
hostname = socket.getfqdn().strip(),
if args.function == host_status:
host_status(args.hostname, args.all)
elif args.function == downtime:
if args.service_group and args.service:
parser.error("You can either specify a hostname and a service " +
"to downtime or a group of services.\n " +
"-s and -g are mutually exclusive.")
elif args.service:
downtime(args.hostname, args.downtime, args.service[0], 0)
elif args.service_group:
downtime("", args.downtime, args.service_group[0], 2)
else:
downtime(args.hostname, args.downtime, "", 1)
elif args.function == ack:
if args.notify is False:
notify = 0
else:
notify = 1
ack(args.hostname, args.service[0], notify, args.comment[0])
if __name__ == "__main__":
main()

Event Timeline