Home Backend Development PHP Tutorial How to use PHP to write Linux scripts for system performance monitoring

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

Oct 05, 2023 am 11:40 AM
linux script php written 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
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!

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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Different ways to run shell script files on Windows Different ways to run shell script files on Windows Apr 13, 2023 am 11:58 AM

Windows Subsystem for Linux The first option is to use Windows Subsystem for Linux or WSL, which is a compatibility layer for running Linux binary executables natively on Windows systems. It works for most scenarios and allows you to run shell scripts in Windows 11/10. WSL is not automatically available, so you must enable it through your Windows device's developer settings. You can do this by going to Settings > Update & Security > For Developers. Switch to developer mode and confirm the prompt by selecting Yes. Next, look for W

How to write an efficient online voting system in PHP How to write an efficient online voting system in PHP Aug 09, 2023 pm 01:07 PM

How to write an efficient online voting system in PHP With the popularity of the Internet, online voting has become a common way to conduct public opinion polls and decision-making. In order to ensure the fairness, transparency and efficiency of the voting process, it is very important to design an efficient online voting system. In this article, I will explain how to write an efficient online voting system using PHP and provide some code examples. Create the database First, we need to create a database to store the voting data. This can be achieved using MySQL or other relational databases. under

How to use Python for scripting and execution in Linux How to use Python for scripting and execution in Linux Oct 05, 2023 am 11:45 AM

How to use Python to write and execute scripts in Linux In the Linux operating system, we can use Python to write and execute various scripts. Python is a concise and powerful programming language that provides a wealth of libraries and tools to make scripting easier and more efficient. Below we will introduce the basic steps of how to use Python for script writing and execution in Linux, and provide some specific code examples to help you better understand and use it. Install Python

An efficient Fibonacci sequence calculator written in PHP An efficient Fibonacci sequence calculator written in PHP Mar 21, 2024 am 10:06 AM

Efficient Fibonacci sequence calculator: PHP implementation of Fibonacci sequence is a very classic mathematical problem. The rule is that each number is equal to the sum of the previous two numbers, that is, F(n)=F(n -1)+F(n-2), where F(0)=0 and F(1)=1. When calculating the Fibonacci sequence, it can be implemented recursively, but performance problems will occur as the value increases. Therefore, this article will introduce how to write an efficient Fibonacci using PHP

How to use PHP to determine the number of digits in a number? How to use PHP to determine the number of digits in a number? Mar 26, 2024 am 11:39 AM

A practical method to use PHP to determine how many digits a number has. In programming, there is often a need to determine how many digits a number has. When writing a program in PHP, you can use some simple but practical methods to determine the number of digits in a number. Below we will introduce some methods of using PHP to determine the number of digits in a number, and attach specific code examples. Method 1: Use the strlen function The strlen function in PHP can return the length of a string. If we first convert the number to a string and then use s

PHP implements many-to-one address book: simple and practical contact management PHP implements many-to-one address book: simple and practical contact management Mar 15, 2024 pm 12:48 PM

PHP realizes many-to-one address book: simple and practical contact management. With the popularity of social networks, people's social relationships have become more and more complex, and managing contact information has become more and more important. In this context, it becomes particularly important to develop a simple and practical contact management system. This article will introduce how to use PHP to implement a many-to-one address book to add, delete, modify and search contact information. Functional design Before designing the contact management system, we need to determine the functional modules of the system, which mainly include: adding contacts

Implement PHP single user login restriction Implement PHP single user login restriction Mar 05, 2024 pm 10:27 PM

Implementing PHP single-user login restrictions requires specific code examples. When developing a website or application, sometimes it is necessary to ensure that users can only log in on one device to avoid multiple people sharing accounts. In order to implement this function, you can write code through PHP to limit single-user login. The specific implementation methods and code examples will be introduced below: Database design First, we need to save the user's login information in the database. You can create a table named user_sessions to store user sessions

What language is WordPress written in? What language is WordPress written in? Apr 15, 2024 pm 11:33 PM

WordPress is written in PHP and is mainly supported by the following programming languages: Core Platform PHP: Used to dynamically generate web pages. Database MySQL: used to store website data. Themes and Plugins HTML: Define website structure and layout. CSS: Defines the look and feel of the website. JavaScript: Add interactivity.

See all articles