-
Notifications
You must be signed in to change notification settings - Fork 2
/
examples.py
189 lines (168 loc) · 5.27 KB
/
examples.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
import os
from ciscoservicability import sxml
from py_dotenv import read_dotenv
import datetime
from ciscoaxl import axl
from prettytable import PrettyTable
pt = PrettyTable()
#read environment variables from a .env file in current path
dotenv_path = os.path.join(os.path.dirname(__file__), '.env')
read_dotenv(dotenv_path)
cucm = os.getenv('cucm')
web_username = os.getenv('web_username')
web_password = os.getenv('web_password')
os_username = os.getenv('os_username')
os_password = os.getenv('os_password')
cucm_version = os.getenv('cucm_version')
#instantiate axl instance to get a list of the nodes in the cluster
ucm = axl(username=web_username,password=web_password,cucm=cucm,cucm_version=cucm_version)
#instantiate sxml instances for all nodes
service_hosts = []
callmans = ucm.list_process_nodes()
for callman in callmans:
if callman.name != 'EnterpriseWideData':
service_hosts.append(
sxml(
username=web_username,
password=web_password,
osusername=os_username,
ospassword=os_password,
cucm=callman.name
)
)
'''
list_products
returns list
.ProductName: String
.ProductVersion: String
.ProductDescription: String
.ProductID: String
.ShortName: String
'''
def listProducts():
products = ucm.list_products()
for product in products:
print(product.ProductName,': ', product.ProductVersion)
'''
list_services
returns list
.ServiceName: String
.ServiceType: String
.Deployable: Boolean
.GroupName: String
.DependentServices: List
'''
def listServices():
services = service_hosts[0].list_services()
for service in services:
print(service.ServiceName)
'''
status
returns list
.ServiceName: String
.ServiceStatus: String
.ReasonCode: int
.ReasonCodeString: String
.StartTime: DateTime
.UpTime: Seconds
'''
def getStatus(service='', node=''):
if node == '':
pt.field_names = ['Host', 'Service', 'Status', 'Uptime']
pt.align = "l"
for host in service_hosts:
statuses = host.status(service)
for status in statuses:
row = []
row.append(host.cucm)
row.append(status.ServiceName)
row.append(status.ServiceStatus)
row.append(str(datetime.timedelta(seconds=status.UpTime)))
pt.add_row(row)
print(pt)
else:
for host in service_hosts:
if host.cucm == node:
statuses = host.status(service)
for status in statuses:
row = []
row.append(host.cucm)
row.append(status.ServiceName)
row.append(status.ServiceStatus)
row.append(str(datetime.timedelta(seconds=status.UpTime)))
pt.add_row(row)
print(pt)
'''
restart(node, service)
returns String
'''
def restart(service, node=''):
if node == '': #restart service on all nodes
for host in service_hosts:
req = host.restart(node=host.cucm, service=service)
print(req)
else: #find instance of node and restart service on that node only
for host in service_hosts:
if host.cucm == node:
req = host.restart(node=host.cucm, service=service)
print(req)
'''
start(node, service)
returns String
'''
def start(service, node=''):
if node == '': #start service on all nodes
for host in service_hosts:
req = host.start(node=host.cucm, service=service)
print(req)
else: #find instance of node and start service on that node only
for host in service_hosts:
if host.cucm == node:
req = host.start(node=host.cucm, service=service)
print(req)
'''
stop(node, service)
returns String
'''
def stop(service, node=''):
if node == '': #stop service on all nodes
for host in service_hosts:
req = host.stop(node=host.cucm, service=service)
print(req)
else: #find instance of node and stop service on that node only
for host in service_hosts:
if host.cucm == node:
req = host.stop(node=host.cucm, service=service)
print(req)
'''
activate(node, service)
returns String
'''
def activate(service, node=''):
if node == '': #activate service on all nodes
for host in service_hosts:
req = host.activate(node=host.cucm, service=service)
print(req)
else: #find instance of node and activate service on that node only
for host in service_hosts:
if host.cucm == node:
req = host.activate(node=host.cucm, service=service)
print(req)
'''
deactivate(node, service)
returns String
'''
def activate(service, node=''):
if node == '': #deactivate service on all nodes
for host in service_hosts:
req = host.deactivate(node=host.cucm, service=service)
print(req)
else: #find instance of node and deactivate service on that node only
for host in service_hosts:
if host.cucm == 'node':
req = host.deactivate(node=host.cucm, service=service)
print(req)
def main():
getStatus(service='Cisco Tomcat')
if __name__ == "__main__":
main()