Created
July 31, 2013 19:09
-
-
Save homingli/6125144 to your computer and use it in GitHub Desktop.
Python scripts used in ActiveState webinar "Advanced Scaling Techniques with PaaS"
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
from stackato.interfaces import StackatoInterface | |
from bs4 import BeautifulSoup | |
import requests | |
import yaml | |
CREDENTIALS = yaml.load(open('credentials.yml')) | |
APP_NAME = 'your-app-name' | |
NEW_RELIC_USER_ID = 'your-user-id' | |
NEW_RELIC_APPLICATION_ID = 'your-app-id' | |
# scaling threshold | |
CPU_THRESHOLD = 80.5 | |
# flags | |
DEBUG = False | |
VERBOSE = True | |
# max instance | |
MAX_INSTANCES = 3 | |
def get_newrelic_threshold_value(user, appid, name='CPU'): | |
headers = {'x-api-key': CREDENTIALS['token']} | |
url = 'https://api.newrelic.com/api/v1/accounts/' + user + '/applications/' + appid + '/threshold_values.xml' | |
if DEBUG: | |
print url | |
print headers | |
r = requests.get(url, headers=headers) | |
# read from the response body as xml data | |
soup = BeautifulSoup(r.text, 'xml') | |
# return the value of the specified threshold_value's name from the response | |
return soup.find_all(attrs={'name': name})[0]['metric_value'] | |
if __name__ == "__main__": | |
# convert to float, since the CPU threshold value is returned as a string | |
metric_value = float(get_newrelic_threshold_value(NEW_RELIC_USER_ID,NEW_RELIC_APPLICATION_ID)) | |
if VERBOSE: | |
print "metric_value is " + str(metric_value) | |
print "CPU_THRESHOLD is " + str(CPU_THRESHOLD) | |
if metric_value < CPU_THRESHOLD: | |
if VERBOSE: | |
print "metric_value < CPU_THRESHOLD" | |
print('application running normally. no new instances spawned.') | |
else: | |
if VERBOSE: | |
print "metric_value => CPU_THRESHOLD" | |
sti = StackatoInterface(CREDENTIALS['target'], CREDENTIALS['username'], CREDENTIALS['password']) | |
if sti.login(): | |
app = sti.get_app(APP_NAME) | |
print APP_NAME + " currently running " + str(app['runningInstances']) + " instances." | |
if app['instances'] < MAX_INSTANCES: | |
# increase the number of instances | |
app['instances'] += 1 | |
# making a PUT request to the application | |
if sti.put_app(APP_NAME, app): | |
print('added one more instance to this application.') | |
else: | |
print('reached max instances.') |
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
target: https://api.stackato-xxxx.local/ | |
username: your-stackato-username | |
password: your-stackato-password | |
token: your-newrelic-api-key |
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
requests | |
PyYAML | |
beautifulsoup4 | |
python-stackato |
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 | |
import yaml | |
CREDENTIALS = yaml.load(open('credentials.yml')) | |
APP_NAME = 'your-app-name' | |
NEW_RELIC_USER_ID = 'your-user-id' | |
NEW_RELIC_APPLICATION_ID = 'your-app-id' | |
headers = {'x-api-key': CREDENTIALS['token']} | |
url = 'https://api.newrelic.com/api/v1/accounts/' + NEW_RELIC_USER_ID + '/applications/' + NEW_RELIC_APPLICATION_ID + '/threshold_values.xml' | |
print requests.get(url, headers=headers).text |
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
from stackato.interfaces import StackatoInterface | |
import yaml | |
import json | |
CREDENTIALS = yaml.load(open('credentials.yml')) | |
sti = StackatoInterface(CREDENTIALS['target'], CREDENTIALS['username'], CREDENTIALS['password']) | |
if sti.login(): | |
print json.dumps(sti.get_apps()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment