Skip to content

Commit ea11a50

Browse files
Initial commit of NodeSniff agent
0 parents  commit ea11a50

File tree

6 files changed

+843
-0
lines changed

6 files changed

+843
-0
lines changed

.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Byte-compiled / cache
2+
__pycache__/
3+
*.py[cod]
4+
*.pyo
5+
*.pyd
6+
7+
# Logs
8+
*.log
9+
10+
# Virtual environments
11+
venv/
12+
.env/
13+
.venv/
14+
15+
# OS files
16+
.DS_Store
17+
Thumbs.db
18+
19+
# IDE/editor settings
20+
.vscode/
21+
.idea/
22+
23+
# Token and secrets
24+
/etc/nodesniff/nsagent.token
25+
*.token
26+
27+
# Local scripts and docs (do not include in repo)
28+
doc/
29+
scripts/

LICENSE

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
GNU GENERAL PUBLIC LICENSE
2+
Version 3, 29 June 2007
3+
4+
Copyright (C) 2007 Free Software Foundation, Inc.
5+
<https://fsf.org/>
6+
7+
Everyone is permitted to copy and distribute verbatim copies
8+
of this license document, but changing it is not allowed.
9+
10+
---
11+
12+
## NodeSniff License Modification Notice
13+
14+
This software is licensed under the GNU General Public License v3 with the following **additional restriction**:
15+
16+
> This software is provided **for non-commercial use only**.
17+
> Any use of this software, or its derivatives, in commercial environments or for-profit services requires a separate **commercial license**.
18+
> Please contact [info@nodesniff.com](mailto:info@nodesniff.com) for inquiries about commercial licensing.
19+
20+
---
21+
22+
You should have received a copy of the GNU General Public License
23+
along with this program. If not, see <https://www.gnu.org/licenses/>.

README.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# NodeSniff Agent
2+
3+
NodeSniff Agent is a lightweight server metrics collector for the NodeSniff monitoring system.
4+
The agent collects and securely sends metrics to your NodeSniff backend over HTTPS.
5+
6+
**License:** GNU GPL v3 (non-commercial use only)
7+
8+
---
9+
10+
## Features
11+
12+
- Collects CPU, RAM, disk, network, and OS metrics (extensible via Python plugins)
13+
- Secure HTTPS metric transmission with HMAC authentication
14+
- CLI modes for install, remove, cleanup
15+
- Systemd integration with `/usr/bin/nsagent` symlink for convenience
16+
17+
---
18+
19+
## Requirements
20+
21+
- Python 3.7+
22+
- Linux (x86, ARM, etc.)
23+
- Python packages:
24+
- `psutil`, `PyYAML`, `requests`
25+
26+
Install dependencies:
27+
```sh
28+
pip install -r requirements.txt
29+
```
30+
31+
---
32+
33+
## Installation
34+
35+
1. **Copy the agent to your server:**
36+
```sh
37+
scp nsagent.py user@your-server:/path/
38+
```
39+
40+
2. **Run the agent for initial setup:**
41+
```sh
42+
sudo python3 ./nsagent.py
43+
```
44+
45+
- Creates `/etc/nodesniff/` with placeholder config and token.
46+
- Paste your company key into `/etc/nodesniff/nsagent.token`.
47+
- Re-run the agent:
48+
```sh
49+
sudo ./nsagent.py
50+
```
51+
52+
- Expected output:
53+
```
54+
[OK] Registration successful.
55+
[OK] 📤 Metrics sent successfully.
56+
```
57+
58+
3. **Install as a systemd service:**
59+
```sh
60+
sudo ./nsagent.py --service
61+
```
62+
63+
- Requires a successful registration.
64+
- Installs `nodesniff-agent` systemd service and enables it on boot.
65+
- Adds `/usr/bin/nsagent` CLI symlink.
66+
67+
---
68+
69+
## CLI Modes
70+
71+
- `sudo ./nsagent.py`
72+
Standard agent run. Registers (if needed) and enters metric loop.
73+
74+
- `sudo ./nsagent.py --service`
75+
Installs as a systemd service (requires registration).
76+
77+
- `sudo ./nsagent.py --clean`
78+
Full cleanup:
79+
- Stops and disables service
80+
- Removes config, token, logs, metrics
81+
- Deletes `/usr/bin/nsagent` symlink
82+
83+
---
84+
85+
## Re-registration (after removal in dashboard)
86+
87+
If the server is deleted from the NodeSniff dashboard, the agent will detect this (`403 Forbidden`).
88+
To re-register:
89+
90+
1. Run:
91+
```sh
92+
sudo ./nsagent.py
93+
```
94+
95+
2. Re-paste your company key if prompted.
96+
97+
3. Re-install the service:
98+
```sh
99+
sudo ./nsagent.py --service
100+
```
101+
102+
---
103+
104+
## Updating the Agent
105+
106+
After downloading a new version:
107+
108+
```sh
109+
sudo ./nsagent.py --clean
110+
# Copy updated nsagent.py
111+
sudo python3 ./nsagent.py
112+
sudo ./nsagent.py --service
113+
```
114+
115+
---
116+
117+
## Uninstall
118+
119+
```sh
120+
sudo ./nsagent.py --clean
121+
```
122+
123+
Completely removes the agent: service, configs, logs, symlink.
124+
125+
---
126+
127+
## License
128+
129+
GPL v3 — **non-commercial use only**.
130+
For commercial use or support, contact us at [info@nodesniff.com](mailto:info@nodesniff.com).
131+
132+
---
133+
134+
## Contact
135+
136+
📧 info@nodesniff.com
137+
🌐 https://www.nodesniff.com

metrics/top_processes.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# ------------------------------------------------------------------------------
2+
# NodeSniff Agent - Lightweight Linux metrics collector
3+
#
4+
# Author: Sebastian Zieba <sebastian@zieba.art>
5+
# License: GNU GPL v3 (non-commercial use only)
6+
#
7+
# This software is licensed under the terms of the GNU General Public License
8+
# version 3 (GPLv3) as published by the Free Software Foundation, **for
9+
# non-commercial use only**.
10+
#
11+
# For commercial licensing, please contact the author directly.
12+
# ------------------------------------------------------------------------------
13+
14+
import psutil
15+
import time
16+
17+
def get():
18+
try:
19+
# Priming: For accurate CPU usage, call cpu_percent(interval=None) on all processes first.
20+
for proc in psutil.process_iter():
21+
try:
22+
proc.cpu_percent(interval=None)
23+
except Exception:
24+
continue # Ignore processes that no longer exist
25+
26+
# Short delay to allow psutil to calculate CPU percent over an interval
27+
time.sleep(0.1)
28+
29+
processes = []
30+
# Gather process info including CPU percent and memory usage
31+
for proc in psutil.process_iter(['pid', 'name', 'cpu_percent', 'memory_info']):
32+
try:
33+
info = proc.info
34+
memory_rss = info.get('memory_info').rss if info.get('memory_info') else 0
35+
processes.append({
36+
"pid": info['pid'], # Process ID
37+
"name": info['name'] or "unknown", # Process name (fallback if None)
38+
"cpu": round(info['cpu_percent'], 1), # CPU usage in percent
39+
"ram": round(memory_rss / 1024 / 1024, 1) # RAM usage in MB (RSS)
40+
})
41+
except (psutil.NoSuchProcess, psutil.AccessDenied):
42+
continue # Skip processes that are gone or inaccessible
43+
44+
# Sort processes by CPU usage descending and return top 10
45+
top = sorted(processes, key=lambda p: p['cpu'], reverse=True)[:10]
46+
return {"top_cpu_processes": top}
47+
48+
except Exception as e:
49+
# If anything goes wrong, return error string in result
50+
return {"top_cpu_processes": f"Error: {str(e)}"}
51+

0 commit comments

Comments
 (0)