#!/usr/bin/python
import logging
import optparse
import sys
import time

from isi.nfs.nfsstat.formatter import *
import isi.nfs.op_codes as op_codes
import isi.nfs.rpc_client as rpc_client
from isi.nfs.nfsstat.stats import get_server_stats
from isi.nfs.rpc_client import NFSMGMT_STAT

def atLeastTwoOfThree(a, b, c):
    return (a and(b or c)) or (b and c)

def atLeastTwoOfFour(a, b, c, d):
    if a and b: return True
    elif a and c: return True
    elif a and d: return True
    else: return atLeastTwoOfThree(b, c, d)

def parse_opnum(opts, parser, text):
    """Turn a string containing either the name or opnum of an nfs/nlm
    operation into the operation number"""
    v3_opnum = v4_opnum = nlm_opnum = None

    try:
        v3_opnum = op_codes.str_to_opnum('nfs3', text)
    except ValueError:
        if opts.nfs3_only:
            parser.error("NFS v3 has no such operation '%s'" %text)

    try:
        v4_opnum = op_codes.str_to_opnum('nfs4', text)
    except ValueError:
        if opts.nfs4_only:
            parser.error("NFS v4 has no such operation '%s'" %text)

    try:
        nlm_opnum = op_codes.str_to_opnum('nlm', text)
    except ValueError:
        if opts.nlm_only:
            parser.error("NLM has no such operation '%s'" %text)

    if v3_opnum is None and v4_opnum is None and nlm_opnum is None:
        parser.error("Invalid operation '%s'" %text)

    return v3_opnum, v4_opnum, nlm_opnum

def parse_histogram_opts(opts, parser):
    if opts.histogram is not None and opts.global_only:
        parser.error("Incompatible arguments: --histogram and --global-only")

    opts.v3_opnum, opts.v4_opnum = parse_opnum(opts, parser, opts.histogram)

    return opts

def parse_opnum_list(opts, args, parser):
    opnums = []

    if len(args) is 1 and args[0].lower() == 'all':
        args = set()
        if opts.show_v3:
            for op in op_codes.V3_OP_NAMES:
                args.add(op[0])
        if opts.show_v4:
            for op in op_codes.V4_OP_NAMES:
                args.add(op[0])
        if opts.show_nlm:
            for op in op_codes.NLM_OP_NAMES:
                args.add(op[0])

        args = sorted(args)

    for op in args:
        opnums.append(parse_opnum(opts, parser, op))

    opts.percent = False

    return opnums

def get_arguments():
    parser = optparse.OptionParser(description="Display NFS statistics")

    parser.add_option('-3', '--nfs3-only', action='store_true',
            help='Show only NFSv3 statistics')
    parser.add_option('-4', '--nfs4-only', action='store_true',
            help='Show only NFSv4 statistics')
    parser.add_option('--nlm-only', action='store_true',
            help='show only NLM statistics')
    parser.add_option('-g', '--global-only', action='store_true',
            help='Show only global NFS server statistics')
    parser.add_option('-r', '--reset', action='store_true', default=False,
            help='Reset counters')
    parser.add_option('-u', '--human-readable', action='store_true',
            default=False, help="Don't show human-readable values")
    parser.add_option('-p', '--percent', action='store_true', default=False,
            help="Don't show percentages")
    parser.add_option('-w', '--interval', action='store', type='float',
            metavar='TIME [OP ...]',
            help='Show a brief summary of activity every TIME seconds')
    parser.add_option('-e', '--errors', action='store_true', default=False,
            metavar='[OP ...]',
            help='Instead of basic statistics, show a table of error counts '
            'for the given operation(s)')
    parser.add_option('--no-zeros', action='store_true', default=False,
            help='With --error, only show non-zero error counts')
    parser.add_option('-k', '--cache', action='store_true', default=False,
            help='Instead of basic statistics, show statistics for the '
            'caches maintained by the NFS server')

    """
    parser.add_option('-t', '--time-hist', action='store', type='string',
            default=None, metavar='OP',
            help='Instead of basic statistics, show a server response-'
            'time histogram for the given operation.')
    """

    parser.add_option('-d', '--debug', action='store_true', default=False,
            help=optparse.SUPPRESS_HELP)
    parser.add_option('--check-cluster', action='store_true',
            help=optparse.SUPPRESS_HELP)

    (opts, args) = parser.parse_args()

    # XXX TEMP - remove when time histograms go in
    opts.time_hist = None

    if opts.global_only:
        opts.server_only = True

    if atLeastTwoOfThree(opts.errors, opts.time_hist, opts.cache):
        parser.error("Incompatible arguments: --cache, --errors, --time-hist")

    if atLeastTwoOfFour(opts.global_only, opts.nfs3_only, opts.nfs4_only, opts.nlm_only):
        parser.error("Incompatible arguments: --nfs3-only, --nfs4-only, --global-only --nlm-only")

    if opts.no_zeros and not opts.errors:
        parser.error("--no-zeros can only be specified with --error")

    if opts.errors and len(args) == 0:
        args.append('all')

    opts.show_globals = not (opts.nfs3_only or opts.nfs4_only or opts.nlm_only)
    opts.show_v3 = not (opts.nfs4_only or opts.nlm_only or opts.global_only)
    opts.show_v4 = not (opts.nfs3_only or opts.nlm_only or opts.global_only)
    opts.show_nlm = not (opts.nfs3_only or opts.nfs4_only or opts.global_only)

    if opts.interval or opts.errors:
        args = parse_opnum_list(opts, args, parser)

    if opts.time_hist:
        opts.time_hist = parse_opnum(opts, parser, opts.time_hist)

    return (opts, args)

def build_formatter(opts):
    if opts.check_cluster:
        formatter = CheckClusterFormatter()
    else:
        formatter = TerminalFormatter(percent=opts.percent, col_width=15,
                human_readable=opts.human_readable)
    return formatter

def nfsstat_interval(opts, opnums):
    try:
        if len(opnums) == 0:
            # Build the default list of operations
            headers = ['Getattr', 'Lookup', 'Readlink', 'Read',
                    'Write', 'Rename', 'Access', 'Readdir']
            for name in headers:
                v3_opnum = v4_opnum = None
                if name in op_codes.V3_SHORT_NAMES:
                    v3_opnum = op_codes.V3_SHORT_NAMES.index(name)
                if name in op_codes.V4_SHORT_NAMES:
                    v4_opnum = op_codes.V4_SHORT_NAMES.index(name)
                opnums.append((v3_opnum, v4_opnum))

        else:
            headers = []
            for op in opnums:
                if op[0] is not None and op[0] < len(op_codes.V3_SHORT_NAMES):
                    headers.append(op_codes.V3_SHORT_NAMES[op[0]])

                elif op[1] is not None and op[1] < len(op_codes.V4_SHORT_NAMES):
                    headers.append(op_codes.V4_SHORT_NAMES[op[1]])

                else:
                    raise ValueError('Got bad opnum tuple:', op)

        formatter = IntervalFormatter(
                headers=headers,
                col_width=8,
                percent=opts.percent,
                hr=opts.human_readable)

        logging.debug("About to contact server")
        server_conn = rpc_client.NfsMgmtRpcClient()

        old_data = None
        while True: # Break on Ctrl+C
            data = {}
            differences = {}

            def extract_opcounts(op_index, opcounts):
                row = []
                for op in opnums:
                    row.append(opcounts[op[op_index]])
                return row

            server_stats = get_server_stats(server_conn,
                    stat_type=NFSMGMT_STAT.BASIC, reset=False)

            if opts.show_v3:
                data['Srv v3'] = extract_opcounts(0, server_stats.v3_procs)

            if opts.show_v4:
                data['Srv v4'] = extract_opcounts(1, server_stats.v4_procs)

            if old_data:
                for key in data:
                    differences[key] = []
                    for i in range(0, len(data[key])):
                        if data[key][i] is not None:
                            differences[key].append(
                                    max((data[key][i] - old_data[key][i]), 0))
                        else:
                            differences[key].append('-')

                formatter.display(differences)

            old_data = data
            time.sleep(opts.interval)

    except KeyboardInterrupt:
        pass

    return 0

def nfsstat_errors(opts, args):
    logging.debug("About to contact server")
    server_conn = rpc_client.NfsMgmtRpcClient()

    formatter = build_formatter(opts)

    v3_ops = [op[0] for op in args if op[0] != None]
    if opts.show_v3 and len(v3_ops) > 0:
        v3_errors = get_server_stats(server_conn,
                stat_type=NFSMGMT_STAT.V3_ERRORS, reset=opts.reset)
        v3_errors.set_formatter(formatter)
        v3_errors.print_all(v3_ops, not opts.no_zeros)

    v4_ops = [op[1] for op in args if op[1] != None]
    if opts.show_v4 and len(v4_ops) > 0:

        v4_errors = get_server_stats(server_conn,
                stat_type=NFSMGMT_STAT.V4_ERRORS, reset=opts.reset)
        v4_errors.set_formatter(formatter)
        v4_errors.print_all(v4_ops, not opts.no_zeros)

    nlm_ops = [op[2] for op in args if op[2] != None]
    if opts.show_nlm and len(nlm_ops) > 0:

        nlm_errors = get_server_stats(server_conn,
                stat_type=NFSMGMT_STAT.NLM_ERRORS, reset=opts.reset)
        nlm_errors.set_formatter(formatter)
        nlm_errors.print_all(nlm_ops, not opts.no_zeros)

    return 0

def nfsstat_time_hist(opts):
    logging.debug("About to contact server")
    server_conn = rpc_client.NfsMgmtRpcClient()

    formatter = build_formatter(opts)
    time_histogram = get_server_stats(server_conn,
            stat_type=NFSMGMT_STAT.OPTIME, reset=opts.reset)
    time_histogram.set_formatter(formatter)
    time_histogram.print_all(opts.time_hist[0], opts.time_hist[1])

    return 0

def nfsstat_cache(opts):
    opts.percent = False

    logging.debug("About to contact server")
    server_conn = rpc_client.NfsMgmtRpcClient()

    formatter = build_formatter(opts)
    cache_stats = get_server_stats(server_conn,
            stat_type=NFSMGMT_STAT.CACHE, reset=opts.reset)
    cache_stats.set_formatter(formatter)
    cache_stats.print_all()

def nfsstat_basic(opts):
    # Get server info
    logging.debug("About to contact server")
    server_conn = rpc_client.NfsMgmtRpcClient()

    formatter = build_formatter(opts)

    # Do queries
    logging.debug("Calling get_stats with reset as %s", opts.reset)
    server_stats = get_server_stats(server_conn,
            stat_type=NFSMGMT_STAT.BASIC, reset=opts.reset)
    server_stats.set_formatter(formatter)
    logging.debug("Finished collecting server")

    # Print stats
    if opts.show_globals:
        server_stats.print_globals()
        formatter.newline()

        # Display some legacy info (for check_cluster.pl)
        if opts.check_cluster:
            formatter.display(["Server: # of current clients:"],
                    [(server_stats.v3_counters.clients +
                        server_stats.v4_counters.clients)])
            formatter.display(["Server: Running total of client connections:"],
                    [(server_stats.v3_counters.totalclients +
                        server_stats.v4_counters.totalclients)])
            formatter.display(["Server: # of clients waiting to be cleaned up:"],
                    [0])
            formatter.newline()

    if opts.show_v3:
        server_stats.print_v3()
        if opts.show_v4 or opts.show_nlm:
            formatter.newline()
    if opts.show_v4:
        server_stats.print_v4()
        if opts.show_nlm:
            formatter.newline()
    if opts.show_nlm:
        formatter.newline()
        server_stats.print_nlm()

    return 0

def main():
    (opts, args) = get_arguments()
    if opts.debug:
        logging.basicConfig(format='%(levelname)s:%(message)s',
                level=logging.DEBUG)
    else:
        logging.basicConfig(format='%(levelname)s:%(message)s')

    try:
        if opts.interval:
            return nfsstat_interval(opts, args)

        elif opts.errors:
            return nfsstat_errors(opts, args)

        elif opts.time_hist:
            return nfsstat_time_hist(opts)

        elif opts.cache:
            return nfsstat_cache(opts)

        else:
            return nfsstat_basic(opts)

    except KeyboardInterrupt:
        print "Interrupted"
        return 130

    except Exception as e:
        # TODO: handle exceptions in other threads
        err_msg = str(e) or repr(e)
        sys.stderr.write(err_msg + "\n")

        if opts.debug:
            raise

        return 1

    return 0

if __name__ == '__main__':
    sys.exit(main())
