TINEWS.ORG - Home
Tom's internal news testing page -

Quote of the Page:

#!/usr/bin/python

import time
import requests
import base64
import sys
from lxml import etree
from lxml.etree import fromstring

def getElementTypes():
    sbcToken = returnToken()
    header = {"Accept" : "application/xml", "Authorization": "Bearer " + sbcToken }
    url = "https://10.xxx.xx.xx/rest/v1.1/configuration/elementTypes"
    response = requests.get(url,headers=header,verify=False)
    file = open("ElementTypes.xml","w")
    file.write(response.content)
    print("Element Types Written to ElementTypes.xml")

def getSessCapaUtil():
    sbcToken = returnToken()
    header = {"Accept" : "application/xml", "Authorization": "Bearer " + sbcToken }
    url = "https://10.xxx.xx.xx/rest/v1.1/statistics/kpis?type=sessCapacityUtil"
    respSessCapaUtil = requests.get(url,headers=header,verify=False)
    print("Session Capacity Util resp:")
    print(respSessCapaUtil.content)
    tree  = etree.fromstring(bytes(respSessCapaUtil.text))
    print(tree)

def getReg():
    sbcToken = returnToken()
    header = {"Accept" : "application/xml", "Authorization": "Bearer " + sbcToken }
    url = "https://10.xxx.xx.xx/rest/v1.1/statistics/kpis?type=sipRegistrations"
    respSipReg = requests.get(url,headers=header,verify=False)
    regFS = fromstring(respSipReg.content.encode('utf-8'))
    reg_sbc = regFS.xpath('/response/data/sysSipStatesActiveLocalContacts/sipReg').pop()
    print("Active Registrations: "),
    print(reg_sbc.text)


def getMemoryUtil():
    sbcToken = returnToken()
    header = {"Accept" : "application/xml", "Authorization": "Bearer " + sbcToken }
    url = "https://10.xxx.xx.xx/rest/v1.1/statistics/kpis?type=sysMemoryUtil"
    respMemory = requests.get(url,headers=header,verify=False)
    memFS = fromstring(respMemory.content.encode('utf-8'))
    mem_sbc = memFS.xpath('/response/data/sysMemoryUtil/percentUsed').pop()
    return(mem_sbc.text)

def getCpuUsage():
    sbcToken = returnToken()
    header = {"Accept" : "application/xml", "Authorization": "Bearer " + sbcToken }
    url = "https://10.xxx.xx.xx/rest/v1.1/statistics/kpis?type=sysCPUUtil"
    respCPU = requests.get(url,headers=header,verify=False)
    cpuFS = fromstring(respCPU.content.encode('utf-8'))
    cpu_sbc = cpuFS.xpath('/response/data/sysCPUUtilPercent/percentUsed').pop()
    sbcCPU = cpu_sbc.text
    return sbcCPU

def getGlobSess():
    sbcToken = returnToken()
    globalCallList = []
    header = {"Accept" : "application/xml", "Authorization": "Bearer " + sbcToken }
    url = "https://10.xxx.xx.xx/rest/v1.1/statistics/kpis?type=globalSessions"
    respGS = requests.get(url,headers=header,verify=False)
    newgsr = fromstring(respGS.content.encode('utf-8'))
    gcps = newgsr.xpath('/response/data/globalSessions/sysGlobalCPS').pop()
    gcs = newgsr.xpath('/response/data/globalSessions/sysGlobalConSessions').pop()
    globalCallList.append(gcps.text)
    globalCallList.append(gcs.text)
    return globalCallList

def getKPIType():
# include the token in all subsequent calls
    sbcToken = returnToken()
    token_header = {"Accept" : "application/xml", "Authorization": "Bearer " + sbcToken }
    test_url = "https://10.xxx.xx.xx/rest/v1.1/statistics/kpiTypes"
    respKpi = requests.get(test_url,headers=token_header,verify=False)
    file = open("systemKPIType.txt", "w")
    file.write(respKpi.content)
    print("Data written to systemKPIType.txt")

def returnToken():
    # set the endpoint
    auth_url = "https://10.xxx.xx.xx/rest/v1.1/auth/token"
    # base64 encode "admin:" for Basic Authentication
    encoded_str = base64.encodestring("admin:myPassword").strip()
    # create the Authorization header with the base64-encoded credentials
    auth_header = { "Authorization": "Basic " + encoded_str }
    # send the POST request - use verify False to not check Cert as it is from lab here
    resp = requests.post(auth_url, headers=auth_header, verify=False)

    # extract the token and create an Authorization header with the token
    tree  = etree.fromstring(bytes(resp.text))
    token = tree.xpath("//accessToken")[0].text
    return token

def main():
    lenOfArgs = len(sys.argv)
    if lenOfArgs != 3:
        print("Usage: restMon.py  ")
        sys.exit(1)
    count=0
    numOfReps = int(sys.argv[1])
    sleepTime = int(sys.argv[2])
    while count < numOfReps:
        statList = []
        sbcMem = getMemoryUtil()
        sbcCpu = getCpuUsage()
        statList.append(sbcCpu)
        statList.append(sbcMem)
        gstatlist = getGlobSess()
        statList.extend(gstatlist)
        time.sleep(sleepTime)
        count+=1
        print("CPU             Memory Util    CPS            Global Sess")
        for val in statList:
            print(val),
            print("            "),
        print("\n")

if __name__=='__main__':
    main()