Last active
March 6, 2023 11:17
-
-
Save MrHamel/1b640a81ded45bfbac564d2fd4f9532c to your computer and use it in GitHub Desktop.
Server Monitor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests, json | |
import os, psutil, re, time | |
def parse_syslog(): | |
syslog = "/var/log/messages" | |
if os.path.isfile("/var/log/syslog"): | |
syslog = "/var/log/syslog" | |
f = open(syslog, 'r') | |
f.seek(0, 2) | |
while True: | |
line = '' | |
while len(line) == 0 or line[-1] != '\n': | |
tail = f.readline() | |
if tail == '': | |
time.sleep(0.1) | |
continue | |
line += tail | |
output = parse_syslog_entry(line) | |
send_data(output) | |
def parse_syslog_entry(line): | |
data = line.split(": ") | |
message = data[1].strip() | |
context = data[0].split() | |
ts = ' '.join(context[0:3]) | |
hostname = context[3] | |
process = context[4] | |
if "[" in process and "]" in process: | |
pid = re.findall(r'\[(\d+)\]', process)[0] | |
process = process.replace("["+pid+"]", "") | |
else: | |
pid = 0 | |
return {"ts":ts, "hostname":hostname, "pid":pid, "process":process, "message":message} | |
def update_file(file, data): | |
file_content = "" | |
if not os.path.isfile("/var/log/health/"+file+".csv"): | |
file_content += 'ts,'+','.join(data.keys()) + "\n" | |
ts = str(time.time())[:-3] + ',' | |
file_content += ts+','.join([str(v) for k, v in data.items()]) + "\n" | |
with open("/var/log/health/"+file+".csv", "a+") as f: | |
f.write(file_content) | |
f.truncate() | |
def update_top_info(): | |
top_data = {} | |
for k in psutil.cpu_times().__dict__: | |
top_data[k] = getattr(psutil.cpu_times(), k) | |
update_file("top_data", top_data) | |
return top_data | |
def update_ram_usage(): | |
ram_data = {} | |
for k in psutil.virtual_memory().__dict__: | |
ram_data[k] = getattr(psutil.virtual_memory(), k) | |
update_file("ram_usage", ram_data) | |
return ram_data | |
def update_swap_usage(): | |
swap_data = {} | |
for k in psutil.swap_memory().__dict__: | |
swap_data[k] = getattr(psutil.swap_memory(), k) | |
update_file("swap_usage", swap_data) | |
return swap_data | |
def update_disk_usage(): | |
disk_data = {} | |
for mp in psutil.disk_partitions(): | |
disk_data[mp.mountpoint] = {"mountpoint":mp.mountpoint} | |
data = psutil.disk_usage(mp.mountpoint) | |
for k in data.__dict__: | |
disk_data[mp.mountpoint][k] = getattr(data, k) | |
update_file("disk_usage", disk_data[mp.mountpoint]) | |
return disk_data | |
def update_disk_io(): | |
disk_data = {} | |
for d, data in psutil.disk_io_counters(perdisk=True).items(): | |
disk_data[d] = {"partition":d} | |
for k in data.__dict__: | |
disk_data[d][k] = getattr(data, k) | |
update_file("disk_io", disk_data[d]) | |
return disk_data | |
def update_network_io(): | |
net_data = {} | |
for n, data in psutil.net_io_counters(pernic=True).items(): | |
net_data[n] = {"nic":n} | |
for k in data.__dict__: | |
net_data[n][k] = getattr(data, k) | |
update_file("net_io", net_data[n]) | |
return net_data | |
def update_temp_data(): | |
temp_data = {} | |
for t, data in psutil.sensors_temperatures().items(): | |
temp_data[t] = [] | |
for d in data: | |
temp = {"probe":t} | |
for k in d.__dict__: | |
temp[k] = getattr(d, k) | |
temp_data[t].append(temp) | |
update_file("temps", temp) | |
return temp_data | |
def update_fan_speed(): | |
temp_data = {} | |
for t, data in psutil.sensors_fans().items(): | |
temp_data[t] = [] | |
for d in data: | |
temp = {"probe":t} | |
for k in d.__dict__: | |
temp[k] = getattr(d, k) | |
temp_data[t].append(temp) | |
update_file("fans", temp) | |
return temp_data | |
def update_cpu_usage(): | |
cpu_data = {} | |
data = psutil.cpu_percent(interval=1, percpu=True) | |
for cpu in data: | |
cpu_data["CPU"+str(data.index(cpu))] = cpu | |
update_file("cpu_usage", cpu_data) | |
return cpu_data | |
def update_process_list(): | |
proc_data = [] | |
for proc in psutil.process_iter(attrs=['pid', 'name']): | |
proc_data.append(proc.info) | |
update_file("process_list", proc.info) | |
return proc_data | |
def send_data(data): | |
data = json.dumps(data) | |
#requests.post("http://collector.box/data", data=data) | |
pass | |
def check_service_status(service): | |
global reported_data | |
status = False | |
for process in reported_data["processes"]: | |
if process["name"] == service: | |
status = True | |
break | |
update_file("service_status", {"service":service, "status":status}) | |
return {"service":service, "status":status} | |
services = ["ssh", "nginx"] | |
if not os.path.isdir("/var/log/health/"): | |
os.mkdir("/var/log/health") | |
reported_data = {} | |
reported_data["top"] = update_top_info() | |
reported_data["cpu"] = update_cpu_usage() | |
reported_data["ram"] = update_ram_usage() | |
reported_data["swap"] = update_swap_usage() | |
reported_data["disk"] = update_disk_usage() | |
reported_data["io"] = update_disk_io() | |
reported_data["net"] = update_network_io() | |
reported_data["temps"] = update_temp_data() | |
reported_data["fans"] = update_fan_speed() | |
reported_data["processes"] = update_process_list(); | |
reported_data["services"] = [] | |
for service in services: | |
reported_data["services"].append(check_service_status(service)) | |
send_data(reported_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment