#!/usr/bin/env python3
# Reinventory a given node (or nodes)

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

import omdclient, os, sys

#########################################################################
### Configuration #######################################################
#########################################################################
## Managed via central libraries and /etc/omd_client

config_file_base = '/etc/omdclient/config.yaml'

## Text for --help
text = "Reinventory a given node through the check_mk/WATO interface"
usage_text = "usage: %prog [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('--tabula_rasa', dest='tabula_rasa',
        action="store_true", default=True,
        help="tabula-rasa refresh? (default: %default)")
    opt, args = p.parse_args()
    argdict = omdclient.parserArgDict(opt)
    argdict['tabula_rasa'] = opt.tabula_rasa

    error = 0
    for host in args:
        value, result = omdclient.discoverServicesHost(host, argdict)
        if value is False:
            if result is None: print("%s - unknown error" % host)
            else: print("error on inventory: %s" % result)
            error = 1
        else:
            print("%s - %s" % (host, result))

    sys.exit(error)

if __name__ == "__main__":
    main()

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

"""

=head1 NAME

omd-reinventory - reinventory a given node (or nodes)

=head1 SYNOPSIS

B<omd-reinventory> HOSTNAME [HOSTNAME]

=head1 USAGE

omd-reinventory runs an inventory on a host or list of hosts.

=head1 ARGUMENTS

=over 4

=item B<--debug>

If set, print debugging information.

=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 2016, 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

"""
