#!/usr/local/bin/expect -f

# check_mf plugin for NetSaint
# Written by Stanley Hopcroft (Stanley.Hopcroft@ipaustralia.gov.au)
#
# This plugin checks to see that guest logon (no username of password)
# to a 3270 database via telnet is Ok by using Expect to hunt for
# "usual" panels.
#
# This plugin requires Expect ( http://www.expect.nist.gov )


# $Id$

# $Log$

# NetSaint Plugin return codes http://www.netsaint.org/dosc/pluginhowto.html

# Numeric Value Service Status Status Description
# 0       Ok
#         The plugin was able to check the service and it appeared to
#               be functioning properly

# 1       Warning
#         The plugin was able to check the service, but it appeared
#               to be above some "warning" threshold or did not appear to
#               be working properly

# -1      Unknown
#         Invalid command line arguments were supplied to the
#         plugin or the plugin was unable to check the status of the
#         given hosts/service

# 2       Critical
#         The plugin detected that either the service was not running
#         or it was above some "critical" threshold

set timeout 20

set env(TERM) vc404

#exp_internal 1

log_user 0

set date [timestamp -format "%c"]

# don't check between midnight and 10 past since the
# mainframe service will be unavailable during the
# scheduled NIM TP monitor restart

if {[regexp "00:0" $date]} {
  puts "Logon to Production database Ok "
  exit 0
                              #     Ok
}

spawn /usr/bin/tn3270 disc-6-29

#exp_internal 1

expect {
  "==> ______" {}
  timeout {
               send -- "^[["  # Telnet escape sequence
                              # 2 characters 0x1b 0x5b
               expect {
                 "tn3270> " {send -- "c\r"; expect "? " {}}
                 timeout {}
               }
               sleep 5
               close
               wait
               puts "## Logon to Production database failed ## "
               puts "$expect_out(buffer)\n"
               exit 2         # +++ Critical +++
          }
}
send -- "\r"
expect {
  "Press ENTER to continue logon or F3 to exit."   {}
  timeout {
               send -- "^[["
               expect {
                 "tn3270> " {send -- "c\r"; expect "? " {}}
                 timeout {}
               }
               sleep 5
               close
               wait
               puts "## Logon to Production database failed ## "
               puts "$expect_out(buffer)\n"
               exit 2         # +++ Critical +++
          }
}
send -- "\r"
expect {
  "Initial Applications Menu Processor" {}
  "Please ensure that you have logged off by 8:00pm" { if {[regexp "^Wed .* 19:"
 $date]} {
                                     puts "Logon to Production database Ok "
                                     exit 0
                         #     Ok
                                   } else {
                                     puts "## Logon to Production database
failed ## "
                                     puts "$expect_out(buffer)\n"
                                     exit 2
                         # +++ Critical +++ }
                                   }
  timeout {
               send -- "^[["
               expect {
                 "tn3270> " {send -- "c\r"; expect "? " {}}
                 timeout {}
               }
               sleep 5
               close
               wait
               puts "## Logon to Production database failed ## "
               puts "$expect_out(buffer)\n"
               exit 2         # +++ Critical +++
          }
}
send -- "^[5"                 # Two character mainframe "pf" key
                              # represented by tn3270 client as 0x1b 0x35
expect {
  "Terminal: JGD"   {}
  timeout {
               send -- "^[["
               expect {
                 "tn3270> " {send -- "c\r"; expect "? " {}}
                 timeout {}
               }
               sleep 5
               close
               wait
               puts "## Logon to Production database failed ## "
               puts "$expect_out(buffer)\n"
               exit 2         # +++ Critical +++
          }
}
send -- "^[["
expect {
  "tn3270> "   {}
  timeout { exit 1
                         # --- Warning ---
          }
}
send -- "c\r"
expect {
  "? "    {}
  timeout { exit 1
                         # --- Warning ---
          }
}
sleep 5
close
wait

puts "Logon to Production database Ok "

exit 0
#     Ok


