check_ora_table_space.pl 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/perl
  2. #
  3. # Program check_ora_table_space
  4. # Written by: Erwan Arzur (erwan@netvalue.com)
  5. # License: GPL
  6. #
  7. # Last Modified: $Date: 2002-02-28 01:42:51 -0500 (Thu, 28 Feb 2002) $
  8. # Revisiin: $Revision: 2 $
  9. #
  10. # "check_ora_table_space.pl" plugin to check the state of Oracle
  11. # table spaces. Scarce documentation.
  12. #
  13. # you need DBD-Oracle-1.03.tar.gz and DBI-1.13.tar.gz from CPAN.org as
  14. # well as some Oracle client stuff to use it.
  15. #
  16. # The SQL request comes from www.dbasupport.com
  17. #
  18. use DBI;
  19. $ENV{"ORACLE_HOME"}="/intranet/apps/oracle";
  20. my $host = shift || &usage ("no host specified");
  21. my $sid = shift || &usage ("no sid specified");
  22. my $port = shift || &usage ("no port specified");
  23. my $dbuser = shift || &usage ("no user specified");
  24. my $dbpass = shift || &usage ("no password specified");
  25. my $tablespace = shift || &usage ("no table space specified");
  26. my $alertpct = int(shift) || &usage ("no warning state percentage specified");
  27. my $critpct = int(shift) || &usage ("no critical state percentage specified");
  28. my $dbh = DBI->connect( "dbi:Oracle:host=$host;port=$port;sid=$sid", $dbuser, $dbpass, { PrintError => 0, AutoCommit => 1, RaiseError => 0 } )
  29. || &error ("cannot connect to $dbname: $DBI::errstr\n");
  30. #$sth = $dbh->prepare(q{SELECT tablespace_name, SUM(BYTES)/1024/1024 FreeSpace FROM dba_free_space group by tablespace_name})
  31. my $exit_code = -1;
  32. $sth = $dbh->prepare(<<EOF
  33. select a.TABLESPACE_NAME, a.total,nvl(b.used,0) USED,
  34. nvl((b.used/a.total)*100,0) PCT_USED
  35. from (select TABLESPACE_NAME, sum(bytes)/(1024*1024) total
  36. from sys.dba_data_files group by TABLESPACE_NAME) a,
  37. (select TABLESPACE_NAME,bytes/(1024*1024) used from sys.SM\$TS_USED) b
  38. where a.TABLESPACE_NAME='$tablespace' and
  39. a.TABLESPACE_NAME=b.TABLESPACE_NAME(+)
  40. EOF
  41. )
  42. || &error("Cannot prepare request : $DBI::errstr\n");
  43. $sth->execute
  44. || &error("Cannot execute request : $DBI::errstr\n");
  45. while (($tbname, $total, $used, $pct_used) = $sth->fetchrow)
  46. {
  47. $pct_used=int($pct_used);
  48. print STDOUT "size: " . $total . " MB Used:" . int($used) . " MB (" . int($pct_used) . "%)\n";
  49. #print "table space $answer\n";
  50. if ($pct_used > $alertpct) {
  51. if ($pct_used > $critpct) {
  52. $exit_code = 2
  53. } else {
  54. $exit_code = 1;
  55. }
  56. } else {
  57. $exit_code = 0;
  58. }
  59. }
  60. $rc = $dbh->disconnect
  61. || &error ("Cannot disconnect from database : $dbh->errstr\n");
  62. exit ($exit_code);
  63. sub usage {
  64. print "@_\n" if @_;
  65. print "usage : check_ora_table_space.pl <host> <sid> <port> <user> <passwd> <tablespace> <pctwarn> <pctcrit>\n";
  66. exit (-1);
  67. }
  68. sub error {
  69. print "@_\n" if @_;
  70. exit (2);
  71. }