1 #!/usr/local/bin/perl -w
3 # (c)2004 John Koyle, RFP Depot, LLC.
4 # This is free software use it however you would like.
9 use lib
"/usr/local/nagios/libexec";
10 use utils
qw(%ERRORS);
13 #*******************************************************************************
14 # Set user configureable options here.
16 # Global Oracle info set here rather than command line to avoid output in ps -ef
17 # Make sure this script is mode 700 and owner of the nrpe user
19 #*******************************************************************************
25 if (!$ENV{ORACLE_HOME}) {
26 $ENV{ORACLE_HOME} = '/u01/app/oracle/product/9.2';
29 #*******************************************************************************
30 my $state = $ERRORS{'UNKNOWN'};
33 my ($MAJOR_VERSION, $MINOR_VERSION) = q$Revision$ =~ /(\d+)\.(\d+)/;
34 my $VERSION = sprintf("%d.%02d", $MAJOR_VERSION - 1, $MINOR_VERSION);
36 my $opt_debug; # -d|--debug
37 my $opt_help; # -h|--help
38 my $opt_version; # -V|--version
39 my $opt_warn_space; # -w|--warn-space
40 my $opt_crit_space; # -c|--crit-space
44 my $help = <<MARK; # help statement
46 check_oracle_tbs v$VERSION
48 Checks the tablespaces in an Oracle database for available free space.
49 Usage: check_oracle_tbs [-w <warn>] [-c <crit>]
51 -d, --debug Output debug to screen.
52 -h, --help Displays this help and exits.
53 -w, --warn-space=... Warning threshold % free (default 15)
54 -c, --crit-space=... Critical threshold % free (default 10)
55 -V, --version Output version information and exit.
59 ## We want exact matches to the switches
61 Getopt
::Long
::config
('no_auto_abbrev', 'no_ignore_case');
65 "debug|d" => \
$opt_debug,
66 "help|h" => \
$opt_help,
67 "w|warn-space=s" => \
$opt_warn_space,
68 "c|crit-space=s" => \
$opt_crit_space,
69 "V|version" => \
$opt_version,
73 #***********************************************************************
74 # Process command-line switches
75 #***********************************************************************
77 if (! $rc || defined $opt_help)
80 exit (defined $opt_help ?
0 : 1);
83 if (defined $opt_version)
85 print STDERR
"check_oracle_tbs v$VERSION\n";
89 if (! defined $opt_warn_space)
91 if(defined $opt_debug) {
92 print STDOUT
"Warn space not defined, using 80%\n\n";
97 if (! defined $opt_crit_space)
99 if(defined $opt_debug) {
100 print STDOUT
"Crit space not defined, using 90%\n\n";
102 $opt_crit_space = 10;
105 my $array_ref = executeSQL
();
107 # Don't match certain tablespaces.
108 foreach my $row (@
$array_ref) {
109 my ( $tbs_name, $free_mb, $tot_mb, $free_pct) = @
$row;
110 if ($opt_debug) { print STDOUT
"Output: $tbs_name\t$tot_mb\t$free_mb\t$free_pct\n\n"; }
111 if ($free_pct < $opt_crit_space && $tbs_name !~ /RBS/ && $tbs_name !~ /PERFSTAT/ && $tbs_name !~ /UNDOTBS/) {
112 $state = $ERRORS{'CRITICAL'};
113 $answer .= "Critical: $tbs_name = $free_pct\% ";
116 if ($free_pct < $opt_warn_space && $tbs_name !~ /RBS/ && $tbs_name !~ /PERFSTAT/ && $tbs_name !~ /UNDOTBS/) {
117 $state = $ERRORS{'WARNING'};
118 $answer .= "Warning: $tbs_name = $free_pct\% ";
122 if ($state != $ERRORS{'CRITICAL'} && $state != $ERRORS{'WARNING'}) {
123 $state = $ERRORS{'OK'};
124 $answer = "All Tablespaces OK";
127 if ($opt_debug && $state != $ERRORS{'OK'}) { print STDOUT
"The following tablespaces are in question: $answer\n\n"; }
129 foreach my $key (keys %ERRORS) {
130 if ($state==$ERRORS{$key}) {
131 print ("$key: $answer");
137 #------------------SUBS-------------------------------------------------------
140 my ($dbh, $sth, $results);
145 $dbh->{RaiseError
} = 1;
146 $sth = $dbh->prepare(q{
147 select ts.tablespace_name,
148 trunc(sum(ts.free_b)/1024/1024) free,
149 trunc(sum(ts.max_b)/1024/1024) total,
150 trunc( sum(ts.free_b)/sum(ts.max_b)*1000) / 10 as pct_free
154 decode(a.autoextensible,'YES',a.maxsize-a.bytes+b.free,'NO',b.free) free_b,
156 from (select file_id,
160 decode(autoextensible,'YES',maxbytes,bytes) maxsize
161 from dba_data_files) a,
166 group by file_id, tablespace_name) b
167 where a.file_id=b.file_id(+)) ts
168 group by tablespace_name
172 $results = $sth->fetchall_arrayref();
174 $dbh->{RaiseError
} = 0;
179 if($opt_debug) { print STDOUT
"DB Failed Query: $@\n"; }
180 CleanupAndExit
($ERRORS{'UNKNOWN'});
188 #------ Open the connection to the database and return the handle
193 $dbh = DBI
->connect("$orasid", "$orauser", "$orapwd", "Oracle");
196 if ($opt_debug) { print "ERROR: Could not connect to Oracle!\n\n"; }
197 CleanupAndExit
($ERRORS{'UNKNOWN'});
199 if ($opt_debug) { print "Connected to Oracle SID $orasid\n\n"; }
203 #------- Close the database connection
211 #------ Exit with the current return code