Created
January 7, 2019 13:39
-
-
Save developer-sdk/c1061f46068a490dcbe06e76d236629d to your computer and use it in GitHub Desktop.
ResourceManager Metric REST API
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
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| import urllib, json, urllib2, datetime | |
| from urllib2 import HTTPError | |
| def request_get(request_url): | |
| return request(request_url, "GET", "", {'Content-Type': 'application/json'}) | |
| def request(request_url, request_type="GET", data="", header={}): | |
| '''send url and get response''' | |
| opener = urllib2.build_opener(urllib2.HTTPHandler) | |
| request_get = urllib2.Request(request_url, data, header) | |
| request_get.get_method = lambda: request_type | |
| try: | |
| response = opener.open(request_get) | |
| except HTTPError as hpe: | |
| print("-- HTTPError --") | |
| print(" error code: {0}".format(hpe.code)) | |
| print(" message: {0}".format(hpe.msg)) | |
| error_html = hpe.read() | |
| print(" error html: {0}".format(error_html)) | |
| return | |
| response_info = response.info() | |
| response_body = response.read() | |
| json_obj = json.loads(response_body) | |
| clusterMetrics = json_obj["clusterMetrics"] | |
| #print(response_info) | |
| #print(json_obj) | |
| #print(json.dumps(json_obj, sort_keys=True, indent=4, separators=(',', ': '))) | |
| print("{0},{1},{2},{3},{4}".format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), clusterMetrics["totalMB"], clusterMetrics["reservedMB"], clusterMetrics["availableMB"], clusterMetrics["allocatedMB"])) | |
| def main(): | |
| rma_url = "http://127.0.0.1:8088/ws/v1/cluster/metrics" | |
| request_get(rma_url) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment