CasaOS Cron Job for SendAllHardwareStatusBySocket: Real-Time Hardware Monitoring Explained
The cron job executes every 5 seconds to gather live hardware metrics and broadcast them via CasaOS' internal notification socket under the event name casaos:system:utilization.
The IceWhaleTech/CasaOS repository implements a lightweight background scheduler that continuously synchronizes the web interface with current system performance data. This cron job for SendAllHardwareStatusBySocket aggregates network statistics, CPU utilization, memory usage, and thermal readings, publishing them through the internal message bus for real-time consumption by front-end clients and third-party integrations.
What Is the SendAllHardwareStatusBySocket Cron Job?
In main/main.go, CasaOS initializes a cron scheduler that triggers every five seconds to collect hardware telemetry. The registration occurs at lines 121-123, where the service creates a new cron instance with seconds precision and adds the periodic function:
c := cron.New(cron.WithSeconds())
c.AddFunc("@every 5s", route.SendAllHardwareStatusBySocket)
c.Start()
This configuration ensures that route.SendAllHardwareStatusBySocket() runs continuously while the CasaOS binary is active, acting as the aggregation engine for system-level metrics.
How Hardware Metrics Are Collected
The implementation in route/periodical.go performs a systematic multi-step collection process before broadcasting the results to subscribed clients.
Network Interface Monitoring
The function iterates over known network interfaces to identify active connections. It records I/O counters for each interface and assigns human-readable states such as Up or Down based on current link status, providing real-time network throughput data.
CPU Statistics and Thermal Data
CPU collection involves reading the current usage percentage, detecting the processor model (ARM, Intel, or AMD), counting physical cores, and fetching temperature and power readings from system sensors. This data is gathered through the system service layer to ensure accurate hardware detection across different architectures.
Memory Utilization
The job pulls total and available memory statistics from the system driver via service.MyService.Notify(), providing immediate insight into RAM consumption and availability on the host system.
Additional Temperature Sensors
Custom temperature entries are retrieved from the notification subsystem using service.MyService.Notify().GetSystemTempMap(), allowing integration with specialized hardware monitoring plugins and custom sensor configurations.
Broadcasting via the Notification Bus
After aggregating data into a structured map containing sys_mem, sys_cpu, sys_net, and temperature keys, the function publishes the payload:
service.MyService.Notify().SendNotify("casaos:system:utilization", body)
This call distributes the hardware snapshot to all subscribers through CasaOS' internal message bus, enabling real-time dashboard updates and alerting mechanisms.
Source Code Implementation Details
The hardware monitoring pipeline relies on coordination between several key components:
main/main.go: Creates the cron scheduler and registers the 5-second interval at lines 121-123route/periodical.go: ImplementsSendAllHardwareStatusBySocket()with the complete collection and broadcasting logicservice/system.go: Provides underlying system APIs such asGetNetInfo()andGetCpuPercent()that supply raw hardware dataservice/notify.go: Manages the internal message bus that handles thecasaos:system:utilizationevent distribution
Consuming Hardware Status Data
Front-end applications and third-party integrations can subscribe to the notification stream to receive real-time updates without polling the system directly.
The following JavaScript example connects to the CasaOS WebSocket endpoint and filters for hardware status events:
const ws = new WebSocket('ws://<casaos-host>:<port>/ws');
ws.addEventListener('message', evt => {
const data = JSON.parse(evt.data);
if (data.event === 'casaos:system:utilization') {
console.log('Hardware status:', data.payload);
// Access data.payload.sys_cpu, data.payload.sys_mem, etc.
}
});
For debugging or development purposes, you can manually trigger a single broadcast by importing the route package and calling the function directly:
package main
import "github.com/IceWhaleTech/CasaOS/route"
func main() {
// Force immediate hardware status broadcast
route.SendAllHardwareStatusBySocket()
}
Summary
- The cron job for SendAllHardwareStatusBySocket runs every 5 seconds via the scheduler defined in
main/main.golines 121-123 - It collects network interface states and I/O counters, CPU usage percentages and model information, memory statistics, and temperature readings
- Data is published under the event name
casaos:system:utilizationthrough the internal notification bus managed byservice/notify.go - Client applications subscribe via WebSocket to receive real-time hardware monitoring updates without polling
- The implementation spans
route/periodical.go,service/system.go, andservice/notify.goto provide cross-platform hardware detection
Frequently Asked Questions
What interval does the SendAllHardwareStatusBySocket cron job use?
The cron job executes every 5 seconds using the @every 5s schedule syntax in the cron.New(cron.WithSeconds()) scheduler initialized in main/main.go at lines 121-123.
Where is the SendAllHardwareStatusBySocket function implemented?
The function is implemented in route/periodical.go. This file contains the aggregation logic that gathers network, CPU, memory, and temperature data before publishing it to the notification system via service.MyService.Notify().SendNotify().
How can I subscribe to hardware status updates from CasaOS?
Client applications can connect to the CasaOS WebSocket endpoint at ws://<host>:<port>/ws and listen for messages where the event field equals casaos:system:utilization. The payload contains current system metrics including CPU utilization, memory usage, and network interface statistics.
What hardware metrics does the cron job collect?
According to the source code in route/periodical.go, the job collects network interface I/O counters and link status, CPU usage percentage and processor model information, total and available memory statistics, and temperature readings from both system sensors and the notification subsystem's custom temperature map.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →