check_ms_spooler.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #!/usr/bin/perl -w
  2. # $Id$
  3. # $Log$
  4. # Revision 1.1 2002/07/16 00:04:42 stanleyhopcroft
  5. # Primitive and in need of refinement test of MS spooler (with smbclient)
  6. #
  7. # Revision 2.5 2002-02-13 07:36:08+11 anwsmh
  8. # Correct 'apostrophe' disaster.
  9. # Apostrophes in plugin output cause Netsaint notification commands
  10. # ( sh echo 'yada $PLUGINOUTPUT$ ..') to fail, usually mysteriously
  11. # eg notify OK works but notify CRITICAL does not.
  12. # Replace '$var' in print "output" with \"$var\".
  13. #
  14. # Revision 2.4 2001-11-21 21:36:05+11 anwsmh
  15. # Minor corrections
  16. # . replace 'die' by print .. exit $ERRORS{CRITICAL}
  17. # . change concluding message to list the queues (sorted) if there are no enqueued docs.
  18. #
  19. # Revision 2.3 2001-11-20 11:00:58+11 anwsmh
  20. # Major corrections.
  21. # 1. to sub AUTOLOAD: coderef parms must be @_ (ie the parm when the new sub is called)
  22. # 2. to processing of queue report (no inspection of $last_line; entire $queue_report is
  23. # checked for errors)
  24. # 3. cosmetic and debug changes in many places.
  25. #
  26. # Revision 2.2 2001-11-17 23:30:34+11 anwsmh
  27. # After adapting two different queue reports resulting from
  28. # different name resolution methods.
  29. #
  30. # Revision 2.1 2001-11-17 13:21:54+11 anwsmh
  31. # Adapt to Netsaint ('use utils, Getopt::Long, and standard switch processing).
  32. # Fix many peculiarities.
  33. #
  34. use strict ;
  35. use Getopt::Long ;
  36. use utils ;
  37. use vars qw($opt_H $opt_s $opt_W $opt_u $opt_p $opt_w $opt_c $debug);
  38. use vars '$AUTOLOAD' ;
  39. use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
  40. my $PROGNAME = 'check_ms_spooler' ;
  41. sub print_help ();
  42. sub print_usage ();
  43. sub help ();
  44. sub version ();
  45. delete @ENV{'PATH', 'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
  46. use constant SMBCLIENT_PATH => '/usr/local/samba/bin/smbclient' ;
  47. use constant MAX_QUEUES_TO_CHECK => 20 ; # So that the check doesn't take longer than $TIMEOUT
  48. use constant SMBCLIENT_SVC => sub { return `${\SMBCLIENT_PATH} -L //$_[0] -U $_[1]%$_[2]` } ;
  49. use constant SMBCLIENT_QUEUE => sub { return `${\SMBCLIENT_PATH} //$_[0]/$_[1] -U $_[2]%$_[3] -c 'queue; quit' 2>/dev/null` } ;
  50. # The queue results depend on the name resolution method.
  51. # Forcing 'wins' or 'bcat' name resolution makes the queue results the
  52. # same for all spoolers (those that are resolved with WINS have an extra line
  53. # 'Got a positive name query response from <ip address of WINS> ..)
  54. # but would fail if there is no WINS and when miscreant spoolers
  55. # don't respond to broadcasts.
  56. use constant MIN => sub { my $min = $_[0] ; foreach (@_) { $min = $_ if $_ <= $min; } return $min ; } ;
  57. $SIG{"ALRM"} = sub { die "Alarm clock restart" } ;
  58. Getopt::Long::Configure('bundling', 'no_ignore_case');
  59. GetOptions
  60. ("V|version" => \&version,
  61. "h|help" => \&help,
  62. "d|debug" => \$debug,
  63. "p|password=s" => \$opt_p,
  64. "u|username=s" => \$opt_u,
  65. "H|hostname=s" => \$opt_H);
  66. ($opt_H) || usage("MS Spooler name not specified\n");
  67. my $spooler = $1 if $opt_H =~ m#(\w+)# ; # MS host names allow __any__ characters (more than \w)
  68. ($spooler) || usage("Invalid MS spooler name: $opt_H\n");
  69. ($opt_u) || ($opt_u = 'guest');
  70. my $user = $1 if $opt_u =~ m#(\w+)# ;
  71. ($user) || usage("Invalid user: $opt_u\n");
  72. ($opt_p) || ($opt_p = 'guest');
  73. my $pass = $1 if ($opt_p =~ /(.*)/);
  74. ($pass) || usage("Invalid password: $opt_p\n");
  75. my ($printer, $queue, @queues, $ms_spooler_status, @results, %junk) ;
  76. my (@fault_messages, @queue_contents, @services, @prandom_queue_indices) ;
  77. my ($queue_contents, $number_of_queues, $state, $queue_report) ;
  78. $state = "getting service list (${\SMBCLIENT_PATH} -L $spooler -U $user%$pass) from spooler\n" ;
  79. eval {
  80. alarm($TIMEOUT) ;
  81. @services = SMBCLIENT_SVC->( $spooler, $user, $pass ) ;
  82. } ;
  83. alarm(0) ;
  84. if ($@ and $@ !~ /Alarm clock restart/) {
  85. print "Failed. $PROGNAME failed $state. Got \"$@\"\n" ;
  86. exit $ERRORS{"CRITICAL"} ;
  87. }
  88. if ($@ and $@ =~ /Alarm clock restart/) {
  89. print "Failed. $PROGNAME timed out $state. Got \"@services\"\n" ;
  90. exit $ERRORS{"CRITICAL"} ;
  91. }
  92. # tsitc> /usr/local/samba/bin/smbclient //ipaprint1/tt03 -U blah%blah -P -c 'queue; quit'
  93. # Added interface ip=10.0.100.252 bcast=10.255.255.255 nmask=255.0.0.0
  94. # Connection to ipaprint1 failed
  95. # tsitc> /usr/local/samba/bin/smbclient -L sna_spl1 -U blah%blah | & more
  96. # Added interface ip=10.0.100.252 bcast=10.255.255.255 nmask=255.0.0.0
  97. # Got a positive name query response from 10.0.100.29 ( 10.0.6.20 )
  98. # session setup failed: ERRDOS - ERRnoaccess (Access denied.)
  99. if ( grep /Connection to $spooler failed|ERR/, @services ) {
  100. print "Failed. $PROGNAME failed $state. Got \"@services\"\n" ;
  101. # print "Failed. Request for services list to $spooler failed. Got \"@services\"\n" ;
  102. exit $ERRORS{"CRITICAL"} ;
  103. }
  104. # tsitc# /usr/local/samba/bin/smbclient -L ipaprint -U blah%blah
  105. # Added interface ip=10.0.100.252 bcast=10.255.255.255 nmask=255.0.0.0
  106. # Domain=[IPAUSTRALIA] OS=[Windows NT 4.0] Server=[NT LAN Manager 4.0]
  107. #
  108. # Sharename Type Comment
  109. # --------- ---- -------
  110. # TH02 Printer TH02
  111. # ADMIN$ Disk Remote Admin
  112. # IPC$ IPC Remote IPC
  113. # S431 Printer S431
  114. # S402 Printer S402
  115. # S401 Printer S401
  116. # C$ Disk Default share
  117. # BW01 Printer BW01
  118. # BW02 Printer BW02
  119. # TL11 Printer TL11
  120. # TL07 Printer TL07
  121. # S225 Printer Discovery South - 2nd Floor - HP CLJ4500
  122. # S224 Printer S224
  123. # S223 Printer Discovery South 2nd Floor Trademarks Training
  124. # S222 Printer S222
  125. # S203 Printer S203
  126. # S202 Printer S202
  127. my @printers = map { my @junk = split; $junk[0] }
  128. grep { my @junk = split; defined $junk[1] and $junk[1] eq 'Printer' } @services ;
  129. # don't check IPC$, ADMIN$ etc.
  130. $ms_spooler_status = 0 ;
  131. $number_of_queues = MIN->(MAX_QUEUES_TO_CHECK, (scalar(@services) >> 3) + 1) ;
  132. $state = "checking queues on $spooler" ;
  133. eval {
  134. # foreach queues to check
  135. # generate a pseudo-random int in 0 .. $#printers
  136. # drop it if the index has already been generated ;
  137. %junk = () ;
  138. @prandom_queue_indices = grep { ! $junk{$_}++ }
  139. map { int( rand($#printers) ) } ( 1 .. $number_of_queues ) ;
  140. @queues = @printers[@prandom_queue_indices] ;
  141. # @queues = @printers[ map { int( rand($#printers) ) } ( 1 .. $number_of_queues ) ] ;
  142. alarm($TIMEOUT) ;
  143. @queue_contents = @fault_messages = () ;
  144. foreach $printer (sort @queues) {
  145. # Expect 3 lines from a queue report.
  146. # If queue is empty, last line is null otherwise
  147. # it will contain a queue report or an SMB error
  148. # Empty Queue.
  149. # Added interface ip=10.0.100.252 bcast=10.255.255.255 nmask=255.0.0.0
  150. # Domain=[IPAUSTRALIA] OS=[Windows NT 4.0] Server=[NT LAN Manager 4.0]
  151. # Queue command from a spooler with a DNS name.
  152. # Added interface ip=10.0.100.252 bcast=10.255.255.255 nmask=255.0.0.0
  153. # Domain=[IPAUSTRALIA] OS=[Windows NT 4.0] Server=[NT LAN Manager 4.0]
  154. # 65 16307 Microsoft Word - Servicesweoffer2.doc
  155. # 68 10410 Microsoft Word - Servicesweoffer.doc
  156. # 143 24997 Microsoft Word - Miss Samantha Anne Craig.doc
  157. # 182 15635 Microsoft Word - services we provide.doc
  158. # Error.
  159. # Added interface ip=10.0.100.252 bcast=10.255.255.255 nmask=255.0.0.0
  160. # Domain=[IPAUSTRALIA] OS=[Windows NT 4.0] Server=[NT LAN Manager 4.0]
  161. # tree connect failed: ERRDOS - ERRnosuchshare (You specified an invalid share name)
  162. # Can't connect error.
  163. # Added interface ip=10.0.100.252 bcast=10.255.255.255 nmask=255.0.0.0
  164. # Connection to sna_spl2 failed
  165. # Empty Queue from a spooler with no DNS name, NetBIOS name resolved by WINS.
  166. # Added interface ip=10.0.100.252 bcast=10.255.255.255 nmask=255.0.0.0
  167. # Got a positive name query response from 10.0.100.29 ( 10.0.6.20 )
  168. # Domain=[SNA_PRINT] OS=[Windows NT 4.0] Server=[NT LAN Manager 4.0]
  169. # There are 3 lines of output from smbclient for those spoolers whose names are
  170. # resolved by WINS (because those names are NetBIOS and therefore not in DNS);
  171. # 4 lines for errors or enqueued jobs
  172. print STDERR "${\SMBCLIENT_PATH} //$spooler/$printer -U $user%$pass -c 'queue; quit' ==>\n" if $debug ;
  173. @results = SMBCLIENT_QUEUE->( $spooler, $printer, $user, $pass ) ;
  174. print STDERR "\"@results\"\n" if $debug ;
  175. # set $ms_spooler_status somehow
  176. chomp( @results ) ;
  177. $queue_report = queue_report->(@results) ;
  178. print STDERR '$queue_report for $printer ', "$printer: \"$queue_report\"\n\n" if $debug ;
  179. if ( defined $queue_report and ($queue_report !~ /ERR/ && $queue_report !~ /failed/) ) {
  180. $ms_spooler_status = 1 ;
  181. push @queue_contents, "$printer: $queue_report" if $queue_report ;
  182. } else {
  183. push @fault_messages, "$printer: $queue_report" ;
  184. }
  185. }
  186. alarm(0) ;
  187. } ;
  188. if ($@ and $@ !~ /Alarm clock restart/) {
  189. print "Failed. $PROGNAME failed at $state. Got \"$@\"\n" ;
  190. exit $ERRORS{"CRITCAL"} ;
  191. }
  192. if ($@ and $@ =~ /Alarm clock restart/) {
  193. my $i ;
  194. foreach (@queues) { $i++ ; last if $_ eq $printer }
  195. print "Failed. Timed out connecting to $printer ($i of $number_of_queues) on //$spooler after $TIMEOUT secs. Got \"@fault_messages\"\n" ;
  196. exit $ERRORS{"CRITICAL"} ;
  197. }
  198. if (! $ms_spooler_status) {
  199. print "Failed. Couldn't connect to @queues on //$spooler as user $user. Got \"@fault_messages\"\n" ;
  200. exit $ERRORS{"CRITICAL"} ;
  201. }
  202. $queue_contents = ( @queue_contents != 0 ? join(" ", (@queue_contents == 1 ? "Queue" : "Queues"), @queue_contents) :
  203. "All Queues empty" ) ;
  204. print "Ok. Connected to ", $queue_contents =~ /empty$/ ? "@{[sort @queues]}" : scalar @queues, " queues on //$spooler. $queue_contents\n" ;
  205. exit $ERRORS{"OK"} ;
  206. sub print_usage () {
  207. print "Usage: $PROGNAME -H <spooler> -u <user> -p <password>\n";
  208. }
  209. sub print_help () {
  210. print_revision($PROGNAME,'$Revision$ ');
  211. print "Copyright (c) 2001 Karl DeBisschop/S Hopcroft
  212. Perl Check MS Spooler plugin for NetSaint. Display a subset of the queues on an SMB (Samba or MS) print spooler.
  213. ";
  214. print_usage();
  215. print '
  216. -H, --hostname=STRING
  217. NetBIOS name of the SMB Print spooler (Either Samba or MS spooler)
  218. -u, --user=STRING
  219. Username to log in to server. (Default: "guest")
  220. -p, --password=STRING
  221. Password to log in to server. (Default: "guest")
  222. -d, --debug
  223. Debugging output.
  224. -h, --help
  225. This stuff.
  226. ';
  227. support();
  228. }
  229. sub version () {
  230. print_revision($PROGNAME,'$Revision$ ');
  231. exit $ERRORS{'OK'};
  232. }
  233. sub help () {
  234. print_help();
  235. exit $ERRORS{'OK'};
  236. }
  237. sub AUTOLOAD {
  238. my @queue_rep = @_ ;
  239. # 'Object Oriented Perl', D Conway, p 95
  240. no strict 'refs' ;
  241. if ( $AUTOLOAD =~ /.*::queue_report/ ) {
  242. if ( grep /Got a positive name query response from/, @queue_rep ){
  243. *{$AUTOLOAD} = sub { return join ' ', splice(@_, 3) } ;
  244. return join '', splice(@queue_rep, 3) ;
  245. } else {
  246. *{$AUTOLOAD} = sub { return join ' ',splice(@_, 2) } ;
  247. return join '', splice(@queue_rep, 2) ;
  248. }
  249. } else {
  250. die "No such subroutine: $AUTOLOAD" ;
  251. }
  252. }