CentOS 시스템 리소스 모니터링

In a CentOS environment, it is important to monitor the system resources to ensure optimal performance and identify any potential issues. With the help of Bash scripting, we can create a custom monitoring script that provides valuable insights into CPU, memory, disk, and network usage.

Prerequisites

Before we begin, make sure you have the following prerequisites installed on your CentOS system:

You can install these packages using the package manager yum by running the following command:

sudo yum install procps bc grep gawk -y

Monitoring CPU Usage

To monitor CPU usage, we can use the top command and extract the required data using awk. The following Bash script demonstrates how to get the current CPU usage percentage:

#!/bin/bash

usage=$(top -b -n 1 | grep "Cpu(s)" | awk '{print $2 + $4}')

echo "CPU Usage: $usage%"

Save the above script to a file (e.g. cpu_monitor.sh), and make it executable using the chmod command:

chmod +x cpu_monitor.sh

Now, when you run the script, it will display the current CPU usage percentage.

Monitoring Memory Usage

To monitor memory usage, we can utilize the information provided by the free command. The following Bash script demonstrates how to get the current memory usage:

#!/bin/bash

mem=$(free | grep Mem | awk '{printf "%.2f", $3/$2 * 100}')

echo "Memory Usage: $mem%"

Save the above script to a file (e.g. memory_monitor.sh) and make it executable.

Monitoring Disk Usage

To monitor disk usage, we can use the df command and extract the required information. The following Bash script demonstrates how to get the current disk usage:

#!/bin/bash

disk=$(df -h / | awk '{print $5}' | grep -o '[0-9]*')

echo "Disk Usage: $disk%"

Save the above script to a file (e.g. disk_monitor.sh) and make it executable.

Monitoring Network Usage

To monitor network usage, we can use the ifconfig command and extract the relevant data. The following Bash script demonstrates how to get the current network usage:

#!/bin/bash

rx=$(ifconfig eth0 | grep "RX packets" | awk '{print $5}')
tx=$(ifconfig eth0 | grep "TX packets" | awk '{print $5}')

echo "Network Usage: RX $rx packets, TX $tx packets"

Save the above script to a file (e.g. network_monitor.sh) and make it executable.

Automating the Monitoring

To automate the monitoring process, you can create a shell script that executes all the individual monitoring scripts and saves the output to a log file. This way, you can run the script periodically using a cron job.

Here’s an example of a shell script that executes our monitoring scripts and logs the output:

#!/bin/bash

# Run the individual monitoring scripts
./cpu_monitor.sh >> monitoring.log
./memory_monitor.sh >> monitoring.log
./disk_monitor.sh >> monitoring.log
./network_monitor.sh >> monitoring.log

Save the above script to a file (e.g. monitoring_script.sh) and make it executable. You can then set up a cron job to run this script at your desired interval.

With the help of these custom monitoring scripts, you can easily keep an eye on the system resources of your CentOS server and take any necessary actions to ensure its smooth operation.