#!/bin/bash
###############################################################################
# This script does a auto-discovery of printers on networks by using ping
# and snmpget cycling through a range of IP that is defined in a separate
# network list file and IP range.
#
# network list source file format: nw-name|nw-baseip
# example:
# honjo|192.49.72
# itami|192.49.104
# tokyochuo|192.49.64
#
# run: printerconf-gen-so.sh > printers-so.cfg 2>/dev/null
###############################################################################

# First, we create the configuration file header and template information
cat<<END
###############################################################################
# HOST GROUP DEFINITIONS printers
###############################################################################
define hostgroup{
  hostgroup_name        printers-salesoffice        ; The name of the hostgroup
  alias                 Printers in Sales Office    ; Alias details of the group
}
define hostextinfo {
  hostgroup_name        printers-salesoffice
  use                   basic
}
define hostgroup{
  hostgroup_name        xerox-printers-so           ; The name of the hostgroup
  alias                 Xerox Printers in SO        ; Alias details of the group
}
define hostgroup{
  hostgroup_name        kyocera-printers-so         ; The name of the hostgroup
  alias                 Kyocera Printers in SO      ; Alias details of the group
}
###############################################################################
# PRINTER DEFINITION templates - These are NOT a real hosts, just a template!
###############################################################################
define host{
  name                  generic-printer-so ; The name of this host template
  use                   generic-host       ; Inherit default values from the generic-host template
  check_period          12x5_weekdays      ; monitor weekdays 8-8
  check_interval        5                  ; Actively check the server every 5 minutes
  retry_interval        1                  ; Schedule host check retries at 1 minute intervals
  max_check_attempts    5                  ; Check each server 10 times (max)
  check_command         check-host-alive   ; Default command to check if servers are "alive"
  notification_period   none               ; printers enter "sleep" mode each night - they go "down"
  notification_interval 240                ; Resend notifications every 120 minutes
  notification_options  d,u,r              ; Only send notifications for specific host states
  contact_groups        frankonly
  hostgroups            printers-salesoffice, 8-all-printers
  register              0                  ; DONT REGISTER THIS - ITS JUST A TEMPLATE
}
define host{
  name                  xerox-printer-so   ; The name of this host template
  use                   generic-printer-so ; Inherit default values from the generic-host template
  hostgroups            xerox-printers-so, printers-salesoffice, 8-all-printers
  icon_image            xerox-logo.png     ; the default image for the device
  statusmap_image       xerox-logo.gd2     ; the default image for the statusmap display
  register              0                  ; DONT REGISTER THIS - ITS JUST A TEMPLATE
}
define host{
  name                  kyocera-printer-so ; The name of this host template
  use                   generic-printer-so ; Inherit default values from the generic-host template
  hostgroups            kyocera-printers-so, printers-salesoffice, 8-all-printers
  icon_image            kyocera-logo.png   ; the default image for the device
  statusmap_image       kyocera-logo.gd2   ; the default image for the statusmap display
  register              0                  ; DONT REGISTER THIS - ITS JUST A TEMPLATE
}
###############################################################################
# PRINTER DEFINITIONS below
###############################################################################
END

# read our input file containing the network name and the 3-octet network base IP
for value in `cat so-nw.txt`; do
  soname=`echo $value | cut -d '|' -f 1,1`
  soip=`echo $value | cut -d '|' -f 2,2`
  # For debug, we can check if our file read is sucessful
  # echo $value $soname $soip

  cnt=1

  # range specifies the actual host ip we are going to check. For our networks, printers usually are deployed
  # in the low IP's, while the higher IP's are used by the laptop/desktop PC's defined in the DHCP ranges.
  # range="4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127";
  range="16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32";

  for ip in $range; do
    # check if the system exists at all and is pingeable
    #echo "IP: $soip.$ip"
    fping -c 1 -q $soip.$ip 2> /dev/null
    # if fping received a response, the exit code will be 0
    if [ $? -eq 0 ]; then
      #snmpget -v 1 -c public 192.168.91.4 HOST-RESOURCES-MIB::hrDeviceDescr.1 -Ov
      response=`snmpget -r 1 -v 1 -c public $soip.$ip HOST-RESOURCES-MIB::hrDeviceDescr.1 -Ov`
      # if snmpget received a response, the exit code will be 0
      if [ $? -eq 0 ]; then 
        # For printers we get
        # typical response 1 (Kyocera Printer)   -> STRING: LS-C5016N
        # typical response 2 (FujiXerox Printer) -> STRING: FUJI XEROX DocuCentre-III C440 v  3.  7.  1 Multifunction System
        echo "define host{"
        # check if we got a Kyocera
        echo $response| grep -q 'STRING: LS-'
        if [ $? -eq 0 ]; then echo "  use                   kyocera-printer-so"
        else
          # check if we got a Xerox
          echo $response| grep -q 'STRING: FUJI XEROX'
          if [ $? -eq 0 ]; then echo "  use                   xerox-printer-so"
          else
            echo "  use                   generic-printer-so"
          fi
        fi
        echo "  host_name             $soname-printer-$cnt"
        echo "  alias                 $soname-printer-$cnt (`echo $response| cut -d ' ' -f 2-5` $cnt)"
        echo "  address               $soip.$ip"
        echo "}"
        cnt=`expr $cnt + 1`
      fi
    fi
  done
done

