Created
November 25, 2014 06:12
-
-
Save wolfdancer/98e1234fff2bcd77a8a0 to your computer and use it in GitHub Desktop.
monitornig check export
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
#!/usr/bin/env python3 | |
""" | |
This script will hit the monitoring API, pull monitoring configuration on the specified entty and check, | |
then generate yaml config file | |
To use: | |
./export.py --user <username> --api-key <api key> --entity <entity-id> --check <check-id> | |
""" | |
import argparse | |
from urllib.request import urlopen, Request, HTTPError | |
import sys | |
import re | |
from datetime import datetime | |
import requests | |
import json | |
import yaml | |
from collections import OrderedDict | |
class literal(str): pass | |
def literal_presenter(dumper, data): | |
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|') | |
yaml.add_representer(literal, literal_presenter) | |
def ordered_dict_presenter(dumper, data): | |
return dumper.represent_dict(data.items()) | |
yaml.add_representer(OrderedDict, ordered_dict_presenter) | |
monitoring_api = 'https://monitoring.api.rackspacecloud.com/v1.0/' | |
def login(user, key): | |
auth_payload = { | |
'auth':{ | |
'RAX-KSKEY:apiKeyCredentials':{ | |
'username': user, | |
'apiKey': key | |
} | |
} | |
} | |
try: | |
auth_req = Request(args.auth_uri) | |
auth_req.add_header('Content-type', 'application/json') | |
auth_resp = json.loads(urlopen(auth_req, json.dumps(auth_payload).encode("utf8")).read().decode("utf8")) | |
except HTTPError: | |
print('status err Unable to authenticate user {0}'.format(args.user)) | |
sys.exit(1) | |
else: | |
auth_token = auth_resp['access']['token']['id'] | |
tenant_id = auth_resp['access']['token']['tenant']['id'] | |
return auth_token, tenant_id | |
def call_api(api, auth_token, tenant_id): | |
url = monitoring_api + api | |
request = Request(url) | |
request.add_header('X-Auth-Token', auth_token) | |
request.add_header('X-Tenant-Id', tenant_id) | |
return json.loads(urlopen(request).read().decode("utf8")) | |
def to_yaml(content): | |
return yaml.dump(content, default_flow_style=False) | |
def main(): | |
auth_token, tenant_id = login(args.user, args.api_key) | |
entity_id = args.entity_id | |
check_id = args.check_id | |
check = call_api("entities/{}/checks/{}".format(entity_id, check_id), auth_token, tenant_id) | |
content = OrderedDict() | |
content["source"] = { | |
"tenant_id": tenant_id, | |
"entity_id": entity_id, | |
"check_id": check_id | |
} | |
for field in ["confd_hash", "confd_name", "id", "metadata", "created_at", "updated_at"]: | |
check.pop(field, None) | |
for field in ["type", "label", "disabled", "period", "timeout", "target_alias", "target_hostname", "target_resolver", "details", "monitoring_zones_poll"]: | |
value = check.pop(field, None) | |
if value: | |
content[field] = value | |
for key,value in check.items(): | |
content[key] = value | |
alarms = OrderedDict() | |
for alarm in call_api("entities/{}/alarms".format(entity_id), auth_token, tenant_id)["values"]: | |
if alarm["entity_id"] == entity_id and alarm["check_id"] == check_id: | |
alarm_content = OrderedDict() | |
alarm_content["label"] = alarm["label"] | |
alarm_content["notification_plan_id"] = alarm["notification_plan_id"] | |
alarm_content["criteria"] = literal(alarm["criteria"]) | |
alarms[alarm["id"]] = alarm_content | |
content["alarms"] = alarms | |
print(to_yaml(content), end="") | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Monitornig host info') | |
parser.add_argument('--user', dest='user', action='store', | |
required=True, help='The Rackspace user') | |
parser.add_argument('--api-key', dest='api_key', action='store', | |
required=True, help='The Rackspace API key') | |
parser.add_argument('--entity-id', dest='entity_id', action='store', | |
required=True, help='The entity ID') | |
parser.add_argument('--check-id', dest='check_id', action='store', | |
required=True, help='The check ID') | |
parser.add_argument('--auth-uri', dest='auth_uri', action='store', | |
default='https://identity.api.rackspacecloud.com/v2.0/tokens', | |
help='The Rackspace Identity token endpoint') | |
args = parser.parse_args() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment