Home>Article>Operation and Maintenance> How to set up system monitoring on Linux
How to set up system monitoring on Linux
On the Linux operating system, system monitoring is an important task. By monitoring the system, we can obtain information about system performance, resource usage, service running status, etc., so that problems can be discovered and solved in a timely manner. This article explains how to set up system monitoring on Linux and provides code examples.
1. Use the top command to monitor system performance
The top command is a very commonly used system monitoring tool, which can display the running status and process information of the system in real time. We can install the top command through the following command:
sudo apt-get install top
After the installation is completed, you can run the top command directly in the terminal to view the running status of the system. The output results of the top command include system load, CPU usage, memory usage, process list, etc.
2. Use the sar command to record system performance data
The sar command is the abbreviation of System Activity Reporter. It can record system performance data and save it in text or binary format. We can install the sar command through the following command:
sudo apt-get install sysstat
After the installation is complete, you can use the following command to generate a report of system performance data:
sar -A > performance_report.txt
This command will display the system’s CPU usage, memory Usage, disk IO, network transmission and other data are saved to the performance_report.txt file.
3. Use the nmon command to monitor system resources
nmon is an efficient system monitoring tool that can display system CPU utilization, memory usage, disk IO, network transmission and other data. We can install the nmon command through the following command:
sudo apt-get install nmon
After the installation is completed, you can use the following command to start nmon:
nmon
After the nmon command is started, various items of the system will be displayed in an interactive interface Resource usage. You can follow the prompts to view detailed information about different resources.
4. Use the sysstat tool for system monitoring
sysstat is a set of system performance monitoring tools, including sar, iostat, mpstat and other commands. We can install the sysstat tool through the following command:
sudo apt-get install sysstat
After the installation is complete, you can use the following command to query system performance data:
sar -u
This command will display the CPU usage of the system. You can use other parameters to view data on different resources, such as sar -r to view memory usage, sar -n DEV to view network transmission, etc.
5. Use Zabbix for remote monitoring
Zabbix is a powerful network monitoring tool that can monitor the performance data of multiple hosts in real time. We can install Zabbix Agent through the following command:
sudo apt-get install zabbix-agent
After the installation is completed, you need to edit some configuration files and set the IP address and port number of Zabbix Server in the zabbix_agentd.conf file. Then restart the Zabbix Agent service:
sudo service zabbix-agent restart
Finally, configure monitoring items and triggers on Zabbix Server to achieve remote monitoring.
6. Use Python to write custom monitoring scripts
In addition to using existing monitoring tools, we can also use Python to write custom monitoring scripts. The following is a simple example that can detect the CPU usage of the system:
import psutil def get_cpu_usage(): cpu_percent = psutil.cpu_percent() return cpu_percent if __name__ == "__main__": cpu_usage = get_cpu_usage() print("CPU Usage: {}%".format(cpu_usage))
The above code uses the psutil library to obtain the CPU usage of the system. You can write monitoring scripts for other functions as needed, such as monitoring memory usage, disk IO, network transmission, etc.
Summary:
On the Linux operating system, the monitoring system is an important means to ensure system stability and performance optimization. This article introduces several common system monitoring methods and tools, including top command, sar command, nmon command, sysstat tool, Zabbix, etc. In addition, we can also use Python to write custom monitoring scripts to meet specific needs. By monitoring the system in a timely manner, we can effectively detect problems and take appropriate measures to deal with them to ensure the normal operation of the system.
The above is the detailed content of How to set up system monitoring on Linux. For more information, please follow other related articles on the PHP Chinese website!