#!/usr/bin/env python
# -*- encoding: utf-8; py-indent-offset: 4 -*-

import httplib, urllib2, pprint, sys, os, getopt, socket
import simplejson as json
import subprocess

# build_apiHeaders function
def build_apiHeaders( sessionKey = "" ):
    headers = {
        "Content-Type": "application/json",
        "Accept": "application/json"
    }

    if sessionKey == "":
        return headers

    headers["X-HP3PAR-WSAPI-SessionKey"] = sessionKey

    return headers
    # end build_apiHeaders function

# get_cred function
def get_cred( sessionHost, sessionUser, sessionPassword, sessionPort = 8080 ):
    credUrl = '/api/v1/credentials'
    credBody = '{"user":"%s","password":"%s"}' % ( sessionUser, sessionPassword )
    apiHeaders = {
        "Content-Type": "application/json",
        "Accept": "application/json"
    }

    conn = httplib.HTTPSConnection(sessionHost, sessionPort, timeout=10)
    conn.request("POST", credUrl, credBody, apiHeaders )
    response = conn.getresponse()
    credResponseData = response.read()
    credResponseStatus = response.status
    credResponseReason = response.reason
    conn.close()

    parsed_response = json.loads( credResponseData )

    responseSessionKey = ""

    if credResponseStatus != 201 or credResponseReason != "Created":
        raise Exception("got no session key")
        return False

    if "key" in parsed_response:
        #print json.dumps(parsed_response, sort_keys=True, indent=4, separators=(',', ': '))
        responseSessionKey = parsed_response["key"]
    else:
        print "******"
        print "%s: %s" % ( "Status", credResponseStatus )
        print "%s: %s" % ( "Reason", credResponseReason )
        print "-----"
        print "No session key received"
        print "******"
        raise Exception("got no session key")
        return False

    return ( responseSessionKey )
   # end get_cred function


def remove_cred( sessionHost, sessionKey, sessionPort = 8080 ):
    apiUrl = '/api/v1/credentials'
    apiUrl = '%s/%s' % ( apiUrl, sessionKey)
    apiHeaders = {
        "Content-Type": "application/json",
        "Accept": "application/json"
    }

    conn = httplib.HTTPSConnection(sessionHost, sessionPort, timeout=10)
    conn.request("DELETE", apiUrl, "", apiHeaders )
    response = conn.getresponse()
    credResponseStatus = response.status
    credResponseReason = response.reason
    conn.close()

    if credResponseStatus != 200 or credResponseReason != "OK":
        print "******"
        print "%s: %s" % ( "URL", apiUrl )
        print "%s: %s" % ( "Status", credResponseStatus )
        print "%s: %s" % ( "Reason", credResponseReason )
        print "******"

def get_system( sessionKey, sessionHost, sessionPort = 8080 ):
    volUrl = '/api/v1/system'
    apiHeaders = build_apiHeaders( sessionKey )

    conn = httplib.HTTPSConnection( sessionHost, sessionPort, timeout=10)
    conn.request("GET", volUrl, "", apiHeaders)
    response = conn.getresponse()
    responseData = response.read()
    responseStatus = response.status
    responseReason = response.reason
    conn.close()

    parsed_response = json.loads( responseData )

    return parsed_response

def get_volumes( sessionKey, sessionHost, sessionPort = 8080 ):
    volUrl = '/api/v1/volumes'
    apiHeaders = build_apiHeaders( sessionKey )

    conn = httplib.HTTPSConnection( sessionHost, sessionPort, timeout=10)
    conn.request("GET", volUrl, "", apiHeaders)
    response = conn.getresponse()
    responseData = response.read()
    responseStatus = response.status
    responseReason = response.reason
    conn.close()

    parsed_response = json.loads( responseData )

    return parsed_response

def get_hosts( sessionKey, sessionHost, sessionPort = 8080 ):
    volUrl = '/api/v1/hosts'
    apiHeaders = build_apiHeaders( sessionKey )

    conn = httplib.HTTPSConnection( sessionHost, sessionPort, timeout=10)
    conn.request("GET", volUrl, "", apiHeaders)
    response = conn.getresponse()
    responseData = response.read()
    responseStatus = response.status
    responseReason = response.reason
    conn.close()

    parsed_response = json.loads( responseData )

    return parsed_response

def get_cpgs( sessionKey, sessionHost, sessionPort = 8080 ):
    volUrl = '/api/v1/cpgs'
    apiHeaders = build_apiHeaders( sessionKey )

    conn = httplib.HTTPSConnection( sessionHost, sessionPort, timeout=10)
    conn.request("GET", volUrl, "", apiHeaders)
    response = conn.getresponse()
    responseData = response.read()
    responseStatus = response.status
    responseReason = response.reason
    conn.close()

    parsed_response = json.loads( responseData )

    return parsed_response

def get_ports( sessionKey, sessionHost, sessionPort = 8080 ):
    volUrl = '/api/v1/ports'
    apiHeaders = build_apiHeaders( sessionKey )

    conn = httplib.HTTPSConnection( sessionHost, sessionPort, timeout=10)
    conn.request("GET", volUrl, "", apiHeaders)
    response = conn.getresponse()
    responseData = response.read()
    responseStatus = response.status
    responseReason = response.reason
    conn.close()

    parsed_response = json.loads( responseData )

    return parsed_response

def get_flashcache( sessionKey, sessionHost, sessionPort = 8080 ):
    volUrl = '/api/v1/flashcache'
    apiHeaders = build_apiHeaders( sessionKey )

    conn = httplib.HTTPSConnection( sessionHost, sessionPort, timeout=10)
    conn.request("GET", volUrl, "", apiHeaders)
    response = conn.getresponse()
    responseData = response.read()
    responseStatus = response.status
    responseReason = response.reason
    conn.close()

    parsed_response = json.loads( responseData )

    return parsed_response

def get_remotecopygroups( sessionKey, sessionHost, sessionPort = 8080 ):
    qUrl = '/api/v1/remotecopygroups'
    apiHeaders = build_apiHeaders( sessionKey )

    conn = httplib.HTTPSConnection( sessionHost, sessionPort, timeout=10)
    conn.request("GET", qUrl, "", apiHeaders)
    response = conn.getresponse()
    responseData = response.read()
    responseStatus = response.status
    responseReason = response.reason
    conn.close()

    parsed_response = json.loads( responseData )

    return parsed_response

def get_sysrep_cachememorystatistics( sessionKey, sessionHost, sessionPort = 8080 ):
    qUrl = '/api/v1/systemreporter/attime/cachememorystatistics/hires'
    apiHeaders = build_apiHeaders( sessionKey )

    conn = httplib.HTTPSConnection( sessionHost, sessionPort, timeout=10)
    conn.request("GET", qUrl, "", apiHeaders)
    response = conn.getresponse()
    responseData = response.read()
    responseStatus = response.status
    responseReason = response.reason
    conn.close()

    parsed_response = json.loads( responseData )

    return parsed_response

def get_sysrep_cpgstatistics( sessionKey, sessionHost, sessionPort = 8080 ):
    qUrl = '/api/v1/systemreporter/attime/cpgstatistics/hires'
    apiHeaders = build_apiHeaders( sessionKey )

    conn = httplib.HTTPSConnection( sessionHost, sessionPort, timeout=10)
    conn.request("GET", qUrl, "", apiHeaders)
    response = conn.getresponse()
    responseData = response.read()
    responseStatus = response.status
    responseReason = response.reason
    conn.close()

    parsed_response = json.loads( responseData )

    return parsed_response

def get_sysrep_physicaldiskcapacity( sessionKey, sessionHost, sessionPort = 8080 ):
    qUrl = '/api/v1/systemreporter/attime/physicaldiskcapacity/hires'
    apiHeaders = build_apiHeaders( sessionKey )

    conn = httplib.HTTPSConnection( sessionHost, sessionPort, timeout=10)
    conn.request("GET", qUrl, "", apiHeaders)
    response = conn.getresponse()
    responseData = response.read()
    responseStatus = response.status
    responseReason = response.reason
    conn.close()

    parsed_response = json.loads( responseData )

    return parsed_response

def get_sysrep_physicaldiskspacedata( sessionKey, sessionHost, sessionPort = 8080 ):
    qUrl = '/api/v1/systemreporter/attime/physicaldiskspacedata/hourly'
    apiHeaders = build_apiHeaders( sessionKey )

    conn = httplib.HTTPSConnection( sessionHost, sessionPort, timeout=10)
    conn.request("GET", qUrl, "", apiHeaders)
    response = conn.getresponse()
    responseData = response.read()
    responseStatus = response.status
    responseReason = response.reason
    conn.close()

    parsed_response = json.loads( responseData )

    return parsed_response
    
def get_sysrep_physicaldiskstatistics( sessionKey, sessionHost, sessionPort = 8080 ):
    qUrl = '/api/v1/systemreporter/attime/physicaldiskstatistics/hires'
    apiHeaders = build_apiHeaders( sessionKey )

    conn = httplib.HTTPSConnection( sessionHost, sessionPort, timeout=10)
    conn.request("GET", qUrl, "", apiHeaders)
    response = conn.getresponse()
    responseData = response.read()
    responseStatus = response.status
    responseReason = response.reason
    conn.close()

    parsed_response = json.loads( responseData )

    return parsed_response
        
def get_sysrep_vlunstatistics( sessionKey, sessionHost, sessionPort = 8080 ):
    qUrl = '/api/v1/systemreporter/attime/vlunstatistics/hires'
    apiHeaders = build_apiHeaders( sessionKey )

    conn = httplib.HTTPSConnection( sessionHost, sessionPort, timeout=10)
    conn.request("GET", qUrl, "", apiHeaders)
    response = conn.getresponse()
    responseData = response.read()
    responseStatus = response.status
    responseReason = response.reason
    conn.close()

    parsed_response = json.loads( responseData )

    return parsed_response

def get_sysrep_portstatistics( sessionKey, sessionHost, sessionPort = 8080 ):
    qUrl = '/api/v1/systemreporter/attime/portstatistics/hires'
    apiHeaders = build_apiHeaders( sessionKey )

    conn = httplib.HTTPSConnection( sessionHost, sessionPort, timeout=10)
    conn.request("GET", qUrl, "", apiHeaders)
    response = conn.getresponse()
    responseData = response.read()
    responseStatus = response.status
    responseReason = response.reason
    conn.close()

    parsed_response = json.loads( responseData )

    return parsed_response    
    
def print_usage():
    print 'usage: agent_3parwsapi -H <Host> -U <User> -P <Password> -v <Value,Value> --sshuser <User>'

#   .--Main----------------------------------------------------------------.
#   |                        __  __       _                                |
#   |                       |  \/  | __ _(_)_ __                           |
#   |                       | |\/| |/ _` | | '_ \                          |
#   |                       | |  | | (_| | | | | |                         |
#   |                       |_|  |_|\__,_|_|_| |_|                         |
#   |                                                                      |
#   +----------------------------------------------------------------------+

_defaultValues = [ "system", "cpgs", "volumes", "flashcache", "ssh" ]
_sysrep_ignoreCodes = [ 9, 70 ]

def main( argv ):
    fetchValues = _defaultValues

    sessionHost = ''
    sessionUser = ''
    sessionPassword = ''
    sessionKey = ''
    sshUser = ''

    try:
        opts, args = getopt.getopt( argv, "hH:U:P:v:s:", ["Host=", "User=", "Password=", "Values=", "sshuser="])
        if not opts:
            print_usage()
            sys.exit(2)
    except getopt.GetoptError:
        print_usage()
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
            print_usage()
            sys.exit()
        elif opt in ("-H", "--Host"):
            sessionHost = arg
        elif opt in ("-v", "--Values"):
            fetchValues = arg.split(",")
        elif opt in ("-U", "--User"):
            sessionUser = arg
        elif opt in ("-P", "--Post"):
            sessionPassword = arg
        elif opt in ("-s", "--sshuser"):
            sshUser = arg

    if sessionHost == "" or sessionUser == "" or sessionPassword == "":
        print_usage()
        sys.exit(2)

    try:
        sessionKey = get_cred( sessionHost, sessionUser, sessionPassword )

        result = {}
        result["sessionHost"] = sessionHost

        #print "*****"
        #print "%s: %s with %s / %s" % ( "Host", sessionHost, sessionUser, sessionPassword )
        #print "%s: %s" % ( "Values", fetchValues )
        #print "-----"
        #print sessionKey
        #print "-----"

        print "<<<3par>>>"

        system = get_system( sessionKey, sessionHost )
        result["system"] = system

        hosts = get_hosts( sessionKey, sessionHost )
        result["hosts"] = hosts

        vols = get_volumes( sessionKey, sessionHost )
        result["volumes"] = vols

        cpgs = get_cpgs( sessionKey, sessionHost )
        result["cpgs"] = cpgs

        ports = get_ports( sessionKey, sessionHost )
        result["ports"] = ports

        flashcache = get_flashcache( sessionKey, sessionHost )
        result["flashcache"] = flashcache

        remotecopygroups = get_remotecopygroups( sessionKey, sessionHost )
        result["remotecopygroups"] = remotecopygroups

        sysrep_cpgstatistics = get_sysrep_cpgstatistics( sessionKey, sessionHost )
        if "code" in sysrep_cpgstatistics and sysrep_cpgstatistics["code"] in _sysrep_ignoreCodes:
            pass
        else:
            result["sysrep_cpgstatistics"] = sysrep_cpgstatistics
        
        sysrep_physicaldiskcapacity = get_sysrep_physicaldiskcapacity( sessionKey, sessionHost )
        if "code" in sysrep_physicaldiskcapacity and sysrep_physicaldiskcapacity["code"] in _sysrep_ignoreCodes:
            pass
        else:
            result["sysrep_physicaldiskcapacity"] = sysrep_physicaldiskcapacity

        sysrep_physicaldiskspacedata = get_sysrep_physicaldiskspacedata( sessionKey, sessionHost )
        if "code" in sysrep_physicaldiskspacedata and sysrep_physicaldiskspacedata["code"] in _sysrep_ignoreCodes:
            pass
        else:
            result["sysrep_physicaldiskspacedata"] = sysrep_physicaldiskspacedata

        sysrep_physicaldiskstatistics = get_sysrep_physicaldiskstatistics( sessionKey, sessionHost )
        if "code" in sysrep_physicaldiskstatistics and sysrep_physicaldiskstatistics["code"] in _sysrep_ignoreCodes:
            pass
        else:
            result["sysrep_physicaldiskstatistics"] = sysrep_physicaldiskstatistics
        
        sysrep_cachememorystatistics = get_sysrep_cachememorystatistics( sessionKey, sessionHost )
        if "code" in sysrep_cachememorystatistics and sysrep_cachememorystatistics["code"] in _sysrep_ignoreCodes:
            pass
        else:
            result["sysrep_cachememorystatistics"] = sysrep_cachememorystatistics
        
        sysrep_portstatistics = get_sysrep_portstatistics( sessionKey, sessionHost )
        if "code" in sysrep_portstatistics and sysrep_portstatistics["code"] in _sysrep_ignoreCodes:
            pass
        else:
            result["sysrep_portstatistics"] = sysrep_portstatistics
        
        sysrep_vlunstatistics = get_sysrep_vlunstatistics( sessionKey, sessionHost )
        if "code" in sysrep_vlunstatistics and sysrep_vlunstatistics["code"] in _sysrep_ignoreCodes:
            pass
        else:
            result["sysrep_vlunstatistics"] = sysrep_vlunstatistics
            
        remove_cred( sessionHost, sessionKey )

        print json.dumps(result, sort_keys=True, indent=4, separators=(',', ': '))
        
        if sshUser != "":
            cmdSsh = "%s/agent_3parssh %s %s" % ( os.path.dirname(__file__), sessionHost, sshUser )
            print subprocess.check_output( cmdSsh, shell=True, executable='/bin/bash' )
        
    except:
        print("Unexpected error:", sys.exc_info()[0])
        if sessionKey != "":
            remove_cred( sessionHost, sessionKey )
        raise
    else:
        if sessionKey != "":
            remove_cred( sessionHost, sessionKey )

if __name__ == "__main__":
    main(sys.argv[1:])
