【问题标题】:Get CPU usage via SSH通过 SSH 获取 CPU 使用率
【发布时间】:2014-01-08 16:39:22
【问题描述】:

我想通过 SSH 检索 CPU 利用率百分比,我尝试了命令“top”,但它不会让我。

我使用的是 CentOS 6。

我试过这个代码

$connection = ssh2_connect("IP", PORT);
ssh2_auth_password($connection, "root", "PASS");
$stream = ssh2_exec($connection, "top");
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);

// Enable blocking for both streams
stream_set_blocking($errorStream, true);
stream_set_blocking($stream, true);

// Whichever of the two below commands is listed first will receive its appropriate output.  The second command receives nothing
echo "Output: " . stream_get_contents($stream);
echo "Error: " . stream_get_contents($errorStream);

// Close the streams        
fclose($errorStream);
fclose($stream);

但它每次都给我一个错误:输出:错误:未设置 TERM 环境变量。

我正在使用 PHP。

【问题讨论】:

  • 您可能需要详细说明“它不会让我”是什么意思……比如错误消息或其他东西……
  • 是否涉及任何编程或这是一个通用的操作系统问题?
  • 如果这是一个通用操作系统问题并且与编程没有直接关系,那么它更适合另一个 Stack Exchange 站点,例如超级用户或服务器故障。查看这些网站的目的并确定最适合该问题的网站。
  • top 假定它默认从交互式 shell 运行,请尝试 top -bn 1。还有man top.
  • 我现在用更多细节编辑我的问题。谢谢!

标签: php linux centos6 libssh2


【解决方案1】:

你可以使用

top -n 1
mpstat
iostat

【讨论】:

    【解决方案2】:

    谢谢大家,但我做到了。 我做了这个命令:

    top -b -n 10 -d.2 | grep 'Cpu' |  awk 'NR==3{ print($2)}'
    

    【讨论】:

      【解决方案3】:

      phpseclib, a pure PHP SSH implementation,对如何处理top in PHP 进行了精彩的讨论:

      http://phpseclib.sourceforge.net/ssh/pty.html#top

      即。你需要一个 PTY。

      $ssh->enablePTY();
      $ssh->exec('top');
      $ssh->setTimeout(5);
      $ansi->appendString($ssh->read());
      echo $ansi->getScreen();
      

      您也可以使用交互模式:

      $ansi->appendString($ssh->read('username@username:~$'));
      $ssh->write("top\n");
      $ssh->setTimeout(5);
      $ansi->appendString($ssh->read());
      echo $ansi->getScreen();
      

      请注意,输出将包含 ANSI 转义码。下面是一个将 ANSI 转义码呈现为 HTML 的示例:

      http://phpseclib.sourceforge.net/ssh/examples.html#top

      【讨论】:

        【解决方案4】:

        最好只安装glances。

        sudo apt install glances
        

        然后只需运行glances。然后您的输出将如下所示。您可以更改更新速度。

        $yourhostname - IP x.x.x.x/x Pub xx.xxx.xxx.xxx                  Uptime: 0:41:24
        
        CPU  [  1.4%]   CPU -     1.4%  MEM -   10.8%  SWAP -    0.0%  LOAD    12-core
        MEM  [ 10.7%]   user:     1.0%  total:  31.4G  total:   2.00G  1 min:    0.71
        SWAP [  0.0%]   system:   0.4%  used:   3.38G  used:        0  5 min:    0.73
                        idle:    98.5%  free:   28.0G  free:    2.00G  15 min:   0.69
        
        NETWORK     Rx/s   Tx/s   TASKS 322 (1136 thr), 1 run, 235 slp, 86 oth 
        eno1          0b     0b
        lo           2Kb    2Kb   Systemd          6    Services loaded: 190 active: 190
        _xxxxxxxx     0b    4Kb
                                    CPU%  MEM%   PID USER        NI S Command 
        DefaultGateway     62ms      4.2   0.9  1923 xxx          0 S cinnamon --replace
                                     2.9   0.1  9055 xxx          0 R /usr/bin/python3 /
        DISK I/O     R/s    W/s      1.9   0.5  1323 root         0 S /usr/lib/xorg/Xorg
        disk1          0    91K      1.3   0.0  1338 root         0 S irq/74-nvidia
        partition1     0      0      1.3   0.1  8586 xxx          0 S /usr/lib/gnome-ter
        partition2     0    91K      1.0   0.4  8907 xxx          0 S /opt/google/chrome
        sda1           0      0      0.6   1.0  5381 xxx          0 S /home/xxx/Programs
        sdc1           0      0      0.3   0.5  2102 xxx          0 S /opt/google/chrome
                                     0.3   0.4  2689 xxx          0 S /opt/google/chrome
        FILE SYS    Used  Total      0.3   0.6  4831 xxx          0 S /home/xxx/Programs
        /          15.6G   228G
        2019-02-13 05:39:04       No warning or critical alert detected
        

        【讨论】:

          猜你喜欢
          • 2019-04-21
          • 2013-02-27
          • 2020-03-03
          • 2018-03-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-28
          相关资源
          最近更新 更多