Skip to main content
  1. All Posts/

php-monitor

Tools PHP

php-monitor

A free, flexible, powerful tool that helps you monitor PHP Service and profiling PHP code.






English | 简体中文

✨ Features

  • 🌈 Get detailed PHP runtime data.
  • 🌍 Monitor production environment time consuming requests.
  • 🛡 Displays the memory and CPU consumption of the underlying function.
  • 🎨 Use various kinds of visual graphics to display data.

⚙️ System requirements

  • uprofiler,xhprof,tideways php extension(default tideways).
  • composer
  • PHP 5.6+

Install tideways extension

PHP 5.6(download tideways v4.1.5)
PHP 7.0+(download tideways v4.1.7)

wget --no-check-certificate https://github.com/tideways/php-xhprof-extension/archive/v4.1.7.tar.gz  && tar zxvf v4.1.7.tar.gz && cd php-xhprof-extension-4.1.7 && phpize && ./configure && make && sudo make install

Add configuration data on php.ini.You should see something like:

extension=tideways.so

Once installed, you can use the following command to check:

> php --ri tideways
tideways
tideways => 4.1.7

Install php-monitor

composer create-project --prefer-dist --ignore-platform-reqs laynefyc/php-monitor php-monitor && cd php-monitor/public && php -S 127.0.0.1:8066

Visit http://127.0.0.1:8066 and input account and password(php/php).

Detailed installation tutorial

  1. Download & Update Projects

    composer create-project --prefer-dist --ignore-platform-reqs laynefyc/php-monitor php-monitor
    <p>
      or
    </p>
    
    <pre>git clone https://github.com/laynefyc/php-monitor.git
    

    cd php-monitor composer update –ignore-platform-reqs

  2. The project can set data storage mode and supports MySQL, MongoDB, SQLite.
    Set in configuration file src/config/config.php,The information is as follows:

    // 'save' => [
    //     'driver'    => 'mysql',
    //     'host'      => '127.0.0.1:3306',
    //     'database'  => 'php_monitor',
    //     'username'  => '',
    //     'password'  => 'abcd1234',
    //     'charset'   => 'utf8mb4'
    // ],
    // 'save' => [
    //     'driver'    => 'mongodb',
    //     'host'      => '127.0.0.1:27017',
    //     'database'  => 'php_monitor',
    //     'username'  => '',
    //     'password'  => ''
    // ],
    'save' => [
        'driver'    => 'sqlite',
        'database'  =>  dirname(__DIR__).'/db/php_monitor.sqlite3'
    ],
    <p>
      SQLite is used by default in this project,if you use other databases, please uncomment them.<br /> If you want to use MySQL to run the following table creation statement (table name cannot be modified):
    </p>
    
    <pre>CREATE TABLE `php_monitor` (
    `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Auto-increment number ',
    `url` text CHARACTER SET utf8 COMMENT 'Request URL',
    `server_name` varchar(255) CHARACTER SET utf8 DEFAULT NULL COMMENT 'Service name',
    `get` text COMMENT 'GET parameter',
    `pmu` int(11) unsigned DEFAULT NULL COMMENT 'Memory spike',
    `wt` int(11) unsigned DEFAULT NULL COMMENT 'Total time spent in microseconds',
    `cpu` int(11) unsigned DEFAULT NULL COMMENT 'Total CPU cycle time',
    `ct` int(3) NOT NULL COMMENT 'Total calls',
    `mu` int(11) unsigned DEFAULT NULL COMMENT 'Current memory consumption',
    `request_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Request time accurate to seconds',
    `request_time_micro` int(10) unsigned DEFAULT '0' COMMENT 'Request time accurate to microseconds',
    `profile` longblob NOT NULL COMMENT 'performance data,
    `server` longblob COMMENT 'SERVER parameter',
    `type` varchar(16) DEFAULT NULL COMMENT 'Request time includes GET,POST',
    `ip` varchar(16) DEFAULT NULL COMMENT 'IP address',
    PRIMARY KEY (`id`),
    KEY `idx_url` (`url`),
    KEY `idx_ip` (`ip`)
    

    ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;

    <p>
      Mongodb database will build its own tables, but it needs to add indexes by yourself.The adding way as follows:
    </p>
    
    <pre>show dbs
    

    use php_monitor //Please select your own database db.php_monitor.createIndex({“url”:1}) db.php_monitor.createIndex({“ip”:1})

    <p>
      The table name for all data storage methods must be <code>php_monitor</code> and does not support modification. </li> 
      
      <li>
        The operation of monitoring platform<br /> It can directly pass the following command during testing:</p> <pre class="notranslate"><code>cd php-monitor/public
    

    php -S 127.0.0.1:8066

        <p>
          After running successfully ,It can be accessed <a rel="nofollow noopener" target="_blank" href="http://127.0.0.1:8066">http://127.0.0.1:8066</a>directly .<br /> Non-test environment please use Nginx.The configuration is as follows:
        </p>
        
        <pre>server {
    listen       8066;
    server_name  localhost;
    root /home/www/cai/php-monitor/public;
    index  index.php index.html;
    location / {
        root /home/www/cai/php-monitor/public;
    }
    
    location ~ .php$ {
        fastcgi_pass   127.0.0.1:9000;
        include        fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME  $document_root/index.php;
    }
    

    }

  3.   <li>
        Login background<br /> Login account password can be modified directly in the configuration file,<code>src/config/config.php</code>。</p> <pre class="notranslate"><code>'user' =&gt; [
    //login account and password
    ['account'=&gt;'php','password'=&gt;'php'],
    ['account'=&gt;'admin','password'=&gt;'abcd']
    

    ]

        <p>
          Please change the account number and password in time after release.<br /> If you require a higher level of security, please extend the method of Login Controller.php file. </li> 
          
          <li>
            Introduce monitoring into the project.<br /> The project is monitored in a non-invasive way, without any interference to the service in operation.<br /> There are two ways to add monitoring to a project. One is to modify the nginx configuration:<br /> For example, to monitor the running service <a rel="nofollow noopener" target="_blank" href="http://www.site.com">www.site.com</a>, you only need to add a line of configuration information in the nginx configuration file</p> <pre>fastcgi_param PHP_VALUE "auto_prepend_file={php-monitor-path}/src/autoPrepend.php";
    

            <p>
              The effect of adding configuration is as follows (other content is just for demonstration, not the same nginx configuration):
            </p>
            
            <pre>server {
    

    listen 80; server_name www.site.com; root your/webroot/; location ~ .php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root/index.php; fastcgi_param PHP_VALUE “auto_prepend_file={php-monitor-path}/src/autoPrepend.php”; } }

            <p>
              This way is to use the <code>auto_prepend_file</code> interface provided by PHP,interfaceing <a rel="nofollow noopener" target="_blank" href="https://www.php.net/manual/zh/ini.core.php#ini.auto-prepend-file">https://www.php.net/manual/zh/ini.core.php#ini.auto-prepend-file</a>.You need to restart nginx after adding configuration.<br /> The second way is to import the entry file that needs to monitor the project directly , usually add it in <code>public/index.php</code>:
            </p>
            
            <pre>require '/home/www/cai/php-monitor/src/autoPrepend.php';</pre>
            
            <p>
              The effect after adding configuration is as follows (except the core code, the other code is for demonstration):
            </p>
            
            <pre>&lt;?php
    

    use pmcommonRouter;

    //The core code is here require ‘/home/www/cai/php-monitor/src/autoPrepend.php’;

    include ‘vendor/autoload.php’; $config = require(‘src/config/config.php’); (new Router($config))->run();

            <p>
              After adding the burying point, the request record of <a rel="nofollow noopener" target="_blank" href="http://www.site.com">www.site.com</a> project can be viewed in the <a rel="nofollow noopener" target="_blank" href="http://127.0.0.1:8066">http://127.0.0.1:8066</a> monitoring background. </li> 
              
              <li>
                More details</p> <ul dir="auto">
                  <li>
                    MongoDB has the fastest storage speed. If you have high performance requirements, please use it first.
                  </li>
                  <li>
                    Modify the profile.enable property of the configuration file to modify the sampling frequency. Generally speaking, it is not necessary to store all requests.For example, &#8216;rand (1, 100) > 60&#8217; is to set the sampling rate to &#8216;40%&#8217;;
                  </li>
                  <li>
                    Modify the profiler.filter_path attribute of the configuration file to filter services that you do not want to collect, such as some intranet services that do not care about execution efficiency;
                  </li>
                </ul>
              </li>
              
              <li>
                Swoole support</p> <pre>public function onReceive(swoole_server $serv, $fd, $from_id, $dataSrc)
    

    { require ‘/home/www/cai/php-monitor/src/autoPrepend.php’;

    //your code
    
    pmcommonPMonitor::shutdown($data['params']['route'],$serv-&gt;getClientInfo($fd,$from_id)['remote_ip'],'TCP');
    

    }

          <h2 dir="auto">
            <a rel="nofollow noopener" target="_blank" id="user-content-todo" class="anchor" aria-hidden="true" href="#todo"></a>TODO
          </h2>
          
          <ul class="contains-task-list">
            <li class="task-list-item">
              Sqlite storage mode development;
            </li>
            <li class="task-list-item">
              Perfect internationalization;
            </li>
            <li class="task-list-item">
              Improve documentation;
            </li>
            <li class="task-list-item">
              CI process access;
            </li>
            <li class="task-list-item">
              Supplementary unit test;
            </li>
            <li class="task-list-item">
              Composer package encapsulation;
            </li>
            <li class="task-list-item">
              Rewrite xhprof extension;
            </li>
            <li class="task-list-item">
              Separation of buried point module and display module;
            </li>
            <li class="task-list-item">
              Docker access;
            </li>
          </ul>
          
          <h2 dir="auto">
            <a rel="nofollow noopener" target="_blank" id="user-content-feedback" class="anchor" aria-hidden="true" href="#feedback"></a>Feedback
          </h2>
          
          <p>
            Please submit your&#8230;
          </p>