-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_domain_expiry_synthetics.py
63 lines (47 loc) · 1.83 KB
/
update_domain_expiry_synthetics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""_summary_
"""
import os
from dotenv import load_dotenv
import synthetics_functions
def main():
load_dotenv()
api_key = os.environ.get("NEW_RELIC_API_KEY")
headers = {"X-Api-Key": api_key, "Content-Type": "application/json"}
graph_url = "https://api.newrelic.com/graphql"
rest_url = "https://synthetics.newrelic.com/synthetics/api/v3/monitors"
url_file = "test_urls.txt"
js_file = 'test.js'
query = "name LIKE 'Domain Expiry'"
c = 0
urls = synthetics_functions.get_urls(url_file)
monitors = synthetics_functions.get_entities(graph_url, query, headers)
for url in urls:
clean_url = url.strip()
for i in range(len(monitors)):
if clean_url:
ids = synthetics_functions.get_ids(i, clean_url, monitors)
else:
print(f"{clean_url} not clean, trying next URL")
break
if ids is IndexError:
i += 1
elif ids is False:
print(f"{clean_url} monitor not found")
break
else:
monitor_id = ids[0]
encoded_script = synthetics_functions.create_domain_check_payload(js_file, clean_url)
payload = {"scriptText": encoded_script, "monitorId": monitor_id}
script_endpoint = f'{rest_url}/{monitor_id}/script'
break
response = synthetics_functions.make_rest_put_request(script_endpoint, headers, payload)
if response.status_code == 204:
c += 1
print(f"{c}/{len(urls)} : {response.status_code}/Ok : `{clean_url}` monitor updated")
else:
raise Exception(f"Query failed: {response.text}")
print("All found URLs have been processed.")
if __name__ == "__main__":
main()