Skip to content

Commit

Permalink
Create network_health.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 7, 2024
1 parent 2846ccd commit 4187184
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions projects/piguardian/monitoring/network_health.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os
import psutil
import ping3

class NetworkHealth:
def __init__(self):
self.nodes = ['node1', 'node2', 'node3'] # list of node names or IP addresses
self.ping_results = {}

def ping_nodes(self):
for node in self.nodes:
try:
ping_time = ping3.ping(node, timeout=1)
self.ping_results[node] = ping_time
except Exception as e:
self.ping_results[node] = str(e)

def check_node_status(self):
for node, ping_time in self.ping_results.items():
if ping_time is not None:
if ping_time > 100:
print(f"Node {node} is slow to respond ({ping_time} ms)")
else:
print(f"Node {node} is online ({ping_time} ms)")
else:
print(f"Node {node} is offline ({ping_time})")

def check_network_io(self):
net_io = psutil.net_io_counters()
print(f"Network I/O: {net_io.bytes_sent} bytes sent, {net_io.bytes_recv} bytes received")

def run(self):
while True:
self.ping_nodes()
self.check_node_status()
self.check_network_io()
time.sleep(10)

if __name__ == '__main__':
network_health = NetworkHealth()
network_health.run()

0 comments on commit 4187184

Please sign in to comment.