forked from mfcodeworks/Server-Monitoring-Script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_monitor.py
More file actions
301 lines (267 loc) · 10.5 KB
/
Copy pathserver_monitor.py
File metadata and controls
301 lines (267 loc) · 10.5 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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import argparse, socket, time, json, datetime, platform, psutil, requests, pprint, uuid
from retrying import retry
import yaml, pynvml
###############################################################################
# GPU
###############################################################################
HAS_CUDA = False
try:
pynvml.nvmlInit()
HAS_CUDA = True
except:
HAS_CUDA = False
def gpu_count():
if not HAS_CUDA:
return 0
else:
return pynvml.nvmlDeviceGetCount()
def gpu_mem_used_pct():
'''
return a list of percentage of memory used
'''
if gpu_count() == 0:
return []
l_GPUs = list(range(gpu_count()))
l_mm = []
for i in l_GPUs:
h = pynvml.nvmlDeviceGetHandleByIndex(i)
info = pynvml.nvmlDeviceGetMemoryInfo(h)
l_mm.append(info.used / info.total)
return l_mm
###############################################################################
# Machine Info
###############################################################################
def get_machine_data(verbose = False):
'''
return details about the machine in JSON format
'''
# Hostname Info
hostname = socket.gethostname()
# CPU Info
cpu_count = psutil.cpu_count()
cpu_usage = psutil.cpu_percent(interval=1)
# Memory Info
memory_stats = psutil.virtual_memory()
memory_total = memory_stats.total
memory_used = memory_stats.used
memory_used_percent = memory_stats.percent
# Disk Info
disk_info = psutil.disk_partitions()
disks = []
for x in disk_info:
# Try fixes issues with connected 'disk' such as CD-ROMS, Phones, etc.
try:
disk = {
"name" : x.device,
"mount_point" : x.mountpoint,
"type" : x.fstype,
"total_size" : psutil.disk_usage(x.mountpoint).total,
"used_size" : psutil.disk_usage(x.mountpoint).used,
"percent_used" : psutil.disk_usage(x.mountpoint).percent
}
disks.append(disk)
except:
print("")
# Bandwidth Info
network_stats = get_bandwidth()
# Network Info
nics = []
for name, snic_array in psutil.net_if_addrs().items():
# Create NIC object
nic = {
"name": name,
"mac": "",
"address": "",
"address6": "",
"netmask": ""
}
# Get NiC values
for snic in snic_array:
if snic.family == -1:
nic["mac"] = snic.address
elif snic.family == 2:
nic["address"] = snic.address
nic["netmask"] = snic.netmask
elif snic.family == 23:
nic["address6"] = snic.address
nics.append(nic)
# Platform Info
system = {
"name" : platform.system(),
"version" : platform.release()
}
# Time Info
timestamp = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S+00:00")
uptime = int(time.time() - psutil.boot_time())
# System UUID
sys_uuid = uuid.getnode()
# Set Machine Info
machine = {
"hostname" : hostname,
"uuid" : sys_uuid,
"system" : system,
"uptime" : uptime,
"cpu_count" : cpu_count,
"cpu_usage" : cpu_usage,
"memory_total" : memory_total,
"memory_used" : memory_used,
"memory_used_percent" : memory_used_percent,
"drives" : disks,
"root_drive_used_percent": psutil.disk_usage("/").percent,
"network_up" : network_stats["traffic_out"],
"network_down" : network_stats["traffic_in"],
"network_cards": nics,
"timestamp" : timestamp
}
if HAS_CUDA:
machine["gpu_memory_max_used_percent"] = round(max(gpu_mem_used_pct())* 100,2)
if verbose:
print("\nData:")
pprint.pprint(machine, indent=4)
return machine
def get_bandwidth():
'''
Get net in/out
'''
net1_out = psutil.net_io_counters().bytes_sent
net1_in = psutil.net_io_counters().bytes_recv
time.sleep(1)
# Get new net in/out
net2_out = psutil.net_io_counters().bytes_sent
net2_in = psutil.net_io_counters().bytes_recv
# Compare and get current speed
if net1_in > net2_in:
current_in = 0
else:
current_in = net2_in - net1_in
if net1_out > net2_out:
current_out = 0
else:
current_out = net2_out - net1_out
network = {"traffic_in" : current_in, "traffic_out" : current_out}
return network
def format_machine_data(machine_data, l_keys = None):
'''
take machine's JSON and return as formatted string
'''
if l_keys:
machine_data = {k : v for k, v in machine_data.items() if k in l_keys}
text = ''
for k, v in machine_data.items():
text += f"*{k}* : `{v}`\n"
return text
###############################################################################
# Sending Alerts
###############################################################################
def send_data(data, endpoint = None, attempts = 30, timeout = 60):
'''
# [DEPRECATED]
# Attempt to send data up to 30 times
# endpoint = monitoring server
'''
if not endpoint:
return None
for attempt in attempts:
try:
response = requests.post(url = endpoint, data = data)
print("\nPOST:")
print("Response:", response.status_code)
print("Headers:")
pprint.pprint(response.headers)
print("Content:", response.content)
# Attempt printing response in JSON if possible
try:
print("JSON Content:")
pprint.pprint(response.json())
except:
print("No JSON content")
break
except requests.exceptions.RequestException as e:
print("\nPOST Error:\n",e)
# Sleep 1 minute before retrying
time.sleep(timeout)
else:
# If no connection established for attempts*timeout, kill script
exit(0)
def retry_if_result_none(result):
"""
Return True if we should retry (in this case when result is None), False otherwise
"""
return result is None
@retry(stop_max_attempt_number = 3, retry_on_result = retry_if_result_none)
def send_slack_data(endpoint, token, channel, str_msg, title = ''):
'''
send a slack message and return true if successful
'''
headers = {"authorization": f"Bearer {token}", "content-type": "application/json"}
text = f"*{title}*\n---------------------\n{str_msg}" if title else str_msg
payload = {"channel": channel, "text": text}
try:
response = requests.post( url = endpoint, data = json.dumps(payload), headers = headers)
if response.status_code != 200:
print(f'send_slack_data: POST response has status code {response.status_code}')
return None
else:
return True
except requests.exceptions.RequestException as e:
print(f'send_slack_data: POST error: {e}')
return None
###############################################################################
# App Entry Point
###############################################################################
def main(endpoint = None, machine_ulimit = None, slack_token = None, slack_channel = None, verbose = True):
'''
'''
if endpoint: # then slack data is required
if not slack_token or not slack_channel:
raise RuntimeError(f'server_monitor.main: args slack_token and slack_channel must be provided with endpoint={endpoint}')
machine = get_machine_data(verbose = verbose)
if machine_ulimit: # only send Slack Message if limit is reach
l_checks = [ machine[k] > float(v)
for k, v in machine_ulimit.items()]
if any(l_checks) and endpoint:
l_limit_reached = [f"`{k}`" for c, k in zip(l_checks, list(machine_ulimit.keys())) if c]
send_slack_data(endpoint = endpoint, token = slack_token,
channel = slack_channel,
str_msg = format_machine_data(machine,
l_keys= ["hostname", "system", "uptime", "cpu_count", "cpu_usage",
"memory_used_percent", "root_drive_used_percent", "gpu_memory_max_used_percent",
"timestamp"]
),
title = f'Server Monitor LIMIT ({", ".join(l_limit_reached)}) REACHED')
else: # send everything
if endpoint:
send_slack_data(endpoint = endpoint, token = slack_token,
channel = slack_channel, str_msg = format_machine_data(machine),
title = 'Server Monitor Update')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Monitoring script to send system info to Slack')
parser.add_argument('-d', '--dest', default= None, help='API Endpoint for Monitoring Data (Defaults to None)')
parser.add_argument('-i', '--interval', default=5, type=int, help='Interval between checks (Seconds. Defaults to 5 seconds)')
parser.add_argument('-a', '--attempts', default=30, type=int, help='Attempts to send data when sending failes (Defaults to 30)')
parser.add_argument('-t', '--timeout', default=60, type=int, help='Timeout between resend attempts (Seconds. Defaults to 60. If attempts is reached script will die)')
parser.add_argument('-c', '--config', default = None, help = 'optional config file path')
parser.add_argument('--slack_token', default = None, help = 'optional slack token')
parser.add_argument('--slack_channel', default = None, help = 'optional slack channel')
args = parser.parse_args()
# Factor in sleep for bandwidth checking
if args.interval >= 2:
args.interval -= 2
while True:
# read config file
if args.config:
with open(args.config, 'r') as ymlfile:
cfg = yaml.load(ymlfile, Loader = yaml.BaseLoader)
cfg = cfg['dev']['environment']
args.interval = int(cfg['interval'])
# --- DEPRECATED ---
# args.attempts = int(cfg['attempts'])
# args.timeout = int(cfg['timeout'])
main(endpoint = cfg['dest'], machine_ulimit=cfg['machine_ulimit'],
slack_token = cfg['slack']['token'],
slack_channel = cfg['slack']['channel'], verbose=True)
else:
main(verbose = True, endpoint = args.dest, slack_token = args.slack_token,
slack_channel = args.slack_channel)
print(f"--------------------- Server Monitor {args.interval}s Update ----------------------")
time.sleep(args.interval)