# sysinfod **Repository Path**: lanceye/sysinfod ## Basic Information - **Project Name**: sysinfod - **Description**: Linux 系统信息监控守护进程。通过读取 `/proc` 文件系统和系统调用(`sysinfo()`、`statfs()` 等)采集 CPU、内存、磁盘、网络、进程等指标,通过嵌入式 HTTP 服务器(libevent)提供 REST API,并定时推送 JSON 数据到远程监控平台。 - **Primary Language**: C - **License**: MulanPSL-1.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-07-04 - **Last Updated**: 2026-07-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # sysinfod A Linux system information monitoring daemon. It collects CPU, memory, disk, network, and process metrics by reading `/proc` filesystem and system calls (`sysinfo()`, `statfs()`, etc.), serves them via an embedded HTTP server (libevent) with REST API endpoints, and periodically pushes JSON data to a remote monitoring platform. ## Features - **CPU**: usage percentage, core count, system boot time, OS distribution name/version - **Memory**: total/free/used physical RAM and swap, process count, memory unit size - **Disk**: mounted filesystem list (device, mount point, type, block size, total/free/available blocks, free inodes) - **Network**: total bytes received/sent, TCP connection details (IP/port/state/queue/timer, 22 fields) - **Process**: list of running processes (PID, command, path, user, memory usage, process name) - **HTTP API**: 7 REST endpoints + remote push - **Remote Push**: periodically POST aggregated JSON to remote server ## Dependencies - libevent (`libevent`, `libevent_core`, `libevent_pthreads`) - pthreads - CMake >= 3.15 - Linux only ## Build ```bash mkdir build && cd build cmake .. make ``` Place `config.json` in the same directory as the executable (or the current working directory at runtime). ## Configuration At startup, the daemon reads `config.json`. If the file is missing or malformed, hardcoded defaults are used. **Example `config.json`:** ```json { "DEFAULT_PORT": 9801, "PULL_SYS_INFO_INTERVAL": 10, "SERVER_URL": "http://172.16.132.7/monitor/server/info", "TOKEN": "CHANGE_THIS_TO_A_STRONG_TOKEN" } ``` | Key | Default | Description | |-----|---------|-------------| | `DEFAULT_PORT` | `9801` | Local HTTP monitoring port (daemon mode) | | `PULL_SYS_INFO_INTERVAL` | `10` | Collection interval (seconds) | | `SERVER_URL` | `http://172.16.132.7/monitor/server/info` | Remote push URL | | `TOKEN` | `CHANGE_THIS_TO_A_STRONG_TOKEN` | HTTP API authentication token (recommended >= 8 chars) | > **Security**: A warning is printed at startup if the default token is detected. Token comparison uses constant-time comparison to prevent timing side-channel attacks. Communication is over plain HTTP — avoid using on public networks. ## Run Modes ### Push-Only Mode (Default) ```bash ./sysinfod ``` The program enters an infinite loop that only performs remote pushes; **the HTTP API server is NOT started**. ### Daemon Mode ```bash ./sysinfod -d # or ./sysinfod --daemon ``` The program will: - Double-fork to detach from the terminal - Use `/tmp/sysinfod` lockfile for singleton enforcement - Redirect stdin/stdout/stderr to `/dev/null` - Start a background thread for periodic push; the main thread starts the HTTP API server - Register `SIGINT`/`SIGTERM` handlers for graceful shutdown ## HTTP API Endpoints All endpoints bind to `0.0.0.0:port`. With the exception of the 404 fallback, **every endpoint requires a `TOKEN` request header for authentication** (value configured in `config.json`). **Example request:** ```bash curl -H "TOKEN: your_token" http://localhost:9801/cpu ``` | Method | Path | Description | Response | |--------|------|-------------|----------| | GET | `/cpu` | CPU usage percentage (1s sample) | `{"usage":""}` | | GET | `/cores` | CPU core count | `{"cores":}` | | GET | `/start_time` | System boot time | `{"start_time":"YYYY-MM-DD HH:MM:SS"}` | | GET | `/mem` | Memory & swap stats | `{"totalram":"...","freeram":"...","totalswap":"...","freeswap":"...","procs":"...","mem_unit":"..."}` | | GET | `/disk` | Mounted filesystems | JSON array | | GET | `/process` | Running processes | JSON array | | GET | `/net_info` | Total bytes sent/received | `{"rec":,"rev":}` | | — | Remote push | POST aggregated data to `SERVER_URL` | See below | ## Remote Push - **Method**: HTTP POST - **URL**: `SERVER_URL` from `config.json` - **Interval**: `PULL_SYS_INFO_INTERVAL` from `config.json` (seconds) - **Connection timeout**: 5 seconds ### Push JSON Format ```json { "start_time": "", "os_name_version": " @x86_64", "cpu_usage": "", "process": "", "disk_info": "", "cores": "", "mem_usage": "", "net_info": "{\"rev\":,\"send\":,\"tcp_num\":}", "connections": "" } ``` > All values are strings; nested JSON objects are double-encoded (stringified JSON). ## Data Sources | System file/call | Module | Data collected | |----------------|--------|----------------| | `/proc/stat` | CPU | Cumulative CPU ticks | | `/etc/os-release`, `/etc/*-release` | CPU | Distribution name/version | | `sysinfo()` | CPU/Memory | Uptime, memory/swap stats | | `uname()` | CPU | Kernel version, architecture | | `/proc/mounts` + `statfs()` | Disk | Mount points, block/inode info | | `/proc/net/dev` | Network | Interface bytes sent/received | | `/proc/net/tcp` | Network | TCP connection details (22 fields) | | `/proc//{cmdline,exe,status}` | Process | Command, path, memory, user | ## Known Issues 1. The `/cpu` endpoint blocks for ~1 second to sample CPU usage 2. The `/process` endpoint iterates over all processes and may be slow on busy systems 3. `/etc/os-release` parsing relies on `PRETTY_NAME`/`NAME`/`VERSION` standard fields; non-standard entries fall back to `uname()` 4. Communication is over plain HTTP — tokens and system data are vulnerable to MITM attacks; use only on trusted internal networks ## Acknowledgements This project is based on [sysinfod](https://gitee.com/28250164/sysinfod). Thanks to the original author. ## License This project is released under the GNU General Public License v3.0.