check_oracle_tbs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #!/usr/local/bin/perl -w
  2. # (c)2004 John Koyle, RFP Depot, LLC.
  3. # This is free software use it however you would like.
  4. use strict;
  5. use DBI;
  6. use Getopt::Long 2.16;
  7. use lib "/usr/local/nagios/libexec";
  8. use utils qw(%ERRORS);
  9. #*******************************************************************************
  10. # Set user configureable options here.
  11. #
  12. # Global Oracle info set here rather than command line to avoid output in ps -ef
  13. # Make sure this script is mode 700 and owner of the nrpe user
  14. #
  15. #*******************************************************************************
  16. my $orasid = "";
  17. my $orauser = "";
  18. my $orapwd = "";
  19. if (!$ENV{ORACLE_HOME}) {
  20. $ENV{ORACLE_HOME} = '/u01/app/oracle/product/9.2';
  21. }
  22. #*******************************************************************************
  23. my $state = $ERRORS{'UNKNOWN'};
  24. my $answer = undef;
  25. my ($MAJOR_VERSION, $MINOR_VERSION) = q$Revision: 1134 $ =~ /(\d+)\.(\d+)/;
  26. my $VERSION = sprintf("%d.%02d", $MAJOR_VERSION - 1, $MINOR_VERSION);
  27. my $opt_debug; # -d|--debug
  28. my $opt_help; # -h|--help
  29. my $opt_version; # -V|--version
  30. my $opt_warn_space; # -w|--warn-space
  31. my $opt_crit_space; # -c|--crit-space
  32. my $help = <<MARK; # help statement
  33. check_oracle_tbs v$VERSION
  34. Checks the tablespaces in an Oracle database for available free space.
  35. Usage: check_oracle_tbs [-w <warn>] [-c <crit>]
  36. -d, --debug Output debug to screen.
  37. -h, --help Displays this help and exits.
  38. -w, --warn-space=... Warning threshold % free (default 15)
  39. -c, --crit-space=... Critical threshold % free (default 10)
  40. -V, --version Output version information and exit.
  41. MARK
  42. ## We want exact matches to the switches
  43. Getopt::Long::config('no_auto_abbrev', 'no_ignore_case');
  44. my $rc = GetOptions(
  45. "debug|d" => \$opt_debug,
  46. "help|h" => \$opt_help,
  47. "w|warn-space=s" => \$opt_warn_space,
  48. "c|crit-space=s" => \$opt_crit_space,
  49. "V|version" => \$opt_version,
  50. );
  51. #***********************************************************************
  52. # Process command-line switches
  53. #***********************************************************************
  54. if (! $rc || defined $opt_help)
  55. {
  56. print STDERR $help;
  57. exit (defined $opt_help ? 0 : 1);
  58. }
  59. if (defined $opt_version)
  60. {
  61. print STDERR "check_oracle_tbs v$VERSION\n";
  62. exit 0;
  63. }
  64. if (! defined $opt_warn_space)
  65. {
  66. if(defined $opt_debug) {
  67. print STDOUT "Warn space not defined, using 80%\n\n";
  68. }
  69. $opt_warn_space = 15;
  70. }
  71. if (! defined $opt_crit_space)
  72. {
  73. if(defined $opt_debug) {
  74. print STDOUT "Crit space not defined, using 90%\n\n";
  75. }
  76. $opt_crit_space = 10;
  77. }
  78. my $array_ref = executeSQL();
  79. # Don't match certain tablespaces.
  80. foreach my $row (@$array_ref) {
  81. my ( $tbs_name, $free_mb, $tot_mb, $free_pct) = @$row;
  82. if ($opt_debug) { print STDOUT "Output: $tbs_name\t$tot_mb\t$free_mb\t$free_pct\n\n"; }
  83. if ($free_pct < $opt_crit_space && $tbs_name !~ /RBS/ && $tbs_name !~ /PERFSTAT/ && $tbs_name !~ /UNDOTBS/) {
  84. $state = $ERRORS{'CRITICAL'};
  85. $answer .= "Critical: $tbs_name = $free_pct\% ";
  86. last;
  87. }
  88. if ($free_pct < $opt_warn_space && $tbs_name !~ /RBS/ && $tbs_name !~ /PERFSTAT/ && $tbs_name !~ /UNDOTBS/) {
  89. $state = $ERRORS{'WARNING'};
  90. $answer .= "Warning: $tbs_name = $free_pct\% ";
  91. }
  92. }
  93. if ($state != $ERRORS{'CRITICAL'} && $state != $ERRORS{'WARNING'}) {
  94. $state = $ERRORS{'OK'};
  95. $answer = "All Tablespaces OK";
  96. }
  97. if ($opt_debug && $state != $ERRORS{'OK'}) { print STDOUT "The following tablespaces are in question: $answer\n\n"; }
  98. foreach my $key (keys %ERRORS) {
  99. if ($state==$ERRORS{$key}) {
  100. print ("$key: $answer");
  101. last;
  102. }
  103. }
  104. exit $state;
  105. #------------------SUBS-------------------------------------------------------
  106. sub executeSQL
  107. {
  108. my ($dbh, $sth, $results);
  109. $dbh = openOracle();
  110. eval {
  111. $dbh->{RaiseError} = 1;
  112. $sth = $dbh->prepare(q{
  113. select ts.tablespace_name,
  114. trunc(sum(ts.free_b)/1024/1024) free,
  115. trunc(sum(ts.max_b)/1024/1024) total,
  116. trunc( sum(ts.free_b)/sum(ts.max_b)*1000) / 10 as pct_free
  117. from
  118. (select a.file_id,
  119. a.tablespace_name,
  120. decode(a.autoextensible,'YES',a.maxsize-a.bytes+b.free,'NO',b.free) free_b,
  121. a.maxsize max_b
  122. from (select file_id,
  123. tablespace_name,
  124. autoextensible,
  125. bytes,
  126. decode(autoextensible,'YES',maxbytes,bytes) maxsize
  127. from dba_data_files) a,
  128. (select file_id,
  129. tablespace_name,
  130. sum(bytes) free
  131. from dba_free_space
  132. group by file_id, tablespace_name) b
  133. where a.file_id=b.file_id(+)) ts
  134. group by tablespace_name
  135. });
  136. $sth->execute();
  137. $results = $sth->fetchall_arrayref();
  138. $sth->finish;
  139. $dbh->{RaiseError} = 0;
  140. };
  141. if ($@) {
  142. closeOracle($dbh);
  143. if($opt_debug) { print STDOUT "DB Failed Query: $@\n"; }
  144. CleanupAndExit($ERRORS{'UNKNOWN'});
  145. }
  146. closeOracle($dbh);
  147. return $results;
  148. }
  149. #------ Open the connection to the database and return the handle
  150. sub openOracle
  151. {
  152. my ($dbh);
  153. $dbh = DBI->connect("$orasid", "$orauser", "$orapwd", "Oracle");
  154. if (!$dbh) {
  155. if ($opt_debug) { print "ERROR: Could not connect to Oracle!\n\n"; }
  156. CleanupAndExit($ERRORS{'UNKNOWN'});
  157. }
  158. if ($opt_debug) { print "Connected to Oracle SID $orasid\n\n"; }
  159. return $dbh;
  160. }
  161. #------- Close the database connection
  162. sub closeOracle()
  163. {
  164. my ($dbh) = @_;
  165. $dbh->disconnect;
  166. }
  167. #------ Exit with the current return code
  168. sub CleanupAndExit
  169. {
  170. my ($rc) = @_;
  171. exit($rc);
  172. }