check_ica_metaframe_pub_apps.pl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #!/usr/bin/perl -w
  2. # $Id: check_ica_metaframe_pub_apps.pl 1098 2005-01-25 09:07:39Z stanleyhopcroft $
  3. # Revision 1.1 2005/01/25 09:07:39 stanleyhopcroft
  4. # Replacement (structured name mainly) for check_citrix: check of ICA browse service
  5. #
  6. # Revision 1.1 2005-01-25 17:00:24+11 anwsmh
  7. # Initial revision
  8. #
  9. use strict ;
  10. use IO::Socket;
  11. use IO::Select;
  12. use Getopt::Long ;
  13. my ($bcast_addr, $timeout, $debug, @citrix_servers, $crit_pub_apps, $warn_pub_apps, $long_list) ;
  14. use lib qw(/usr/local/nagios/libexec) ;
  15. use utils qw(%ERRORS &print_revision &support &usage) ;
  16. use packet_utils qw(&pdump &tethereal) ;
  17. my $PROGNAME = 'check_ica_metaframe_pub_apps' ;
  18. sub print_help ();
  19. sub print_usage ();
  20. sub help ();
  21. sub version ();
  22. # You might have to change this...
  23. my $PACKET_TIMEOUT = 1;
  24. # Number of seconds to wait for further UDP packets
  25. my $TEST_COUNT = 2;
  26. # Number of datagrams sent without reply
  27. my $BUFFER_SIZE = 1500;
  28. # buffer size used for 'recv' calls.
  29. my $LONG_LIST = 0 ;
  30. # this is for if you have many published applications.
  31. # if you set it, it won't do any harm, but may slow the test
  32. # down a little. (Since it does a 'recv' twice instead of
  33. # once and therefore may have to wait for a timeout).
  34. my $ICA_PORT = 1604;
  35. # what port ICA runs on. Unlikely to change.
  36. Getopt::Long::Configure('bundling', 'no_ignore_case');
  37. GetOptions
  38. ("V|version" => \&version,
  39. "h|help" => \&help,
  40. "v|verbose" => \$debug,
  41. "B|broadcast_addr:s" => \$bcast_addr,
  42. "C|citrix_servers:s" => \@citrix_servers,
  43. "L|long_list" => \$long_list,
  44. "P|crit_pub_apps:s" => \$crit_pub_apps,
  45. "T|Packet_timeout:i" => \$timeout,
  46. "W|warn_pub_apps:s" => \$warn_pub_apps,
  47. ) ;
  48. my $broadcast_addr = $1 if $bcast_addr and $bcast_addr =~ m#(\d+\.\d+\.\d+\.\d+)# ;
  49. usage("Invalid broadcast address: $bcast_addr\n")
  50. if $bcast_addr and not defined($broadcast_addr) ;
  51. usage("You must provide either the names of citrix servers or the broadcast address of the subnet containing them\n")
  52. unless (@citrix_servers or $broadcast_addr) ;
  53. my @target = defined $broadcast_addr ? ($broadcast_addr) : @citrix_servers ;
  54. usage("You must provide the names of the published applications that the Citrix browser should be advertising\n")
  55. unless $crit_pub_apps or $warn_pub_apps ;
  56. my $Timeout = $timeout
  57. if defined $timeout ;
  58. $Timeout = $PACKET_TIMEOUT
  59. unless defined $Timeout ;
  60. $long_list = $LONG_LIST
  61. unless defined $long_list ;
  62. my @crit_pub_apps = $crit_pub_apps ? split(/,/, $crit_pub_apps) : () ;
  63. my @warn_pub_apps = $warn_pub_apps ? split(/,/, $warn_pub_apps) : () ;
  64. # Definitions of query strings. Change at your own risk :)
  65. # this info was gathered with tcpdump whilst trying to use an ICA client,
  66. # so I'm not 100% sure of what each value is.
  67. my $bcast_helo = &tethereal(<<'End_of_Tethereal_trace', '1e') ;
  68. 0020 ff ff 04 d6 06 44 00 26 4a 76 1e 00 01 30 02 fd .....D.&Jv...0..
  69. 0030 a8 e3 00 02 f5 95 9f f5 30 07 00 00 00 00 00 00 ........0.......
  70. 0040 00 00 00 00 00 00 01 00 .......
  71. End_of_Tethereal_trace
  72. my $bcast_query_app = &tethereal(<<'End_of_Tethereal_trace', '24') ;
  73. 0020 64 17 04 50 06 44 00 2c 85 6a 24 00 01 32 02 fd d..P.D.,.j$..2..
  74. 0030 a8 e3 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
  75. 0040 00 00 00 00 00 00 21 00 02 00 00 00 00 00 ......!......
  76. End_of_Tethereal_trace
  77. my $direct_helo = &tethereal(<<'End_of_Tethereal_trace', '20') ;
  78. 0020 64 17 05 0f 06 44 00 28 ab b5 20 00 01 30 02 fd d....D.(.. ..0..
  79. 0030 a8 e3 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
  80. 0040 00 00 00 00 00 00 00 00 00 00 .........
  81. End_of_Tethereal_trace
  82. my $direct_query_app = &tethereal(<<'End_of_Tethereal_trace', '2c') ;
  83. 0020 64 17 05 10 06 44 00 34 7a 9a 2c 00 02 32 02 fd d....D.4z.,..2..
  84. 0030 a8 e3 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
  85. 0040 00 00 00 00 00 00 21 00 02 00 01 00 00 00 00 00 ......!.........
  86. 0050 00 00 00 00 00 00 ......
  87. End_of_Tethereal_trace
  88. my $Udp = IO::Socket::INET->new( Proto => 'udp' )
  89. || die "Socket failure: $!";
  90. # Select is here to allow us to set timeouts on the connections.
  91. # Otherwise they just 'stop' until a server appears.
  92. my $select = IO::Select->new($Udp)
  93. || die "Select failure: $!";
  94. # Helo needs to be broadcastt, but query does not.
  95. $Udp->sockopt(SO_BROADCAST, 1 );
  96. my ($remote_host, $buff, $buff2, $raddr, $rport, $rhost, @remote_response);
  97. my ($query_message, $send_addr, $this_test) ;
  98. $buff = $buff2 = '';
  99. $this_test = 0;
  100. # If there is no response to the first helo packet it will be resent
  101. # up to TEST_COUNT (see at the top).
  102. while ( ++$this_test <= $TEST_COUNT && !$buff ) {
  103. print "Sending helo datagram. datagram number: ", $this_test, "\n"
  104. if $debug ;
  105. # If we have multiple targets, we probe each of them until we get a
  106. # response...
  107. foreach my $destination (@target) {
  108. $query_message = $broadcast_addr ? $bcast_helo : $direct_helo ;
  109. print "Querying $destination for master browser\n"
  110. if $debug ;
  111. $send_addr = sockaddr_in($ICA_PORT, inet_aton($destination) );
  112. &pdump($query_message)
  113. if $debug ;
  114. $Udp->send($query_message, 0, $send_addr );
  115. if ( $select->can_read($Timeout) ) {
  116. $remote_host = $Udp->recv($buff, $BUFFER_SIZE, 0 );
  117. }
  118. last
  119. if $buff ;
  120. sleep 1 ;
  121. }
  122. }
  123. # Ok we've looped several times, looking for a response. If we don't have one
  124. # yet, we simply mark the whole lot as being unavailable.
  125. unless ( $buff ) {
  126. print "Failed. No response to helo datagram (master browser query) from ", $broadcast_addr ? $broadcast_addr : "@citrix_servers", ".\n" ;
  127. exit $ERRORS{CRITICAL} ;
  128. }
  129. ($rport, $raddr) = sockaddr_in( $remote_host );
  130. $rhost = gethostbyaddr( $raddr, AF_INET );
  131. my @tmpbuf = unpack('C*', $buff );
  132. if ( $debug ) {
  133. print "$rhost:$rport responded with: ", length($buff), " bytes\n";
  134. &pdump($buff) ;
  135. }
  136. # Now we have a response, then we need to figure out the master browser, and
  137. # query it for published applications...
  138. my $master_browser = join '.', @tmpbuf[32..35] ;
  139. # Ok should probably error check this, because it's remotely possible
  140. # that a server response might be completely wrong...
  141. print "Master browser = $master_browser\n"
  142. if $debug ;
  143. $send_addr = sockaddr_in($ICA_PORT, inet_aton($master_browser));
  144. if ( $broadcast_addr ) {
  145. print "using broadcast query\n"
  146. if $debug ;
  147. $query_message = $bcast_query_app;
  148. } else {
  149. print "using directed query\n"
  150. if $debug ;
  151. $query_message = $direct_query_app;
  152. }
  153. # Now we send the appropriate query string, to the master browser we've found.
  154. $buff = '';
  155. $this_test = 0 ;
  156. print "Querying master browser for published application list\n"
  157. if $debug ;
  158. while ( ++$this_test <= $TEST_COUNT && !$buff ) {
  159. print "Sending application query datagram. datagram number: ", $this_test, "\n"
  160. if $debug ;
  161. &pdump($query_message)
  162. if $debug ;
  163. $Udp->send($query_message, 0, $send_addr);
  164. if ( $select->can_read($Timeout) ) {
  165. $remote_host = $Udp->recv($buff, $BUFFER_SIZE, 0 );
  166. # $buff = substr($buff, 32) ;
  167. # Hope that ICA preamble is first 32 bytes
  168. }
  169. # Long application lists are delivered in multiple packets
  170. my $buff2 = '' ;
  171. while ( $long_list && $select->can_read($Timeout) ) {
  172. $remote_host = $Udp->recv($buff2, $BUFFER_SIZE, 0);
  173. $buff .= $buff2
  174. if $buff2 ;
  175. # $buff .= substr($buff2, 32) if $buff2 ;
  176. # Hope that ICA preamble is first 32 bytes
  177. }
  178. last if $buff ;
  179. sleep 1 ;
  180. }
  181. unless ( $buff ) {
  182. print "Failed. No response to application query datagram from ", $master_browser, ".\n" ;
  183. exit $ERRORS{CRITICAL} ;
  184. }
  185. # we got a response from a couple of retries of the app query
  186. ($rport, $raddr) = sockaddr_in ( $remote_host );
  187. $rhost = gethostbyaddr ( $raddr, AF_INET );
  188. if ( $debug ) {
  189. print "$rhost:$rport responded to app query with: ", length($buff), " bytes\n";
  190. &pdump($buff) ;
  191. }
  192. my $app_list = $buff ;
  193. # delete nulls in unicode
  194. # but only if there is unicode (usually from
  195. # broadcast query)
  196. $app_list =~ s/(?:(\w| |-)\x00)/$1/g
  197. if $app_list =~ /(?:(?:(?:\w| |-)\x00){3,})/ ;
  198. # FIXME an application name is
  199. # 3 or more unicoded characters
  200. # FIXME locale
  201. # extract null terminated strings
  202. my (@clean_app_list, $clean_app_list) ;
  203. $clean_app_list = join(',', @clean_app_list = $app_list =~ m#([A-Za-z](?:\w| |-|[ÄÖÜäöüß])+?(?=\x00))#g ) ;
  204. # patch for German umlauts et al from Herr Mike Gerber.
  205. # $clean_app_list = join(',', @clean_app_list = $app_list =~ m#([A-Z](?:\w| |-)+?(?=\x00))#g ) ;
  206. # FIXME everyones apps don't start with caps
  207. print qq(Received list of applications: "$clean_app_list".\n)
  208. if $debug ;
  209. if ( scalar @crit_pub_apps and my @missing = &simple_diff(\@clean_app_list, \@crit_pub_apps) ) {
  210. print qq(Failed. "@missing" not found in list of published applications),
  211. qq(" $clean_app_list" from master browser "$master_browser".\n) ;
  212. exit $ERRORS{CRITICAL} ;
  213. }
  214. if ( my @missing = &simple_diff(\@clean_app_list, \@warn_pub_apps) ) {
  215. print qq(Warning. "@missing" not found in list of published applications),
  216. qq(" $clean_app_list" from master browser "$master_browser".\n) ;
  217. exit $ERRORS{WARNING} ;
  218. }
  219. my @x = (@crit_pub_apps, @warn_pub_apps) ;
  220. my $blah = ( scalar(@x) == 1
  221. ? 'the published application "' . join(',', @x) . '" is available'
  222. : 'the published applications "' . join(',', @x) . '" are available' ) ;
  223. print qq(Ok. Citrix master browser "$master_browser" reported that $blah.\n) ;
  224. exit $ERRORS{OK} ;
  225. # sleep $Timeout;
  226. # because otherwise we can get responses from
  227. # the WRONG servers. DOH
  228. close $Udp;
  229. sub print_usage () {
  230. print "Usage: $PROGNAME (-B <broadcast_address>| -C <citrix_server>..) -W <pub_app1,pub_app2..> -P <pub_app1,pub_app2,>\n";
  231. }
  232. sub print_help () {
  233. print_revision($PROGNAME,'$Revision: 1098 $ ');
  234. print "Copyright (c) 2002 Ed Rolison/Tom De Blende/S Hopcroft
  235. Perl Check Citrix plugin for Nagios.
  236. Returns OK if the Citrix master browser returns a 'published application' list that contain names specified by the -W or -P options
  237. The plugin works by
  238. If the -B option is specified, sending a broadcast helo to find the address of the Citrix master browser in the specified subnet.
  239. return critical if there is no reply;
  240. Else if the -C option is specified
  241. send a direct helo to the specified server until there is a response (containing the address of the Citrix master browser)
  242. Query the master browser (using a 'broadcast published applications query ' if -B) and compare the published applications returned
  243. to those specified by -W and -P options
  244. return Critical if the published applications specified by -P is not a subset of the query responses;
  245. return Warning if the published applications specified by -W is not a subset of the query responses;
  246. return OK
  247. ";
  248. print_usage();
  249. print '
  250. -B, --broadcast_address=STRING
  251. The broadcast address that should contain Citrix master browser. This option takes precedence over -C.
  252. -C, --citrix_server:STRING
  253. Optional __name(s)__ of Citrix servers that could be the master browser (used when broadcast not possible).
  254. -L, --long_list
  255. Set this if you have heaps of published applications (ie more than will fit in _one_ UDP packet)
  256. -P, --crit_published_app=STRING
  257. Optional comma separated list of published application that must be in the response from the master browser.
  258. Check returns critical otherwise.
  259. -T, --packet-timeout:INTEGER
  260. Time to wait for UDP packets (default 1 sec).
  261. -W, --warn_published_app=STRING
  262. Optional comma separated list of published application that should be in the response from the master browser.
  263. Check returns warning otherwise.
  264. -v, --verbose
  265. Debugging output.
  266. -h, --help
  267. This stuff.
  268. ';
  269. support();
  270. }
  271. sub version () {
  272. print_revision($PROGNAME,'$Revision: 1098 $ ');
  273. exit $ERRORS{'OK'};
  274. }
  275. sub help () {
  276. print_help();
  277. exit $ERRORS{'OK'};
  278. }
  279. sub simple_diff {
  280. my ( $a_list, $b_list) = @_ ;
  281. # simple set difference 'Recipe 4.7 Perl Cookbook', Christiansen and Torkington
  282. my (%seen, @missing) ;
  283. @seen{@$a_list} = () ;
  284. foreach my $item (@$b_list) {
  285. push @missing, $item
  286. unless exists $seen{$item} ;
  287. }
  288. @missing ;
  289. }