Skip to content
Snippets Groups Projects
Commit 460bbf01 authored by Théo - Le Filament's avatar Théo - Le Filament
Browse files

add check_docker shell script

parent 03246197
No related branches found
No related tags found
No related merge requests found
#!/bin/bash
# Default threshold values.
cpu_threshold_warning=25
cpu_threshold_critical=50
mem_threshold_warning=50
mem_threshold_critical=75
filter=''
print_help() {
echo "usage: ${0} [OPTIONS]"
echo "options:"
echo -e " -c, --cpu <warning:critical>\t\tthreshold values for cpu usage"
echo -e " -f, --filter <regex>\t\t\tcontainers to check"
echo -e " -h, --help\t\t\t\tprint this help"
echo -e " -m, --memory <warning:critical>\tthreshold values for memory usage"
}
while [[ $# -gt 0 ]]; do
case "$1" in
-c|--cpu)
cpu_threshold_warning=`awk -F ':' '{print +$1}' <(echo $2)`
cpu_threshold_critical=`awk -F ':' '{print +$2}' <(echo $2)`
shift 2
;;
-f|--filter)
filter=$2
shift 2
;;
-m|--memory)
mem_threshold_warning=`awk -F ':' '{print +$1}' <(echo $2)`
mem_threshold_critical=`awk -F ':' '{print +$2}' <(echo $2)`
shift 2
;;
--help|-h)
print_help
exit 0
;;
*)
echo "Error while parsing argument '$1'."
exit 1
;;
esac
done
# Gathering facts.
all_containers=`sudo docker ps --format '{{.Names}}' --filter "name=${filter}" --no-trunc --quiet --all`
running_containers=`sudo docker ps --format '{{.Names}}' --filter "name=${filter}" --no-trunc --quiet`
running_containers_stats=`sudo docker stats --format '{{.Name}}:{{.CPUPerc}}:{{.MemPerc}}' --no-stream`
not_running_containers=`comm -23 <(echo $all_containers) <(echo $running_containers)`
# Format data.
output=''
perf=''
cpu_scale=";${cpu_threshold_warning};${cpu_threshold_critical};0;100"
mem_scale=";${mem_threshold_warning};${mem_threshold_critical};0;100"
# Running containers.
for container in ${running_containers}; do
output+=" OK: ${container} status is running"
done
# Not running containers.
for container in ${not_running_containers}; do
output+=" NOK: ${container} status is not running"
done
# CPU and memory statistics for running containers.
for container_stats in ${running_containers_stats}; do
name=`awk -F ':' '{print $1}' <(echo $container_stats)`
[[ ! $name =~ ${filter} ]] && continue
cpu=`awk -F ':' '{print +$2}' <(echo $container_stats)`
mem=`awk -F ':' '{print +$3}' <(echo $container_stats)`
status='OK'
[[ ${cpu%.*} -ge $cpu_threshold_warning ]] && status='WARNING'
[[ ${cpu%.*} -ge $cpu_threshold_critical ]] && status='CRITICAL'
output+=" ${status}: ${name} cpu is ${cpu}%"
status='OK'
[[ ${mem%.*} -ge $mem_threshold_warning ]] && status='WARNING'
[[ ${mem%.*} -ge $mem_threshold_critical ]] && status='CRITICAL'
output+=" ${status}: ${name} memory is ${mem}%"
perf+=" ${name}_cpu=${cpu}%${cpu_scale}"
perf+=" ${name}_mem=${mem}%${mem_scale}"
done
# Nagios OK status.
exit_code=0
# Nagios WARNING status.
grep --quiet 'WARNING:' <(echo $output) && exit_code=1
# Nagios CRITICAL status.
grep --quiet 'NOK:\|CRITICAL:' <(echo $output) && exit_code=2
# Plugin output.
echo "${output#' '}|${perf#' '}"
exit $exit_code
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment