"Fossies" - the Fresh Open Source Software Archive

Member "phpMyAdmin-5.1.0-all-languages/libraries/classes/Server/SysInfo/Linux.php" (24 Feb 2021, 2620 Bytes) of package /linux/www/phpMyAdmin-5.1.0-all-languages.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 
    3 declare(strict_types=1);
    4 
    5 namespace PhpMyAdmin\Server\SysInfo;
    6 
    7 use function array_combine;
    8 use function array_merge;
    9 use function file_get_contents;
   10 use function intval;
   11 use function is_array;
   12 use function is_readable;
   13 use function mb_strpos;
   14 use function mb_substr;
   15 use function preg_match_all;
   16 use function preg_split;
   17 
   18 /**
   19  * Linux based SysInfo class
   20  */
   21 class Linux extends Base
   22 {
   23     /**
   24      * The OS name
   25      *
   26      * @var string
   27      */
   28     public $os = 'Linux';
   29 
   30     /**
   31      * Gets load information
   32      *
   33      * @return array<string, int> with load data
   34      */
   35     public function loadavg()
   36     {
   37         $buf = file_get_contents('/proc/stat');
   38         if ($buf === false) {
   39             $buf = '';
   40         }
   41         $pos = mb_strpos($buf, "\n");
   42         if ($pos === false) {
   43             $pos = 0;
   44         }
   45         $nums = preg_split(
   46             '/\s+/',
   47             mb_substr(
   48                 $buf,
   49                 0,
   50                 $pos
   51             )
   52         );
   53 
   54         if (! is_array($nums)) {
   55             return ['busy' => 0, 'idle' => 0];
   56         }
   57 
   58         return [
   59             'busy' => (int) $nums[1] + (int) $nums[2] + (int) $nums[3],
   60             'idle' => (int) $nums[4],
   61         ];
   62     }
   63 
   64     /**
   65      * Checks whether class is supported in this environment
   66      *
   67      * @return bool true on success
   68      */
   69     public function supported()
   70     {
   71         return @is_readable('/proc/meminfo') && @is_readable('/proc/stat');
   72     }
   73 
   74     /**
   75      * Gets information about memory usage
   76      *
   77      * @return array with memory usage data
   78      */
   79     public function memory()
   80     {
   81         $content = @file_get_contents('/proc/meminfo');
   82         if ($content === false) {
   83             return [];
   84         }
   85 
   86         preg_match_all(
   87             SysInfo::MEMORY_REGEXP,
   88             $content,
   89             $matches
   90         );
   91 
   92         $mem = array_combine($matches[1], $matches[2]);
   93         if ($mem === false) {
   94             return [];
   95         }
   96 
   97         $defaults = [
   98             'MemTotal'   => 0,
   99             'MemFree'    => 0,
  100             'Cached'     => 0,
  101             'Buffers'    => 0,
  102             'SwapTotal'  => 0,
  103             'SwapFree'   => 0,
  104             'SwapCached' => 0,
  105         ];
  106 
  107         $mem = array_merge($defaults, $mem);
  108 
  109         foreach ($mem as $idx => $value) {
  110             $mem[$idx] = intval($value);
  111         }
  112 
  113         /** @var array<string, int> $mem */
  114         $mem['MemUsed'] = $mem['MemTotal'] - $mem['MemFree'] - $mem['Cached'] - $mem['Buffers'];
  115         $mem['SwapUsed'] = $mem['SwapTotal'] - $mem['SwapFree'] - $mem['SwapCached'];
  116 
  117         return $mem;
  118     }
  119 }