Wednesday, June 22, 2011

If you use Nagios to monitor your Jet Direct printers, you may notice that the check_hpjd plugin is a bit useless when it comes to telling you exactly what toner cartridge is low. This forces you to either hunt down the printer's IP address and visit the printer's built-in web interface or you have to hoof it to the printer and see what cartridge is needed.

Not anymore.

About 90% of the time a printer is throwing a warning hissy fit in Nagios, it's because the toner is low or out. I wrote this plugin so that it tells me exactly what toner cartridge needs replaced (color and part number).

To invoke, edit commands.cfg to include the following:

define command{
        command_name    check_hpjd_new
        command_line    php /path/to/check_hpjd_new.php $HOSTADDRESS$ [community string = public] [warn = 10] [critical = 5]
}


The optional parameters (defaults) can be overridden in Version 4.

Use this in your device configs as you would use check_hpjd.

Limitations


This will not work with the old school cable-connected Jet Direct boxes. This will only work on newer printers with the Jet Direct module built in.

UPDATES

Version 4

  • Reduced the use of snmp_get from around 1 billion to 0
  • Addition of defaults for community string, warn and critical thresholds
  • Code cleanup
  • cartridges() was re-written
  • Better use of arrays


Version 3
  • Support for pretty much all recent LaserJet products (Possibly more vendors) that are RFC 1514 compliant
  • When a cartridge is low, the percent is shown in the Status Information box
  • Support added for custom critical and warning levels as percentage. To do this, edit commands.cfg to include something like this to have 10% warn and 5% critical:

define command{
        command_name    check_hpjd_new
        command_line    php /path/to/check_hpjd_new.php $HOSTADDRESS$ public 10 5
}


Version 2
  • Kills the plugin at the start if the host can't be found.
  • Code cleanup and consolidation
  • Added accounting for Processing, Checking, Printing, Jam, Intervention and Fuser keywords in status messages.
  • Interface cleanup (No more '-  -' at the end of statuses with exit code 0)
  • Code is commented. 
  • Added support for older models that advertise cartridge information in hex

    Download

    Version 4 (current)

    Version 3

    Version 2

    Version 1
    Reactions:
    Categories: , , , , ,

    52 comments:

    1. Thank you! - Works fine for me.
      "check_hpjd -H myprinter.local -C public" generated "Error in packet ()".

      rgrds, MC

      ReplyDelete
    2. I don't think I've ever seen the "Error in packet ()" message before. If you're using version 3, be sure to specify warn and critical thresholds.

      ReplyDelete
    3. Hi,

      I have tried the following service definition for my HP3503

      define service{
      use generic-service
      host_name AD-HP3505
      service_description Printer Status
      check_command check_hpjd_new!-C public
      normal_check_interval 10
      retry_check_interval 1
      }

      but getting (null) in the status information


      plese give me the exact service definition is this is wrong

      ReplyDelete
    4. This comment has been removed by the author.

      ReplyDelete
    5. Here's what I use in my configs:
      define service{
      use generic-service
      host_name the_printer
      service_description Jet Direct
      check_command check_hpjd_new
      notifications_enabled 1
      }

      Also, define the command in commands.cfg:

      define command{
      command_name check_hpjd_new
      command_line php $USER1$/check_hpjd_new.php $HOSTADDRESS$ public 10 5
      }


      I think you may have made it a bit more complicated than it needs to be.

      ReplyDelete
    6. I have installed this

      php-snmp-5.1.6-27.el5_5.3.i386


      please check

      ReplyDelete
    7. Hello,

      I have configured the command.cfg and also printer.cfg as in your previous thread.

      I have installed php-snmp-5.1.6-27.el5_5.3.i386

      but still getting (null) ,

      Is there any other packages for php do i install?
      I am using centos 5 and nagios 3.2.3

      Please help me

      Thanks you

      ReplyDelete
    8. Is snmp configured on the printer? Sorry to ask.

      ReplyDelete
    9. Yes snmp is configured and working fine with
      check_hpjd. Is there any specific configuration
      for this plugin

      ReplyDelete
    10. Could you please help me on this

      ReplyDelete
    11. Bijo - You missed the "php" in the command (so did i at first)

      Justin - Im getting "Toner Low See Printer for Details link " in the details instead of the cartridge and percentage its low.
      Its a HP Color LaserJet CM2320nf MFP, is this because its not supported?

      ReplyDelete
    12. That's because the printer is sending a "toner low" error yet it isn't sending cartridge information. I've seen this behavior with monochrome printers especially, but since that's a CLJ, I'm a little stumped.

      ReplyDelete
    13. Hi Justin Kendall,

      I'm actually getting the (NULL) error like BIJO P.G was.

      Double and triple checked the command/service.

      Just wondering maybe it doesn't work with my model? HP OfficeJet 6310.

      Thanks.

      Kind Regards,

      Gary

      ReplyDelete
      Replies
      1. Is the RTMP service on the printer turned on? It's been a while since I did any development on this. I haven't tested it on any of the 6000 series.

        Delete
      2. Hi Justin Kendall,

        The RTMP service as in media service? I didn't know that printers supported that functionality, I'm not sure if it's actually turned on for our printer or not... How would I go about checking this? Or am I thinking of the wrong RTMP?

        Thank you.

        Kind Regards,

        Gary Shergill

        Delete
      3. Oh, or is it Routing Table Maintenance Protocol?

        Delete
      4. *SNMP Wow... I think I had a braindead moment.

        Delete
    14. Hi Justin Kendall,

      Sorry for your time, SNMP is available but not enabled on our printer - the admin doesn't wish it to be either.

      Thank you for the quick replies and responses.

      Kind Regards,

      Gary Shergill

      ReplyDelete
    15. I believe I have it setup correctly but keep getting (Return code of 255 is out of bounds) on several different printers I am testing this on. I'm testing on HP Color LJ 4700n & HP LJ 4250. All other plugins that use SNMP for these printers work great so I must have something not right. Have PHP 5.3.10-1ubuntu3.4, the plugin is in the same directory as the rest. Here is my command "php $USER1$/check_hpjd_new.php $HOSTADDRESS$ public 10 5" The $USER1$ is the path to the files and that is what I have for all the other commands. The only difference between this and the other 33 other commands is none have the php in front of them. Please help resolve this issue.

      ReplyDelete
      Replies
      1. Have you tried using the full path to the php binary?

        Delete
    16. Did the Null return ever get figured out on this? Running into the same trouble where all printers are returning a null. HP LJ 4350 is a pretty common one that I would think shouldn't have an issue, but does...

      Here is my setup... Any ideas?

      # 'check_hpjd_new' command definition
      define command{
      command_name check_hpjd_new
      command_line php $USER1$/check_hpjd_new.php $HOSTADDRESS$ public 10 5
      }

      define service{
      use generic-service
      host_name Printer H
      service_description Printer JetDirect
      check_command check_hpjd_new
      notifications_enabled 1
      }

      Thank you!

      ReplyDelete
      Replies
      1. You sometimes have to specify the path to the php executable. Also do you have the php-snmp module installed?

        Delete
      2. Just an update. I got side tracked from this.
        The following worked for me.

        # 'check_hpjd_new' command definition
        define command{
        command_name check_hpjd_new
        command_line php /usr/lib/nagios/plugins/check_hpjd_new.php $HOSTADDRESS$ Public 10 5
        }

        define service{
        use generic-service
        host_name Printer Name
        service_description Printer JetDirect
        check_command check_hpjd_new!Printer Name
        notifications_enabled 1
        }

        Using the full path worked correctly.
        To test out any changes I used the direct method in the terminal

        php /usr/lib/nagios/plugins/check_hpjd_new.php $HOSTADDRESS$ Public 10 5

        and it returned the "printer OK" so, then I knew it needed the full path to the module as you suggested.

        Thanks for this gem!

        Delete
    17. For some reason this plugin always returns "Cannot reach the printer. Possibly offline. " for each printer I have tested with even though the original check_hpjd returns that the toner cartridge is low. I have tried about 6 different printers. Here is my command:

      $USER1$/check_hpjd_new -H $HOSTADDRESS$ -C public 10 5

      FYI: I am using NagiosQL and I know the plugin is working since this same text is within the php command file right at the start.

      Thanks for any help

      ReplyDelete
      Replies
      1. -H and -C aren't necessary. As a matter of fact, the way things are in the code, it's seeing -H as the host name and $HOSTADDRESS$ as the community string. I know this is a non-standard way of doing things.

        "$USER1$/check_hpjd_new $HOSTADDRESS$ public 10 5" would likely fix things.

        Delete
    18. Hmmm. As per the code:
      //If the printer can't be reached, there is no sense in continuing past this point.
      if(!@snmpget($host, $community, 'iso.3.6.1.2.1.1.1.0')){
      echo "Cannot reach the printer. Possibly offline.";
      exit(2);
      }

      All it's doing is fetching a commonly used snmp value. Try commenting-out this if statement in the code.

      Also, do you have "php5-snmp" installed? It would throw this error if that isn't installed.

      ReplyDelete
      Replies
      1. This comment has been removed by the author.

        Delete
      2. Commenting out that check seems to have fixed the issue. But with my testing it always reports the the "Printer OK" even though the printer is reporting the Black cartridge is at 1%. Well i guess its progress.

        Delete
      3. The reason that if statement is in there is to safeguard against having the "Printer OK" status, which is the default, when the printer can't be reached.

        Try "$USER1$/check_hpjd_new $HOSTADDRESS$ public 10 5" Notice the lack of "-H" and "-C".

        Delete
      4. That did it...in my case if I'd RTFM more closely then I would have noticed that those options were not required! Thanks for the help. This plugin has already proved useful several times!

        Delete
      5. This comment has been removed by the author.

        Delete
      6. I have same issue said message :
        php5 check_hpjd_new.php 192.168.43.14 public 10 5
        Cannot reach the printer. Possibly offline.

        It worked for a bit then it started to give this message not sure why

        Delete
      7. This comment has been removed by the author.

        Delete
    19. I also get (Return code of 255 is out of bounds) I have specified the path to PHP and still get this error.

      ReplyDelete
      Replies
      1. I needed to install php5-snmp, works fine now.

        Delete
      2. Excellent. I should put that requirement in giant bold letters.

        Delete
    20. How are you defining and executing the command?

      ReplyDelete
    21. Hi Justin,
      I configured my nagios in this way:

      define command{
      command_name check_hpjd_new
      command_line php $USER1$/check_hpjd_new.php $HOSTADDRESS$ public 10 5
      }

      define service{
      use generic-service
      host_name printer_1
      service_description Jet Direct
      check_command check_hpjd_new
      #notifications_enabled 1
      }
      In web interface I have critical status (Return code of 127 is out of bounds - plugin may be missing)
      Also when I try to execute script manually I have this response:
      ./check_hpjd_new.php: line 1: ?php: No such file or directory
      ./check_hpjd_new.php: line 3: =[1]: command not found
      ./check_hpjd_new.php: line 4: =[2]: command not found
      ./check_hpjd_new.php: line 5: =[3]: command not found
      ./check_hpjd_new.php: line 6: =[4]: command not found
      ./check_hpjd_new.php: line 8: //If: No such file or directory
      ./check_hpjd_new.php: line 67: syntax error near unexpected token `{'
      ./check_hpjd_new.php: line 67: ` if($return_string == ''){'
      I would appreciate any suggestion.


      ReplyDelete
      Replies
      1. Something must be seriously wrong here. It looks as though Nagios is trying to run this PHP script as a BASH script. You might also want to use the full path to the php binary instead of just using "php" in the command definition.

        Delete
    22. Just for documentation/contribution purposes:
      We have an HP CM3530, monitoring server is Red Hat 4, PHP 5.1.6 with Nagios 3.2.3 with Centreon front end.

      The problem is that if the cartridges are above the minimum percentage configured in the command, but the printer has the manufacturer "Need to change toner" flag on, it sends the alarm anyway.

      We made basically 3 changes:
      First, we commented the following lines (67, 68 and 69), which were the ones that overrode our configuration:
      66. //If the JetDirect SNMP isn't advertising cartrige information...
      67. //if($return_string == ''){
      68. // $return_string .= "Toner Low See Printer for Details link";
      69. //}

      We also had following errors was shown when executed manually:
      [root@centreon ~]# php ~/check_hpjd_new.php xxx.xxx.xxx.xxx community 10 5
      PHP Notice: Undefined offset: 1 in /root/check_hpjd_new.php on line 121
      PHP Notice: Undefined variable: hex_string in /root/check_hpjd_new.php on line 124
      Order Cyan Cartridge HP CE251A

      In the line 121 we had this:
      121. $cartridge = $cartridge[1];

      The $cartridge array has a length of 1 (that's the result of the previous line) so this should be changed to:
      $cartridge = $cartridge[0];
      *Note: there are 2 lines (121 and 131) with the same modification made.

      Finally we defined the $hex_string variable at the start of the function cartridges():
      118. $hex_string = '';

      I don't know if this is the same for other printers, I guess it all depends of the MIBs that the printers have installed.

      Regards.

      ReplyDelete
      Replies
      1. Wow. Thanks for the info. I haven't done any development work on this plugin in about 2 years. I wouldn't be surprised if things have changed to the point where your changes would be necessary for newer printers.

        Delete
      2. You're welcome. Great job on the plugin by the way ;)

        Delete
    23. This comment has been removed by the author.

      ReplyDelete
    24. Hi,
      I am trying to run this plugin in my nagios, but:

      # /usr/bin/php5 check_hpjd_new.php 10.172.1.4 public 10 5
      PHP Notice: Undefined variable: screen_text in /root/check_hpjd_new.php on line 35
      PHP Notice: Undefined variable: screen_text in /root/check_hpjd_new.php on line 36
      PHP Notice: Undefined variable: screen_text in /root/check_hpjd_new.php on line 39
      PHP Notice: Undefined variable: screen_text in /root/check_hpjd_new.php on line 42
      PHP Notice: Undefined variable: screen_text in /root/check_hpjd_new.php on line 45
      PHP Notice: Undefined variable: screen_text in /root/check_hpjd_new.php on line 35
      PHP Notice: Undefined variable: screen_text in /root/check_hpjd_new.php on line 61
      PHP Notice: Undefined variable: screen_text in /root/check_hpjd_new.php on line 62
      PHP Notice: Undefined variable: screen_text in /root/check_hpjd_new.php on line 61
      Toner Low See Printer for Details link

      the same with different printer:
      /usr/bin/php5 check_hpjd_new.php 10.171.22.8 public 10 5
      PHP Notice: Undefined variable: screen_text in /root/check_hpjd_new.php on line 61
      PHP Notice: Undefined variable: screen_text in /root/check_hpjd_new.php on line 62
      PHP Notice: Undefined variable: screen_text in /root/check_hpjd_new.php on line 61
      Printer OK

      how can I fix it?

      ReplyDelete
      Replies
      1. It's your PHP configuration. If you put "error_reporting(0);" on the first line after "<?php", the debug stuff will stop spamming your screen.

        For more, see http://www.w3schools.com/php/func_error_reporting.asp

        Delete
    25. Just found and loaded your new hpjd checker.

      A couple of suggestions:

      1) The check for 'iso.3.6.1.2.1.1.1.0' should be changed to an "if (empty(@snmparray)" so that the check doesn't have to rely on an spurious snmpget().

      2) If you are not using the same MIBs as some other people, you can get index mismatches in the cartridge() function (and probably somewhere else). For example, when I grab the entire SNMP array from the device, instead if being indexed against ".iso.n.n.n.n...." values, my system returns "SNMPv2-SMI::mib-2.43....". This caused my system to report that the printer was OK (since the array wasn't empty) but never matched the cartridge uitilization information.

      ReplyDelete
    26. How you can use this on Icinga2 ?

      ReplyDelete
    27. I managed to make it works with ICINGA2,
      if someone needs here is the script :

      object CheckCommand "check_hpjd_new" {
      import "plugin-check-command"

      command = [ PhpExe , PluginDir + "/check_hpjd_new.php" ]

      arguments = {
      "-H" ={ value = "$check_hpjd_new_address$"
      skip_key = true
      order = -1
      }
      "-C" ={ value = "$snmp_community$"
      skip_key = true
      order = 0
      }


      "-w" ={value = "$hpjd_new_warning$"
      skip_key = true
      order = 1
      }

      "-c" ={value = "$hpjd_new_critical$"
      skip_key = true
      order = 2
      }

      }

      vars.check_hpjd_new_address = "$address$"
      vars.snmp_community = "public"
      vars.hpjd_new_warning= "5"
      vars.hpjd_new_critical= "2"
      }


      Is missing one check the "Drum",
      could you add to the new version ?

      thanks

      ReplyDelete
      Replies
      1. One of these days I'll have to pick this project up again. I switched jobs a while back and I don't have the many printer types that I used to have to test my scripts with. It seems from some posts on here that JetDirect has changed a bit in the three years since I released version 4. I might have to get myself a LaserJet for home just to poke at it with a stick.

        Delete
    28. I don't think this works with my model printer HP LaserJet 400 color M451dn. It always tells me the printer is ok when in fact I have low toner on my cyan

      ReplyDelete
    29. I only get the status but not the actual toner details. Any ideas?

      ReplyDelete

    Subscribe to RSS Feed Follow me on Twitter!