perl協程運算元據庫
perl用open2開啟兩個管道,接管sqlplus的標準輸入輸出,管理資料庫。
點選(此處)摺疊或開啟
-
use IPC::Open2;
-
use POSIX;
-
-
sub conn
-
{
-
my $r = shift;
-
my $w = shift;
-
my $conn_str = shift;
-
my $pid = open2($r, $w, "sqlplus -S ".$conn_str) or die "$!";
-
print $w "set lin 200\n" or die "$!";
-
print $w "set pages 0\n";
-
print $w "set feedback off\n";
-
$pid;
-
}
-
-
sub exec_sql
-
{
-
my $r = shift;
-
my $w = shift;
-
my $sql = shift;
-
my @ret;
-
if($sql !~ /;$/){$sql .= ";";}
-
print $w $sql."\n";
-
print Writer "select 'OK' from dual;\n";
-
while(<Reader>)
-
{
-
chomp;
-
trim;
-
if(/^OK$/) {last;}
-
push @ret, $_;
-
}
-
@ret;
-
}
-
-
sub exec_sql_single_row
-
{
-
my $r = shift;
-
my $w = shift;
-
my $sql = shift;
-
my @ret = &exec_sql($r, $w, $sql);
-
shift @ret;
-
}
-
-
sub get_param
-
{
-
my $r = shift;
-
my $w = shift;
-
my $param = shift;
-
my $sql = "select value from v\$parameter where name = '$param'";
-
my @ret = &exec_sql($r, $w, $sql);
-
shift @ret;
-
}
-
-
sub date_format
-
{
-
my $r = shift;
-
my $w = shift;
-
my $date = shift;
-
my $from_format = shift;
-
my $to_format = shift;
-
my $sql = "select to_char(to_date('$date', '$from_format'), '$to_format') from dual";
-
&exec_sql_single_row($r, $w, $sql);
-
}
-
-
sub get_db_time
-
{
-
my $r = shift;
-
my $w = shift;
-
my $format = shift;
-
my $add = shift;
-
my $sql = "select to_char(sysdate $add, '$format') from dual";
-
&exec_sql_single_row($r, $w, $sql);
-
}
-
-
sub get_alert
-
{
-
my $r = shift;
-
my $w = shift;
-
my $bdump = &get_param($r, $w, "background_dump_dest");
-
my $sid = &get_param($r, $w, "instance_name");
-
my $logfile = $bdump."/alert_".$sid.".log";
-
}
-
-
sub get_alert_time
-
{
-
my $add_days = shift;
-
@months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
-
@days = qw(Sun Mon Tue wed Thu Fri Sat);
-
my $str = strftime "%m-%d-%w", localtime(time + $add_days*60*60*24);
-
my($mm, $dd, $day) = split /-/, $str;
-
my $ret = "$days[$day] $months[$mm-1] $dd";
-
}
-
-
sub check_alert
-
{
-
my $r = shift;
-
my $w = shift;
-
my $check_days = shift;
-
my $logfile = &get_alert($r, $w);
-
my $start = &get_alert_time($check_days * -1);
-
my $flag = 0;
-
my $last_time = "";
-
my @ret;
-
open(ALERT, $logfile) or die "open $logfile error: $!";
-
while(<ALERT>)
-
{
-
if(/^$start/)
-
{
-
$flag = 1;
-
$last_time = $_;
-
}
-
if($flag && /ORA-|Err/)
-
{
-
push @ret, "$last_time";
-
$last_time = "";
-
push @ret, $_;
-
}
-
}
-
close ALERT;
-
@ret;
-
}
-
-
sub check_logfile
-
{
-
my $logfile = shift;
-
my $start = shift;
-
my $parts = shift;
-
my $flag = 0;
-
my @ret;
-
my $tmp_line;
-
open(LOGFILE, $logfile) or die "open $logfile error: $!";
-
while(<LOGFILE>)
-
{
-
$tmp_line = $_;
-
if(/^$start/)
-
{
-
$flag = 1;
-
$last_time = $_;
-
}
-
if( $flag && $tmp_line =~ /$parts/)
-
{
-
push @ret, $_;
-
}
-
}
-
close LOGFILE;
-
@ret;
-
}
-
-
my $r = \*Reader;
-
my $w = \*Writer;
-
-
my @ret;
-
my $pid = &conn($r, $w, "/ as sysdba");
-
-
print "logfiles:\n";
-
@ret = &exec_sql($r, $w, "select member from v\$logfile order by 1");
-
foreach (@ret)
-
{
-
print $_."\n";
-
}
-
-
-
$sid = &get_param($r, $w, "instance_name");
-
print "sid = $sid\n";
-
-
$date = &exec_sql_single_row($r, $w, "select sysdate from dual;");
-
print "date = $date\n";
-
-
$str = &date_format($r, $w, "2014-1-1 23:11:44", "yyyy-mm-dd hh24:mi:ss", "dy mon dd hh24:mi");
-
print "$str\n";
-
-
$str = &get_db_time($r, $w, "yyyymmdd", -100);
-
print "$str\n";
-
-
my $timeStr1 = strftime "%Y-%m-%d %H:%M:%S", localtime;
-
print $timeStr1."\n";
-
-
print "alert time ".&get_alert_time(-22)."\n";
-
-
print "check alert <".&get_alert($r, $w).">\n";
-
@errs = &check_alert($r, $w, 22);
-
foreach(@errs)
-
{
-
print ;
-
}
-
-
@errs = &check_logfile(&get_alert($r, $w), &get_alert_time(-22), "^ORA-|Err");
-
foreach(@errs)
-
{
-
print ;
-
}
-
-
close Reader;
-
close Writer;
- waitpid $pid, 0;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26239116/viewspace-2125606/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 運算元據庫
- 運算元據庫表
- jmeter運算元據庫JMeter
- DDL:運算元據庫
- Python運算元據庫(3)Python
- 利用 Sequelize 來運算元據庫
- java 運算元據庫備份Java
- Python學習:運算元據庫Python
- [python] 基於Dataset庫運算元據庫Python
- Android中使用LitePal運算元據庫Android
- Django在Ubuntu下運算元據庫DjangoUbuntu
- python運算元據Python
- 肖sir__jmeter之運算元據庫JMeter
- 資料庫誤運算元據恢復資料庫
- 教你如何用python運算元據庫mysql!!PythonMySql
- MySQL DML運算元據MySql
- 如何讓Designer更好地運算元據庫物件物件
- lavavel 中運算元據庫查詢別名
- spring-boot-route(九)整合JPA運算元據庫Springboot
- spring-boot-route(七)整合jdbcTemplate運算元據庫SpringbootJDBC
- spring-boot-route(八)整合mybatis運算元據庫SpringbootMyBatis
- uniapp單機軟體運算元據庫(安卓)APP安卓
- Go語言運算元據庫及其常規操作Go
- Oracle OCP(10):運算元據Oracle
- 好程式設計師分享DDL之運算元據庫程式設計師
- sql運算元據庫(2)--->DQL、資料庫備份和還原SQL資料庫
- HelloDjango 系列教程:第 04 篇:Django 遷移、運算元據庫Django
- Golang 學習系列第四天:運算元據庫 PostgreSQLGolangSQL
- Pandas 基礎 (19) - 運算元據庫 (read_sql, to_sql)SQL
- 一文快速回顧 Java 運算元據庫的方式-JDBCJavaJDBC
- 到底應該先操作快取還是先運算元據庫?快取
- Oracle delete誤運算元據恢復(BBED)Oracledelete
- 透過延時從庫+binlog複製,恢復誤運算元據
- 併發環境下,先運算元據庫還是先操作快取?快取
- 分散式高效能狀態與原子運算元據庫slock簡介分散式
- 好程式設計師Java培訓分享JDBC運算元據庫的步驟程式設計師JavaJDBC
- Spring Boot入門系列(十四)使用JdbcTemplate運算元據庫,配置多資料來源!Spring BootJDBC
- JAVA中直接用Jdbc就能運算元據庫了,為什麼還要用spring框架?JavaJDBCSpring框架
- foreach 實現 MyBatis 遍歷集合與批量運算元據MyBatis