This repository has been archived by the owner on Oct 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
audit_healthbot_configuration.py
178 lines (140 loc) · 8.02 KB
/
audit_healthbot_configuration.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
############################################################################################################################
# This script prints some details of the healthbot configuration
############################################################################################################################
############################################################################################################################
# requirements: pip install requests
############################################################################################################################
############################################################################################################################
# usage:
# vi python_input.yml
# python ./audit_healthbot_configuration.py
############################################################################################################################
############################################################################################################################
# This block indicates the various imports
############################################################################################################################
import os
import json
import yaml
import requests
from jinja2 import Template
from requests.auth import HTTPBasicAuth
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from pprint import pprint
############################################################################################################################
# This block defines the functions we will use
############################################################################################################################
def import_variables_from_file():
my_variables_file=open('python_input.yml', 'r')
my_variables_in_string=my_variables_file.read()
my_variables_in_yaml=yaml.load(my_variables_in_string)
my_variables_file.close()
return my_variables_in_yaml
def create_directory(directory):
if not os.path.exists(directory):
os.makedirs(directory)
def get_devices_name_in_running_configuration():
r = requests.get(url + '/device/', auth=HTTPBasicAuth(authuser, authpwd), headers=headers, verify=False)
print '\n****************** list of devices in running configuration ******************'
pprint(r.json())
return r.status_code
def get_device_details_in_running_configuration(device):
r = requests.get(url + '/device/' + device['device-id'] + '/', auth=HTTPBasicAuth(authuser, authpwd), headers=headers, verify=False)
print '\n****************** device '+ device['device-id'] + ' ******************'
pprint (r.json())
return r.status_code
def get_devices_details_in_running_configuration():
r = requests.get(url + '/devices/', auth=HTTPBasicAuth(authuser, authpwd), headers=headers, verify=False)
print '\n****************** devices details in running configuration ******************'
pprint (r.json())
return r.status_code
def get_devices_name_in_candidate_configuration():
r = requests.get(url + '/device/?working=true', auth=HTTPBasicAuth(authuser, authpwd), headers=headers, verify=False)
print '\n****************** list of devices in candidate configuration ******************'
pprint(r.json())
return r.status_code
def get_devices_details_in_candidate_configuration():
r = requests.get(url + '/devices/', auth=HTTPBasicAuth(authuser, authpwd), headers=headers, verify=False)
print '\n****************** devices details in candidate configuration ******************'
pprint (r.json())
return r.status_code
def get_tables_and_views(table):
files = {'up_file': open('tables_and_views/' + table,'r')}
r=requests.get(url + '/files/helper-files/' + table, auth=HTTPBasicAuth(authuser, authpwd), headers={ 'Accept' : 'application/json', 'Content-Type': 'multipart/form-data' }, verify=False)
print '\n****************** table ' + table + ' ******************'
print r.content
return r.status_code
def get_device_group(group):
r = requests.get(url + '/device-group/'+ group + '/', auth=HTTPBasicAuth(authuser, authpwd), headers=headers, verify=False)
print '\n****************** device group ' + group + ' ******************'
pprint (r.json())
return r.status_code
def get_device_groups():
r = requests.get(url + '/device-group/', auth=HTTPBasicAuth(authuser, authpwd), headers=headers, verify=False)
print '\n****************** list of devices groups in running configuration ******************'
pprint (r.json())
return r.status_code
def get_notification(notification):
r = requests.get(url + '/notification/'+ notification['notification-name'] + '/', auth=HTTPBasicAuth(authuser, authpwd), headers=headers, verify=False
)
print '\n****************** notification ' + notification['notification-name'] + ' ******************'
pprint (r.json())
return r.status_code
def get_notifications():
r = requests.get(url + '/notification/', auth=HTTPBasicAuth(authuser, authpwd), headers=headers, verify=False)
print '\n****************** list of notifications in running configuration ******************'
pprint (r.json())
return r.status_code
def get_playbooks():
r = requests.get(url + '/playbook/', auth=HTTPBasicAuth(authuser, authpwd), headers=headers, verify=False)
print '\n****************** list of playbooks in running configuration ******************'
pprint (r.json())
return r.status_code
def get_playbook(playbook):
r = requests.get(url + '/playbook/'+ playbook['playbook-name'] + '/', auth=HTTPBasicAuth(authuser, authpwd), headers=headers, verify=False)
print '\n****************** playbook ' + playbook['playbook-name'] + ' ******************'
pprint (r.json())
return r.status_code
def get_topics():
r = requests.get(url + '/topic/', auth=HTTPBasicAuth(authuser, authpwd), headers=headers, verify=False)
print '\n****************** list of topics in running configuration ******************'
pprint (r.json())
return r.status_code
def get_topic(topic):
r = requests.get(url + '/topic/'+ topic['topic-name'] + '/', auth=HTTPBasicAuth(authuser, authpwd), headers=headers, verify=False)
print '\n****************** topic '+ topic['topic-name'] + ' ******************'
pprint (r.json())
return r.status_code
def get_rules(topic):
r = requests.get(url + '/topic/' + topic + '/rule/' , auth=HTTPBasicAuth(authuser, authpwd), headers=headers, verify=False)
pprint (r.json())
return r.status_code
def get_rule(topic, rule):
r = requests.get(url + '/topic/' + topic + '/rule/' + rule['rule-name'] + '/' , auth=HTTPBasicAuth(authuser, authpwd), headers=headers, verify=False)
print '\n****************** rule '+ topic + '/' + rule['rule-name'] + ' ******************'
pprint (r.json())
return r.status_code
############################################################################################################################
# Below block is the REST calls to print healthbot configuration
############################################################################################################################
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
my_variables_in_yaml=import_variables_from_file()
server = my_variables_in_yaml['server']
authuser = my_variables_in_yaml['authuser']
authpwd = my_variables_in_yaml['authpwd']
url = 'https://'+ server + ':8080/api/v1'
headers = { 'Accept' : 'application/json', 'Content-Type' : 'application/json' }
get_devices_name_in_running_configuration()
for item in my_variables_in_yaml['devices_list']:
get_device_details_in_running_configuration(item)
for item in my_variables_in_yaml['tables_and_views']:
get_tables_and_views(item)
get_notifications()
for item in my_variables_in_yaml['notifications']:
get_notification(item)
for item in my_variables_in_yaml['topics']:
get_topic(item)
for item in my_variables_in_yaml['playbooks']:
get_playbook(item)
get_device_groups()
for item in my_variables_in_yaml['device_groups']:
get_device_group(item['device-group-name'])