#!/usr/bin/perl -w
#
# usage: 
#    check_xerox_printer lprng_device
#
#
# Do SNMP query of Xerox printer; return necessary codes based on what we find
#


#BEGIN {
#	if ($0 =~ m/^(.*?)[\/\\]([^\/\\]+)$/) {
#		$runtimedir = $1;
#		$PROGNAME = $2;
#	}
#}

use strict;
use Getopt::Long;
use Net::SNMP;

my $community="CSL";
$community="public";
my $timeout=5;
my $retries=3;
my $status_oid=".1.3.6.1.4.1.253.8.59.6.1.1.9";
$status_oid=".1.3.6.1.4.1.11.2.4.3.8";
my $error;
my $response;
my $session;
my $status;
my $complete=0;
my $verbose=1;
my $host;
my $key;

$host = $ARGV[0];
$host =~ s/\%.*//;
print "Host: $host\n" if $verbose;

#create the SNMP session
print "Opening session to $host with PW $community, timeout $timeout and retries $retries\n" if $verbose;

print "Entering while loop" if $verbose;
print "Running get_table $status_oid\n" if $verbose;
($session, $error) = Net::SNMP->session(-hostname=>$host,
					  -community=>$community,
					  -timeout=>$timeout,
					  -retries=>$retries);
if( $error ){
  print "Error: $error\n" if $verbose;
  exit 1;
}

my $attempt = 0;
while(!($complete)) {
  print "attempt $attempt\n" if $verbose;
  $response = $session->get_table(($status_oid));
  if ($response) {
    $complete=1;
    foreach $key (sort keys (%$response)) {
      print "response for $key: $response->{$key}\n" if $verbose;
      if ($response->{$key} ne 17) {
        $complete=0;
      }
    }
    if (!$complete) {
      print "Sleeping 1\n" if $verbose;
      sleep 1;
    }
  } else {
    print "No reponse, Sleeping 5\n" if $verbose;
    sleep 5;
  }
}
  $session->close;

# exit with JSUCC
$status = 0;
print "Exit status: 0\n" if $verbose;
exit $status;
