Skip to content

Instantly share code, notes, and snippets.

@arungpro
Last active June 10, 2020 13:23
Show Gist options
  • Save arungpro/a4d74102d449013a915dda8821929ae8 to your computer and use it in GitHub Desktop.
Save arungpro/a4d74102d449013a915dda8821929ae8 to your computer and use it in GitHub Desktop.
Python Script - To mark node historical
__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:])
@arungpro
Copy link
Author

/controller/rest/mark-nodes-historical

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment