summaryrefslogtreecommitdiffstats
path: root/contrib/check_oracle_instance.pl
blob: 3ba8d57a73b0ef7e2bfdf21e2b2d666dd0c63e0b (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#!/usr/bin/perl
# $Id: check_oracle_instance.pl 10 2002-04-03 02:58:47Z sghosh $

#  Copyright (c) 2002  Sven Dolderer
#  some pieces of Code adopted from Adam vonNieda's oracletool.pl
#                                      (http://www.oracletool.com)
#
#   You may distribute under the terms of either the GNU General Public
#   License or the Artistic License, as specified in the Perl README file,
#   with the exception that it cannot be placed on a CD-ROM or similar media
#   for commercial distribution without the prior approval of the author.

#   This software is provided without warranty of any kind.

require 5.003;

use strict;
use Getopt::Long;

# We need the DBI and DBD-Oracle Perl modules:
require DBI || die "It appears that the DBI module is not installed! aborting...\n";
require DBD::Oracle || die "It appears that the DBD::Oracle module is not installed! aborting...\n";

use vars qw($VERSION $PROGNAME $logfile $debug $state $dbh $database $username $password $message $sql $cursor $opt_asession $opt_nsession $opt_tablespace $opt_nextents $opt_fextents $opt_aextents $privsok $warn $critical);

'$Revision: 10 $' =~ /^.*(\d+.\d+) \$$/;  # Use The Revision from RCS/CVS
$VERSION = $1;
$0 =~ m!^.*/([^/]+)$!;
$PROGNAME = $1;
#$debug="true";
$logfile = "/tmp/check_oracle_instance.log";
my %ERRORS = (UNKNOWN => -1, OK => 0, WARNING => 1, CRITICAL => 2);

# Read cmdline opts:
Getopt::Long::Configure('bundling', 'no_ignore_case');
GetOptions (
         "V|version"    => \&version,
         "h|help"       => \&usage,
         "u|user=s" => \$username,
         "p|passwd=s" => \$password,
         "c|connect=s" => \$database,
         "a|active-sessions:s"  => \$opt_asession,
         "s|num-sessions:s"  => \$opt_nsession,
         "t|tablespaces:s" => \$opt_tablespace,
         "n|num-extents:s" => \$opt_nextents,
         "f|free-extents:s" => \$opt_fextents,
         "x|allocate-extents" => \$opt_aextents
        );
($database && $username && $password) || die "mandatory parameters missing (try -h)\n";
logit("    \$opt_asession = \"$opt_asession\"");
logit("    \$opt_nsession = \"$opt_nsession\"");
logit("    \$opt_tablespace = \"$opt_tablespace\"");
logit("    \$opt_nextents = \"$opt_nextents\"");
logit("    \$opt_fextents = \"$opt_fextents\"");
logit("    \$opt_aextents = \"$opt_aextents\"");

# so let's connect to the instance...
$dbh = dbConnect($database,$username,$password);

$message="$database: ";
check_sessions($opt_nsession)   if ($opt_nsession && $privsok);
check_sessions($opt_asession,"active")   if ($opt_asession && $privsok);
check_tablespaces($opt_tablespace)   if ($opt_tablespace && $privsok);
check_nextents($opt_nextents)   if ($opt_nextents && $privsok);
check_fextents($opt_fextents)   if ($opt_fextents && $privsok);
check_aextents()   if ($opt_aextents && $privsok);

$message=$message . "ok. " . getDbVersion($dbh)   unless ($state);
print "$message\n";
exit $state;


sub usage {
   copyright();
   print "
This plugin will check various things of an oracle database instance.

Prerequisties are: a local oracle client,
                   perl > v5.003, and DBI and DBD::Oracle perl modules.

Usage: $PROGNAME -u <user> -p <passwd> -c <connectstring>
          [-a <w>/<c>] [-s <w>/<c>] [-t <w>/<c>] [-n <w>/<c>] [-f <w>/<c>] [-x]
       $PROGNAME [-V|--version]
       $PROGNAME [-h|--help]
";
   print "
Options:
 -u, --user=STRING
    the oracle user
 -p, --passwd=STRING
    the oracle password
 -c, --connect=STRING
    the oracle connectstring as defined in tnsnames.ora
 -a, --active-sessions=WARN/CRITICAL
    check the number of active (user-)sessions
      WARN(Integer): number of sessions to result in warning status,
      CRITICAL(Integer): number of sessions to result in critical status
 -s, --num-sessions=WARN/CRITICAL
    check the total number of (user-)sessions
      WARN(Integer): number of sessions to result in warning status,
      CRITICAL(Integer): number of sessions to result in critical status
 -t, --tablespaces=WARN/CRITICAL
    check the percent of used space in every tablespace
      WARN(Integer): percentage to result in warning status,
      CRITICAL(Integer): percentage to result in critical status
 -n, --num-extents=WARN/CRITICAL
    check the number of extents of every object (excluding SYS schema)
      WARN(Integer): number of extents to result in warning status,
      CRITICAL(Integer): number of extents to result in critical status
 -f, --free-extents=WARN/CRITICAL
    check the number of free extents of every object: max_extents - #extents
      WARN(Integer): number of free extents to result in warning status,
      CRITICAL(Integer): number of free extents to result in critical status
 -x, --allocate-extents
    warn if an object cannot allocate a next extent.
";
   exit $ERRORS{"UNKNOWN"};
}


sub version {
   copyright();
   print "
$PROGNAME $VERSION
";
   exit $ERRORS{"UNKNOWN"};
}


sub copyright {
   print "The netsaint plugins come with ABSOLUTELY NO WARRANTY. You may redistribute
copies of the plugins under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.
Copyright (c) 2002 Sven Dolderer\n";
}


sub logit {
   my $text = shift;
   if ($debug) {
      open (LOG,">>$logfile") || die "Cannot open log file \"$logfile\"!";
      print LOG "$text\n";
      close (LOG);
   }
}


sub dbConnect {
   logit("Enter subroutine dbConnect");

   my $database = shift;
   my $username = shift;
   my $password = shift;

# Attempt to make connection to the database..
   my $data_source = "dbi:Oracle:$database";
   $dbh = DBI->connect($data_source,$username,$password,{PrintError=>0});

# Show an error message for these errors.
# ORA-12224 - "The connection request could not be completed because the listener is not running."
# ORA-01034 - "Oracle was not started up."
# ORA-01090 - "Shutdown in progress - connection is not permitted""
# ORA-12154 - "The service name specified is not defined correctly in the TNSNAMES.ORA file."
# ORA-12505 - "TNS:listener could not resolve SID given in connect descriptor."
# ORA-12545 - "TNS:name lookup failure."

   unless ($dbh) {
         logit("      Error message is ~$DBI::errstr~");
         if ( $DBI::errstr =~ /ORA-01017|ORA-1017|ORA-01004|ORA-01005/ ) {
            $message="Login error: ~$DBI::errstr~";
	    $state=$ERRORS{"UNKNOWN"};
         } elsif ( $DBI::errstr =~ /ORA-12224/ ) {
            $message= "You received an ORA-12224, which usually means the listener is down, or your connection definition in your tnsnames.ora file is incorrect. Check both of these things and try again.";
	    $state=$ERRORS{"CRITICAL"};
         } elsif ( $DBI::errstr =~ /ORA-01034/ ) {
            $message= "You received an ORA-01034, which usually means the database is down. Check to be sure the database is up and try again.";
	    $state=$ERRORS{"CRITICAL"};
         } elsif ( $DBI::errstr =~ /ORA-01090/ ) {
            $message= "You received an ORA-01090, which means the database is in the process of coming down.";
	    $state=$ERRORS{"CRITICAL"};
         } elsif ( $DBI::errstr =~ /ORA-12154/ ) {
            $message= "You received an ORA-12154, which probably means you have a mistake in your TNSNAMES.ORA file for the database that you chose.";
	    $state=$ERRORS{"UNKNOWN"};
         } elsif ( $DBI::errstr =~ /ORA-12505/ ) {
            $message= "You received an ORA-12505, which probably means you have a mistake in your TNSNAMES.ORA file for the database that you chose, or the database you are trying to connect to is not defined to the listener that is running on that node.";
	    $state=$ERRORS{"UNKNOWN"};
         } elsif ( $DBI::errstr =~ /ORA-12545/ ) {
            $message= "You received an ORA-12545, which probably means you have a mistake in your TNSNAMES.ORA file for the database that you chose. (Possibly the node name).";
	    $state=$ERRORS{"UNKNOWN"};
         } else {
            $message="Unable to connect to Oracle ($DBI::errstr)\n";
	    $state=$ERRORS{"UNKNOWN"};
         }
	 
   } else {
         logit("      Login OK.");

         # check to be sure this user has "SELECT ANY TABLE" privilege.
         logit("      checking for \"SELECT ANY TABLE\" privilege");
         if (checkPriv("SELECT ANY TABLE") < 1) {
            $message="user $username needs \"SELECT ANY TABLE\" privilege.";
            $state=$ERRORS{"UNKNOWN"};
         } else {
            $privsok="yep";
	    $state=$ERRORS{"OK"};
         }
   }
   return ($dbh);
}


sub getDbVersion {

   logit("Enter subroutine getDbVersion");

   my $dbh = shift;
   my $oraversion;

# Find out if we are dealing with Oracle7 or Oracle8
   logit("   Getting Oracle version");
   $sql = "select banner from v\$version where rownum=1";

   $cursor = $dbh->prepare($sql) or logit("Error: $DBI::errstr");
   $cursor->execute;
   (($oraversion) = $cursor->fetchrow_array);
   $cursor->finish;
   logit("   Oracle version = $oraversion");
   return $oraversion;
}


sub checkPriv {
   logit("Enter subroutine checkPriv");
   my ($privilege,$yesno);
   $privilege = shift;
   logit("   Checking for privilege \"$privilege\"");
   
   $sql = "SELECT COUNT(*) FROM SESSION_PRIVS WHERE PRIVILEGE = '$privilege'";  
   $cursor=$dbh->prepare($sql);
   $cursor->execute;                                                               $yesno = $cursor->fetchrow_array;
   $cursor->finish;

   return($yesno);
}


sub get_values {
   logit("Enter subroutine get_values");
   my ($args, $inverse, $abort);
   $args = shift;
   $inverse = shift;
   if ($args =~ m!^(\d+)/(\d+)$!) {
     $warn = $1;
     $critical = $2;

     # TODO: check for positive numbers!
     
     if (! $inverse && $warn >= $critical) {
        print "\"$args\": warning threshold must be less than critical threshold. aborting...\n";
	$abort="yep";
     }
     if ($inverse && $warn <= $critical) {
        print "\"$args\": warning threshold must be greater than critical threshold. aborting...\n";
	$abort="yep";
     }
   } else {
     print "\"$args\": invalid warn/critical thresholds. aborting...\n";
     $abort="yep";
   }
   exit $ERRORS{"UNKNOWN"}  if $abort;
   logit ("      args=$args, warn=$warn, critical=$critical");
}


sub check_sessions {
   logit("Enter subroutine check_sessions");
   my ($args, $add, $sqladd, $count);
   $args = shift;
   $add = shift || '#';   # Default: Number of sessions
   $sqladd = "AND STATUS = 'ACTIVE'"   if ($add eq "active");

   get_values($args);
   
   $sql = "SELECT COUNT(*) FROM V\$SESSION WHERE TYPE <> 'BACKGROUND' $sqladd";  
   $cursor=$dbh->prepare($sql);
   $cursor->execute;
   $count = $cursor->fetchrow_array;
   $cursor->finish;
   logit ("      $add sessions is $count");

   if ($count >= $critical) {
      $message = $message . "$add sessions critical ($count)    ";
      $state=$ERRORS{"CRITICAL"};
   } elsif ($count >= $warn) {
      $message = $message . "$add sessions warning ($count)    ";
      $state=$ERRORS{"WARNING"}  if $state < $ERRORS{"WARNING"};
   }
}


sub check_tablespaces {
   logit("Enter subroutine check_tablespaces");
   my ($args, $tablespace, $pctused, $mymsg, $mywarn, $mycritical);
   $args = shift;
   $mymsg = "Tablespace usage ";

   get_values($args);
   
   $sql = "SELECT
     DF.TABLESPACE_NAME \"Tablespace name\",
     NVL(ROUND((DF.BYTES-SUM(FS.BYTES))*100/DF.BYTES),100) \"Percent used\"
   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
   ORDER BY 2 DESC";

   $cursor=$dbh->prepare($sql);
   $cursor->execute;
   while (($tablespace, $pctused) = $cursor->fetchrow_array) {
      logit ("      $tablespace - $pctused% used");
      if ($pctused >= $critical) {
         unless ($mycritical) {
           $mymsg = $mymsg . "critical: ";
	   $mycritical="yep";
	 }
         $mymsg = $mymsg . "$tablespace ($pctused%) ";
         $state=$ERRORS{"CRITICAL"};
      } elsif ($pctused >= $warn) {
         unless ($mywarn) {
           $mymsg = $mymsg . "warning: ";
	   $mywarn="yep";
	 }
         $mymsg = $mymsg . "$tablespace ($pctused%) ";
         $state=$ERRORS{"WARNING"}  if $state < $ERRORS{"WARNING"};
      }
   }
   $cursor->finish;
   $message = $message . $mymsg . "   "   if ($mycritical || $mywarn);
}


sub check_nextents {
   logit("Enter subroutine check_nextents");
   my ($args, $owner, $objname, $objtype, $extents, $mymsg, $mywarn, $mycritical);
   $args = shift;
   $mymsg = "#Extents ";

   get_values($args);
   
   $sql = "SELECT
             OWNER              \"Owner\",
             SEGMENT_NAME       \"Object name\",
             SEGMENT_TYPE       \"Object type\",
             COUNT(*)           \"Extents\"
           FROM DBA_EXTENTS WHERE OWNER <> 'SYS'
             GROUP BY SEGMENT_TYPE, SEGMENT_NAME, TABLESPACE_NAME, OWNER
             HAVING COUNT(*) >= $warn
             ORDER BY 4 DESC";

   $cursor=$dbh->prepare($sql);
   $cursor->execute;
   while (($owner, $objname, $objtype, $extents) = $cursor->fetchrow_array) {
      if ($extents >= $critical) {
         unless ($mycritical) {
           $mymsg = $mymsg . "critical: ";
           $mycritical="yep";
         }
         $mymsg = $mymsg . "$owner.$objname($objtype)=$extents ";
         $state=$ERRORS{"CRITICAL"};
      } elsif ($extents >= $warn) {
         unless ($mywarn) {
           $mymsg = $mymsg . "warning: ";
           $mywarn="yep";
         }
         $mymsg = $mymsg . "$owner.$objname($objtype)=$extents ";
         $state=$ERRORS{"WARNING"}  if $state < $ERRORS{"WARNING"};
      }
   }
   $cursor->finish;
   $message = $message . $mymsg . "   "   if ($mycritical || $mywarn);
}


sub check_fextents {
   logit("Enter subroutine check_fextents");
   my ($args, $owner, $objname, $objtype, $extents, $maxextents, $freextents, $mymsg, $mywarn, $mycritical);
   $args = shift;
   $mymsg = "Free extents ";

   get_values($args, "inverse");
   
   $sql = "SELECT
      OWNER                        \"Owner\",
      SEGMENT_NAME                 \"Object name\",
      SEGMENT_TYPE                 \"Object type\",
      EXTENTS                      \"Extents\",
      MAX_EXTENTS                  \"Max extents\",
      MAX_EXTENTS - EXTENTS        \"Free extents\"
   FROM DBA_SEGMENTS
      WHERE (EXTENTS + $warn) >= MAX_EXTENTS
      AND SEGMENT_TYPE != 'CACHE'
      ORDER BY 6";

   $cursor=$dbh->prepare($sql);
   $cursor->execute;
   while (($owner, $objname, $objtype, $extents, $maxextents, $freextents) = $cursor->fetchrow_array) {
      if ($freextents <= $critical) {
         unless ($mycritical) {
           $mymsg = $mymsg . "critical: ";
           $mycritical="yep";
         }
         $mymsg = $mymsg . "$owner.$objname($objtype)=$extents ";
         $state=$ERRORS{"CRITICAL"};
      } elsif ($freextents <= $warn) {
         unless ($mywarn) {
           $mymsg = $mymsg . "warning: ";
           $mywarn="yep";
         }
         $mymsg = $mymsg . "$owner.$objname($objtype)=$extents/$maxextents ";
         $state=$ERRORS{"WARNING"}  if $state < $ERRORS{"WARNING"};
      }
   }
   $cursor->finish;
   $message = $message . $mymsg . "   "   if ($mycritical || $mywarn);
}


sub check_aextents {
   logit("Enter subroutine check_aextents");
   my ($args, $owner, $objname, $objtype, $tablespace_name, $mymsg, $mywarn);
   my (@tablespaces);

   # Get a list of all tablespaces
   $sql = "SELECT TABLESPACE_NAME
           FROM DBA_TABLESPACES ORDER BY TABLESPACE_NAME";
   $cursor = $dbh->prepare($sql);
   $cursor->execute;
   while ($tablespace_name = $cursor->fetchrow_array) {
      push @tablespaces, $tablespace_name;
   }
   $cursor->finish;

   # Search every tablespace for objects which cannot allocate a next extent.
   foreach $tablespace_name(@tablespaces) {
      logit ("        checking tablespace $tablespace_name");
      $sql = "SELECT
	   OWNER            \"Owner\",
	   SEGMENT_NAME     \"Object name\",
	   SEGMENT_TYPE     \"Object type\"
	FROM DBA_SEGMENTS
	   WHERE TABLESPACE_NAME = '$tablespace_name'
	   AND NEXT_EXTENT > (SELECT NVL(MAX(BYTES),'0') FROM DBA_FREE_SPACE
			      WHERE TABLESPACE_NAME = '$tablespace_name')";
      $cursor = $dbh->prepare($sql);
      $cursor->execute;
      while (($owner, $objname, $objtype) = $cursor->fetchrow_array) {
         logit ("        found: $owner.$objname($objtype)");
         unless ($mywarn) {
           $mymsg = $mymsg . "warning: ";
           $mywarn="yep";
         }
         $mymsg = $mymsg . "$owner.$objname($objtype) ";
         $state=$ERRORS{"WARNING"}  if $state < $ERRORS{"WARNING"};
      }
      $cursor->finish;
   }
   $message = $message . $mymsg . "cannot allocate a next extent.   "   if $mywarn;
}