Skip to content

Zabbix 7.4 and Proxmox LXC #11

@netcore-24

Description

@netcore-24

Hi, I have a situation where I have two Proxmox hosts with a NUT server and an LXC container that is a NUT client. I wanted to add monitoring to Zabbix, and here's what I would like to suggest.

#!/bin/bash
# Zabbix <-> NUT helper (LLD + metrics), safe for Zabbix Agent 2
set -euo pipefail
PATH=/usr/sbin:/usr/bin:/sbin:/bin
export LANG=C

a1="${1:-}"   # 'ups.discovery'  or UPSNAME or 'UPSNAME@HOST'
a2="${2:-}"   # HOST or PARAM (if a1='UPS@HOST')
a3="${3:-}"   # PARAM ( a1='UPS', a2='HOST')

# ---- helper: получить список UPS на HOST, вычистив шум ----
list_names() {
  local h="$1" out rc=0
  out="$(/usr/bin/upsc -l "$h" 2>&1 || rc=$?)" || true
  if [[ $rc -eq 0 ]]; then printf '%s\n' "$out"; return 0; fi
  rc=0; out="$(/usr/bin/upsc -l "@$h" 2>&1 || rc=$?)" || true
  if [[ $rc -eq 0 ]]; then printf '%s\n' "$out"; return 0; fi
  return 1
}

# ---------- LLD ----------
if [[ "$a1" == "ups.discovery" ]]; then
  host="$a2"
  names=""
  if [[ -n "$host" ]]; then
    if list_names "$host" >/dev/null 2>&1; then
      names="$( list_names "$host" | grep -Ev '^(Init SSL|SSL|Error:|usage:|Network UPS Tools|[[:space:]]*$|First form|Second form|Third form|Common arguments:)' || true )"
    fi
  else
    names="$( /usr/bin/upsc -l 2>&1 | grep -Ev '^(Init SSL|SSL|Error:|usage:|Network UPS Tools|[[:space:]]*$|First form|Second form|Third form|Common arguments:)' || true )"
  fi
  printf '{\n\t"data":['
  first=1
  while IFS= read -r discovered; do
    [[ -n "$discovered" ]] || continue
    [[ $first -eq 1 ]] || printf ','
    printf '\n\t\t{"{#UPSNAME}":"%s"}' "$discovered"
    first=0
  done <<< "$names"
  printf '\n\t]\n}\n'
  exit 0
fi

#  A) upsmon[UPSNAME, HOST, PARAM]  -> ups="UPSNAME@HOST", key="PARAM"
#  B) upsmon[UPSNAME@HOST, PARAM]   -> ups="UPSNAME@HOST", key="PARAM"
if [[ -n "$a3" ]]; then
  ups="${a1}@${a2}"
  key="$a3"
else
  ups="$a1"
  key="$a2"
fi

[[ -n "$ups" && -n "$key" ]] || exit 0

if [[ "$key" == "ups.status" ]]; then
  state=$(/usr/bin/upsc "$ups" "$key" 2>&1 | grep -Ev '^(Init SSL|SSL|Error:)' | xargs || true)
  case "$state" in
    "OL")         echo 1  ;;  # On line
    "OB")         echo 2  ;;  # On battery
    "LB")         echo 3  ;;
    "RB")         echo 4  ;;
    "CHRG")       echo 5  ;;
    "DISCHRG")    echo 6  ;;
    "BYPASS")     echo 7  ;;
    "CAL")        echo 8  ;;
    "OFF")        echo 9  ;;
    "OVER")       echo 10 ;;
    "TRIM")       echo 11 ;;
    "BOOST")      echo 12 ;;
    "OL CHRG")    echo 13 ;;
    "OB DISCHRG") echo 14 ;;
    *)            echo 0  ;;  # unknown/empty/error
  esac
else
  case "$key" in
    battery.temperature|output.frequency|battery.voltage|input.frequency|output.voltage|ups.load)
      raw=$(/usr/bin/upsc "$ups" "$key" 2>&1 | grep -Ev '^(Init SSL|SSL|Error:|usage:|Network UPS Tools)' | xargs || true)
      num="$(echo "$raw" | awk 'match($0, /[0-9]+(\.[0-9]+)?/){print substr($0, RSTART, RLENGTH)}')"
      if [[ -n "$num" ]]; then echo "$num"; else echo 0; fi
      ;;
    *)
      /usr/bin/upsc "$ups" "$key" 2>&1 | grep -Ev '^(Init SSL|SSL|Error:|usage:|Network UPS Tools)' || true
      ;;
  esac
fi

upsmon.conf
UserParameter=upsmon[*],/usr/lib/zabbix/externalscripts/ups_status.sh $1 $2 $3

Template_UPS_fixed_with_host.xml

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions