Created
October 1, 2015 06:39
-
-
Save samof76/0bd0e7f4efbb59a76b39 to your computer and use it in GitHub Desktop.
Crued way to monitor your Cloud Foundry application's resource utilization
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
from optparse import OptionParser | |
from time import sleep | |
import requests | |
def main(): | |
parser = OptionParser(usage="usage %prog [options]", version="%prog 1.0") | |
parser.add_option("-d", "--domain", | |
action = "store", | |
dest = "domain", | |
help = "Domain of your cloudfound installation, eg: cf.mydomain.com[Mandatory]") | |
parser.add_option("-u", "--username", | |
action = "store", | |
dest = "username", | |
help = "Username to use to login[Mandatory]") | |
parser.add_option("-p", "--password", | |
action = "store", | |
dest = "password", | |
help = "Password to use to login[Mandatory]") | |
parser.add_option("-a", "--app", | |
action = "store", | |
dest = "app", | |
help = "Application to monitor[Mandatory]") | |
parser.add_option("-i", "--interval", | |
action = "store", | |
dest = "interval", | |
default = 5, | |
type = int, | |
help = "Time interval between every check [Optional]") | |
opts, rem = parser.parse_args() | |
# if len(args) < 1: | |
# parser.error("wrong number of args") | |
if opts.interval < 5: | |
parser.error("INTERVAL cannot be less than 5") | |
if opts.domain == None: | |
parser.error("DOMAIN is a mandatory") | |
if opts.username == None: | |
parser.error("USERNAME is mandatory") | |
if opts.password == None: | |
parser.error("PASSWORD is mandatory") | |
if opts.app == None: | |
parser.error("APP is mandatory") | |
return opts | |
def get_token(opts): | |
uaa_url = "http://uaa.{0}/oauth/token".format(opts.domain) | |
headers = {'Accept': 'application/json', 'Cache-Control':'no-cache'} | |
data = {'username': opts.username, 'password': opts.password, 'response_type':'token', 'client_id':'cf', 'grant_type': 'password'} | |
response = requests.post(uaa_url, auth=('cf',''), headers=headers, data=data) | |
token = response.json()['access_token'] | |
return token | |
def get_app_guid(token, opts): | |
apps_url = "http://api.{0}/v2/apps".format(opts.domain) | |
bearer = "bearer {0}".format(token) | |
headers = {'Accept': 'application/json', 'Cache-Control':'no-cache', 'Authorization': bearer} | |
response = requests.get(apps_url, headers=headers) | |
resources = response.json()['resources'] | |
for r in resources: | |
if r['entity']['name'] == opts.app: | |
app_guid = r['metadata']['guid'] | |
break | |
return app_guid | |
def display_metrics(app_guid, token, opts): | |
stats_url = "http://api.{0}/v2/apps/{1}/stats".format(opts.domain, app_guid) | |
bearer = "bearer {0}".format(token) | |
headers = {'Accept': 'application/json', 'Cache-Control':'no-cache', 'Authorization': bearer} | |
while True: | |
response = requests.get(stats_url, headers=headers) | |
for i in response.json().keys(): | |
print response.json()[i]['stats'] | |
sleep(opts.interval) | |
if __name__ == '__main__': | |
opts = main() | |
token = get_token(opts) | |
app_guid = get_app_guid(token, opts) | |
display_metrics(app_guid, token, opts) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice work! You might be interested in adding some code to send this information to a time series database like OpenTSDB or InfluxDB or even sending it to a SaaS like datadog. You could also make it a CF app 😄