4
0

check_rpc.pl 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. #!@PERL@ -w
  2. #
  3. # check_rpc plugin for nagios
  4. #
  5. # usage:
  6. # check_rpc host service
  7. #
  8. # Check if an rpc serice is registered and running
  9. # using rpcinfo - $proto $host $prognum 2>&1 |";
  10. #
  11. # Use these hosts.cfg entries as examples
  12. #
  13. # command[check_nfs]=/some/path/libexec/check_rpc $HOSTADDRESS$ nfs
  14. # service[check_nfs]=NFS;24x7;3;5;5;unix-admin;60;24x7;1;1;1;;check_rpc
  15. #
  16. # initial version: 3 May 2000 by Truongchinh Nguyen and Karl DeBisschop
  17. # Modified May 2002 Subhendu Ghosh - support for ePN and patches
  18. #
  19. # Copyright Notice: GPL
  20. #
  21. use strict;
  22. use FindBin;
  23. use lib "$FindBin::Bin";
  24. use lib '@libexecdir@';
  25. use utils qw($TIMEOUT %ERRORS &print_revision &support);
  26. use vars qw($PROGNAME);
  27. my ($verbose,@proto,%prognum,$host,$response,$prognum,$port,$cmd,$progver,$state);
  28. my ($array_ref,$test,$element,@progkeys,$proto,$a,$b);
  29. my ($opt_V,$opt_h,$opt_C,$opt_p,$opt_H,$opt_c,$opt_u,$opt_t);
  30. my ($line, @progvers, $response2,$response3);
  31. $opt_V = $opt_h = $opt_C = $opt_p = $opt_H = $opt_u = $opt_t ='';
  32. $state = 'UNKNOWN';
  33. $progver = $response=$response2= $response3 ='';
  34. $PROGNAME = "check_rpc";
  35. sub print_help ();
  36. sub print_usage ();
  37. sub in ($$);
  38. $ENV{'PATH'}='@TRUSTED_PATH@';
  39. $ENV{'BASH_ENV'}='';
  40. $ENV{'ENV'}='';
  41. $ENV{'LC_ALL'}='C';
  42. #Initialise protocol for each progname number
  43. # 'u' for UDP, 't' for TCP
  44. $proto[10003]='u';
  45. $proto[10004]='u';
  46. $proto[10007]='u';
  47. use Getopt::Long;
  48. Getopt::Long::Configure('bundling');
  49. GetOptions(
  50. "V" => \$opt_V, "version" => \$opt_V,
  51. "h" => \$opt_h, "help" => \$opt_h,
  52. "C=s" => \$opt_C, "command=s" => \$opt_C,
  53. "p=i" => \$opt_p, "port=i" => \$opt_p,
  54. "H=s" => \$opt_H, "hostname=s" => \$opt_H,
  55. "c=s" => \$opt_c, "progver=s" => \$opt_c,
  56. "v+" => \$verbose, "verbose+" => \$verbose,
  57. "u" => \$opt_u, "udp" => \$opt_u,
  58. "t" => \$opt_t, "tcp" => \$opt_t
  59. );
  60. # -h means display verbose help screen
  61. if ($opt_h) { print_help(); exit $ERRORS{'OK'}; }
  62. # -V means display version number
  63. if ($opt_V) {
  64. print_revision($PROGNAME,'@NP_VERSION@');
  65. exit $ERRORS{'OK'};
  66. }
  67. # Hash containing all RPC program names and numbers
  68. # Add to the hash if support for new RPC program is required
  69. %prognum = (
  70. "portmapper" => 100000 ,
  71. "portmap" => 100000 ,
  72. "sunrpc" => 100000 ,
  73. "rpcbind" => 100000 ,
  74. "rstatd" => 100001 ,
  75. "rstat" => 100001 ,
  76. "rup" => 100001 ,
  77. "perfmeter" => 100001 ,
  78. "rstat_svc" => 100001 ,
  79. "rusersd" => 100002 ,
  80. "rusers" => 100002 ,
  81. "nfs" => 100003 ,
  82. "nfsprog" => 100003 ,
  83. "ypserv" => 100004 ,
  84. "ypprog" => 100004 ,
  85. "mountd" => 100005 ,
  86. "mount" => 100005 ,
  87. "showmount" => 100005 ,
  88. "ypbind" => 100007 ,
  89. "walld" => 100008 ,
  90. "rwall" => 100008 ,
  91. "shutdown" => 100008 ,
  92. "yppasswdd" => 100009 ,
  93. "yppasswd" => 100009 ,
  94. "etherstatd" => 100010 ,
  95. "etherstat" => 100010 ,
  96. "rquotad" => 100011 ,
  97. "rquotaprog" => 100011 ,
  98. "quota" => 100011 ,
  99. "rquota" => 100011 ,
  100. "sprayd" => 100012 ,
  101. "spray" => 100012 ,
  102. "3270_mapper" => 100013 ,
  103. "rje_mapper" => 100014 ,
  104. "selection_svc" => 100015 ,
  105. "selnsvc" => 100015 ,
  106. "database_svc" => 100016 ,
  107. "rexd" => 100017 ,
  108. "rex" => 100017 ,
  109. "alis" => 100018 ,
  110. "sched" => 100019 ,
  111. "llockmgr" => 100020 ,
  112. "nlockmgr" => 100021 ,
  113. "x25_inr" => 100022 ,
  114. "statmon" => 100023 ,
  115. "status" => 100024 ,
  116. "bootparam" => 100026 ,
  117. "ypupdated" => 100028 ,
  118. "ypupdate" => 100028 ,
  119. "keyserv" => 100029 ,
  120. "keyserver" => 100029 ,
  121. "sunlink_mapper" => 100033 ,
  122. "tfsd" => 100037 ,
  123. "nsed" => 100038 ,
  124. "nsemntd" => 100039 ,
  125. "showfhd" => 100043 ,
  126. "showfh" => 100043 ,
  127. "ioadmd" => 100055 ,
  128. "rpc.ioadmd" => 100055 ,
  129. "NETlicense" => 100062 ,
  130. "sunisamd" => 100065 ,
  131. "debug_svc" => 100066 ,
  132. "dbsrv" => 100066 ,
  133. "ypxfrd" => 100069 ,
  134. "rpc.ypxfrd" => 100069 ,
  135. "bugtraqd" => 100071 ,
  136. "kerbd" => 100078 ,
  137. "event" => 100101 ,
  138. "na.event" => 100101 ,
  139. "logger" => 100102 ,
  140. "na.logger" => 100102 ,
  141. "sync" => 100104 ,
  142. "na.sync" => 100104 ,
  143. "hostperf" => 100107 ,
  144. "na.hostperf" => 100107 ,
  145. "activity" => 100109 ,
  146. "na.activity" => 100109 ,
  147. "hostmem" => 100112 ,
  148. "na.hostmem" => 100112 ,
  149. "sample" => 100113 ,
  150. "na.sample" => 100113 ,
  151. "x25" => 100114 ,
  152. "na.x25" => 100114 ,
  153. "ping" => 100115 ,
  154. "na.ping" => 100115 ,
  155. "rpcnfs" => 100116 ,
  156. "na.rpcnfs" => 100116 ,
  157. "hostif" => 100117 ,
  158. "na.hostif" => 100117 ,
  159. "etherif" => 100118 ,
  160. "na.etherif" => 100118 ,
  161. "iproutes" => 100120 ,
  162. "na.iproutes" => 100120 ,
  163. "layers" => 100121 ,
  164. "na.layers" => 100121 ,
  165. "snmp" => 100122 ,
  166. "na.snmp" => 100122 ,
  167. "snmp-cmc" => 100122 ,
  168. "snmp-synoptics" => 100122 ,
  169. "snmp-unisys" => 100122 ,
  170. "snmp-utk" => 100122 ,
  171. "traffic" => 100123 ,
  172. "na.traffic" => 100123 ,
  173. "nfs_acl" => 100227 ,
  174. "sadmind" => 100232 ,
  175. "nisd" => 100300 ,
  176. "rpc.nisd" => 100300 ,
  177. "nispasswd" => 100303 ,
  178. "rpc.nispasswdd" => 100303 ,
  179. "ufsd" => 100233 ,
  180. "ufsd" => 100233 ,
  181. "pcnfsd" => 150001 ,
  182. "pcnfs" => 150001 ,
  183. "amd" => 300019 ,
  184. "amq" => 300019 ,
  185. "bwnfsd" => 545580417 ,
  186. "fypxfrd" => 600100069 ,
  187. "freebsd-ypxfrd" => 600100069 ,
  188. );
  189. # -v means verbose, -v-v means verbose twice = print above hash
  190. if (defined $verbose && ($verbose > 1) ){
  191. my $key;
  192. print "Supported programs:\n";
  193. print " name\t=>\tnumber\n";
  194. print " ===============================\n";
  195. foreach $key (sort keys %prognum) {
  196. print " $key \t=>\t$prognum{$key} \n";
  197. }
  198. print "\n\n";
  199. print_usage();
  200. exit $ERRORS{'OK'};
  201. }
  202. # -H means host name
  203. unless ($opt_H) { print_usage(); exit $ERRORS{'UNKNOWN'}; }
  204. if (! utils::is_hostname($opt_H)){
  205. print "$opt_H is not a valid host name\n";
  206. print_usage();
  207. exit $ERRORS{"UNKNOWN"};
  208. }else{
  209. $host = $opt_H;
  210. }
  211. if ($opt_t && $opt_u) {
  212. print "Cannot define tcp AND udp\n";
  213. print_usage();
  214. exit $ERRORS{'UNKNOWN'};
  215. }
  216. # -C means command name or number
  217. $opt_C = shift unless ($opt_C);
  218. unless ($opt_C) { print_usage(); exit -1; }
  219. @progkeys = keys %prognum;
  220. if ($opt_C =~ m/^([0-9]+)$/){
  221. # $response = "RPC ok: program $opt_C (version ";
  222. $prognum = $1;
  223. } elsif ( in( \@progkeys, $opt_C)) {
  224. # $response = "RPC ok: $opt_C (version ";
  225. $prognum = $prognum{$opt_C};
  226. } else {
  227. print "Program $opt_C is not defined\n";
  228. exit $ERRORS{'UNKNOWN'};
  229. }
  230. # -p means port number
  231. if($opt_p =~ /^([0-9]+)$/){
  232. $port = "-n $1";
  233. } else {
  234. $port = "";
  235. }
  236. $proto = 'u';
  237. $proto = $proto[$prognum] if ($proto[$prognum]);
  238. $proto = 't' if ($opt_t);
  239. $proto = 'u' if ($opt_u);
  240. # Just in case of problems, let's not hang Nagios
  241. $SIG{'ALRM'} = sub {
  242. print ("ERROR: No response from RPC server (alarm)\n");
  243. exit $ERRORS{"UNKNOWN"};
  244. };
  245. alarm($TIMEOUT);
  246. # -c is progver - if we need to check multiple specified versions.
  247. if (defined $opt_c ) {
  248. my $vers;
  249. @progvers = split(/,/ ,$opt_c );
  250. foreach $vers (sort @progvers) {
  251. if($vers =~ /^([0-9]+)$/){
  252. $progver = "$1";
  253. print "Checking $opt_C version $progver proto $proto\n" if $verbose;
  254. get_rpcinfo();
  255. }else{
  256. print "Version $vers is not an integer\n" if $verbose;
  257. }
  258. }
  259. }else{
  260. get_rpcinfo();
  261. }
  262. ## translate proto for output
  263. if ($proto eq "u" ){
  264. $proto = "udp";
  265. }else{
  266. $proto = "tcp";
  267. }
  268. if ($state eq 'OK') {
  269. print "$state: RPC program $opt_C".$response." $proto running\n";
  270. }else{
  271. if($response){
  272. print "$state: RPC program $opt_C".$response2." $proto is not running,".$response." $proto is running\n";
  273. }else{
  274. print "$state: RPC program $opt_C $response2 $proto is not running\n";
  275. }
  276. }
  277. exit $ERRORS{$state};
  278. ######## Subroutines ==========================
  279. sub get_rpcinfo {
  280. $cmd = "$utils::PATH_TO_RPCINFO $port -" . "$proto $host $prognum $progver 2>&1 |";
  281. print "$cmd\n" if ($verbose);
  282. open CMD, $cmd or die "Can't fork for rpcinfo: $!\n" ;
  283. while ( $line = <CMD> ) {
  284. printf "$line " if $verbose;
  285. chomp $line;
  286. if ( $line =~ /program $prognum version ([0-9]*) ready and waiting/ ) {
  287. $response .= " version $1";
  288. $state = 'OK' unless $state ne 'UNKNOWN';
  289. print "1:$response \n" if $verbose;
  290. }
  291. if ( $line =~ /program $prognum version ([0-9]*) is not available/ ) {
  292. $response2 .= " version $1";
  293. $state = 'CRITICAL';
  294. print "2:$response2 \n" if $verbose;
  295. }
  296. if ( $line =~ /program $prognum is not available/ ) {
  297. $response3 = "";
  298. $response3 = "tcp" if $opt_t;
  299. $response3 = "udp" if $opt_u;
  300. $state = 'CRITICAL';
  301. print "3:$response3 \n" if $verbose;
  302. }
  303. }
  304. close CMD;
  305. }
  306. sub print_help() {
  307. print_revision($PROGNAME,'@NP_VERSION@');
  308. print "Copyright (c) 2002 Karl DeBisschop/Truongchinh Nguyen/Subhendu Ghosh\n";
  309. print "\n";
  310. print "Check if a rpc service is registered and running using\n";
  311. print " rpcinfo -H host -C rpc_command \n";
  312. print "\n";
  313. print_usage();
  314. print "\n";
  315. print " <host> The server providing the rpc service\n";
  316. print " <rpc_command> The program name (or number).\n";
  317. print " <program_version> The version you want to check for (one or more)\n";
  318. print " Should prevent checks of unknown versions being syslogged\n";
  319. print " e.g. 2,3,6 to check v2, v3, and v6\n";
  320. print " [-u | -t] Test UDP or TCP\n";
  321. print " [-v] Verbose \n";
  322. print " [-v -v] Verbose - will print supported programs and numbers \n";
  323. print "\n";
  324. support();
  325. }
  326. sub print_usage () {
  327. print "Usage: \n";
  328. print " $PROGNAME -H host -C rpc_command [-p port] [-c program_version] [-u|-t] [-v]\n";
  329. print " $PROGNAME [-h | --help]\n";
  330. print " $PROGNAME [-V | --version]\n";
  331. }
  332. sub in ($$) {
  333. $array_ref = shift;
  334. $test = shift;
  335. while ( $element = shift @{$array_ref} ) {
  336. if ($test eq $element) {
  337. return 1;
  338. }
  339. }
  340. return 0;
  341. }