CentOS is a popular Linux distribution known for its reliability and stability. In this tutorial, we will learn how to set up threshold alerts or 경보 (gyeongbo) to monitor system metrics on CentOS using bash scripting.
Prerequisites
Before getting started, ensure that you have the following:
- CentOS (version 7 or later) installed
- Root or sudo access to the CentOS server
- Basic knowledge of bash scripting
Step 1: Install Required Packages
To set up threshold alerts, we need a few packages installed on our CentOS server. Open your terminal and run the following command to install them:
sudo yum install -y lm-sensors net-snmp-utils
lm-sensors
is a command-line utility for monitoring hardware sensors.net-snmp-utils
provides tools for SNMP monitoring.
Step 2: Configure lm-sensors
Now that we have installed lm-sensors
, we need to configure it to monitor the system’s hardware sensors. Run the following command to start the configuration process:
sudo sensors-detect
This command will detect and attempt to load all available kernel modules for sensor hardware detection. Press Enter to accept the default options for most of the questions. At the end of the configuration process, press “yes” to generate the sensors.conf
file.
Step 3: Test Sensor Readings
To ensure that the sensors are working correctly, use the following command to display sensor readings:
sensors
This command will output the current readings of various sensors on your CentOS server. Make sure to check if the sensor values are within the normal range. If the sensors are not working or showing incorrect values, you might need to troubleshoot the sensor configuration or check for compatibility with your hardware.
Step 4: Set Up Bash Script
We will now create a bash script to monitor the sensor values and send alerts when the readings exceed certain thresholds. Create a new file using your preferred text editor, for example:
sudo nano monitor_sensors.sh
Add the following code to the monitor_sensors.sh
script:
#!/bin/bash
email="your-email@example.com"
threshold=70
while true; do
temp=$(sensors | grep "Core 0" | awk '{print $3}' | cut -c2-3)
if [ $temp -gt $threshold ]; then
echo "Temperature exceeded the threshold. Current temperature: $temp" | mail -s "Temperature Alert" $email
fi
sleep 60
done
In the script, replace your-email@example.com
with your email address and 70
with your desired temperature threshold value. This script monitors the temperature sensor (in this case, Core 0) every 60 seconds and sends an email alert if the temperature exceeds the specified threshold.
Save the script and make it executable:
chmod +x monitor_sensors.sh
Step 5: Run the Bash Script
To start monitoring the sensor values and receive alerts, execute the bash script:
./monitor_sensors.sh
Make sure to keep the terminal window open or run the script as a background process.
Conclusion
By following this tutorial, you have learned how to set up threshold alerts for monitoring system metrics on CentOS using bash scripting. This allows you to take proactive measures whenever any threshold is exceeded, helping to maintain the health and stability of your CentOS server.