PHP monitors Linux server load

Source: Internet
Author: User

Let's talk about how to execute Linux commands in PHP: http://www.ccvita.com/390.html, which is used to pave the way for this article. In actual project applications, due to the reality of various conditions, using PHP to implement server load monitoring is a more flexible method.

Due to the limited implementation methods of Web server and PHP, it is difficult for us to use PHP in the real environment to call some Linux commands that require the root permission for execution.ProgramIn this regard, I found another way to bypass this restriction from the Internet. First, write a C program to call the system command, and then use PHP to execute the C program.

C program
First, write a c file, such as/usr/local/ismole/W. C.

// Note for WordPress editor reasons, please refer to thisCodeRemove unnecessary spaces in header files
# Include<Stdio.H>
# Include<Stdlib.H>
# Include<Policypes.H>
# Include<Unistd.H>
 
IntMain()
{
Uid_t uid,EUID;
 
// Note to get the current uid
UID=Getuid();
// Note to obtain the current EUID
EUID=Geteuid();
 
// Note exchange the two IDs
If(Setreuid(EUID,UID))
Perror("Setreuid");
 
// Note: run the Linux Command.
System("/Usr/bin/W");
Return0;
}


Compile this fileGcc-o w-wall W. CThe program W is generated in the current directory.
Owner of the programChmod U + S./W.

 

 

PHP Execution
The file content is as follows. Put it in the web directory, the access will output the current server load.

<? PHP
/*
More & original PHP framwork
Copyright (c) 2007-2008 ismole Inc.
 
$ ID: servermonitor. php 408 08: 07: 40z Kimi $
*/
 
// Note key verification process
If ($ key! = $ Authkey ){
// Exit ('key error );
}
 
$ Last_line = exec ('/usr/local/ismole/W', $ retval );
 
$ Returnarray = explode ("load average:", $ retval [0]);
$ Returnstring = $ returnarray [1];
 
Echo $ returnstring;

According to the above example, we can use PHP to execute any Linux system commands, SVN updates, server monitoring, backup, recovery, and routine maintenance that we want to execute. For example, use SVN to build a test server: http://www.ccvita.com/383.html.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.