NRPE script for monitoring load average This script converts a server load value into percentage, compares it with non desired state ($warning and $critical) and returns all information in NRPE (Nagios Remote Plugin Executor) friendly output.

Desired output:

CPU_CORES=1 load=0.30 | load_in_percent=30;95;100
(And the main thing exit code.)

Goal: Use so much less external commands how is possible. Why? I would like to have the script independent. The second thing is just training purpose of bash scripting.

#!/usr/bin/env bash

set -o errexit -o pipefail

warning=95
critical=100

# 1. Find out a load was before 15 minutes.
# citation: "Frankly, if your box spikes above 1.0 on the one-minute average, you're still fine. It's when the 15-minute average goes north of 1.0 and stays there that you need to snap to."
# source: Andre Lewis, https://scoutapm.com/blog/understanding-load-averages
read -r _ _ quarter_load_average _ _ &lt; /proc/loadavg


# 2. Count all cores.
# citation: "How the cor es are spread out over CPUs doesn't matter. Two quad-cores == four dual-cores == eight single-cores. It's all eight cores for these purposes."
# source: Andre Lewis, https://scoutapm.com/blog/understanding-load-averages
while read -a row; do
  if [[ ${row[0]} = "cpu" ]] &amp;&amp; [[ ${row[1]} = "cores" ]]; then
    cpu_cores=$(( cpu_cores + "${row[3]}" ))
  fi
done &lt; /proc/cpuinfo


# 3. Convert load value into percentage.
# citation: "Sometimes it's helpful to consider load as percentage of the available resources (the load value divided by the number or cores).
# source: https://access.redhat.com/solutions/30554
load_mlt=$(echo "$quarter_load_average*100/1" | bc)
load_prc=$(( "$load_mlt" / "$cpu_cores" ))


# 4. Compare result with desired status and prepare returned value.
if [[ -z "$load_prc" ]]; then
  returned_text="LOAD UNKNOWN - check script"
  returned_code=3
else
  returned_text="CPU_CORES=$cpu_cores load=$quarter_load_average | load_in_percent=$load_prc;$warning;$critical"
  if [[ $load_prc -gt $critical ]]; then
    returned_code=2
  elif [[ $load_prc -gt $warning ]]; then
    returned_code=1
  else
    returned_code=0
  fi
fi

echo "$returned_text"
exit $returned_code
This looks pretty good. Some suggestions:


[[ is preferred over [ in Bash.
This script could benefit from set -o errexit -o nounset -o pipefail. You'll need to set cpu_cores=0 before referring to it in the loop, but that's just best practice.
Dividing by one isn't going to do much :)
The quotes are a cute feature, but I'd much rather see a summary of the point in them than the full sentences. Something like


  Treat 15 minute load average above 1 as critical.


A useful tactic I've found when dealing with comments is to think how you could name things to avoid comments altogether. Code can't lie, and names are very much like comments enshrined in code, so if you for example rename minute15 to quarter_load_average or something else human readable you should be able to remove the


  
  Find out a load was before 15 minutes.
  


comment above it.
Since you're only interested in one row from /proc/cpuinfo I would grep for it. Your goal of making the script stand-alone should be weighed against at least


maintainability, which will suffer greatly if you only use builtin commands,
speed, which is already not great for read, and
availability of grep, which is pretty much universal by now.

You can redirect a line to standard input using some_command &lt;&lt;&lt; "$line" to avoid redundant echos. So echo "$quarter_load_average*100/1" | bc could instead be bc &lt;&lt;&lt; "$quarter_load_average*100/1".