Skip to content

Commit

Permalink
update readme and add address argument
Browse files Browse the repository at this point in the history
  • Loading branch information
sentrychris committed Jul 21, 2024
1 parent e0adf63 commit 6e86791
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 35 deletions.
101 changes: 70 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
# Pi Monitor Websocket Server

This is the websocket server used for sending data to and from the [Pi Monitor](https://github.com/chrisrowles/pi-monitor-v3).
# psutil-websocket-monitor

A simple system and network monitoring solution with real-time data transmission via a websocket server. This project was originally designed for connecting with the [Pi Monitor](https://github.com/sentrychris/pi-monitor-v3), however it can be used on any generic system.

This project includes [psutil](https://pypi.org/project/psutil/) scripts for gathering system and network statistics, and a [tornado](https://pypi.org/project/tornado/)-based websocket server for transmitting these statistics to connected clients.

## Requirements

- Python 3
- Supervisor (required if you would like to run the websocket server process as a daemon)

## Set Up
## Features

- **Asynchronous Data Collection**: Utilizes asyncio and ThreadPoolExecutor for efficient, non-blocking data collection.
- **Real-Time Monitoring**: Transmits live system and network statistics to clients via WebSocket.
- **System Statistics**: Provides CPU usage, memory usage, disk usage, system uptime, and top processes by memory usage.
- **Network Statistics**: Monitors data sent and received on a specified network interface.
- **Websocket Server**: Tornado-based server for real-time data transmission.

## Quick Start

1. Clone this repository:
```sh
git clone https://github.com/chrisrowles/pi-monitor-wss.git
git clone git@github.com:sentrychris/psutil-websocket-monitor.git
```

2. Install dependencies:
Expand All @@ -21,12 +32,12 @@ This is the websocket server used for sending data to and from the [Pi Monitor](

3. Run the server:
```sh
python manage.py run
python manage.py run --port=<port> --address=<address>
```

### Testing it works

Pi Monitor WSS comes with a simple frontend to test the websocket connection:
The websocket server comes with a simple frontend to test the websocket connection:

![Image](https://i.imgur.com/d52ULxS.png)

Expand All @@ -41,47 +52,75 @@ If you would like to run the server in the background, you can use the superviso

2. Make any necessary changes (for example, you might need to modify the filepath in the command):
```conf
[program:pi-monitor-websoscket]
process_name = pi-monitor-websocket-%(process_num)s
command = python /var/www/tornado-websocket/manage.py --daemon
[program:psutil-websocket-monitor]
process_name = psutil-websocket-monitor-%(process_num)s
command = python /var/www/psutil-websocket-monitor/manage.py --daemon
stdout_logfile=/var/log/supervisor/%(program_name)s-%(process_num)s.log
numprocs = 1
numprocs_start = 9005
```

2. Start supervisor
3. Start supervisor
```sh
supervisord
```

### Next steps

To connect to the websocket server, you first have to make an XHR POST request to retrieve an assigned worker. For example:
```js
const client = await fetch(location.pathname, {
method: 'POST',
body: { connection: 'monitor' }
});
To connect to the WebSocket server, you can use any WebSocket client. Here is an example of how to connect using JavaScript:

const worker = await client.json()
```
1. Retrieve the assigned worker:

Once you have your assigned worker, you can proceed to open the websocket connection and retrieve data:
```js
const url = `ws://{{WSS_URL}}:{{WSS_PORT}}/ws?id=${worker.id}`;
```js
const client = await fetch(location.pathname, {
method: 'POST',
body: { connection: 'monitor' }
});
connection = new WebSocket(url);
const worker = await client.json()
```
connection.onopen = () => {
log.write('event', 'websocket is connected');
}
2. Open the WebSocket connection and retrieve data:
```js
const url = `ws://{{WSS_URL}}:{{WSS_PORT}}/ws?id=${worker.id}`;
connection = new WebSocket(url);
connection.onopen = () => {
log.write('event', 'websocket is connected');
}
connection.onmessage = (response) => {
const data = JSON.parse(response.data);
}
```
connection.onmessage = (response) => {
const data = JSON.parse(response.data);
}
```
You can also use WebSocket clients in other programming languages, such as Python, to connect to the server:
1. Retrieve an assigned worker:
```python
import requests
response = requests.post('http://<server-address>/your-endpoint', json={'connection': 'monitor'})
worker = response.json()
```
2. Open the WebSocket connection and retrieve data:
```python
import asyncio
import websockets
async def connect():
uri = f"ws://<server-address>:<port>/ws?id={worker['id']}"
async with websockets.connect(uri) as websocket:
async for message in websocket:
print(message)
asyncio.run(connect())
```
You can use the frontend client located at `public/index.html` to find out more.
You can use the frontend client located at `public/index.html` for further testing and exploration.
If you are using the [Pi Monitor](https://github.com/chrisrowles/pi-monitor-v3), there are instructions contained there to help you get setup and connected.
Expand All @@ -91,7 +130,7 @@ I hope you like it!
Feel free to contribute to this project, or the [Pi Monitor](https://github.com/chrisrowles/pi-monitor-v3), or the [Pi Monitor API](https://github.com/chrisrowles/pi-monitor-api).
I'm alaways looking for help and new ideas, this is a fun personal project and so if you're a bit new of a bit anxious about contributing to projects, then please feel free to get in tocuh with me and we'll find a way to get you started, we all start somewhere! :)
I'm always looking for help and new ideas, this is a fun personal project and so if you're a bit new of a bit anxious about contributing to projects, then please feel free to get in tocuh with me and we'll find a way to get you started, we all start somewhere! :)
## License
This software is open-sourced software licensed under the MIT license.
9 changes: 5 additions & 4 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@


# Define command-line options
define('port', default=4200, help='Listen port', type=int)
define('address', default='0.0.0.0', help='Listen address for the application')
define('port', default=4200, help='Listen port for the application', type=int)


def run():
Expand All @@ -33,10 +34,10 @@ def run():
# Parse command line arguments
parse_command_line()

# Create the server and listen on the specified port and address
http = HTTPServer(app)
http.listen(options.port)

print("Listening on http://localhost:" + str(options.port))
http.listen(port=options.port, address=options.address)
print("Listening on http://{}:{}".format(options.address, options.port))
IOLoop.current().start()


Expand Down

0 comments on commit 6e86791

Please sign in to comment.