#!/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
#  and used in https://www.ioblogger.de/2016/09/hp-officejet-6700-tintenstand-munin/
#
#  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
#  21/08/2014, V4.0 - Julian Wolter, julian-w.de - updated to work with more printer, workaround snmp bug
# -------------------------------------------------------

: << =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 VERSION

  $Id$

=head1 BUGS

None known.

=head1 AUTHOR

Copyright (C) 2014 Nicolas Bernaerts, edited 2016 by Julian Wolter

=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"

# -------------------------------------------------------
#  Configure all MIBs adresses
# -------------------------------------------------------

# printer name
MIB_NAME="iso.3.6.1.2.1.1.5.0"

# cartridge colors max level
MIB_MAX_BLACK="iso.3.6.1.2.1.43.11.1.1.8.0.1"
MIB_MAX_CYAN="iso.3.6.1.2.1.43.11.1.1.8.0.3"
MIB_MAX_MAGENTA="iso.3.6.1.2.1.43.11.1.1.8.0.4"
MIB_MAX_YELLOW="iso.3.6.1.2.1.43.11.1.1.8.0.2"

# cartridge colors current level
MIB_LEVEL_BLACK="iso.3.6.1.2.1.43.11.1.1.9.0.1"
MIB_LEVEL_CYAN="iso.3.6.1.2.1.43.11.1.1.9.0.3"
MIB_LEVEL_MAGENTA="iso.3.6.1.2.1.43.11.1.1.9.0.4"
MIB_LEVEL_YELLOW="iso.3.6.1.2.1.43.11.1.1.9.0.2"

# -------------------------------------------------------
#  Get printer specs
# -------------------------------------------------------

# get printer name from script name
# PRINTER_IP=$(basename $0 | cut -d'_' -f2)
PRINTER_IP="192.168.178.31"

# -------------------------------------------------------
#  Read all MIBs data from the printer
# -------------------------------------------------------

# if not in autoconf process, read SNMP data
if [ "$1" != "snmpconf" ]; then 
  # read SNMP MIB
#  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)

  MIB_RESULT=$(snmpwalk -v1 -c public $PRINTER_IP)

  # 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')
  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')
  LEVEL_BLACK=$(echo $MIB_RESULT | sed 's/.*'$MIB_LEVEL_BLACK'[ =:A-Z]*\([0-9]*\).*/\1/g')
  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

# -------------------------------------------------------
#  Handle SNMP autoconf call
# -------------------------------------------------------

# snmpconf : announce required MIBs
if [ "$1" = "snmpconf" ]; then
  echo "require $MIB_NAME"
  echo "require $MIB_MAX_BLACK"
  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"

# -------------------------------------------------------
#  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"
  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"

  echo "black-left.warning 15:100"
  echo "black-left.critical 7:100"

  # --------------------
  # 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"

  echo "cyan-left.warning 15:100"
  echo "cyan-left.critical 7: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"

  echo "magenta-left.warning 15:100"
  echo "magenta-left.critical 7: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 e6e600"
  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 ffffcd"
  echo "yellow-used.min 0"
  echo "yellow-used.max 100"

  echo "yellow-left.warning 15:100"
  echo "yellow-left.critical 7: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" 

  # --------------------
  # 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
