Search

Munin - SNMP plugin to display HP Color Laser printer Toner levels

Contents[Hide]

dropcap-munin

Munin allows simple supervision of any SNMP enabled device.

As HP Color Laser Printers are SNMP enabled, it becomes possible to follow all printer internal levels thru Munin.

This article provides a plugin implementation that allows you to follow all 4 toner levels (black, cyan, magenta & yellow) on any HP Color Laser printer.

With the help of this plugin you'll be able to display you printer toner level in almost real time. As a pre-requisite, you need a fuly operational instance of Munin supervision.

This has been tested on a HP Color LaserJet CM1312nfi MFP with Munin running on a Debian server.

1. General Explanation

This plugin allows you to follow toner levels of a HP Color Laser Printer in a graphical way.

It supports all 3 Munin SNMP plugins calling modes :

  • snmpconf : to check if a given printer is compatible with the plugin
  • config : to describe the plugin
  • normal : to read toner levels

This plugin can run as a simple plugin with one graph display all 4 toner levels or as a multigraph plugin, displaying one graph per toner.

To configure its behaviour, you need to set the MULTIGRAPH parameter :

on (default) off
Uses Munin multigraph capabilities,
to display one graph per toner cartridge
Displays one unique graph,
with all 4 toner cartridges levels
  munin-snmp-toner-black 
    munin-snmp-toner-cyan
  munin-snmp-toner-multi 

 

2. Installation

First step is to download the plugin and to install the plugin from my GitHub Munin plugins repository.

Terminal
# wget -O /usr/share/munin/plugins/snmp__tonerlevel_hp https://raw.githubusercontent.com/NicolasBernaerts/munin-plugin/master/snmp__tonerlevel_hp
# chmod +x /usr/share/munin/plugins/snmp__tonerlevel_hp

/usr/share/munin/plugins/snmp__tonerlevel_hp
#!/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
# -------------------------------------------------------

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

=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="1.3.6.1.2.1.1.6.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
  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)

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

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

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

It's now time to disable multigraph mode (set MULTIGRAPH parameter to off) if needed.

By default it is working as a multigraph plugin (one graph per toner cartridge).

3. Activation

As with any SNMP plugin, you first need to declare your SNMP enabled printer in your Munin server /etc/hosts.

/etc/hosts

...
# printer
192.168.0.10    hp1312cm

You can now declare your printer in Munin configuration file /etc/munin/munin.conf.

As we are dealing with an SNMP plugin, the printer should be declared as follow :

/etc/munin/munin.conf

...
[hp1312cm]
    address 127.0.0.1
    use_node_name no
...

Once your printer is declared, you just need to activate the plugin like any other Munin SNMP plugin with a symbolic link including your printer name.

Munin-mode needs to be restarted for the new node to become active.

Terminal
# ln -s /usr/share/munin/plugins/snmp__tonerlevel_hp /etc/munin/plugins/snmp_hp1312cm_tonerlevel_hp
# service munin-node restart

4. Test

You are now ready to test your plugin with munin-run :

Terminal
# munin-run snmp_hp1312cm_tonerlevel_hp config
host_name hp1312cm
multigraph black
graph_title Black Toner
graph_vlabel Cartridge level (%)
graph_category toner
graph_info This graph shows the Black toner level in %
graph_args --upper-limit 100 -l 0 --rigid
black-left.info HP Color LaserJet CM1312nfi MFP black toner level is 1170411 out of 3442386
black-left.label Black available
black-left.draw AREASTACK
black-left.colour 000000
black-left.min 0
black-left.max 100
black-used.label Black used
black-used.draw AREASTACK
black-used.colour d0d0d0
black-used.min 0
black-used.max 100
multigraph cyan
...
# munin-run snmp_hp1312cm_tonerlevel_hp
multigraph black
black-left.value 33
black-used.value 67
multigraph cyan
cyan-left.value 51
cyan-used.value 49
multigraph magenta
magenta-left.value 45
magenta-used.value 55
multigraph yellow
yellow-left.value 45
yellow-used.value 55

Everything is working.

You should start to follow your HP Color Laser printer toner levels after few minutes.

Signature Technoblog

This article is published "as is", without any warranty that it will work for your specific need.
If you think this article needs some complement, or simply if you think it saved you lots of time & trouble,
just let me know at This email address is being protected from spambots. You need JavaScript enabled to view it.. Cheers !

icon linux icon debian icon apache icon mysql icon php icon piwik icon googleplus