Skip to content

Commit 80847b2

Browse files
author
MF Softworks
committed
[script] added 30 minute timeout retry when connection is unsuccessful
1 parent 2b75830 commit 80847b2

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

README.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ The script will gather information:
1313
- Network
1414
- Hard Drives
1515
- System OS
16-
- Current UTC Timestamp
16+
- System Uptime
17+
- UTC Timestamp
1718

1819
The script will produce a JSON output at 5 second intervals for use with any software or server accepting a JSON input.
1920
Example:
@@ -65,29 +66,41 @@ Clone the script with `git clone`.
6566

6667
Install Python.
6768

68-
If any library is missing do `pip install` *library*.
69+
If any library is missing do `pip3 install` *library*.
6970

7071
To test the script output run with `python3 monitor.py`.
7172

72-
### Linux Autostart
73+
### **Linux Autostart**
7374

7475
Create a cron job to run the script on every boot.
7576

7677
Edit cron with `crontab -e`.
7778

78-
Add the script at the bottom of the cron list as `@reboot python3 /path/to/script/monitor.py &`.
79+
Add the script at the bottom of the cron list as `@reboot /usr/bin/python3 /path/to/script/monitor.py &`.
7980

80-
### Windows Autostart
81+
### **Windows Autostart**
8182

8283
`Windows/monitor.bat` and `Windows/monitor.vbs` scripts are included.
8384

8485
`monitor.bat` will call python to launch `monitor.py`.
8586

8687
`monitor.vbs` silently calls `monitor.bat` to run in the background.
8788

88-
To create an autostart for Windows open the startup folder with keyboard `Windows + R`, enter `shell:startup` and create a shortcut here to `monitor.vbs`.
89+
To create an autostart for Windows open start menu and search for `Task Scheduler`.
8990

90-
This will run the script in the background on every boot.
91+
Select `Create Task`.
92+
93+
Enter a name e.g. monitor.
94+
95+
Add new trigger at login.
96+
97+
Add a new action and select the `Windows/monitor.vbs` script.
98+
99+
**Unselect** Stop task if it runs longer than 3 days.
100+
101+
Select Okay.
102+
103+
A new task will be created that runs the python monitoring script in the background every time a user starts the system and logs in.
91104

92105
## Author
93106

monitor.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def main():
5656
print("System Uptime:")
5757
print(uptime)
5858

59-
## Set Machine Info
59+
# Set Machine Info
6060
machine = {
6161
"hostname" : hostname,
6262
"system" : system,
@@ -104,21 +104,30 @@ def get_bandwidth():
104104
return network
105105

106106
def send_data(data):
107-
try:
108-
endpoint = "http://monitor.localhost.local/api/"
109-
response = requests.get(url = endpoint, params = {"data" : data})
110-
print("\nGET:")
111-
print("Response:", response.status_code)
112-
print("Headers:")
113-
pprint.pprint(response.headers)
114-
print("Content:", response.content)
107+
# Attempt to send data up to 30 times
108+
for attempt in range(30):
115109
try:
116-
print("JSON Content:")
117-
pprint.pprint(response.json())
118-
except:
119-
print("No JSON content")
120-
except requests.exceptions.RequestException as e:
121-
print("\nGET Error:\n",e)
110+
# endpoint = monitoring server
111+
endpoint = "http://monitor.localhost.local/api/"
112+
response = requests.get(url = endpoint, params = {"data" : data})
113+
print("\nGET:")
114+
print("Response:", response.status_code)
115+
print("Headers:")
116+
pprint.pprint(response.headers)
117+
print("Content:", response.content)
118+
# Attempt printing response in JSON if possible
119+
try:
120+
print("JSON Content:")
121+
pprint.pprint(response.json())
122+
except:
123+
print("No JSON content")
124+
break
125+
except requests.exceptions.RequestException as e:
126+
print("\nGET Error:\n",e)
127+
# Sleep 1 minute before retrying
128+
time.sleep(60)
129+
else:
130+
# If no connection established for half an hour, kill script
122131
exit(0)
123132

124133
while True:

0 commit comments

Comments
 (0)