1

I'm using Ubuntu 14.04 in AWS, It doesn't have any Graphical UI, I want to implement an automated process that sends a screenshot of the top command whenever my CPU goes >70%.

And sometimes we need to send the output of df -h, du -sh, htop, etc.

The problem that I'm facing is, How to take a snapshot of a page without any GUI? Can anyone help me on this?

agc
  • 7,973
  • 2
  • 29
  • 50
TheDataGuy
  • 2,712
  • 6
  • 37
  • 89

3 Answers3

4
top -n 1 -b > top-output.txt

About option -b and -n from the top command man page:

    -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

tso
  • 4,732
  • 2
  • 22
  • 32
2

EDIT 2021 about MacOS

Don't use screenshot for monitoring!

A bit-mapped graphic screenshot needs to process a lot of bytes, compress them, and so on, using them for monitoring is a not-so-good idea!

Even when you read mail containing a screenshot, you won't be able to read values directly from your mail... (use of OCR tools with a lot of resources again)...

About top syntax

Running top in batch mode for only one shot syntax is:

top -bn 1

Under MacOS, you will run:

top -l 1

For more info, feel free to hit man top in your console, then rtfm!

Send top by mail when cpu load become high

This little script will send top results, by mail, if the CPU load is over 70%, by using . This could be run by crontab, for example...

#!/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);

This will read and store the 3 first lines of the top command, compute the total for user and system percents, then if that total is greater than 70%, render $pct for mail subject, then send the 3 stored lines and the rest of the top command, by using cat on STDIN.

... ( syntax ${user//.} will drop the dot, then multiply by 10 output of top command) ...

Mac version of this:
#!/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);

Send mail when storage becomes full

#!/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  / )

This will send a mail with subject: "MyHost: N disks over limit!" with N as number of disks, and content output of df -h for only N disks.

This script was tested under MacOS, seem work fine too!

In order to prevent useless values to be printed, you have to list devices you want to survey as df arguments:

...
} < <(LANG=C df -h  / /usr /home /etc )
F. Hauri - Give Up GitHub
  • 64,122
  • 17
  • 116
  • 137
1

Other answers solve most of the problem. F.Hauri shows how to check for a condition, and send mail when required. Tso shows how to dump top output to a file, which file can be sent as needed (as per F. Hauri's answer).

What remains:

df -h output can be redirected to a file like so df -h > output1. Same with du, i.e. du -h > output2.

htop to plain text: see k0fe's answer to "htop output to human readable file".

agc
  • 7,973
  • 2
  • 29
  • 50