#!/usr/bin/env python2.7
############################################################################
##
## COPYRIGHTHERE
##
############################################################################
#
#%# family=auto
#%# capabilities=autoconf suggest

try:
    from zorpctl import Instances
    have_zorpctl = True
except ImportError:
    have_zorpctl = False

import subprocess
import sys

rss_not_vsz = True


def format_label(name):
    # getting rid of '#0' for backward compatibility
    if name[-2:] == '#0':
        name = name[:-2]
    return name.replace('#', '_') + "_" + ('rss' if rss_not_vsz else 'vsz')


def print_config():
    print 'graph_title Zorp instances ' + ('RSS' if rss_not_vsz else 'VSZ') +\
          ' memory usage'
    print 'graph_args --base 1024'
    print 'graph_vlabel Bytes'
    print 'graph_category Zorp'

    for process_status in Instances.ZorpHandler.pids():
        if process_status.pid > 0:
            print format_label(process_status.name) + ".label " + \
                  process_status.name


def print_values():
    def compute_vm_value(pid):
        scale = {'KB': 1000, 'MB': 1000*1000, 'GB': 1000*1000*1000}
        label = 'Vm' + ('RSS' if rss_not_vsz else 'Size')  # Size = VSZ

        with open('/proc/' + str(pid) + '/status', 'r') as proc_status:
            for line in proc_status.readlines():
                if len(line) > 0 and line.startswith(label):
                    items = line.split()
                    if len(items) > 2:
                        return int(items[1]) * scale[items[2].upper()]

        return 0  # not found

    for process_status in Instances.ZorpHandler.pids():
        if process_status.pid > 0:
            usage = compute_vm_value(process_status.pid)
            if usage > 0:
                print format_label(str(process_status.name)) + ".value " +\
                      str(usage)

def detect_zorp():
    if not have_zorpctl:
        return "No zorpctl library found"
    try:
        zorpctl_status = subprocess.call("zorpctl --status")
        if zorpctl_status != 0:
            return "zorpctl return with an error"
    except OSError:
            return "No zorpctl found"
    return ""

if __name__ == '__main__':
    # check if the launched script's name ends in VSZ
    if __file__ is not None and \
       __file__.upper()[-3:] == 'VSZ':
        rss_not_vsz = False

    if len(sys.argv) > 1 and sys.argv[1] == 'autoconf':
        excuse = detect_zorp()
        if excuse == "":
            print("yes")
        else:
            print("no ({})".format(excuse))
    elif len(sys.argv) > 1 and sys.argv[1] == 'suggest':
        print("rss")
        print("vsz")
    elif have_zorpctl:
        if len(sys.argv) > 1 and sys.argv[1] == 'config':
            print_config()
        else:
            print_values()
    else:
        print("No zoprctl found, the plugin cannot be run")
        sys.exit(1)
