-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlbstats.py
223 lines (189 loc) · 7.8 KB
/
lbstats.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python3
import requests
import json
import sys
import argparse
import getpass
from auth import auth
parser = argparse.ArgumentParser(description="A tool that will generate all lb vservers and check its backend servers' status.")
parser.add_argument("-t", "--target", required=True, help="Target LB Vserver", metavar="")
parser.add_argument("-u", "--username", required=True, help="Username for logging in.", metavar="")
parser.add_argument("-v", "--vserver", help="Check specific LB Vserver", metavar="")
args = parser.parse_args()
host = args.target
uname = args.username
passwd = getpass.getpass("Password: ")
vserver = args.vserver
session = requests.Session()
base_path = "https://{}/nitro/v1".format(host)
session.verify=False
def main():
# Main function to execute script
print("\n[*] Connecting to {}".format(base_path))
try:
token = auth("{}/config".format(base_path), uname, passwd)
headers = {
"Content-Type": "application/json",
"Cookie": "NITRO_AUTH_TOKEN={}".format(token)
}
session.headers.update(headers)
check_ha_node()
get_route()
if vserver:
get_lb(vserver)
else:
get_all_lbs()
except KeyboardInterrupt:
print("[X] Quitting...")
sys.exit()
def get_route():
url = "{}/config/route".format(base_path)
response = session.get(url)
if response.ok:
print("\n[*] Route Information: ")
data = json.loads(response.text)
route = data["route"]
for net in route:
network = net["network"]
netmask = net["netmask"]
gw = net["gateway"]
type = net["routetype"]
print("\nNetwork: {}".format(network))
print("Netmask: {}".format(netmask))
print("Gateway: {}".format(gw))
print("Route Type: {}".format(type))
def check_ha_node():
url = "{}/config/hanode".format(base_path)
response = session.get(url)
if response.ok:
print("\n[*] HA Nodes Information: ")
data = json.loads(response.text)
hanode = data["hanode"]
for ha in hanode:
ip = ha["ipaddress"]
state = ha["state"]
status = ha["hastatus"]
print("\nIP Address: {}".format(ip))
print("State: {}".format(state))
print("Status: {}".format(status))
def get_lb(vserver):
url = "{}/config/lbvserver/{}".format(base_path, vserver)
response = session.get(url)
if response.ok:
data = json.loads(response.text)
lb_vservers = data["lbvserver"]
for lb_vserver in lb_vservers:
name = lb_vserver["name"]
state = lb_vserver["curstate"]
ip = lb_vserver["ipv46"]
port = lb_vserver["port"]
lbmethod = lb_vserver["lbmethod"]
blbmethod = lb_vserver["backuplbmethod"]
persistence = lb_vserver["persistencetype"]
print("\n[*] LB Vserver {}:".format(name))
print("IP Address: {}".format(ip))
print("Port: {}".format(port))
print("State {}".format(state))
print("Configured Method: {}, BackupMethod: {}".format(lbmethod, blbmethod))
print("Persistence Type: {}".format(persistence))
stats("lbvserver", name)
get_serviceGroups(name)
get_service(name)
def get_all_lbs():
# Retrieve all lb vservers and run each to the get_lb_binding() function.
url = "{}/config/lbvserver".format(base_path)
response = session.get(url)
if response.ok:
data = json.loads(response.text)
lb_vservers = data["lbvserver"]
for lb_vserver in lb_vservers:
name = lb_vserver["name"]
state = lb_vserver["curstate"]
ip = lb_vserver["ipv46"]
port = lb_vserver["port"]
lbmethod = lb_vserver["lbmethod"]
blbmethod = lb_vserver["backuplbmethod"]
persistence = lb_vserver["persistencetype"]
print("\n[*] LB Vserver {}:".format(name))
print("IP Address: {}".format(ip))
print("Port: {}".format(port))
print("State {}".format(state))
print("Configured Method: {}, BackupMethod: {}".format(lbmethod, blbmethod))
print("Persistence Type: {}".format(persistence))
stats("lbvserver", name)
get_serviceGroups(name)
get_service(name)
def get_serviceGroups(lb):
# Retrieve serviceGroups bound to an lb vserver.
url = "{}/config/lbvserver_servicegroup_binding/{}".format(base_path, lb)
response = session.get(url)
if response.ok:
data = json.loads(response.text)
try:
svc_grps = data["lbvserver_servicegroup_binding"]
for svc_grp in svc_grps:
name = svc_grp["servicegroupname"]
print("\nService Group {}: ".format(name))
get_serviceGroupBindings(name)
except KeyError:
svc_grps = None
def get_service(lb):
# Retrieve serviceGroups bound to an lb vserver.
url = "{}/config/lbvserver_service_binding/{}".format(base_path, lb)
response = session.get(url)
if response.ok:
data = json.loads(response.text)
try:
services = data["lbvserver_service_binding"]
for service in services:
name = service["servicename"]
ip = service["ipv46"]
port = service["port"]
cur_state = service["curstate"]
print("\nService: {}".format(name))
print("IP Address: {}".format(ip))
print("Port: {}".format(port))
print("Current State: {}".format(cur_state))
stats("service", name)
except KeyError:
service = None
def get_serviceGroupBindings(svc_grp):
# Retrieve the servers bound to a serviceGroup
url = "{}/config/servicegroup_binding/{}".format(base_path, svc_grp)
response = session.get(url)
if response.ok:
data = json.loads(response.text)
svrs = data["servicegroup_binding"]
for svr in svrs:
members = svr["servicegroup_servicegroupmember_binding"]
for member in members:
name = member["servicegroupname"]
ip = member["ip"]
port = member["port"]
state = member["svrstate"]
servername = member["servername"]
print("\nServer Name: {}".format(servername))
print("IP Address: {}".format(ip))
print("Port: {}".format(port))
print("State: {}".format(state))
stats("servicegroupmember", name, servername, port)
def stats(resource, object, ip=None, port=None):
if resource == "servicegroupmember":
url = "{}/stat/{}/{}?args=servername:{},port:{}".format(base_path, resource, object, ip, port)
elif resource == "service":
url = "{}/stat/{}/{}".format(base_path, resource, object)
elif resource == "lbvserver":
url = "{}/stat/{}/{}".format(base_path, resource, object)
response = session.get(url)
if response.ok:
data = json.loads(response.text)
members = data[resource]
for member in members:
t_req = member["totalrequests"]
t_resp = member["totalresponses"]
current_svr_conn = member["cursrvrconnections"]
print("Total Requests: {}".format(t_req))
print("Total Response: {}".format(t_resp))
print("Current server connections: {}".format(current_svr_conn))
if __name__ == '__main__':
main()