#!/usr/bin/perl -w #===================================================================== # # PROGRAM: get_data.pl # # PURPOSE: This program retrieves data from the SMARTS Oracle DB # # AUTHOR: Ron Bueler # # DATE: Apr 22, 2000 # # Revision History: # # # # #===================================================================== use DBI; use strict; use diagnostics; #===================================================================== # Print Switch #--------------------------------------------------------------------- select (STDOUT); $| = 1; #===================================================================== # Debugging Option Switch #--------------------------------------------------------------------- my ($DEBUG) = 1; #===================================================================== # Check for the correct number of arguments #--------------------------------------------------------------------- my ($DTG) = ` date +"%Y%m%d%H%M" `; my ($report_file) = "./report_file."; my ($host) = ""; my ($buffer) = ""; my ($class) = ""; my ($key) = ""; my ($comparison) = ""; my ($value) = ""; my ($duration) = ""; my ($alarm) = ""; my ($email) = ""; my ($startTime) = ""; my ($endTime) = ""; my ($elaspedTime) = ""; my (@cmd) = (); my (@response) = (); my ($dbh) = ""; my ($sth) = ""; my (@row) = (); my ($sql) = ""; my (@headers) = (); #===================================================================== # Start of get_data.pl #--------------------------------------------------------------------- { if ($DEBUG) { &log("Starting program $0\n"); } chomp ($DTG); $report_file = $report_file . $DTG; if ($DEBUG) { &log("report file = |" . $report_file . "|\n"); } open (REPORT, ">$report_file") || die "Cannot open $report_file"; if ($DEBUG) { &log ("Connecting to Oracle...\n"); } $dbh = DBI->connect('dbi:Oracle:', q{webdb/webdb@(DESCRIPTION= (ADDRESS=(PROTOCOL=TCP)(HOST=pm02rg.rst.cw.net)(PORT=1521)) (CONNECT_DATA=(SID=smart)))}, ""); if ($DEBUG) { &log ("Creating SQL statement...\n"); } # $sql = "SELECT to_char(DTG, 'yyyy-mm-dd hh24:mi'), host, key, value FROM SMART.PM02RG_RAW"; # $sql = "SELECT to_char(DTG, 'yyyy-mm-dd hh24:mi'), HOST, USR, SYSTEM, UTILIZATION, IDLE FROM SMART.CPUS"; $sql = "SELECT HOST, round(avg(USR),2), round(avg(SYSTEM),2), round(avg(UTILIZATION),2), round(avg(IDLE),2) FROM SMART.CPUS GROUP BY HOST"; # $sql = "SELECT * FROM SMART.CPUS"; if ($DEBUG) { &log ("Preparing SQL statment...\n"); } $sth = $dbh->prepare($sql) || die "\nCouldn't prepare statement: " . $dbh->errstr; if ($DEBUG) { &log ("Executing SQL statement...\n"); } $sth->execute() || die "\nCouldn't execute statement: " . $sth->errstr; if ($DEBUG) { &log ("printing results...\n"); } # print "$sth->{NAME}->[0] "; # print "$sth->{NAME}->[1] "; # print "$sth->{NAME}->[2] "; # print "$sth->{NAME}->[3] "; # print "$sth->{NAME}->[4] "; # print "$sth->{NAME}->[5]\n"; # while (@headers=$sth->{NAME}) { # print "@headers\n"; # } # print "$sth->{NAME} \n"; # while (@row=$sth->fetchall_arrayref) { while (@row=$sth->fetchrow_array) { print "@row\n"; print REPORT "@row\n"; } close (REPORT); if ($DEBUG) { &log ("Closing Statement Handle...\n"); } $sth->finish() || die "\nCouldn't finish statement: " . $sth->errstr; # if ($DEBUG) { &log ("Disconnecting from Oracle Database...\n"); } # $dbh = DBI->disconnect() || die $dbh->errstr; system ("mailx -s 'Stats' joe.hall\@cw.com < $report_file"); # system ("mailx -s 'Stats' buelerra\@codenet.net< $report_file"); if ($DEBUG) { &log ("Finished program $0\n"); } exit (0); } #===================================================================== # log subroutine #--------------------------------------------------------------------- sub log { my ($text) = @_; printf (&time_stamp() . " - " . $text); } #===================================================================== # time_stamp subroutine #--------------------------------------------------------------------- sub time_stamp { my ($now) = ` date +"%Y-%m-%d %H:%M:%S" `; chomp ($now); return ($now); } #===================================================================== # get_short_hostname(); #--------------------------------------------------------------------- sub get_short_hostname { my ($full_host) = @_; my ($host_index) = index ($full_host, "."); my ($short_host) = substr ($full_host, 0, $host_index); return ($short_host); } #===================================================================== # End of get_data.pl #---------------------------------------------------------------------