Last active
December 17, 2019 14:49
-
-
Save SonyaEick/32fbad744e04cb09a29c to your computer and use it in GitHub Desktop.
Cisco Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
class HTTPMethod: | |
GET = 1 | |
POST = 2 | |
PUT = 3 | |
DELETE = 4 | |
class NSAPI: | |
""" Class that talks to Service Catalog via NS API """ | |
LOGIN_URL = "/RequestCenter/nsapi/authentication/login" | |
__session = requests.Session() | |
def __init__(self, username, password, auth_url): | |
self.username = username | |
self.password = password | |
self.auth_url = auth_url | |
def authenticate(self): | |
auth_headers = {"username":self.username, "password":self.password} | |
r = self.__session.get(self.auth_url, headers = auth_headers) | |
if r.status_code != 200: | |
r.raise_for_status() | |
def generic_call(self, server, path_url, http_method = HTTPMethod.GET, request = None, headers = None): | |
if http_method == HTTPMethod.GET: | |
print(server+path_url) | |
return self.__session.get(server + path_url, headers = headers) | |
elif http_method == HTTPMethod.POST: | |
return self.__session.post(server + path_url, data = request, headers = headers) | |
elif http_method == HTTPMethod.PUT: | |
return self.__session.put(server + path_url, data = request, headers = headers) | |
elif http_method == HTTPMethod.DELETE: | |
return self.__session.delete(server + path_url, headers = headers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from assurancecontrol.ppm import ppm_api_properties | |
from flask import Flask, jsonify, abort | |
from builtins import any as ac_any | |
import datetime | |
class retriveppmdata(): | |
# TODO: add durationSelect = lastDay | |
# get cpu usage by interval and instance | |
# by default we are setting this to HOUR | |
def getInstanceCPUusage(self,instanceid): | |
url = (ppm_api_properties.ppm_host + ppm_api_properties.cpu_report_url + | |
ppm_api_properties.instance_id_filter + "\"" + instanceid + "\"" + | |
ppm_api_properties.hourlyinterval) | |
print(url) | |
print (datetime.datetime.utcnow()) | |
print (datetime.datetime.now().isoformat()) | |
try: | |
r = requests.get(url) | |
if (r.status_code == 200): | |
for i in r.json()['report']['data']: | |
print(i[1]) | |
print(i[2]) | |
return r.json() | |
else: | |
return r.reason | |
except: | |
return "ERROR" | |
def getInstanceCPUusageFill(self,instanceid): | |
url = (ppm_api_properties.ppm_host + ppm_api_properties.cpu_report_url + | |
ppm_api_properties.instance_id_filter + "\"" + instanceid + "\"" + | |
ppm_api_properties.hourlyinterval) | |
print(url) | |
try: | |
r = requests.get(url) | |
if (r.status_code == 200): | |
return r.json() | |
else: | |
return r.reason | |
except: | |
return "ERROR" | |
#return r.status_code | |
def getInstanceDiskReadBytes(self): | |
url = () | |
r = requests.get(url) | |
return r.json() | |
""" | |
possible values for interval are | |
FIFTEEN_SECS | |
THIRTY_SECS | |
ONE_MINUTE | |
FIVE_MINUTE | |
QUARTER_HOUR | |
HOUR | |
DAY | |
WEEK | |
""" | |
def getInstanceCPUintervalUsage(self,instanceid, interval): | |
url = (ppm_api_properties.ppm_host + ppm_api_properties.cpu_report_url + | |
ppm_api_properties.instance_id_filter + "\"" + instanceid + "\"" + | |
ppm_api_properties.interval + interval) | |
#get cpu usage of all instances by interval | |
r = requests.get(url) | |
return jsonify(r.json()) | |
def getOpenstackReportCatalog(self): | |
url = (ppm_api_properties.ppm_host + ppm_api_properties.nova + ppm_api_properties.output_type) | |
print(url) | |
r = requests.get(url) | |
catalog_elements = [] | |
for i in r.json()['reports']['reportInfo']: | |
print(i['name']) | |
catalog_elements.append(i['name']) | |
return catalog_elements | |
def getOpenstackDiskCatalog(self, cat_list): | |
disk_elements = [] | |
#ac_any("Disk" in x for x in cat_list) | |
disk = "Disk" | |
for item in cat_list: | |
if(disk in item): | |
disk_elements.append(item) | |
if __name__ == "__main__": | |
rppm = retriveppmdata() | |
instanceID = "" | |
instanceID = "" | |
instanceID = "" | |
instanceID = "" | |
# took out instance IDs for security | |
print(rppm.getInstanceCPUusage(instanceID)) | |
rppm.getOpenstackReportCatalog() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'sonya' | |
from nsapi import nsapi | |
import argparse | |
# Standard input module to absorb commands from CLI | |
parser = argparse.ArgumentParser(description='User inputs source and destination tables to transfer data.') | |
parser.add_argument('src_table', help='Source table not supplied.', type=str) | |
parser.add_argument('dest_table', help='Destination table not supplied.', nargs='?', type=str) # optional arg | |
args = parser.parse_args() | |
src_table = args.src_table | |
dest_table = args.dest_table | |
print('The CLI specified to move {} to {}'.format(src_table, dest_table)) | |
# Authenticate to SC | |
server = 'http://nim-int-psc1.tidalsoft.local' | |
auth = nsapi.NSAPI.LOGIN_URL | |
username = 'admin' | |
password = 'admin' | |
client = nsapi.NSAPI(username, password, server + auth) | |
client.authenticate() | |
# This function retrieves data from specified SI table, but does not return any data. | |
# Arguments: server = service catalog server url | |
# client = authorization into nsapi | |
# src_table = in-going table data | |
def get_serviceitem(server, client, src_table): | |
return client.generic_call( | |
server, '/RequestCenter/nsapi/serviceitem/' + | |
src_table + '/' + '?responseType=json').json() | |
def create_serviceitem(server, client, src_table, attributes, headers): | |
body = ("\n" | |
" <serviceitem>\n" | |
" <logicName>{type}</logicName>\n" | |
" <type>process</type>\n" | |
" <serviceItemData>" | |
) | |
body = body.format(type=src_table) | |
# data variable fills in the 'type' placemarker in body | |
for k, v in attributes.items(): | |
body += ('<serviceItemAttribute name=\'%{attr_name}s\'>%{attr_value}s</serviceItemAttribute>' | |
.format(attr_name=k, attr_value=v)) | |
# this function fills in, and adds, data to the key & value markers into body. | |
body += '</serviceItemData></serviceitem>' | |
print(body) | |
return (client.generic_call(server, '/RequestCenter/nsapi/serviceitem/process?responseType=json', | |
nsapi.HTTPMethod.POST, request=body, headers=headers)) | |
service_item = get_serviceitem(server, client, src_table) | |
print(service_item) # prints out full data set of current si_table | |
def delete_serviceitem(server, client, src_table): | |
return (client.generic_call(server, '/RequestCenter/nsapi/serviceitem/process?responseType=json', | |
nsapi.HTTPMethod.DELETE)) | |
if __name__ == '__main__': | |
for item in service_item['serviceitem']['serviceItemData']: | |
si_name = item['items'][0]['Name'] | |
si_id = item['items'][0]['id'] | |
new_item = create_serviceitem(server, client, dest_table, {'Name': si_name, 'id': si_id}, | |
headers={"content-type": "application/xml"}) | |
print(new_item.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment