-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_recur_tasks.py
More file actions
146 lines (133 loc) · 6.07 KB
/
get_recur_tasks.py
File metadata and controls
146 lines (133 loc) · 6.07 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from dotenv import load_dotenv
import os
import requests
import json
import csv
from datetime import datetime, date
from urllib.parse import quote
load_dotenv()
RUNZERO_CLIENT_ID = os.getenv("RUNZERO_CLIENT_ID")
RUNZERO_CLIENT_SECRET = os.getenv("RUNZERO_CLIENT_SECRET")
RUNZERO_BASE_URL = 'https://console.runzero.com/api/v1.0'
# Authentication with client ID and secret and obtain bearer token
def get_token():
token_request_url = f'{RUNZERO_BASE_URL}/account/api/token'
token_request_header = {"Content-Type": "application/x-www-form-urlencoded"}
token_request_data = {"grant_type": "client_credentials"}
token_response = requests.post(token_request_url, data=token_request_data, headers=token_request_header, verify=True, auth=(RUNZERO_CLIENT_ID, RUNZERO_CLIENT_SECRET))
if token_response.status_code != 200:
print("Failed to obtain token from OAuth server.")
exit(1)
else:
token_json = json.loads(token_response.text)
return token_json['access_token']
# Get client id
def get_client_id(token):
account = requests.get(f'{RUNZERO_BASE_URL}/account/orgs', headers={"Content-Type": "application/json", "Authorization": "Bearer " + token})
if account.status_code != 200:
print("Failed to retrieve account information.")
exit(1)
account_json = account.json()
return account_json[0].get('client_id','')
# Get all organization within defined account
def get_organizations(token):
orgs = requests.get(f'{RUNZERO_BASE_URL}/account/orgs', headers={"Content-Type": "application/json", "Authorization": "Bearer " + token})
if orgs.status_code != 200:
print("Failed to retrieve organization data.")
exit(1)
return json.loads(orgs.text)
# Get all tasks within specified organization
def get_recurring_tasks(token, org_id):
search = quote('recur:=true')
tasks = requests.get(f'{RUNZERO_BASE_URL}/org/tasks?search={search}&_oid={org_id}', headers={"Content-Type": "application/json", "Authorization": "Bearer " + token})
if tasks.status_code != 200:
print("Failed to retrieve task data.")
exit(1)
return tasks
# Output final results to a csv file
def write_to_csv(output: list, filename: str, fieldnames: list):
file = open(filename, "w")
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(output)
file.close()
def main():
recur_tasks_output = []
recur_tasks_fields = [
"organization_name",
"organization_id",
"task_id",
"task_name",
"description",
"type",
"status",
"error",
"created_by",
"created_at",
"updated_at",
"site_id",
"site_name",
"agent_id",
"agent_name",
"start_time",
"grace_period",
"recur_frequency",
"recur_last",
"recur_next",
"template_id",
"scan_rate",
"max_host_rate",
"max_group_size",
"host_ping",
"subnet_ping",
"subnet_ping_sample_rate",
"nameservers",
"targets",
"excludes"
]
access_token = get_token()
orgs = get_organizations(access_token)
for o in orgs:
org_id = o.get('id', '')
org_name = o.get('name', '')
recur_tasks = get_recurring_tasks(access_token, org_id)
recur_tasks_json = recur_tasks.json()
for item in recur_tasks_json:
recur_tasks_output.append({
'organization_name':org_name,
'organization_id':org_id,
'task_id':item.get('id', ''),
'task_name':item.get('name', ''),
'description':item.get('description', ''),
'type':item.get('type', ''),
'status':item.get('status', ''),
'error':item.get('error', ''),
'created_by':item.get('created_by', ''),
'created_at':datetime.fromtimestamp(item.get('created_at', '')).strftime('%Y-%m-%d %H:%M:%S'),
'updated_at':datetime.fromtimestamp(item.get('updated_at', '')).strftime('%Y-%m-%d %H:%M:%S'),
'site_id':item.get('site_id', ''),
'site_name':item.get('site_name', ''),
'agent_id':item.get('agent_id', ''),
'agent_name':item.get('agent_name', ''),
'start_time':datetime.fromtimestamp(item.get('start_time', '')).strftime('%Y-%m-%d %H:%M:%S'),
'grace_period':item.get('grace_period', ''),
'recur_frequency':item.get('recur_frequency', ''),
'recur_last':datetime.fromtimestamp(item.get('recur_last', '')).strftime('%Y-%m-%d %H:%M:%S'),
'recur_next':datetime.fromtimestamp(item.get('recur_next', '')).strftime('%Y-%m-%d %H:%M:%S'),
'template_id':item.get('template_id', ''),
'scan_rate':item.get('params', {}).get('rate', ''),
'max_host_rate':item.get('params', {}).get('max-host-rate', ''),
'max_group_size':item.get('params', {}).get('max-group-size', ''),
'host_ping':item.get('params', {}).get('host-ping', ''),
'subnet_ping':item.get('params', {}).get('subnet-ping', ''),
'subnet_ping_sample_rate':item.get('params', {}).get('subnet-ping-sample-rate', ''),
'nameservers':item.get('params', {}).get('nameservers', ''),
'targets':item.get('params', {}).get('targets', ''),
'excludes':item.get('params', {}).get('excludes', ''),
})
client_id = get_client_id(access_token)
recur_tasks_output_file = 'tasks_recurring_' + client_id + '_' + date.today().strftime("%Y%m%d") + '.csv'
write_to_csv(output=recur_tasks_output, filename=recur_tasks_output_file, fieldnames=recur_tasks_fields)
print('Recurring tasks saved to ' + os.getcwd() + '/' + recur_tasks_output_file)
if __name__ == '__main__':
main()