PHP monitors linux server load

Source: Internet
Author: User

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 programs that require root permission in Linux, 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.

 

 
  1. // Note due to the wordpress Editor, remove unnecessary spaces in the header file in this Code.
  2. # Include <stdio. h>
  3. # Include <stdlib. h>
  4. # Include <policypes. h>
  5. # Include <unistd. h>
  6.  
  7. Int main ()
  8. {
  9. Uid_t uid, euid;
  10.  
  11. // Note to get the current uid
  12. Uid = getuid ();
  13. // Note to obtain the current euid
  14. Euid = geteuid ();
  15.  
  16. // Note exchange the two IDs
  17. If (setreuid (euid, uid ))
  18. Perror ("setreuid ");
  19.  
  20. // Note: run the linux Command.
  21. System ("/usr/bin/w ");
  22. Return 0;
  23. }

// Note due to the wordpress Editor, remove unnecessary spaces in the header file in this Code.

Compile the gcc-o EPT-Wall w. c file. The program w is generated in the current directory.

Change the chmod u + s./w owner of this program.

PHP Execution

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

 

 
  1. <? Php
  2. // Note key verification process
  3. If ($ key! = $ Authkey ){
  4. // Exit (key error );
  5. }
  6.  
  7. $ Last_line = exec (/usr/local/ismole/w, $ retval );
  8.  
  9. $ ReturnArray = explode ("load average:", $ retval [0]);
  10. $ ReturnString = $ returnArray [1];
  11.  
  12. 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

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.