forked from flakshack/SysAdminBoard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msexchange.py
264 lines (231 loc) · 11.3 KB
/
msexchange.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env python
"""msexchange - This script will contact the pyPerfmon webservice on some Exchange servers to get perfmon data"""
import json
import time
from datetime import datetime
from urllib.request import urlopen
import logging.config
__author__ = '[email protected] (Scott Vintinner)'
# =================================SETTINGS======================================
SAMPLE_INTERVAL = 60 # How often do we update the performance counter data
MAX_DATAPOINTS = 30 # How many datapoints to we keep
# ===============================================================================
class MonitorJSON:
"""This is a simple class passed to Monitor threads so we can access the current JSON data in that thread"""
def __init__(self):
self.edge1_send_total = 0
self.edge1_receive_total = 0
self.edge1_previous_send_total = 0
self.edge1_previous_receive_total = 0
self.edge1_send_datapoints = []
self.edge1_receive_datapoints = []
self.edge2_send_total = 0
self.edge2_receive_total = 0
self.edge2_previous_send_total = 0
self.edge2_previous_receive_total = 0
self.edge2_send_datapoints = []
self.edge2_receive_datapoints = []
self.outlook1_rpc_avg_latency = []
self.outlook1_rpc_active_users = []
self.outlook1_rpc = 0
self.outlook1_rpc_total = []
self.outlook2_rpc_avg_latency = []
self.outlook2_rpc_active_users = []
self.outlook2_rpc_total = []
self.outlook2_rpc = 0
self.datetime = datetime(2000, 1, 1)
self.json = json.dumps({
"edge1_send_datapoints": [0],
"edge1_receive_datapoints": [0],
"edge1_send_total": "--",
"edge1_receive_total": "--",
"edge2_send_datapoints": [0],
"edge2_receive_datapoints": [0],
"edge2_send_total": "--",
"edge2_receive_total": "--",
"outlook1_rpc_avg_latency": [],
"outlook1_rpc_active_users": [],
"outlook1_rpc_total": [],
"outlook2_rpc_avg_latency": [],
"outlook2_rpc_active_users": [],
"outlook2_rpc_total": []
})
def generate_json(perf_monitor):
"""This function is a callback that is executed as a separate thread from the webserver"""
logger = logging.getLogger("msexchange")
# ====================EDGE1=====================
try:
response = urlopen('http://edge1:8001')
html = (response.read()).decode('utf-8')
logger.debug("edge1:" + html)
data = json.loads(html)
except Exception as error:
logger.error("Error getting data from edge1: " + str(error))
perf_monitor.json = json.dumps({"error": "Error with edge1: " + str(error)})
return
smtp_send_total = int(data["smtp_send_total"])
smtp_receive_total = int(data["smtp_receive_total"])
if perf_monitor.edge1_send_total == 0: # If this is the first run, just set it to zero
smtp_send_delta = 0
smtp_receive_delta = 0
else:
# Calculate the delta by old total minus new total
smtp_send_delta = smtp_send_total - perf_monitor.edge1_send_total
smtp_receive_delta = smtp_receive_total - perf_monitor.edge1_receive_total
# Add the delta to our datapoints lists
perf_monitor.edge1_send_datapoints.append(smtp_send_delta)
perf_monitor.edge1_receive_datapoints.append(smtp_receive_delta)
# The Send/Recieve_Total counters are since last server reboot. Instead of showing that total,
# we start at 0 for the day, then reset each new day.
if perf_monitor.datetime.date() != datetime.today().date():
perf_monitor.edge1_send_total = 0
perf_monitor.edge1_receive_total = 0
perf_monitor.edge1_previous_send_total = smtp_send_total
perf_monitor.edge1_previous_receive_total = smtp_receive_total
else: #
perf_monitor.edge1_send_total = smtp_send_total - perf_monitor.edge1_previous_send_total
perf_monitor.edge1_receive_total = smtp_receive_total - perf_monitor.edge1_previous_receive_total
# If we've reached the max datapoints, delete the oldest
if len(perf_monitor.edge1_send_datapoints) >= MAX_DATAPOINTS:
del(perf_monitor.edge1_send_datapoints[0])
if len(perf_monitor.edge1_receive_datapoints) >= MAX_DATAPOINTS:
del(perf_monitor.edge1_receive_datapoints[0])
# ====================EDGE2=====================
try:
response = urlopen('http://edge2:8001')
html = (response.read()).decode('utf-8')
logger.debug("edge2:" + html)
data = json.loads(html)
except Exception as error:
logger.error("Error getting data from edge2: " + str(error))
perf_monitor.json = json.dumps({"error": "Error with edge2: " + str(error)})
return
smtp_send_total = int(data["smtp_send_total"])
smtp_receive_total = int(data["smtp_receive_total"])
if perf_monitor.edge2_send_total == 0: # If this is the first run, just set it to zero
smtp_send_delta = 0
smtp_receive_delta = 0
else:
# Calculate the delta by old total minus new total
smtp_send_delta = smtp_send_total - perf_monitor.edge2_send_total
smtp_receive_delta = smtp_receive_total - perf_monitor.edge2_receive_total
# Add the delta to our datapoints lists
perf_monitor.edge2_send_datapoints.append(smtp_send_delta)
perf_monitor.edge2_receive_datapoints.append(smtp_receive_delta)
# The Send/Recieve_Total counters are since last server reboot. Instead of showing that total,
# we start at 0 for the day, then reset each new day.
if perf_monitor.datetime.date() != datetime.today().date():
perf_monitor.edge2_send_total = 0
perf_monitor.edge2_receive_total = 0
perf_monitor.edge2_previous_send_total = smtp_send_total
perf_monitor.edge2_previous_receive_total = smtp_receive_total
perf_monitor.datetime = datetime.today()
else: #
perf_monitor.edge2_send_total = smtp_send_total - perf_monitor.edge2_previous_send_total
perf_monitor.edge2_receive_total = smtp_receive_total - perf_monitor.edge2_previous_receive_total
# If we've reached the max datapoints, delete the oldest
if len(perf_monitor.edge2_send_datapoints) >= MAX_DATAPOINTS:
del(perf_monitor.edge2_send_datapoints[0])
if len(perf_monitor.edge2_receive_datapoints) >= MAX_DATAPOINTS:
del(perf_monitor.edge2_receive_datapoints[0])
# CAS Perf Data Format: {"rpc_avg_latency": 7.0, "rpc_active_users": 22.0, "rpc_total": 30777797.0}
# ====================OUTLOOK1=====================
try:
response = urlopen('http://outlook1:8002')
html = (response.read()).decode('utf-8')
logger.debug("outlook1:" + html)
data = json.loads(html)
except Exception as error:
logger.error("Error getting data from outlook1: " + str(error))
perf_monitor.json = json.dumps({"error": "Error with outlook1: " + str(error)})
return
# Add the datapoints
perf_monitor.outlook1_rpc_active_users.append(int(data["rpc_active_users"]))
perf_monitor.outlook1_rpc_avg_latency.append(int(data["rpc_avg_latency"]))
# RPC count is a total, so convert it to deltas
current_rpc_total = int(data["rpc_total"])
if perf_monitor.outlook1_rpc == 0:
perf_monitor.outlook1_rpc_total = [0]
perf_monitor.outlook1_rpc = current_rpc_total
else:
rpc_delta = current_rpc_total - perf_monitor.outlook1_rpc
perf_monitor.outlook1_rpc_total.append(rpc_delta)
perf_monitor.outlook1_rpc = current_rpc_total
# If we've reached the max datapoints, delete the oldest
if len(perf_monitor.outlook1_rpc_active_users) >= MAX_DATAPOINTS:
del(perf_monitor.outlook1_rpc_active_users[0])
del(perf_monitor.outlook1_rpc_avg_latency[0])
del(perf_monitor.outlook1_rpc_total[0])
# ====================OUTLOOK2=====================
try:
response = urlopen('http://outlook2:8001')
html = (response.read()).decode('utf-8')
logger.debug("outlook2:" + html)
data = json.loads(html)
except Exception as error:
logger.error("Error getting data from: outlook2: " + str(error))
perf_monitor.json = json.dumps({"error": "Error with outlook2: " + str(error)})
return
# Add the datapoints
perf_monitor.outlook2_rpc_active_users.append(int(data["rpc_active_users"]))
perf_monitor.outlook2_rpc_avg_latency.append(int(data["rpc_avg_latency"]))
# RPC count is a total, so convert it to deltas
current_rpc_total = int(data["rpc_total"])
if perf_monitor.outlook2_rpc == 0:
perf_monitor.outlook2_rpc_total = [0]
perf_monitor.outlook2_rpc = current_rpc_total
else:
rpc_delta = current_rpc_total - perf_monitor.outlook2_rpc
perf_monitor.outlook2_rpc_total.append(rpc_delta)
perf_monitor.outlook2_rpc = current_rpc_total
# If we've reached the max datapoints, delete the oldest
if len(perf_monitor.outlook2_rpc_active_users) >= MAX_DATAPOINTS:
del(perf_monitor.outlook2_rpc_active_users[0])
del(perf_monitor.outlook2_rpc_avg_latency[0])
del(perf_monitor.outlook2_rpc_total[0])
# Create the JSON string for output
perf_monitor.json = json.dumps({
"edge1_send_datapoints": perf_monitor.edge1_send_datapoints,
"edge1_receive_datapoints": perf_monitor.edge1_receive_datapoints,
"edge1_send_total": perf_monitor.edge1_send_total,
"edge1_receive_total": perf_monitor.edge1_receive_total,
"edge2_send_datapoints": perf_monitor.edge2_send_datapoints,
"edge2_receive_datapoints": perf_monitor.edge2_receive_datapoints,
"edge2_send_total": perf_monitor.edge2_send_total,
"edge2_receive_total": perf_monitor.edge2_receive_total,
"outlook1_rpc_avg_latency": perf_monitor.outlook1_rpc_avg_latency,
"outlook1_rpc_active_users": perf_monitor.outlook1_rpc_active_users,
"outlook1_rpc_total": perf_monitor.outlook1_rpc_total,
"outlook2_rpc_avg_latency": perf_monitor.outlook2_rpc_avg_latency,
"outlook2_rpc_active_users": perf_monitor.outlook2_rpc_active_users,
"outlook2_rpc_total": perf_monitor.outlook2_rpc_total
})
logger.debug(perf_monitor.json)
# ======================================================
# __main__
#
# If you run this module by itself, it will instantiate
# the MonitorJSON class and start an infinite loop
# printing data.
# ======================================================
#
if __name__ == '__main__':
# When run by itself, we need to create the logger object (which is normally created in webserver.py)
try:
f = open("log_settings.json", 'rt')
log_config = json.load(f)
f.close()
logging.config.dictConfig(log_config)
except FileNotFoundError as e:
print("Log configuration file not found: " + str(e))
logging.basicConfig(level=logging.DEBUG) # fallback to basic settings
except json.decoder.JSONDecodeError as e:
print("Error parsing logger config file: " + str(e))
raise
monitor = MonitorJSON()
while True:
main_logger = logging.getLogger(__name__)
generate_json(monitor)
# Wait X seconds for the next iteration
main_logger.debug("Waiting for " + str(SAMPLE_INTERVAL) + " seconds")
time.sleep(SAMPLE_INTERVAL)