Last active
June 10, 2020 13:23
-
-
Save arungpro/a4d74102d449013a915dda8821929ae8 to your computer and use it in GitHub Desktop.
Python Script - To mark node historical
This file contains hidden or 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__ = 'ArunG' | |
# python marknodeshistorical.py -c http://aruganga-m-l2db.local:8090 -n admin@customer1 -p admin -a TestApp | |
''' | |
Marks nodes as historical for an AppDynamics application | |
Usage: python marknodeshistorical.py [options] | |
Options: | |
-h, --help show this help | |
-c ..., --controllerURL=... controller URL | |
-n ..., --userName=... user name | |
-p ..., --userPassword=... user password | |
-a ..., --application=... application | |
''' | |
import getopt | |
import requests | |
import json | |
import sys | |
import pdb | |
def usage(): | |
print __doc__ | |
def main(argv): | |
controllerURL = 'http://yourcontroller:8090' | |
userName = 'youruser@customer1' | |
userPassword = 'yourpassword' | |
application = 'yourapplication' | |
try: | |
opts, args = getopt.getopt(argv, "hc:n:p:a:", ["help", "controllerURL=", "userName=", "userPassword=", "application="]) | |
except getopt.GetoptError: | |
usage() | |
sys.exit(2) | |
for opt, arg in opts: | |
if opt in ("-h", "--help"): | |
usage() | |
sys.exit() | |
elif opt in ("-c", "--controllerURL"): | |
controllerURL = arg | |
elif opt in ("-n", "--userName"): | |
userName = arg | |
elif opt in ("-p", "--userPassword"): | |
userPassword = arg | |
elif opt in ("-a", "--application"): | |
application = arg | |
if not (controllerURL and userName and userPassword and application): | |
usage() | |
sys.exit(2) | |
resp = requests.get(controllerURL + '/controller/rest/applications/' + application + '/nodes?output=JSON', auth=(userName, userPassword), verify=True, timeout=60) | |
if (resp.ok): | |
nodeList = json.loads(resp.content) | |
'''presp = requests.get(controllerURL + '/controller/restui/nodeUiService/deleteNode/10551') | |
print(presp.status_code) | |
print(presp.text)''' | |
for node in nodeList: | |
metricPath = 'Application Infrastructure Performance|' + node['tierName'] + '|Individual Nodes|' + node['name'] + '|Agent|App|Availability' | |
try: | |
aresp = requests.get(controllerURL + '/controller/rest/applications/' + application + '/metric-data?metric-path=' + metricPath + '&time-range-type=BEFORE_NOW&duration-in-mins=5&output=JSON&rollup=true', auth=(userName, userPassword), verify=True, timeout=10) | |
if (aresp.ok): | |
metrics = json.loads(aresp.content) | |
for item in metrics: | |
element = metrics[0] | |
metric_values = element['metricValues'] | |
for mitem in metric_values: | |
if element['metricValues'][0]['sum'] == 0: | |
payload = {'application-component-node-ids':str(node['id'])} | |
print 'Marking node ' + node['name'] + ' historical' | |
hresp = requests.post(controllerURL + '/controller/rest/mark-nodes-historical', data=payload, auth=(userName, userPassword), verify=True, timeout=10) | |
except: | |
# print node['name'] + ' exception checking availability' | |
pass | |
else: | |
print resp.raise_for_status() | |
print "Hit exception" | |
if __name__ == "__main__": | |
main(sys.argv[1:]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
/controller/rest/mark-nodes-historical