Skip to content

Commit dd552ef

Browse files
author
mfsoftworks
committed
[json] added timestamp for records
1 parent 0be0be1 commit dd552ef

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.pythonPath": "C:\\Users\\arranf\\AppData\\Local\\Programs\\Python\\Python37-32\\python.exe"
3+
}

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
## Purpose
44

55
The Python script is designed to be run as a cronjob on every boot to run in the background.
6-
The script will gather information on:
6+
The script will gather information:
77

88
- CPU
99
- Memory
1010
- Network
1111
- Hard Drives
1212
- System OS
13+
- Current UTC Timestamp
1314

1415
The script will produce a JSON output at 5 second intervals for use with any software or server accepting a JSON input.
1516
Example:
@@ -45,11 +46,12 @@ Example:
4546
}
4647
],
4748
"network_up": 54,
48-
"network_down": 4150
49+
"network_down": 4150,
50+
"timestamp" : "2018-10-10 01:41:21"
4951
}
5052
```
5153

52-
The script includes a function to POST JSON to a remote server.
54+
The script includes a function to send JSON to a remote server.
5355

5456
This script can be installed on several machines that report to a central monitoring server.
5557

@@ -59,6 +61,8 @@ Clone the script with `git clone`.
5961

6062
Install Python.
6163

64+
If any library is missing do `pip install` *library*.
65+
6266
Create a cron job to run the script on every boot.
6367

6468
To test the script output run with `python monitor.py` or to run in background use `pythonw monitor.py`.

monitor.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import socket, psutil, requests, time, json, platform
1+
import socket, time, json, datetime, platform, psutil, requests
22

33
def main():
44
# Hostname Info
@@ -49,10 +49,12 @@ def main():
4949
"version" : platform.release()
5050
}
5151
print(system["name"],system["version"])
52-
52+
53+
# Time Info
54+
timestamp = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
5355

5456
## Set Machine Info
55-
machine_info = {
57+
machine = {
5658
"hostname" : hostname,
5759
"system" : system,
5860
"cpu_count" : cpu_count,
@@ -62,10 +64,14 @@ def main():
6264
"memory_used_percent" : memory_used_percent,
6365
"drives" : disks,
6466
"network_up" : network_stats["traffic_out"],
65-
"network_down" : network_stats["traffic_in"]
67+
"network_down" : network_stats["traffic_in"],
68+
"timestamp" : timestamp
6669
}
6770

68-
data = json.dumps(machine_info)
71+
data = json.dumps(machine)
72+
print("\nData:")
73+
print(data)
74+
6975
post_data(data)
7076

7177
def get_bandwidth():
@@ -94,15 +100,17 @@ def get_bandwidth():
94100
return network
95101

96102
def post_data(data):
97-
## POST data
98103
try:
99-
endpoint = "https://your-monitoring-server.com"
100-
r = requests.post(url = endpoint, data = data)
101-
print(r)
104+
endpoint = "http://monitor.localhost.local/api/"
105+
response = requests.get(url = endpoint, params = {"data" : data})
106+
print("\nGET:")
107+
print("Response:", response.status_code)
108+
print("Headers:", response.headers)
109+
print("Content:\n", response.json())
102110
except requests.exceptions.RequestException as e:
103-
print("\nPOST Error:\n",e)
111+
print("\nGET Error:\n",e)
104112

105113
while True:
106114
main()
107115
print("-----------------------------------------------------------------")
108-
time.sleep(5)
116+
time.sleep(3)

0 commit comments

Comments
 (0)