advanced rest api scripting with appdynamics
Post on 12-Aug-2015
491 Views
Preview:
TRANSCRIPT
Advanced REST API Scripting
Todd Radel
Senior Manager, Enablement and Adoption
Copyright © 2014 AppDynamics. All rights reserved. 2
About Me
Todd RadelSENIOR MANAGER
ENABLEMENT & ADOPTION, US EAST
• More than 20 years experience in the software industry
• Java, .NET, Python, Perl• Author of the Python SDK for
AppDynamics REST API• Author of six AppDynamics
Extensions: F5, Tibco EMS, Redis, Keynote, SQL, URL Monitor
tradel@appdynamics.com
+1 (484) 857-2335
tradel
todd.radel
linkedin.com/in/tradel
Copyright © 2014 AppDynamics. All rights reserved. 3
Today’s Agenda
1
2 Installing and using the Python SDK
3 Reporting on license usage
Hello World (in 3 languages!)
BUILDING ON THE FIRST REST API WEBINAR, WE WILL SHOW YOU HOW TO SCRIPT AND AUTOMATE COMMON TASKS
4 DevOps integration – disabling health rules during code deployment
Hello World
Copyright © 2014 AppDynamics. All rights reserved. 5
Hello World
OBJECTIVE• Retrieve a list of business applications from the controller• Print to stdout
FLAVORS• Bash/cURL• PowerShell• Python – The Hard Way• Python – The Easy Way
Copyright © 2014 AppDynamics. All rights reserved. 6
Bash and cURL
#! /bin/bash# Print the list of tiers for an application
curl -X GET -u user1@customer1:welcome "http://localhost:8080/controller/rest/applications?output=json"
Windows PowerShell
# Print the list of tiers for an application
$usr="user1@customer1" $pwd="welcome"
$headers = @{ Authorization = "Basic " +
[System.Convert]::ToBase64String( [System.Text.Encoding]::ASCII.GetBytes( "$($usr):$($pwd)")) }
$url="http://localhost:8080/controller/rest/applications?output=json"
$response = Invoke-RestMethod $url -Headers $Headers
$response | ForEach-Object { [string]$_.id + " " + $_.name }
Copyright © 2014 AppDynamics. All rights reserved. 8
Python – The Hard Way
#! /usr/bin/env python # Print the list of tiers for an application
import requests
usr = "user1@customer1" pwd = "welcome" url = "http://localhost:8080/controller/rest/applications?output=json"
response = requests.get(url, auth=(usr, pwd)) json = response.json()
for app in json: print app['id'], app['name']
Copyright © 2014 AppDynamics. All rights reserved. 9
Python – The Easy Way
#! /usr/bin/env python # Print the list of tiers for an application
from appd.request import AppDynamicsClient
c = AppDynamicsClient("http://localhost:8080", "user1", "welcome")
for app in c.get_applications(): print app.id, app.name
# Wasn’t that easy?
Getting Started With The Python SDK
Copyright © 2014 AppDynamics. All rights reserved. 11
BREAKING NEWS:
AppDynamicsREST is now in the Python Package Index!
Copyright © 2014 AppDynamics. All rights reserved. 12
Downloading and Installing
1. Install Python 2.7 from https://www.python.org/downloads/
2. Install PIP (if earlier than Python 2.7.9)curl https://bootstrap.pypa.io/get-pip.py > get-pip.pypython get-pip.py
3. Install AppDynamicsRESTpip install AppDynamicsREST
4. Get coding!
Copyright © 2014 AppDynamics. All rights reserved. 13
Hello World – Your First Script
#! /usr/bin/env python # Print the list of tiers for an application
from appd.request import AppDynamicsClient
c = AppDynamicsClient("http://localhost:8080", "user1", "welcome")
for app in c.get_applications(): print app.id, app.name
Copyright © 2014 AppDynamics. All rights reserved. 14
Retrieving Metric Data
#! /usr/bin/env python # Export key business metrics from AppDynamics
from appd.request import AppDynamicsClient
c = AppDynamicsClient("http://localhost:8080", "user1", "welcome")
metrics = c.get_metrics(metric_path='Business Transaction Performance|Business Transactions|ECommerce Server|ViewCart.addToCart|Average Response Time (ms)', app_id='ACME Book Store Application', time_range_type='BEFORE_NOW', duration_in_mins=60, rollup=False)
for point in metrics[0].values: print point.start_time, 'Average Response Time: ', point.value
License Usage Report
Copyright © 2014 AppDynamics. All rights reserved. 16
Hello World
OBJECTIVE• Get count of licenses used per product (Java, .NET, etc.)
in the past 15 days• Make a beautiful chart
TOOLS• Python• AppDynamicsREST• PyGal
Copyright © 2014 AppDynamics. All rights reserved. 17
Source Code, Page 1
#! /usr/bin/env python # -*- coding: utf-8 -*-
from datetime import datetime, timedelta from collections import OrderedDict, defaultdict import pygal import tzlocal from appd.request import AppDynamicsClient import creds.demo2 as creds
# Set up API client c = AppDynamicsClient(creds.url, creds.user, creds.password, creds.account)
# Get my tenant account info my_acct = c.get_my_account()
# Calculate start and end dates - we will start at midnight and go back 15 days days = 15 mytz = tzlocal.get_localzone() end_dt = datetime.now(mytz).replace(hour=0, minute=0, second=0, microsecond=0) start_dt = end_dt - timedelta(days)
# Get license usage for my account usage = c.get_license_usage(my_acct.id, None, start_dt, end_dt)
Copyright © 2014 AppDynamics. All rights reserved. 18
Source Code, Page 2
def daterange(start_dt, end_dt): for n in range(int((end_dt - start_dt).days)): yield start_dt + timedelta(days=n)
# Get the list of all licensed products: products = set(x.license_module for x in usage.usages) products = [x for x in products if 'eum' not in x and 'analytics' not in x]
usage_by_product = defaultdict(OrderedDict) for product in products: for day in daterange(start_dt, end_dt): units = [x.max_units_used for x in usage.usages if x.created_on.date() == day.date() and x.license_module == product] usage_by_product[product][day] = max(units)
# Make a simple graph and display it chart = pygal.StackedBar(x_label_rotation=45, width=1000) chart.title = 'License Usage By Product - ' + c.base_url chart.x_labels = [str(x.date()) for x in daterange(start_dt, end_dt)] for product in products: chart.add(product, usage_by_product[product].values())
chart.render_to_file('all_usage.svg')
Copyright © 2014 AppDynamics. All rights reserved. 19
Output
Enable & Disable Health Rules
Copyright © 2014 AppDynamics. All rights reserved. 21
Hello World
OBJECTIVES• Programmatically enable and disable health rules• Perform code deployments without triggering false alerts
TOOLS• Python• AppDynamicsREST v2
Copyright © 2014 AppDynamics. All rights reserved. 22
Wait … What is v2?
• Version 2 of AppDynamicsREST is being developed
• Supports original AppDynamics REST API and v2.0• Supports XML or JSON output• Fully RESTful object model
Old: c.get_nodes(app_id=10, tier_id=12)
New: c.application(10).tier(12).nodes
Best: c.application(‘ACME Book Store’) .tier(‘E-Commerce’) .nodes
Copyright © 2014 AppDynamics. All rights reserved. 23
New Features in v2
• Accounts• Users• Policies• Health rules• License usage• License status
• App Agent Operations
• Collect log files• Collect debug logs• Take thread dumps
• Configuration import/export
Copyright © 2014 AppDynamics. All rights reserved. 24
Disabling Health Rules
#! /usr/bin/env python # -*- coding: utf-8 -*-
from appd.http.service import AppDynamicsService
c = AppDynamicsService('user1', 'welcome', base_url='http://localhost:8080')
app = c.my_account.application('ACME Book Store')
for hr in app.health_rules: hr.disable()
Copyright © 2014 AppDynamics. All rights reserved. 25
Output
Copyright © 2014 AppDynamics. All rights reserved. 26
Enabling Health Rules
#! /usr/bin/env python # -*- coding: utf-8 -*-
from appd.http.service import AppDynamicsService
c = AppDynamicsService('user1', 'welcome', base_url='http://localhost:8080')
app = c.my_account.application('ACME Book Store')
for hr in app.health_rules: hr.enable()
Copyright © 2014 AppDynamics. All rights reserved. 27
Output
Office Hours/Q&A
Further Reading
• Product Documentationhttps://docs.appdynamics.com/display/PRO41/Use+the+AppDynamics+REST+API
• REST API SDK On Python Package Indexhttps://pypi.python.org/pypi/AppDynamicsREST
Thank You!
top related