Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0be0be1

Browse files
author
mfsoftworks
committed
[script] updated script, new script post json function
1 parent 015baec commit 0be0be1

File tree

3 files changed

+184
-47
lines changed

3 files changed

+184
-47
lines changed

‎README.md‎

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,76 @@
1-
Server-Monitoring-Script
1+
# Server Monitoring Script
2+
3+
## Purpose
4+
5+
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:
7+
8+
- CPU
9+
- Memory
10+
- Network
11+
- Hard Drives
12+
- System OS
13+
14+
The script will produce a JSON output at 5 second intervals for use with any software or server accepting a JSON input.
15+
Example:
16+
17+
```json
18+
{
19+
"hostname": "HOME-LAPTOP1",
20+
"system": {
21+
"name": "Windows",
22+
"version": "10"
23+
},
24+
"cpu_count": 4,
25+
"cpu_usage": 17.9,
26+
"memory_total": 8440942592,
27+
"memory_used": 6244225024,
28+
"memory_used_percent": 74.0,
29+
"drives": [
30+
{
31+
"name": "C:\\",
32+
"mount_point": "C:\\",
33+
"type": "NTFS",
34+
"total_size": 536224985088,
35+
"used_size": 167306108928,
36+
"percent_used": 31.2
37+
},
38+
{
39+
"name": "D:\\",
40+
"mount_point": "D:\\",
41+
"type": "NTFS",
42+
"total_size": 463332921344,
43+
"used_size": 49498419200,
44+
"percent_used": 10.7
45+
}
46+
],
47+
"network_up": 54,
48+
"network_down": 4150
49+
}
50+
```
51+
52+
The script includes a function to POST JSON to a remote server.
53+
54+
This script can be installed on several machines that report to a central monitoring server.
55+
56+
## Usage
57+
58+
Clone the script with `git clone`.
59+
60+
Install Python.
61+
62+
Create a cron job to run the script on every boot.
63+
64+
To test the script output run with `python monitor.py` or to run in background use `pythonw monitor.py`.
65+
66+
### Creating Cron Job
67+
68+
Edit cron with `crontab -e`.
69+
70+
Add the script at the bottom of the cron list as `@reboot /path/to/script/monitor.py`.
71+
72+
## Author
73+
74+
MF Softworks <mf@nygmarosebeauty.com>
75+
76+
mf.nygmarosebeauty.com

‎format.json‎

Lines changed: 0 additions & 21 deletions
This file was deleted.

‎monitor.py‎

Lines changed: 108 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,108 @@
1-
import socket, psutil
2-
3-
# Hostname Info
4-
hostname = socket.gethostname()
5-
print("Hostname", hostname)
6-
7-
# CPU Info
8-
cpu_count = psutil.cpu_count()
9-
cpu_usage = psutil.cpu_percent(interval=1)
10-
print("CPU:")
11-
print("Count", cpu_count, "Usage", cpu_usage)
12-
memory_stats = psutil.virtual_memory()
13-
14-
# Memory Info
15-
memory_total = memory_stats.total/1e+6
16-
memory_used = memory_stats.used/1e+6
17-
memory_used_percent = memory_stats.percent
18-
print("Memory:")
19-
print("Percent:", memory_used_percent, "\tTotal:", "%.2f" % memory_total, "MB", "\tUsed:", "%.2f" % memory_used, "MB")
20-
21-
# Disk Info
22-
disk_info = psutil.disk_partitions()
23-
print("Disks:")
24-
for disk in disk_info:
25-
print("Disk name",disk.device,"\tMount Point:",disk.mountpoint,"\tType",disk.fstype,"\tSize:",psutil.disk_usage(disk.mountpoint).total,"\tUsage:",psutil.disk_usage(disk.mountpoint))
1+
import socket, psutil, requests, time, json, platform
2+
3+
def main():
4+
# Hostname Info
5+
hostname = socket.gethostname()
6+
print("Hostname:", hostname)
7+
8+
# CPU Info
9+
cpu_count = psutil.cpu_count()
10+
cpu_usage = psutil.cpu_percent(interval=1)
11+
print("CPU:")
12+
print("Count:", cpu_count, "Usage:", cpu_usage)
13+
14+
# Memory Info
15+
memory_stats = psutil.virtual_memory()
16+
memory_total = memory_stats.total
17+
memory_used = memory_stats.used
18+
memory_used_percent = memory_stats.percent
19+
print("Memory:")
20+
print("Percent:", memory_used_percent, "\tTotal:", memory_total / 1e+6, "MB", "\tUsed:", memory_used / 1e+6, "MB")
21+
22+
# Disk Info
23+
disk_info = psutil.disk_partitions()
24+
print("Disks:")
25+
disks = []
26+
for x in disk_info:
27+
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,
33+
"percent_used" : psutil.disk_usage(x.mountpoint).percent
34+
}
35+
36+
disks.append(disk)
37+
38+
print("Disk name",disk["name"], "\tMount Point:", disk["mount_point"], "\tType",disk["type"], "\tSize:", disk["total_size"] / 1e+9,"\tUsage:", disk["used_size"] / 1e+9, "\tPercent Used:", disk["percent_used"])
39+
40+
# Network Info
41+
print("Network:")
42+
network_stats = get_bandwidth()
43+
print("Traffic in:",network_stats["traffic_in"] / 1e+6,"\tTraffic out:",network_stats["traffic_out"] / 1e+6)
44+
45+
# Platform Info
46+
print("OS:")
47+
system = {
48+
"name" : platform.system(),
49+
"version" : platform.release()
50+
}
51+
print(system["name"],system["version"])
52+
53+
54+
## Set Machine Info
55+
machine_info = {
56+
"hostname" : hostname,
57+
"system" : system,
58+
"cpu_count" : cpu_count,
59+
"cpu_usage" : cpu_usage,
60+
"memory_total" : memory_total,
61+
"memory_used" : memory_used,
62+
"memory_used_percent" : memory_used_percent,
63+
"drives" : disks,
64+
"network_up" : network_stats["traffic_out"],
65+
"network_down" : network_stats["traffic_in"]
66+
}
67+
68+
data = json.dumps(machine_info)
69+
post_data(data)
70+
71+
def get_bandwidth():
72+
# Get net in/out
73+
net1_out = psutil.net_io_counters().bytes_sent
74+
net1_in = psutil.net_io_counters().bytes_recv
75+
76+
time.sleep(1)
77+
78+
# Get new net in/out
79+
net2_out = psutil.net_io_counters().bytes_sent
80+
net2_in = psutil.net_io_counters().bytes_recv
81+
82+
# Compare and get current speed
83+
if net1_in > net2_in:
84+
current_in = 0
85+
else:
86+
current_in = net2_in - net1_in
87+
88+
if net1_out > net2_out:
89+
current_out = 0
90+
else:
91+
current_out = net2_out - net1_out
92+
93+
network = {"traffic_in" : current_in, "traffic_out" : current_out}
94+
return network
95+
96+
def post_data(data):
97+
## POST data
98+
try:
99+
endpoint = "https://your-monitoring-server.com"
100+
r = requests.post(url = endpoint, data = data)
101+
print(r)
102+
except requests.exceptions.RequestException as e:
103+
print("\nPOST Error:\n",e)
104+
105+
while True:
106+
main()
107+
print("-----------------------------------------------------------------")
108+
time.sleep(5)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /