#!/usr/bin/env python3
"""
List all hosts in Nagios.
"""

#########################################################################
### Declarations ########################################################
#########################################################################

import omdclient, os, sys

#########################################################################
### Configuration #######################################################
#########################################################################

## Central configuration file.
config_file_base = '/etc/omdclient/config.yaml'

## Text for --help
text = "List all hosts in Nagios, using the WATO API"
usage_text = "usage: %prog PROBLEM [options]"

#########################################################################
### main () #############################################################
#########################################################################

def main():
    config_file = os.environ.get('OMDCONFIG', config_file_base)
    try:
        config = omdclient.loadCfg(config_file)
    except Exception as e:
        print("failed to load config: %s" % (e))
        sys.exit(3)

    p = omdclient.generateParser(text, usage_text, config)
    p.add_option('--verbose', dest='verbose', action="store_true",
        default=False, help='print more status information')
    p.add_option('--filter-site', dest='filter_site',
        default=False, help='Filter result by monitored site. Default: disabled')
    opt, args = p.parse_args()

    argdict = omdclient.parserArgDict(opt)

    try:
        if opt.filter_site:
            hosts = omdclient.listHostsFiltered(opt.filter_site, argdict)
        else:
            hosts = omdclient.listHosts(argdict)
        h = []
        for i in hosts[1]:
            if i: h.append(i)
        h.sort()
        for i in h: print(i)

    except Exception as e:
        print("failed: %s" % (e))
        sys.exit(-1)

if __name__ == "__main__":
    main()

#########################################################################
### POD Documentation ###################################################
#########################################################################

"""

=head1 NAME

omd-nagios-hostlist - list all hosts in nagios

=head1 SYNOPSIS

B<omd-nagios-hostlist>

=head1 USAGE

omd-nagios-hostlist pulls the list of monitored hosts from check_mk and
prints them to STDOUT.

=head1 ARGUMENTS

=head2 DEFAULT

=over 4

=item B<--debug>

If set, print debugging information.

=item B<--help>

Print this information and exit.

=item B<--filter-site>

Filter host by site monitored. Default: disabled

=back

=head2 CONNECTION OPTIONS

=over 4

=item B<--apikey> I<key>

Password for the API User.  Default: comes from the configuration file.

=item B<--server> I<server>

Host name of the server.  Default: comes from the configuration file.

=item B<--site> I<site>

Site name within the server.  Default: comes from the configuration file.

=item B<--user> I<user>

API User name.  The user must exist on the server, and be an 'automation
user'.  Default: comes from the configuration file.

=back

=head1 FILES

=over 4

=item F</etc/omdclient/config.yaml>

=back

=head1 SEE ALSO

https://mathias-kettner.de/checkmk_wato_webapi.html

=head1 AUTHOR

Tim Skirvin <tskirvin@fnal.gov>

=head1 LICENSE + COPYRIGHT

Copyright 2017, Fermi National Accelerator Laboratory

This program is free software; you may redistribute it and/or modify it
under the same terms as Perl itself.

=cut

"""
