Skip to content

Commit

Permalink
Create check_pve Proxmox VE check command
Browse files Browse the repository at this point in the history
  • Loading branch information
NEMSLinux committed Mar 14, 2024
1 parent 83dca25 commit 874280e
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 1 deletion.
2 changes: 1 addition & 1 deletion debpack/DEBIAN/control
Expand Up @@ -4,7 +4,7 @@ Priority: required
Section: plugins
Maintainer: NEMS Linux <nems@category5.tv>
Architecture: all
Version: 1.6.055
Version: 1.6.056
Provides: nems-plugins
Description: NEMS Plugins for check commands within NEMS Linux
Depends: fuse, owfs, libow-perl, libnagios-plugin-perl | libmonitoring-plugin-perl, openjdk-11-jre-headless | openjdk-17-jre-headless, curl, jq, bc, wmic, 9590, iperf, iperf3, libswitch-perl, libio-socket-ssl-perl, monit
210 changes: 210 additions & 0 deletions debpack/usr/lib/nagios/plugins/check_pve
@@ -0,0 +1,210 @@
#!/bin/bash
# check_pve - Proxmox Virtual Environment server check
# For NEMS Linux (Compatible with Nagios / Icinga / etc)
# By Robbie Ferguson // https://nemslinux.com/

for ARGUMENT in "$@"
do
KEY=$(echo $ARGUMENT | cut -f1 -d=)

KEY_LENGTH=${#KEY}
VALUE="${ARGUMENT:$KEY_LENGTH+1}"

export "$KEY"="$VALUE"
done

ticket=`curl -s -k -d "username=${username}@${realm}" --data-urlencode "password=${password}" https://${ip}:${port}/api2/json/access/ticket | jq --raw-output '.data.ticket' | sed 's/^/PVEAuthCookie=/'`

json_data=`curl -s --insecure --cookie "${ticket}" https://${ip}:${port}/api2/json/nodes/${node}/status | jq "."`

# Define warning and critical thresholds
warning_threshold=80
critical_threshold=95

check_load() {
# Parse JSON data and extract relevant fields
cpus=$(echo "$json_data" | jq -r '.data.cpuinfo.cpus')
loadavg=$(echo "$json_data" | jq -r '.data.loadavg[0]')
loadavg5=$(echo "$json_data" | jq -r '.data.loadavg[1]')
loadavg15=$(echo "$json_data" | jq -r '.data.loadavg[2]')

# Calculate the percentage of loadavg against cpus
loadavg_percentage=$(bc <<< "scale=2; $loadavg / $cpus * 100")

# 1 decimal place or a whole number if the decimal is zero
loadavg_percentage=$(printf "%.1f" "$loadavg_percentage")
if [[ "$loadavg_percentage" == *\.0 ]]; then
loadavg_percentage=${loadavg_percentage%\.0}
fi

# Compare loadavg percentage against thresholds
if (( $(bc <<< "$loadavg_percentage >= $critical_threshold") )); then
echo "CRITICAL: CPU Load ($loadavg_percentage%) exceeds critical threshold ($critical_threshold%) | load=$loadavg;$warning_threshold;$critical_threshold;;"
exit 2
elif (( $(bc <<< "$loadavg_percentage >= $warning_threshold") )); then
echo "WARNING: CPU Load ($loadavg_percentage%) exceeds warning threshold ($warning_threshold%) | load=$loadavg;$warning_threshold;$critical_threshold;;"
exit 1
else
echo "OK: CPU Load ($loadavg_percentage%) is within thresholds | load=$loadavg;$warning_threshold;$critical_threshold;;"
exit 0
fi
}

check_rootfs() {
# Parse JSON data and extract relevant fields
total_rootfs=$(echo "$json_data" | jq -r '.data.rootfs.total')
used_rootfs=$(echo "$json_data" | jq -r '.data.rootfs.used')

# Calculate the percentage of rootfs used
rootfs_percentage=$(bc <<< "scale=2; $used_rootfs / $total_rootfs * 100")

# Check if rootfs usage exceeds thresholds
if (( $(bc <<< "$rootfs_percentage >= 95") )); then
echo "CRITICAL: Root filesystem usage $rootfs_percentage% | rootfs_usage=$rootfs_percentage%;80;95;;"
exit 2
elif (( $(bc <<< "$rootfs_percentage >= 80") )); then
echo "WARNING: Root filesystem usage $rootfs_percentage% | rootfs_usage=$rootfs_percentage%;80;95;;"
exit 1
else
echo "OK: Root filesystem usage $rootfs_percentage% | rootfs_usage=$rootfs_percentage%;80;95;;"
exit 0
fi
}

check_proxmox_version() {
# Fetch remote JSON data
proxmox_json=$(curl -sS https://endoflife.date/api/proxmox-ve.json)

# Extract our Proxmox version
our_version=$(echo "$json_data" | jq -r '.data.pveversion' | cut -d'/' -f2)

# Extract the latest version from the remote JSON data
latest_version=$(echo "$proxmox_json" | jq -r '[.[].latest] | map(split(".") | map(tonumber)) | max | join(".")')

# Check if our version is not the latest
if [ "$our_version" != "$latest_version" ]; then
echo "WARNING: Your Proxmox version ($our_version) is not the latest ($latest_version) | proxmox_version=$our_version;;;;"
exit 1
fi

# Find the corresponding entry for our version in the remote JSON
entry=$(echo "$proxmox_json" | jq --arg latest_version "$latest_version" '.[] | select(.latest == $latest_version)')

# Extract EOL date
eol_date=$(echo "$entry" | jq -r '.eol')

# Check if we are within 90 days of EOL
current_date=$(date +%Y-%m-%d)
days_to_eol=$(( ($(date -d "$eol_date" +%s) - $(date -d "$current_date" +%s)) / 86400 ))
if [ "$days_to_eol" -le 90 ]; then
echo "CRITICAL: Your Proxmox version ($our_version) is within 90 days of EOL ($eol_date) | proxmox_version=$our_version;;;;"
exit 2
fi

echo "OK: Your Proxmox version ($our_version) is up to date | proxmox_version=$our_version;;;;"
exit 0
}

check_memory_usage() {
# Parse JSON data and extract relevant fields
total_memory=$(echo "$json_data" | jq -r '.data.memory.total')
used_memory=$(echo "$json_data" | jq -r '.data.memory.used')

# Calculate the percentage of memory used
memory_percentage=$(bc <<< "scale=2; $used_memory / $total_memory * 100")

# 1 decimal place or a whole number if the decimal is zero
memory_percentage=$(printf "%.1f" "$memory_percentage")
if [[ "$memory_percentage" == *\.0 ]]; then
memory_percentage=${memory_percentage%\.0}
fi

# Check if memory usage exceeds thresholds
if (( $(bc <<< "$memory_percentage >= 95") )); then
echo "CRITICAL: Memory usage is $memory_percentage% | memory_usage=$memory_percentage%;80;95;;"
exit 2
elif (( $(bc <<< "$memory_percentage >= 80") )); then
echo "WARNING: Memory usage is $memory_percentage% | memory_usage=$memory_percentage%;80;95;;"
exit 1
else
echo "OK: Memory usage is $memory_percentage% | memory_usage=$memory_percentage%;80;95;;"
exit 0
fi
}

check_swap_usage() {
# Parse JSON data and extract relevant fields
total_swap=$(echo "$json_data" | jq -r '.data.swap.total')
used_swap=$(echo "$json_data" | jq -r '.data.swap.used')

# Calculate the percentage of swap used
swap_percentage=$(bc <<< "scale=2; $used_swap / $total_swap * 100")

# Check if swap usage exceeds thresholds
if (( $(bc <<< "$swap_percentage >= 95") )); then
echo "CRITICAL: Swap usage is $swap_percentage% | swap_usage=$swap_percentage%;80;95;; swap_total=${total_swap} swap_used=${used_swap};;"
exit 2
elif (( $(bc <<< "$swap_percentage >= 80") )); then
echo "WARNING: Swap usage is $swap_percentage% | swap_usage=$swap_percentage%;80;95;; swap_total=${total_swap} swap_used=${used_swap};;"
exit 1
else
echo "OK: Swap usage is $swap_percentage% | swap_usage=$swap_percentage%;80;95;; swap_total=${total_swap} swap_used=${used_swap};;"
exit 0
fi
}

# Check if any required variables are unset
if [[ -z "$ip" || -z "$port" || -z "$node" || -z "$username" || -z "$password" || -z "$realm" ]]; then
WHITE='\033[0;97m'
CYAN='\033[0;36m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

echo -e "${WHITE}"
echo "check_pve 1.0 by Robbie Ferguson // https://nemslinux.com/"
echo -e "${NC}"

echo -e "${YELLOW}Usage:${NC}"
echo "$0 variable=value"
echo

echo -e "${YELLOW}Variables:${NC}"
echo -e " - ${CYAN}ip:${NC} IP address of the Proxmox server ${WHITE}[${RED}Required${WHITE}]${NC}"
echo -e " - ${CYAN}port:${NC} Port number Proxmox is accessible on (default: 8006)"
echo -e " - ${CYAN}node:${NC} The name of the node you wish to check ${WHITE}[${RED}Required${WHITE}]${NC}"
echo -e " - ${CYAN}username:${NC} Username of user with PVEAuditor permission set ${WHITE}[${RED}Required${WHITE}]${NC}"
echo -e " - ${CYAN}password:${NC} Password for that user ${WHITE}[${RED}Required${WHITE}]${NC}"
echo -e " - ${CYAN}realm:${NC} Authentication realm (Eg., pve or pam) ${WHITE}[${RED}Required${WHITE}]${NC}"
echo -e " - ${CYAN}check:${NC} Specify the check to perform (load, rootfs, version, memory, swap) ${WHITE}[${RED}Required${WHITE}]${NC}"
echo -e " - ${CYAN}warn:${NC} Warning threshold percentage"
echo -e " - ${CYAN}crit:${NC} Critical threshold percentage"
echo

echo -e "${YELLOW}Example:${NC}"
echo "$0 ip=10.0.0.5 port=8006 node=myserver username=auditor password=Str0ngP4ssw0rd realm=pve check=load warn=80 crit=95"
echo
exit 1
fi

case "$check" in
load)
check_load
;;
rootfs)
check_rootfs
;;
version)
check_proxmox_version
;;
memory)
check_memory_usage
;;
swap)
check_swap_usage
;;
*)
echo "Unknown check: $check"
exit 3
;;
esac

0 comments on commit 874280e

Please sign in to comment.