Skip to content

Commit 86a6e92

Browse files
author
MF Softworks
authored
[script] added args parsing for easy customization
1 parent 9ed59bf commit 86a6e92

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

monitor.py

+28-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
import socket, time, json, datetime, platform, psutil, requests, pprint, uuid
1+
import argparse, socket, time, json, datetime, platform, psutil, requests, pprint, uuid
2+
3+
# parse args
4+
parser = argparse.ArgumentParser(description='Monitoring script to send system info to a tracking server')
5+
parser.add_argument('-d', '--dest', default='http://localhost:8080/', help='API Endpoint for Monitoring Data (Defaults to http://localhost:8080/)')
6+
parser.add_argument('-i', '--interval', default=5, type=int, help='Interval between checks (Seconds. Defaults to 5 seconds)')
7+
parser.add_argument('-a', '--attempts', default=30, type=int, help='Attempts to send data when sending failes (Defaults to 30)')
8+
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)')
9+
args = parser.parse_args()
10+
11+
# Factor in sleep for bandwidth checking
12+
if args.interval >= 2:
13+
args.interval -= 2
214

315
def main():
416
# Hostname Info
@@ -25,11 +37,11 @@ def main():
2537
# Try fixes issues with connected 'disk' such as CD-ROMS, Phones, etc.
2638
try:
2739
disk = {
28-
"name" : x.device,
29-
"mount_point" : x.mountpoint,
30-
"type" : x.fstype,
31-
"total_size" : psutil.disk_usage(x.mountpoint).total,
32-
"used_size" : psutil.disk_usage(x.mountpoint).used,
40+
"name" : x.device,
41+
"mount_point" : x.mountpoint,
42+
"type" : x.fstype,
43+
"total_size" : psutil.disk_usage(x.mountpoint).total,
44+
"used_size" : psutil.disk_usage(x.mountpoint).used,
3345
"percent_used" : psutil.disk_usage(x.mountpoint).percent
3446
}
3547

@@ -39,7 +51,7 @@ def main():
3951
except:
4052
print("")
4153

42-
# Bandwidth Info
54+
# Bandwidth Info
4355
network_stats = get_bandwidth()
4456
print("Network:\n\tTraffic in:",network_stats["traffic_in"] / 1e+6,"\n\tTraffic out:",network_stats["traffic_out"] / 1e+6)
4557

@@ -66,7 +78,7 @@ def main():
6678
nic["address6"] = snic.address
6779
nics.append(nic)
6880
print("\tNIC:",nic["name"], "\tMAC:", nic["mac"], "\tIPv4 Address:",nic["address"], "\tIPv4 Subnet:", nic["netmask"], "\tIPv6 Address:", nic["address6"])
69-
81+
7082
# Platform Info
7183
system = {
7284
"name" : platform.system(),
@@ -81,7 +93,7 @@ def main():
8193

8294
# System UUID
8395
sys_uuid = uuid.getnode()
84-
96+
8597
# Set Machine Info
8698
machine = {
8799
"hostname" : hostname,
@@ -122,21 +134,21 @@ def get_bandwidth():
122134
current_in = 0
123135
else:
124136
current_in = net2_in - net1_in
125-
137+
126138
if net1_out > net2_out:
127139
current_out = 0
128140
else:
129141
current_out = net2_out - net1_out
130-
142+
131143
network = {"traffic_in" : current_in, "traffic_out" : current_out}
132144
return network
133145

134146
def send_data(data):
135147
# Attempt to send data up to 30 times
136-
for attempt in range(30):
148+
for attempt in range(args.attempts):
137149
try:
138150
# endpoint = monitoring server
139-
endpoint = "http://monitor.localhost.local/api/"
151+
endpoint = args.dest
140152
response = requests.post(url = endpoint, data = data)
141153
print("\nPOST:")
142154
print("Response:", response.status_code)
@@ -153,12 +165,12 @@ def send_data(data):
153165
except requests.exceptions.RequestException as e:
154166
print("\nPOST Error:\n",e)
155167
# Sleep 1 minute before retrying
156-
time.sleep(60)
168+
time.sleep(args.timeout)
157169
else:
158-
# If no connection established for half an hour, kill script
170+
# If no connection established for attempts*timeout, kill script
159171
exit(0)
160172

161173
while True:
162174
main()
163175
print("-----------------------------------------------------------------")
164-
time.sleep(3)
176+
time.sleep(args.interval)

0 commit comments

Comments
 (0)