122 lines
4.1 KiB
Python
Executable File
122 lines
4.1 KiB
Python
Executable File
import json
|
|
import time
|
|
|
|
import requests
|
|
import yaml
|
|
|
|
service_config = yaml.load(open('service.yaml', 'r').read())
|
|
kairos_url = service_config['kairosdb_url'] + "api/v1/datapoints/"
|
|
kairos_query = kairos_url + "query"
|
|
metrics_list = []
|
|
status1 = "RECOVERY"
|
|
status2 = "WARNING"
|
|
status3 = "CRITICAL"
|
|
|
|
json_string1 = """{"name": "aom_test_metric","datapoints": """
|
|
json_string2 = ""","tags": {"host": "aom_host","data_center": "AOM"},"ttl": 500}"""
|
|
|
|
# WRITE ALERT CONFIG FILE
|
|
|
|
alert_file = {'alerts': {'sensu': {'slack': 'aom_test_channel'}},
|
|
'critical_lower_threshold': 100,
|
|
'critical_upper_threshold': 5000,
|
|
'id': 'test_metric',
|
|
'interval': 30,
|
|
'occurrences_threshold': 1,
|
|
'query': {'cache_time': 0,
|
|
'end_relative': {'unit': 'seconds', 'value': '30'},
|
|
'metrics': [{'name': 'aom_test_metric', 'tags': {}}],
|
|
'start_relative': {'unit': 'seconds', 'value': '60'}},
|
|
'tags': {},
|
|
'url': 'AOM_TESTING',
|
|
'warning_lower_threshold': 1000,
|
|
'warning_upper_threshold': 2000}
|
|
|
|
query_intro = """{
|
|
"metrics": [
|
|
{
|
|
"tags": {
|
|
"alert": [
|
|
"test_metric"
|
|
]
|
|
},
|
|
"name": "telegraf.aom_"""
|
|
|
|
query_outro = """_value",
|
|
"aggregators": [
|
|
{
|
|
"name": "sum",
|
|
"align_sampling": true,
|
|
"sampling": {
|
|
"value": "9",
|
|
"unit": "minutes"
|
|
},
|
|
"align_start_time": false
|
|
}
|
|
]
|
|
}
|
|
],
|
|
"cache_time": 0,
|
|
"start_relative": {
|
|
"value": "8",
|
|
"unit": "minutes"
|
|
}
|
|
}"""
|
|
|
|
|
|
def main():
|
|
# noinspection PyBroadException
|
|
try:
|
|
with open('alert_configs/test.yaml', 'w') as yaml_file:
|
|
yaml.dump(alert_file, yaml_file, default_flow_style=False)
|
|
except Exception:
|
|
print("Error writing alert config file")
|
|
return False
|
|
|
|
now = int(time.time() * 1000)
|
|
metrics_list.append([now, 1501])
|
|
now += 32000
|
|
metrics_list.append([now, 202])
|
|
now += 32000
|
|
metrics_list.append([now, 23])
|
|
now += 32000
|
|
metrics_list.append([now, 1504])
|
|
now += 32000
|
|
metrics_list.append([now, 2005])
|
|
now += 32000
|
|
metrics_list.append([now, 5006])
|
|
now += 32000
|
|
metrics_list.append([now, 1507])
|
|
full_string = json_string1 + str(metrics_list) + json_string2
|
|
try:
|
|
ret = requests.post(kairos_url, data=json.dumps(json.loads(full_string)), timeout=200)
|
|
assert ret.status_code == 204, "Wrong status code received from KairosDB"
|
|
except AssertionError as e:
|
|
print("Error: {}".format(str(e)))
|
|
except Exception as e:
|
|
print("Problem talking to KairosDB: {}".format(str(e)))
|
|
return False
|
|
print("Metrics sent to KairosDB. Check alerts in the #aom_test_channel in Slack")
|
|
time.sleep(360)
|
|
|
|
try:
|
|
ret = requests.post(kairos_query, data=json.dumps(json.loads(query_intro + status1 + query_outro)), timeout=200)
|
|
print("Recovery {}".format(dict(ret.json())['queries'][0]['results'][0]['values'][0][1]))
|
|
assert dict(ret.json())['queries'][0]['results'][0]['values'][0][1] == 2, "Wrong RECOVERY result"
|
|
ret = requests.post(kairos_query, data=json.dumps(json.loads(query_intro + status2 + query_outro)), timeout=200)
|
|
print("Warning {}".format(dict(ret.json())['queries'][0]['results'][0]['values'][0][1]))
|
|
assert dict(ret.json())['queries'][0]['results'][0]['values'][0][1] == 2, "Wrong WARNING result"
|
|
ret = requests.post(kairos_query, data=json.dumps(json.loads(query_intro + status3 + query_outro)), timeout=200)
|
|
print("Critical {}".format(dict(ret.json())['queries'][0]['results'][0]['values'][0][1]))
|
|
assert dict(ret.json())['queries'][0]['results'][0]['values'][0][1] == 4, "Wrong CRITICAL result"
|
|
except AssertionError as e:
|
|
print("Error: {}".format(str(e)))
|
|
except Exception as e:
|
|
print("Problem getting results from KairosDB: {}".format(str(e)))
|
|
return False
|
|
return True
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|