-
-
Save markfaine/d54667b78ffc81ba686894065e533fa5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# shellcheck shell=bash | |
message="Message from $0" | |
cpuload="$(uptime | cut -d"," -f4 | cut -d":" -f2 | cut -d" " -f2 | sed -e "s/\.//g")" | |
message="${message}\ | |
cpuload: ${cpuload}" | |
threshold=500 | |
message="${message}\ | |
threshold: ${threshold}\n" | |
pid="$(ps -eo pid -eo pcpu -eo command | sort -k 2 -r | grep -v PID | head -n 1 | xargs | cut -d' ' -f 1)" | |
message="${message}\ | |
Higest CPU Usage PID: ${pid}" | |
if ps -ef -o pid="$pid" | head -n 1 | xargs; then | |
name="$(tr -d '\0' <"/proc/${pid}/cmdline")" | |
message="${message}\ | |
Higest CPU Usage Process: ${name}" | |
fi | |
pid2="$(ps -eo pid -eo pcpu -eo command | sort -k 2 -r | grep -v PID | head -n 2 | sort -r | head -n 1 | xargs | cut -d ' ' -f 1)" | |
message="${message}\ | |
Second Higest CPU Usage PID: ${pid2}" | |
if ps -ef -o pid="${pid2}" | head -n 1 | xargs; then | |
name2="$(tr -d '\0' <"/proc/${pid}/cmdline")" | |
message="${message}\ | |
Second Higest CPU Usage Process: ${name}" | |
fi | |
if [[ ${cpuload#0} -gt $threshold ]] ; then | |
message="${message}\ | |
High CPU Threshold exceeded: (${cpuload}/{$threshold})" | |
if [[ -n "$name" ]]; then | |
# Exclude plex | |
if [[ "$name" =~ Plex ]]; then | |
if [[ -e "/proc/$pid2/cmdline" ]]; then | |
name="$(cat "/proc/${pid2}/cmdline")" | |
message="${message}\ | |
Won't kill plex" | |
if [[ -n "$name" ]]; then | |
message="${message}\ | |
Killing $name2" | |
fi | |
kill -9 "$pid2" | |
fi | |
else | |
message="${message}\ | |
Killing ${name}" | |
kill -9 "$pid" | |
fi | |
fi | |
else | |
message="Message from $0\ | |
Success, CPU Threshold maintained: (${cpuload}/${threshold})" | |
fi | |
if [[ -n "$message" ]]; then | |
/root/bin/pushover.sh "$message" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please be very, very careful with this script. It does not add up on a lab-ubuntu I use.
(I needed a break from Golang, so I took a look at your script. Please file my nagging in the "German behavior" category 🤣 - it's only well-intentioned ^^)
Some smaller things I spotted:
4.0
would be considered greater than10.0
.0.10
but will not on0.01
(should be${var##0*}
); Problem with leading zeros is that bash will recognize the number as octal ([[ "010" -eq "10" ]]
is false){}
in line 30 are misplaced, leading to plain output of the brackets.ps
4 times, while 1 should be sufficient.ps
calls and many pipes are not needed, if the threshold isn't reached at all, because there's nothing appended to your message.sort
in line 19 sorts by PID not PCPU and thename
collected in line 23 gets its command from$pid
, not$pid2
.The first of two bigger problems I see is, that line 5 assumes that there is a fixed number of "," in the return of
uptime
, that in my case is not true:Instead of
I would therefor recommend (with
loadindex=3
if the 15 min avg should be considered):As second most-important thing, there is massive usage of pipes (and with that processes spawned) while it's not necessary at all. Example:
could also be 50% less processes with
while
:1
at pid-field sets field width to one, so the line will never start with a space-char. Appending=
to all fields will suppress headlines, and--sort pcpu
will (oh, wonder ^^) sort the list by the CPU load.So, finally, I reimplemented the script with 47% less processes running (even more, if threshold isn't hit), better readability, and way less indentation (and little more verbosity in line 50):