#!python
# Just lists the current host/service counts

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

import omdclient, re, os, socket, sys
from datetime import datetime

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

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

## Text for --help
text = "ack/unack host/service counts to STDOUT"
usage_text = "usage: %prog [options]"

#########################################################################
### Subroutines #########################################################
#########################################################################

#########################################################################
### 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)
    opt, args = p.parse_args()
    argdict = omdclient.parserArgDict(opt)

    try:
        host_ack = omdclient.nagiosReport('host_ack',   argdict)
        host_unack = omdclient.nagiosReport('host_unack', argdict)
        host_total = host_ack + host_unack
        svc_ack = omdclient.nagiosReport('svc_ack',   argdict)
        svc_unack = omdclient.nagiosReport('svc_unack', argdict)
        svc_total = svc_ack + svc_unack

        string = "%10s  Ack  %5d  Unack  %5d  Total  %5d"
        print(string % ('Hosts', len(host_ack), len(host_unack), len(host_total)))
        print(string % ('Services', len(svc_ack), len(svc_unack), len(svc_total)))

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

if __name__ == "__main__":
    main()

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

"""

=head1 NAME

omd-nagios-count - just lists the current nagios report counts

=head1 SYNOPSIS

B<omd-nagios-count>

=head1 USAGE

Lists the count of ack'd and unack'd alerts in nagios.

=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

L<https://docs.checkmk.com/latest/en/rest_api.html>

=head1 AUTHOR

Tim Skirvin <tskirvin@fnal.gov>

=head1 LICENSE + COPYRIGHT

Copyright 2025-2026, 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

"""
