#!/bin/bash # -*- sh -*- # ------------------------------------------------------- # Munin node script # # Monitor the toner level of HP color printers # Retrieves toner levels for black, cyan, magenta & yellow cartridges # # Used in http://bernaerts.dyndns.org/linux/320-munin-snmp-plugin-hp-laser-toner-level # # Parameters : # $1 : none, "snmpconf" or "config" # # 20/06/2014, V1.0 - Creation by N. Bernaerts # 07/07/2014, V2.0 - Changed to multigraph # 21/08/2014, V3.0 - Handle multigraph as an option # 01/12/2016, V4.0 - Handle black_only as an option; added warning/critical thresholds # ------------------------------------------------------- : << =cut =head1 NAME snmp__tonerlevel_hp - Munin plugin to monitor the toner level of HP Color Laser Printers =head1 APPLICABLE SYSTEMS Any SNMP-capable HP Color Printer =head1 CONFIGURATION No specific configuration needed. This plugin uses public community. =head1 INTERPRETATION The level in percentage of printer toner cartridges =head1 MIB INFORMATION This plugin requires support for the cartridge level MIBs. =head1 MAGIC MARKERS #%# family=snmpauto #%# capabilities=snmpconf =head1 VERSION $Id$ =head1 BUGS None known. =head1 AUTHOR Copyright (C) 2014 Nicolas Bernaerts Copyright (C) 2016 Damian Pasternok =head1 LICENSE GPLv2. =cut # ------------------------------------------------------- # User defined configuration # ------------------------------------------------------- # if defines if display is multigraph (one graph per toner) # or if you want to get all toner levels on the same graph # on : one graph per toner # off : all toner levels on the same graph MULTIGRAPH="on" # for monochrome Laser Printers BLACK_ONLY="on" # ------------------------------------------------------- # Configure all MIBs adresses # ------------------------------------------------------- # printer name MIB_NAME="1.3.6.1.2.1.1.5.0" # cartridge colors max level MIB_MAX_BLACK="1.3.6.1.2.1.43.11.1.1.8.1.1" MIB_MAX_CYAN="1.3.6.1.2.1.43.11.1.1.8.1.2" MIB_MAX_MAGENTA="1.3.6.1.2.1.43.11.1.1.8.1.3" MIB_MAX_YELLOW="1.3.6.1.2.1.43.11.1.1.8.1.4" # cartridge colors current level MIB_LEVEL_BLACK="1.3.6.1.2.1.43.11.1.1.9.1.1" MIB_LEVEL_CYAN="1.3.6.1.2.1.43.11.1.1.9.1.2" MIB_LEVEL_MAGENTA="1.3.6.1.2.1.43.11.1.1.9.1.3" MIB_LEVEL_YELLOW="1.3.6.1.2.1.43.11.1.1.9.1.4" # ------------------------------------------------------- # Get printer specs # ------------------------------------------------------- # get printer name from script name PRINTER_IP=$(basename $0 | cut -d'_' -f2) # ------------------------------------------------------- # Read all MIBs data from the printer # ------------------------------------------------------- # if not in autoconf process, read SNMP data if [ "$1" != "snmpconf" ]; then # read SNMP MIB if [ "$BLACK_ONLY" = "off" ]; then MIB_RESULT=$(snmpget -v1 -One -c public $PRINTER_IP $MIB_NAME \ $MIB_MAX_BLACK $MIB_MAX_CYAN $MIB_MAX_MAGENTA $MIB_MAX_YELLOW \ $MIB_LEVEL_BLACK $MIB_LEVEL_CYAN $MIB_LEVEL_MAGENTA $MIB_LEVEL_YELLOW) fi if [ "$BLACK_ONLY" = "on" ]; then MIB_RESULT=$(snmpget -v1 -One -c public $PRINTER_IP $MIB_NAME \ $MIB_MAX_BLACK $MIB_LEVEL_BLACK) fi # get values from result PRINTER_NAME=$(echo $MIB_RESULT | sed 's/.*'$MIB_NAME'[^"]*"\([^"]*\).*/\1/g') MAX_BLACK=$(echo $MIB_RESULT | sed 's/.*'$MIB_MAX_BLACK'[ =:A-Z]*\([0-9]*\).*/\1/g') if [ "$BLACK_ONLY" = "off" ]; then MAX_CYAN=$(echo $MIB_RESULT | sed 's/.*'$MIB_MAX_CYAN'[ =:A-Z]*\([0-9]*\).*/\1/g') MAX_MAGENTA=$(echo $MIB_RESULT | sed 's/.*'$MIB_MAX_MAGENTA'[ =:A-Z]*\([0-9]*\).*/\1/g') MAX_YELLOW=$(echo $MIB_RESULT | sed 's/.*'$MIB_MAX_YELLOW'[ =:A-Z]*\([0-9]*\).*/\1/g') fi LEVEL_BLACK=$(echo $MIB_RESULT | sed 's/.*'$MIB_LEVEL_BLACK'[ =:A-Z]*\([0-9]*\).*/\1/g') if [ "$BLACK_ONLY" = "off" ]; then LEVEL_CYAN=$(echo $MIB_RESULT | sed 's/.*'$MIB_LEVEL_CYAN'[ =:A-Z]*\([0-9]*\).*/\1/g') LEVEL_MAGENTA=$(echo $MIB_RESULT | sed 's/.*'$MIB_LEVEL_MAGENTA'[ =:A-Z]*\([0-9]*\).*/\1/g') LEVEL_YELLOW=$(echo $MIB_RESULT | sed 's/.*'$MIB_LEVEL_YELLOW'[ =:A-Z]*\([0-9]*\).*/\1/g') fi fi # ------------------------------------------------------- # Handle SNMP autoconf call # ------------------------------------------------------- # snmpconf : announce required MIBs if [ "$1" = "snmpconf" ]; then echo "require $MIB_NAME" echo "require $MIB_MAX_BLACK" if [ "$BLACK_ONLY" = "off" ]; then echo "require $MIB_MAX_CYAN" echo "require $MIB_MAX_MAGENTA" echo "require $MIB_MAX_YELLOW" echo "require $MIB_LEVEL_BLACK" echo "require $MIB_LEVEL_CYAN" echo "require $MIB_LEVEL_MAGENTA" echo "require $MIB_LEVEL_YELLOW" fi # ------------------------------------------------------- # Handle configuration call # ------------------------------------------------------- # config : read node configuration elif [ "$1" = "config" ]; then # printer general infos echo "host_name $PRINTER_IP" if [ "$MULTIGRAPH" = "off" ]; then echo "graph_title Printer Toners" echo "graph_vlabel Cartridge level (%)" echo "graph_category toner" echo "graph_info This graph shows all toner levels in %" echo "graph_args --upper-limit 400 -l 0 --rigid" fi # -------------------- # Black cartridge # -------------------- if [ "$MULTIGRAPH" = "on" ]; then echo "multigraph black" echo "graph_title Black Toner" echo "graph_vlabel Cartridge level (%)" echo "graph_category toner" echo "graph_info This graph shows the Black toner level in %" echo "graph_args --upper-limit 100 -l 0 --rigid" fi echo "black-left.info $PRINTER_NAME black toner level is $LEVEL_BLACK out of $MAX_BLACK" echo "black-left.label Black available" echo "black-left.draw AREASTACK" echo "black-left.colour 000000" echo "black-left.min 0" echo "black-left.max 100" if [ "$BLACK_ONLY" = "on" ]; then echo "black-left.warning 10:" echo "black-left.critical 5:" fi echo "black-used.label Black used" echo "black-used.draw AREASTACK" echo "black-used.colour d0d0d0" echo "black-used.min 0" echo "black-used.max 100" [ "$BLACK_ONLY" = "on" ] && exit 0 # -------------------- # Cyan cartridge # -------------------- if [ "$MULTIGRAPH" = "on" ]; then echo "multigraph cyan" echo "graph_title Cyan Toner" echo "graph_vlabel Cartridge level (%)" echo "graph_category toner" echo "graph_info This graph shows the Cyan toner level in %" echo "graph_args --upper-limit 100 -l 0 --rigid" fi echo "cyan-left.info $PRINTER_NAME cyan toner level is $LEVEL_CYAN out of $MAX_CYAN" echo "cyan-left.label Cyan available" echo "cyan-left.draw AREASTACK" echo "cyan-left.colour 00ffff" echo "cyan-left.min 0" echo "cyan-left.max 100" echo "cyan-used.label Cyan used" echo "cyan-used.draw AREASTACK" echo "cyan-used.colour d0ffff" echo "cyan-used.min 0" echo "cyan-used.max 100" # -------------------- # Magenta cartridge # -------------------- if [ "$MULTIGRAPH" = "on" ]; then echo "multigraph magenta" echo "graph_title Magenta Toner" echo "graph_vlabel Cartridge level (%)" echo "graph_category toner" echo "graph_info This graph shows the Magenta toner level in %" echo "graph_args --upper-limit 100 -l 0 --rigid" fi echo "magenta-left.info $PRINTER_NAME magenta toner level is $LEVEL_MAGENTA out of $MAX_MAGENTA" echo "magenta-left.label Magenta available" echo "magenta-left.draw AREASTACK" echo "magenta-left.colour ff00ff" echo "magenta-left.min 0" echo "magenta-left.max 100" echo "magenta-used.label Magenta used" echo "magenta-used.draw AREASTACK" echo "magenta-used.colour ffd0ff" echo "magenta-used.min 0" echo "magenta-used.max 100" # -------------------- # Yellow cartridge # -------------------- if [ "$MULTIGRAPH" = "on" ]; then echo "multigraph yellow" echo "graph_title Yellow Toner" echo "graph_vlabel Cartridge level (%)" echo "graph_category toner" echo "graph_info This graph shows the Yellow toner level in %" echo "graph_args --upper-limit 100 -l 0 --rigid" fi echo "yellow-left.info $PRINTER_NAME yellow toner level is $LEVEL_YELLOW out of $MAX_YELLOW" echo "yellow-left.label Yellow available" echo "yellow-left.draw AREASTACK" echo "yellow-left.colour ffff00" echo "yellow-left.min 0" echo "yellow-left.max 100" echo "yellow-used.label Yellow used" echo "yellow-used.draw AREASTACK" echo "yellow-used.colour ffffa0" echo "yellow-used.min 0" echo "yellow-used.max 100" # ------------------------------------------------------- # Handle data read call # ------------------------------------------------------- # normal case : read cartridge levels else # -------------------- # Black level # -------------------- BLACK_AVAILABLE=$((100 * LEVEL_BLACK / MAX_BLACK)) BLACK_USED=$((100 - BLACK_AVAILABLE)) [ "$MULTIGRAPH" = "on" ] && echo "multigraph black" echo "black-left.value $BLACK_AVAILABLE" echo "black-used.value $BLACK_USED" [ "$BLACK_ONLY" = "on" ] && exit 0 # -------------------- # Cyan level # -------------------- CYAN_AVAILABLE=$((100 * LEVEL_CYAN / MAX_CYAN)) CYAN_USED=$((100 - CYAN_AVAILABLE)) [ "$MULTIGRAPH" = "on" ] && echo "multigraph cyan" echo "cyan-left.value $CYAN_AVAILABLE" echo "cyan-used.value $CYAN_USED" # -------------------- # Magenta level # -------------------- MAGENTA_AVAILABLE=$((100 * LEVEL_MAGENTA / MAX_MAGENTA)) MAGENTA_USED=$((100 - MAGENTA_AVAILABLE)) [ "$MULTIGRAPH" = "on" ] && echo "multigraph magenta" echo "magenta-left.value $MAGENTA_AVAILABLE" echo "magenta-used.value $MAGENTA_USED" # -------------------- # Yellow level # -------------------- YELLOW_AVAILABLE=$((100 * LEVEL_YELLOW / MAX_YELLOW)) YELLOW_USED=$((100 - YELLOW_AVAILABLE)) [ "$MULTIGRAPH" = "on" ] && echo "multigraph yellow" echo "yellow-left.value $YELLOW_AVAILABLE" echo "yellow-used.value $YELLOW_USED" fi exit 0