search
HomeBackend DevelopmentPHP TutorialHow to use PHP to write Linux scripts for system performance monitoring

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

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();

?>

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

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!

Statement
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
How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

What is autoloading in PHP?What is autoloading in PHP?Apr 30, 2025 pm 03:37 PM

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

What are PHP streams?What are PHP streams?Apr 30, 2025 pm 03:36 PM

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

What is the maximum size of a file that can be uploaded using PHP ?What is the maximum size of a file that can be uploaded using PHP ?Apr 30, 2025 pm 03:35 PM

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

What is Nullable types in PHP ?What is Nullable types in PHP ?Apr 30, 2025 pm 03:34 PM

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

What is the difference between the unset() and unlink() functions ?What is the difference between the unset() and unlink() functions ?Apr 30, 2025 pm 03:33 PM

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools