Scan the machine/server using php

Asked

Viewed 2,312 times

5

I am creating a system report, I honestly did not know that PHP did this, I researched and I found that I did, but I found no useful explanation, I just found PHP SYS INFO as a reference.

When I say machine I mean = server/pc/machine where the system is running

My points of doubt, follow this listing..

  1. Address Information -> Server IP, Host Name
  2. System -> Last boot, active for how long, active and inactive processes, CPU and SYS temperature'
  3. Use of Memory -> How much of the total is used

if there is any article, or guidance to get this thank you.

2 answers

7

There is an interesting project on Github that allows us to get a "universe" of information about the system:

phpsysinfo

A great advantage is the support we get because the project is tested in numerous platforms:

• Linux 2.6.x
• Freebsd 7.x
• Openbsd 2.8+
• Netbsd
• Dragonfly
• IBM AIX
• HP-UX
• Darwin/OSX
• Win 2000 / Win 2003 / Win XP / Win Vista / Win 7 / Win 8 / Win 8.1
• > PHP 5.2 or later With PCRE, XML, XSL, Mbstring and Simplexml Extension.

Installation

The installation process is relatively simple but lacks two points of attention described below along with the installation notes:

  1. Unzip the source code that can be discharged here to the server root.

  2. In the folder there is a configuration file called phpsysinfo.ini.new, if the installation being done is a new installation, you should copy this file to phpsysinfo.ini and edit it:

    cp phpsysinfo.ini.new phpsysinfo.ini
    
  3. Perform the following checks on file php.ini server:

    • Check whether the input include_path contains .
    • Because phpsysinfo requires access to many files on /proc among others, it is important to have the safe_mode deactivated:

      For this purpose in the archive php.ini, change the line of safe_mode for:

      safe_mode = Off
      

    Finally, ensure that the PHP extension with the name is installed php-xml, the same is necessary for the proper functioning of phpsysinfo.

If file changes have been made php.ini, just restart the server and ready, the fun can begin.

Note: To locate the file php.ini on the server, from the command line we can use the following command:

find / -name php.ini -print

Demonstration

There is a multi-language online demonstration that demonstrates the numerous potentialities of this project:

http://phpsysinfo.sourceforge.net/phpsysinfo/index.php?disp=dynamic

Captura de tela do link em cima

And much more...

Data format

Data can also be extracted through the API in the format that fits what we want to do:

  • XML see example

    /phpsysinfo/xml.php?plugin=complete
    
  • JSON see example

    /phpsysinfo/xml.php?plugin=complete&json
    
  • JSONP see example

    /phpsysinfo/xml.php?plugin=complete&jsonp&callback=getData
    

7

Considering that your PHP server is Linux (hardly has PHP installation on Windows), how to get the information you want:

  • IP: $_SERVER['SERVER_ADDR']

  • Host name: $_SERVER['SERVER_NAME']

  • Latest boot: shell_exec('uptime') or read the file /proc/uptime containing 2 values. 1st is Uptime in seconds

  • Active and inactive processes: shellexec('ps -eF') process the output according to your needs.

  • Temperature

    $temp    = exec('lm_sensors | grep \'°\'');
    $tempr   = explode('+', $temp);
    $tempval = preg_replace("/[^0-9,.]/", "", $tempr[1]);
    
  • Memory being used: memory_get_usage(true) (for your PHP script)

  • Total memory being used on the server: parse the output of the command free (contrib. @Guilhermenascimento)

  • Total memory: read the file /proc/meminfo

Edited based on @Guilhermenascimento’s comment:

"for lm-Sensors to work, the server must have installed it and usually SHARED servers cannot install on their own (maybe the server has installed or not installed)."

  • A good answer, but there are 3 problems, first memory_get_usage does not take the amount of memory on the server, but rather from the script executed, according to lm_sensors in its code is a constant and not a string and third for lm-Sensors to work, the server has to have installed it and usually SHARED servers can not install on their own (maybe the server has or not installed).

  • About lm_sensors, I fixed the code. When there is no lm_sensors on the server, if there is no such thing, there is no way the person can access that data. Unless there’s another method of access I don’t know about. If you know another way to access this data, please include the answer that also helps me. As for the memory being used, in my understanding that’s what he asked for. How much memory his script is using so you can see if there’s any place in the code that’s consuming too much memory. But I included the method to get the full memory being used on the server. Thanks for the help.

  • I didn’t say about other methods, what you have to do is pass these details in the answer, that sometimes it is necessary to install some things, because these commands are not native to PHP.

  • I believe your comment is enough to clarify this matter. :)

  • 1

    IF the person read the comments :)

  • Ready @Gulhermenascimento. Is it okay ? : ) If you’re not there, say what else to add. I’m in the spirit of New Year :D

  • Yes this very good, I do not want to give order to anyone rs, just wanted to help improve your answer, which was already very good! Congratulations! + 1

  • :) I know I was just kidding. Hug and happy new year.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.