Crates.io | tonic-sysinfo |
lib.rs | tonic-sysinfo |
version | 0.1.4 |
source | src |
created_at | 2024-09-09 16:05:02.343131 |
updated_at | 2024-10-13 21:29:07.190263 |
description | A gRPC service for monitoring system resources such as CPU, memory, and disk. |
homepage | https://github.com/RustLangLatam |
repository | https://github.com/RustLangLatam/tonic-sysinfo |
max_upload_size | |
id | 1369455 |
size | 81,841 |
AquĆ tienes un archivo README.md
que puedes usar para documentar tu servicio gRPC de monitoreo del sistema usando Tonic en Rust:
This project provides a basic gRPC service built using Tonic in Rust to monitor system resources such as memory, CPU, and disk usage. The service offers two endpoints:
get_system_info
: Retrieves system information upon request.watch_system_info
: Provides real-time monitoring of system information, streaming updates at regular intervals.Tonic
crate for building gRPC servicessysinfo
crate to retrieve system informationtokio
for asynchronous programmingasync-stream
for streaming responsesEnsure you include the following in your Cargo.toml
:
[dependencies]
tonic = "0.6"
tokio = { version = "1", features = ["full"] }
futures-util = "0.3"
sysinfo = "0.22"
async-stream = "0.3"
The SysInfoContext
struct implements the gRPC service using Tonic. It provides two key endpoints for monitoring system resources.
get_system_info
This endpoint allows a client to request system information (memory, CPU, disk usage). You can specify which information to include through the SystemInfoRequest
.
Parameters:
include_memory_info
: Boolean flag to include memory information.include_cpu_info
: Boolean flag to include CPU information.include_disk_info
: Boolean flag to include disk information.Response: A SystemInfoResponse
that contains the requested data.
watch_system_info
This endpoint streams system information at regular intervals, allowing real-time monitoring of system resources. The client can specify the refresh interval in the request.
Parameters:
include_memory_info
: Boolean flag to include memory information.include_cpu_info
: Boolean flag to include CPU information.include_disk_info
: Boolean flag to include disk information.refresh_interval
: Interval in seconds to refresh the information stream.Response: A stream of SystemInfoResponse
objects, updated at the specified interval.
You can start the gRPC server using the SysInfoContext::service()
method. This sets up the service to listen for incoming gRPC requests.
use tonic::transport::Server;
use sysinfo_service::SysInfoContext;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "[::1]:50051".parse().unwrap();
let sys_info_service = SysInfoContext::service();
println!("SystemInfoService listening on {}", addr);
Server::builder()
.add_service(sys_info_service)
.serve(addr)
.await?;
Ok(())
}
To make requests, you can use a gRPC client or any tool that can communicate via gRPC (such as grpcurl
or your own Rust client). Below is an example request using a Rust client:
use tonic::transport::Channel;
use sysinfo_service::system_info_service_client::SystemInfoServiceClient;
use sysinfo_service::SystemInfoRequest;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut client = SystemInfoServiceClient::connect("http://[::1]:50051").await?;
let request = tonic::Request::new(SystemInfoRequest {
include_memory_info: true,
include_cpu_info: true,
include_disk_info: true,
refresh_interval: 5, // for watch_system_info
});
// For get_system_info
let response = client.get_system_info(request.clone()).await?;
println!("System Info: {:?}", response.into_inner());
// For watch_system_info (Streaming)
let mut stream = client.watch_system_info(request).await?.into_inner();
while let Some(system_info) = stream.message().await? {
println!("System Info Stream: {:?}", system_info);
}
Ok(())
}
This project is licensed under the APACHE License.