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 bash. 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 )