#!@PYTHON@

# Copyright (C) 2012  Internet Systems Consortium.
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

"""
ISC sysinfo program.

"""

import sys; sys.path.append ('@@PYTHONPATH@@')
import getopt
from isc.sysinfo import *

def usage():
    print("Usage: %s [-h] [-o <output-file>]" % sys.argv[0], \
              file=sys.stderr)
    exit(1)

def write_value(out, fmt, call):
    '''Helper function for standard value writing.
       Writes the result from the call in the given format to out.
       Does not write anything if the result of the call is None.
    '''
    value = call()
    if value is not None:
        out.write(fmt % value)

def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "o:h", \
                                       ["output", "help"])
    except getopt.GetoptError as e:
        print(str(e))
        usage()
        exit(1)

    output_filename = None
    for option, arg in opts:
        if option in ("-o", "--output"):
            output_filename = arg
        elif option in ("-h", "--help"):
            usage()
        else:
            assert False, "unhandled option"

    if output_filename is None:
        f = sys.stdout
    else:
        f = open(output_filename, 'w')

    s = SysInfoFromFactory()

    f.write('ISC Sysinfo tool\n')
    f.write('================\n')

    f.write('\nCPU\n');
    write_value(f, ' + Number of processors: %d\n', s.get_num_processors)
    write_value(f, ' + Endianness: %s\n', s.get_endianness)

    f.write('\nPlatform\n');
    write_value(f, ' + Operating system: %s\n', s.get_platform_name)
    write_value(f, ' + Distribution: %s\n', s.get_platform_distro)
    write_value(f, ' + Kernel version: %s\n', s.get_platform_version)

    if s.get_platform_is_smp() is not None:
        f.write(' + SMP kernel: ')
        if s.get_platform_is_smp():
            f.write('yes')
        else:
            f.write('no')
        f.write('\n')

    write_value(f, ' + Machine name: %s\n', s.get_platform_machine)
    write_value(f, ' + Hostname: %s\n', s.get_platform_hostname)
    write_value(f, ' + Uptime: %d seconds', s.get_uptime)
    write_value(f, ' (%s)\n', s.get_uptime_desc)

    write_value(f, ' + Loadavg: %f %f %f\n', s.get_loadavg)

    f.write('\nMemory\n');
    write_value(f, ' + Total: %d bytes\n', s.get_mem_total)
    write_value(f, ' + Free: %d bytes\n', s.get_mem_free)
    write_value(f, ' + Cached: %d bytes\n', s.get_mem_cached)
    write_value(f, ' + Buffers: %d bytes\n', s.get_mem_buffers)
    write_value(f, ' + Swap total: %d bytes\n', s.get_mem_swap_total)
    write_value(f, ' + Swap free: %d bytes\n', s.get_mem_swap_free)

    f.write('\n\nNetwork\n');
    f.write('-------\n\n');

    f.write('Interfaces\n')
    f.write('~~~~~~~~~~\n\n')

    write_value(f, '%s', s.get_net_interfaces)

    f.write('\nRouting table\n')
    f.write('~~~~~~~~~~~~~\n\n')
    write_value(f, '%s', s.get_net_routing_table)

    f.write('\nStatistics\n')
    f.write('~~~~~~~~~~\n\n')
    write_value(f, '%s', s.get_net_stats)

    f.write('\nConnections\n')
    f.write('~~~~~~~~~~~\n\n')
    write_value(f, '%s', s.get_net_connections)

    try:
        if os.getuid() != 0:
            sys.stderr.write('\n')
            sys.stderr.write('NOTE: You have to run this program as the root user so that it can\n')
            sys.stderr.write('      collect all the information it requires. Some information is\n')
            sys.stderr.write('      only available to the root user.\n\n')
    except Exception:
        pass

    if f != sys.stdout:
        f.close()

if __name__ == '__main__':
    main()
