How to use PHP to write Linux scripts for system performance monitoring

WBOY
Release: 2023-10-05 11:42:01
Original
1433 people have browsed it

How to use PHP to write Linux scripts for system performance monitoring

How to use PHP to write Linux scripts for system performance monitoring

In Linux systems, system performance monitoring is very important for administrators and developers. By monitoring the performance of the system, we can understand the load of the system, discover potential problems in time, and take corresponding measures to ensure the stable operation of the system. This article will introduce how to use PHP to write Linux scripts for system performance monitoring and provide specific code examples.

1. Preparation

First, make sure that PHP is installed on your Linux system. You can check whether PHP is installed by running the following command:

php -v
Copy after login

If the version information of PHP is displayed, it means that PHP is installed.

2. Write a PHP script

Next, we will write a PHP script to monitor the performance of the system. We will use some tools provided by the Linux system to obtain system performance-related data, such as CPU usage, memory usage, etc.

The following is a simple sample code:

<?php

// 获取系统CPU使用率
function getCpuUsage() {
    $output1 = shell_exec("cat /proc/stat | grep cpu");
    sleep(1);
    $output2 = shell_exec("cat /proc/stat | grep cpu");
    
    $cpus = explode("
", $output1);
    $cpu1 = explode(" ", $cpus[0]);
    $cpu2 = explode(" ", $output2);
    
    $totalCpu1 = $cpu1[1] + $cpu1[2] + $cpu1[3] + $cpu1[4];
    $totalCpu2 = $cpu2[1] + $cpu2[2] + $cpu2[3] + $cpu2[4];
    $usage = ($totalCpu2 - $totalCpu1) / ($totalCpu2 + $cpu2[5] - $totalCpu1 + $cpu1[5]) * 100;
    
    return round($usage, 2);
}

// 获取内存使用情况
function getMemoryUsage() {
    $output = shell_exec("free -m | grep Mem");
    $memory = explode(" ", $output);
    
    $total = $memory[1];
    $used = $memory[2];
    $usage = $used / $total * 100;
    
    return round($usage, 2);
}

// 获取磁盘使用情况
function getDiskUsage() {
    $output = shell_exec("df -h --total | tail -n 1");
    $usage = explode(" ", $output);
    
    return $usage[4];
}

// 打印系统性能信息
function printSystemPerformance() {
    $cpuUsage = getCpuUsage();
    $memoryUsage = getMemoryUsage();
    $diskUsage = getDiskUsage();
    
    echo "CPU使用率: ".$cpuUsage."%
";
    echo "内存使用率: ".$memoryUsage."%
";
    echo "磁盘使用情况: ".$diskUsage."
";
}

// 调用函数获取并打印系统性能信息
printSystemPerformance();

?>
Copy after login

In the above sample code, we define several functions to obtain the CPU usage, memory usage and disk usage of the system. These functions use Shell commands to obtain relevant data, perform simple calculations and processing and return results.

Finally, we call the printSystemPerformance() function to obtain and print the system performance information.

3. Run the PHP script

Save the above code to a file with a .php suffix, such as performance.php.

In the terminal, go to the directory where the file is located and run the following command to execute the PHP script:

php performance.php
Copy after login

You will see the system's CPU usage, memory usage and disk usage Usage is printed.

If you want to automatically monitor system performance on a regular basis, you can use Linux's scheduled task tool cron to execute the PHP script. By setting appropriate time intervals, you can ensure that system performance data is updated and recorded in a timely manner.

Summary

This article introduces how to use PHP to write Linux scripts to monitor system performance. By using the tools and Shell commands provided by the system, we can obtain the system's CPU usage, memory usage, disk usage and other data, and process and display them accordingly. In this way, we can understand the operating status of the system in time, discover problems and take corresponding measures to ensure the stability and efficiency of the system. These tips are very useful for system administrators and developers, and I hope readers can get some help from them.

The above is the detailed content of How to use PHP to write Linux scripts for system performance monitoring. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template