check_oracle_tbs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #!/usr/local/bin/perl -w
  2. # (c)2003 John Koyle, RFP Depot, LLC.
  3. # This is free software use it however you would like.
  4. # Thanks to the folks at http://www.think-forward.com for the SQL query
  5. use strict;
  6. use DBI;
  7. use Getopt::Long 2.16;
  8. use lib "/usr/local/nagios/libexec";
  9. use utils qw(%ERRORS);
  10. #*******************************************************************************
  11. # Set user configureable options here.
  12. #
  13. # Global Oracle info set here rather than command line to avoid output in ps -ef
  14. # Make sure this script is mode 700 and owner of the nrpe user
  15. #
  16. #*******************************************************************************
  17. my $orasid = "";
  18. my $orauser = "";
  19. my $orapwd = "";
  20. if (!$ENV{ORACLE_HOME}) {
  21. $ENV{ORACLE_HOME} = '/a01/app/oracle/product/9.2.0.1';
  22. }
  23. #*****************You shouldn't need to modify anything below here *************
  24. my $state = $ERRORS{'UNKNOWN'};
  25. my $answer = undef;
  26. my ($MAJOR_VERSION, $MINOR_VERSION) = q$Revision$ =~ /(\d+)\.(\d+)/;
  27. my $VERSION = sprintf("%d.%02d", $MAJOR_VERSION - 1, $MINOR_VERSION);
  28. my $opt_debug; # -d|--debug
  29. my $opt_help; # -h|--help
  30. my $opt_version; # -V|--version
  31. my $opt_warn_space; # -w|--warn-space
  32. my $opt_crit_space; # -c|--crit-space
  33. my $help = <<MARK; # help statement
  34. check_oracle_tbs v$VERSION
  35. Checks the tablespaces in an Oracle database for available free space.
  36. Usage: check_oracle_tbs [-w <warn>] [-c <crit>]
  37. -d, --debug Output debug to screen.
  38. -h, --help Displays this help and exits.
  39. -w, --warn-space=... Warning threshold % free (default 15)
  40. -c, --crit-space=... Critical threshold % free (default 10)
  41. -V, --version Output version information and exit.
  42. MARK
  43. ## We want exact matches to the switches
  44. Getopt::Long::config('no_auto_abbrev', 'no_ignore_case');
  45. my $rc = GetOptions(
  46. "debug|d" => \$opt_debug,
  47. "help|h" => \$opt_help,
  48. "w|warn-space=s" => \$opt_warn_space,
  49. "c|crit-space=s" => \$opt_crit_space,
  50. "V|version" => \$opt_version,
  51. );
  52. #***********************************************************************
  53. # Process command-line switches
  54. #***********************************************************************
  55. if (! $rc || defined $opt_help)
  56. {
  57. print STDERR $help;
  58. exit (defined $opt_help ? 0 : 1);
  59. }
  60. if (defined $opt_version)
  61. {
  62. print STDERR "check_oracle_tbs v$VERSION\n";
  63. exit 0;
  64. }
  65. if (! defined $opt_warn_space)
  66. {
  67. if(defined $opt_debug) {
  68. print STDOUT "Warn space not defined, using 80%\n\n";
  69. }
  70. $opt_warn_space = 15;
  71. }
  72. if (! defined $opt_crit_space)
  73. {
  74. if(defined $opt_debug) {
  75. print STDOUT "Crit space not defined, using 90%\n\n";
  76. }
  77. $opt_crit_space = 10;
  78. }
  79. my $array_ref = executeSQL();
  80. foreach my $row (@$array_ref) {
  81. my ( $tbs_name, $tot_mb, $free_mb, $free_pct, $used_pct, $fsfi) = @$row;
  82. if ($opt_debug) { print STDOUT "Output: $tbs_name\t$tot_mb\t$free_mb\t$free_pct\t$used_pct\t$fsfi\n\n"; }
  83. if ($used_pct > (100 - $opt_crit_space) && $tbs_name !~ /RBS/) {
  84. $state = $ERRORS{'CRITICAL'};
  85. $answer .= "$tbs_name = $used_pct\% ";
  86. last;
  87. }
  88. if ($used_pct > (100 - $opt_warn_space) && $tbs_name !~ /RBS/) {
  89. $state = $ERRORS{'WARNING'};
  90. $answer .= "$tbs_name = $used_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. sub executeSQL
  106. {
  107. my ($dbh, $sth, $results);
  108. $dbh = openOracle();
  109. eval {
  110. $dbh->{RaiseError} = 1;
  111. # This query is taken from this URL and used with permission: http://www.think-forward.com/sql/tspace.htm
  112. $sth = $dbh->prepare(q{
  113. select df.tablespace_name tspace,
  114. df.bytes/(1024*1024) tot_ts_size,
  115. sum(fs.bytes)/(1024*1024) free_ts_size,
  116. round(sum(fs.bytes)*100/df.bytes) ts_pct,
  117. round((df.bytes-sum(fs.bytes))*100/df.bytes) ts_pct1,
  118. ROUND(100*SQRT(MAX(fs.bytes)/SUM(fs.bytes))*
  119. (1/SQRT(SQRT(COUNT(fs.bytes)))) ,2) FSFI
  120. from dba_free_space fs, (select tablespace_name, sum(bytes) bytes
  121. from dba_data_files
  122. group by tablespace_name ) df
  123. where fs.tablespace_name = df.tablespace_name
  124. group by df.tablespace_name, df.bytes
  125. });
  126. $sth->execute();
  127. $results = $sth->fetchall_arrayref();
  128. $sth->finish;
  129. $dbh->{RaiseError} = 0;
  130. };
  131. if ($@) {
  132. closeOracle($dbh);
  133. if($opt_debug) { print STDOUT "DB Failed Query: $@\n"; }
  134. CleanupAndExit($ERRORS{'UNKNOWN'});
  135. }
  136. closeOracle($dbh);
  137. return $results;
  138. }
  139. #------ Open the connection to the database and return the handle
  140. sub openOracle
  141. {
  142. my ($dbh);
  143. $dbh = DBI->connect("$orasid", "$orauser", "$orapwd", "Oracle");
  144. if (!$dbh) {
  145. if ($opt_debug) { print "ERROR: Could not connect to Oracle!\n\n"; }
  146. CleanupAndExit($ERRORS{'UNKNOWN'});
  147. }
  148. if ($opt_debug) { print "Connected to Oracle SID $orasid\n\n"; }
  149. return $dbh;
  150. }
  151. #------- Close the database connection
  152. sub closeOracle()
  153. {
  154. my ($dbh) = @_;
  155. $dbh->disconnect;
  156. }
  157. #------ Exit with the current return code
  158. sub CleanupAndExit
  159. {
  160. my ($rc) = @_;
  161. exit($rc);
  162. }