【问题标题】:Linux Terminal - Take screenshot of top command and email itLinux 终端 - 截取 top 命令并通过电子邮件发送
【发布时间】:2017-09-09 09:57:29
【问题描述】:

我在 AWS 中使用 Ubuntu 14.04,它没有任何图形 UI,我想实现一个自动化流程,每当我的 CPU 运行时发送top 命令的屏幕截图>70%。

有时我们需要发送df -hdu -shhtop等的输出。

我面临的问题是,如何在没有任何 GUI 的情况下拍摄页面快照?谁可以帮我这个事?

【问题讨论】:

  • 由于处理位图可能会使用大量处理器,因此请考虑使用仅文本监控!
  • 请说明输出应该是纯文本ASCII,还是可以包含终端控制代码。

标签: linux shell terminal screenshot


【解决方案1】:
top -n 1 -b > top-output.txt

关于顶部命令手册页中的选项 -b 和 -n:

    -b  :Batch-mode operation
        Starts top in Batch mode, which could be useful  for  sending
        output  from  top  to  other  programs or to a file.  In this
        mode, top will not accept input and runs until the iterations
        limit  you've  set with the `-n' command-line option or until
        killed.

   -n  :Number-of-iterations limit as:  -n number
        Specifies  the  maximum  number of iterations, or frames, top
        should produce before ending.

UPD
see this link

【讨论】:

  • 这对top有好处,但有时我们需要发送htop、df -h和du -sh的输出,所以如果我们有截图就更好了。 (有问题更新)
  • 这些都是 X 服务器所必需的。
  • 我们试试scrot
【解决方案2】:

编辑 2021 关于MacOS

不要使用截图进行监控!

位图图形截图需要处理很多字节,压缩它们等等,用它们来监控是一个不太好的想法

即使您阅读包含屏幕截图的邮件,您也无法直接从邮件中读取值...(再次使用具有大量资源的 OCR 工具)...

关于top语法

批处理模式下运行top一个镜头语法是:

top -bn 1

在 MacOS 下,你会运行:

top -l 1

有关更多信息,请随时在您的控制台中点击man top,然后点击 rtfm!

当cpu负载变高时通过邮件发送top

这个小脚本将通过邮件发送top 结果,如果CPU 负载超过70%,使用。这可以由crontab 运行,例如...

#!/bin/bash
{
    declare -a out=()
    read; out+=("$REPLY")
    read; out+=("$REPLY")
    read; out+=("$REPLY")
    read foo user foo system foo <<<"$REPLY"
    ((tot=(${user//.}+${system//.}),tot>700))&& {
        tot=000$tot
        printf -v pct "%.1f\n" ${tot:0:${#tot}-1}.${tot:${#tot}-1}
        (
            printf "%s\n" "${out[@]}"
            cat
        ) |
        mail -s "$HOSTNAME's CPU load: $pct tot, $user us, $system sy!" user@host
    }
} < <(top -bn1);

这将读取并存储top 命令的前 3 行,计算 usersystem 百分比的总和,然后如果该总和大于 70 %,为邮件主题渲染$pct,然后在STDIN上使用cat发送3个存储的行和top命令的其余部分。

...(语法${user//.} 将删除点,然后乘以top 命令的10 输出)...

Mac 版本:
#!/bin/bash
{
    declare -a out=()
    for i in {1..4} ;do
        read; out+=("$REPLY")
    done
    read foo{,} user foo system foo <<<"${REPLY//%}"
    printf -v user %.2f $user
    printf -v system %.2f $system
    ((tot=(${user//.}+${system//.}),tot>7000))&& {
        tot=000$tot
        printf -v pct "%.2f\n" ${tot:0:${#tot}-2}.${tot:${#tot}-2}
        (
            printf "%s\n" "${out[@]}"
            cat
        ) |
        mail -s "$HOSTNAME's CPU load: $pct tot, $user us, $system sy!" user@host
    }
} < <(top -l1);

存储空间已满时发送邮件

#!/bin/bash

{
    read headline
    declare -a over=()
    while read;do
        read filesys size use avail pct mpnt <<<"$REPLY"
        ((${pct%%%}>70)) && over+=("$REPLY")
    done

    ((${#over[@]}>0))&&{
        printf "%s\n" "$headline" "${over[@]}" |
        mail -s "$HOSTNAME: ${#over[@]} disks over limit!" user@host
    }
} < <(LANG=C df -h  / )

这将发送一封主题为:“MyHost: N disks over limit!” 的邮件,其中N 作为磁盘数,内容输出为df -h,仅用于@987654342 @ 磁盘。

这个脚本在 MacOS 下测试过,看起来也很好用!

为了防止打印出无用的值,您必须将要调查的设备列为df 参数:

...
} < <(LANG=C df -h  / /usr /home /etc )

【讨论】:

    【解决方案3】:

    其他答案解决了大部分问题。 F.Hauri 展示了如何检查条件,并在需要时发送邮件。 Tso 展示了如何将 top 输出转储到文件中,该文件可以根据需要发送(根据 F. Hauri 的 回答)。

    剩下的:

    df -h 输出可以重定向到像df -h &gt; output1 这样的文件。与du 相同, du -h &gt; output2

    htop 转纯文本:见k0fe's answer to "htop output to human readable file"

    【讨论】:

      猜你喜欢
      • 2011-10-04
      • 1970-01-01
      • 2014-09-20
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 2014-03-22
      • 2011-01-17
      相关资源
      最近更新 更多