Last active
July 19, 2018 17:32
-
-
Save solo-seven/5c9976245e380123e82cd297f15966b0 to your computer and use it in GitHub Desktop.
Python Script for creating charts and a dashboard based on a service name
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
# | |
# create_dashboards.py - A script for creating a set of dashboards for a specific service | |
# | |
import json | |
import sys | |
import requests | |
def main(): | |
service_name = sys.argv[1] | |
print(f"Service Name: {service_name}") | |
token = sys.argv[2] | |
headers = { | |
'X-SF-TOKEN': token, | |
'Content-Type': 'application/json' | |
} | |
dashboard_group_api = 'https://api.signalfx.com/v2/dashboardgroup' | |
dashboard_api = 'https://api.signalfx.com/v2/dashboard' | |
chart_api = 'https://api.signalfx.com/v2/chart' | |
# Create a chart for CPU | |
chart_data = { | |
'name': f"{service_name} Service CPU", | |
'options': { | |
'type': 'TimeSeriesChart' | |
}, | |
'programText': "data('container_cpu_percent'," | |
f" filter('container_name', '{service_name}'))" | |
".mean(over='10m')" | |
".scale(100)" | |
".publish()" | |
} | |
response = requests.post(chart_api, headers=headers, json=chart_data) | |
if response.status_code != 200: | |
print(f"Failed to create CPU Chart: {response.status_code}") | |
print(f"Response body: {response.text}") | |
return 1 | |
r_json = json.loads(response.text) | |
cpu_chart_id = r_json['id'] | |
# Create chart for Memory | |
chart_data = { | |
'name': f"{service_name} Service Memory Usage", | |
'options': { | |
'type': 'TimeSeriesChart' | |
}, | |
'programText': "data('container_memory_usage_bytes', " | |
f"filter('container_name', '{service_name}'))" | |
".mean(over='10m')" | |
".publish()" | |
} | |
response = requests.post(chart_api, headers=headers, json=chart_data) | |
if response.status_code != 200: | |
print(f"Failed to create memory chart: {response.status_code}") | |
return 1 | |
r_json = json.loads(response.text) | |
mem_chart_id = r_json['id'] | |
# Create a dashboard group for the dashboard | |
dashboard_group_data = { | |
'name': f"{service_name} Service Dashboards", | |
'description': f"A set of dashboards generated for {service_name}" | |
} | |
response = requests.post(dashboard_group_api, headers=headers, json=dashboard_group_data) | |
if response.status_code != 200: | |
print(f"Failed to create dashboard group: {response.status_code}") | |
return 1 | |
r_json = json.loads(response.text) | |
dashboard_group_id = r_json['id'] | |
# Create the dashboard | |
dashboard_data = { | |
'name': f"{service_name} Dashboard", | |
'description': f"A Dashboard generated for {service_name}", | |
'groupId': dashboard_group_id, | |
'charts': [ | |
{ | |
'chartId': cpu_chart_id, | |
'row': 0, 'column': 0, | |
'height': 1, 'width': 6, | |
}, | |
{ | |
'chartId': mem_chart_id, | |
'row': 0, 'column': 6, | |
'height': 1, 'width': 6, | |
} | |
] | |
} | |
response = requests.post(dashboard_api, headers=headers, json=dashboard_data) | |
if response.status_code != 200: | |
print(f"Failed to create dashboard: {response.status_code}") | |
return 1 | |
r_json = json.loads(response.text) | |
dashboard_id = r_json['id'] | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment