A lightweight system daemon that monitors CPU usage in real-time and logs the metrics to syslog. This daemon is designed to run in the background and provide continuous CPU utilization statistics with minimal system overhead.
- Runs as a proper Unix daemon process
- Real-time CPU usage monitoring
- Secure process isolation with double-forking
- Configurable PID file location
- Detailed syslog integration
- Clean process management and termination
- Automated build and run script
- Linux-based operating system
- G++ compiler
- Root privileges (for daemon operations)
- System logger (syslog) configured
- Clone the repository:
git clone https://github.com/stackanthony/cpu-usage-daemon.git
cd cpu-usage-daemon
- Compile the project:
g++ -o mydaemon main.cpp utils.cpp
The project includes a convenient shell script to handle the building, running, and monitoring of the daemon.
- Run the daemon:
./launch.sh
-
The script will:
- Compile the source code
- Prompt for a PID file location
- Launch the daemon process
- Start monitoring the syslog output
-
Monitor the daemon's output:
- CPU usage statistics will be logged to syslog
- The script automatically filters and displays relevant log entries
-
To stop the daemon:
- Press
Ctrl+C
in the terminal running the script - The cleanup process will automatically terminate the daemon and remove the PID file
- Press
The daemon supports the following configurations:
- PID File Location: Specified at runtime
- Polling Rate: Default is 1 second (can be modified in
main.cpp
) - Working Directory: Automatically set to root (/)
- Log Level: Uses syslog with INFO level for metrics and ERR level for errors
- Implements the double-fork technique for proper daemonization
- Creates a new session using
setsid()
- Manages standard file descriptors
- Handles process isolation and zombie prevention
- Reads CPU statistics from
/proc/stat
- Calculates CPU usage percentage across all cores
- Tracks user, system, idle, I/O wait, and interrupt time
- Updates metrics at configurable intervals
- Comprehensive error checking and reporting
- Secure file operations
- Proper resource cleanup on termination
- Detailed error logging through syslog
The daemon logs its activity to syslog with the following format:
- CPU usage metrics:
LOG_INFO
level - Operational messages:
LOG_NOTICE
level - Error conditions:
LOG_ERR
level
Monitor the logs in real-time:
tail -f /var/log/syslog | grep --line-buffered mydaemon
The daemon may exit with the following status codes:
- 0: Normal termination
- 1: Configuration error
- Other non-zero values: System-specific error codes
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
[MIT License]
- Based on standard Unix daemon design patterns
- Utilizes Linux process management best practices