summaryrefslogtreecommitdiffstats
path: root/contrib/check_oracle_tbs
blob: bcc4af87d86493f93650a455a130db022401e9b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/local/bin/perl -w

# (c)2003 John Koyle, RFP Depot, LLC.
# This is free software use it however you would like.
# Thanks to the folks at http://www.think-forward.com for the SQL query


use strict;
use DBI;
use Getopt::Long 2.16;
use lib "/usr/local/nagios/libexec";
use utils qw(%ERRORS);


#*******************************************************************************
# Set user configureable options here.
#
# Global Oracle info set here rather than command line to avoid output in ps -ef
# Make sure this script is mode 700 and owner of the nrpe user
#
#*******************************************************************************
my $orasid = "";
my $orauser = "";
my $orapwd = "";


if (!$ENV{ORACLE_HOME}) {
        $ENV{ORACLE_HOME} = '/a01/app/oracle/product/9.2.0.1';
}

#*****************You shouldn't need to modify anything below here *************
my $state = $ERRORS{'UNKNOWN'};
my $answer = undef;

my ($MAJOR_VERSION, $MINOR_VERSION) = q$Revision$ =~ /(\d+)\.(\d+)/;
my $VERSION = sprintf("%d.%02d", $MAJOR_VERSION - 1, $MINOR_VERSION);

my $opt_debug;                  # -d|--debug
my $opt_help;                   # -h|--help
my $opt_version;				# -V|--version
my $opt_warn_space; 	        # -w|--warn-space
my $opt_crit_space; 	        # -c|--crit-space

my $help = <<MARK;      # help statement

check_oracle_tbs v$VERSION

Checks the tablespaces in an Oracle database for available free space.
Usage: check_oracle_tbs [-w <warn>] [-c <crit>]

  -d, --debug              Output debug to screen.
  -h, --help               Displays this help and exits.
  -w, --warn-space=...     Warning threshold % free (default 15)
  -c, --crit-space=...     Critical threshold % free (default 10)
  -V, --version            Output version information and exit.

MARK

## We want exact matches to the switches

Getopt::Long::config('no_auto_abbrev', 'no_ignore_case');

my $rc = GetOptions(
                "debug|d"               => \$opt_debug,
                "help|h"                => \$opt_help,
                "w|warn-space=s"        => \$opt_warn_space,
                "c|crit-space=s"        => \$opt_crit_space,
                "V|version"             => \$opt_version,
                   );


#***********************************************************************
# Process command-line switches
#***********************************************************************

if (! $rc || defined $opt_help)
{
    print STDERR $help;
    exit (defined $opt_help ? 0 : 1);
}

if (defined $opt_version)
{
    print STDERR "check_oracle_tbs v$VERSION\n";
    exit 0;
}

if (! defined $opt_warn_space)
{
    if(defined $opt_debug) {
        print STDOUT "Warn space not defined, using 80%\n\n";
    }
    $opt_warn_space = 15;
}

if (! defined $opt_crit_space)
{
    if(defined $opt_debug) {
        print STDOUT "Crit space not defined, using 90%\n\n";
    }
    $opt_crit_space = 10;
}

my $array_ref = executeSQL();

foreach my $row (@$array_ref) {
    my ( $tbs_name, $tot_mb, $free_mb, $free_pct, $used_pct, $fsfi) = @$row;
    if ($opt_debug) { print STDOUT "Output: $tbs_name\t$tot_mb\t$free_mb\t$free_pct\t$used_pct\t$fsfi\n\n"; }
    if ($used_pct > (100 - $opt_crit_space) && $tbs_name !~ /RBS/) {
        $state = $ERRORS{'CRITICAL'};
        $answer .= "$tbs_name = $used_pct\% ";
        last;
    }
    if ($used_pct > (100 - $opt_warn_space) && $tbs_name !~ /RBS/) {
        $state = $ERRORS{'WARNING'};
        $answer .= "$tbs_name = $used_pct\% ";
    }
}

if ($state != $ERRORS{'CRITICAL'} && $state != $ERRORS{'WARNING'}) {
    $state = $ERRORS{'OK'};
    $answer = "All Tablespaces OK";
}

if ($opt_debug && $state != $ERRORS{'OK'}) { print STDOUT "The following tablespaces are in question: $answer\n\n"; }

foreach my $key (keys %ERRORS) {
        if ($state==$ERRORS{$key}) {
                print ("$key: $answer");
                last;
        }
}
exit $state;

sub executeSQL
{
    my ($dbh, $sth, $results);

    $dbh = openOracle();

    eval {
      $dbh->{RaiseError} = 1;
      # This query is taken from this URL and used with permission: http://www.think-forward.com/sql/tspace.htm
      $sth = $dbh->prepare(q{
            select df.tablespace_name tspace, 
                   df.bytes/(1024*1024) tot_ts_size,
                   sum(fs.bytes)/(1024*1024) free_ts_size,
                   round(sum(fs.bytes)*100/df.bytes) ts_pct,
                   round((df.bytes-sum(fs.bytes))*100/df.bytes) ts_pct1,
                   ROUND(100*SQRT(MAX(fs.bytes)/SUM(fs.bytes))*
                   (1/SQRT(SQRT(COUNT(fs.bytes)))) ,2) FSFI
              from dba_free_space fs, (select tablespace_name, sum(bytes) bytes
                                         from dba_data_files
                                     group by tablespace_name ) df          
             where fs.tablespace_name = df.tablespace_name
          group by df.tablespace_name, df.bytes
         });

      $sth->execute();
      $results = $sth->fetchall_arrayref();
      $sth->finish;
      $dbh->{RaiseError} = 0;
    };

    if ($@) {
      closeOracle($dbh);
      if($opt_debug) { print STDOUT "DB Failed Query: $@\n"; }
      CleanupAndExit($ERRORS{'UNKNOWN'});
    }

    closeOracle($dbh);

    return $results;
}

#------ Open the connection to the database and return the handle
sub openOracle
{
   my ($dbh);

   $dbh = DBI->connect("$orasid", "$orauser", "$orapwd", "Oracle");

   if (!$dbh) {
      if ($opt_debug) { print "ERROR: Could not connect to Oracle!\n\n"; }
      CleanupAndExit($ERRORS{'UNKNOWN'});
   }
   if ($opt_debug) { print "Connected to Oracle SID $orasid\n\n"; }
   return $dbh;
}

#------- Close the database connection
sub closeOracle()
{
   my ($dbh) = @_;

   $dbh->disconnect;
}

#------ Exit with the current return code
sub CleanupAndExit
{
        my ($rc) = @_;

        exit($rc);
}