[Shell] Linux monitor OS process and DB session

tolilong發表於2016-03-15
1.create table
SQL> create table mon_process
  2  (ip_address varchar2(30),
  3   hostname varchar2(20),
  4   datetime varchar2(20),
  5   all_process int,
  6   running_process int,
  7   sleeping_process int,
  8   stopped_process int,
  9   zmobie_process int,
 10   all_session int,
 11   act_session int
 12  );

Table created.

2.shell script
如下指令碼的前提是在/etc/hosts配置的名字和db link上的名字是一樣的.這樣方便處理
[mon@oracle6 source]$ more mon_process 
#!/bin/bash
export ORACLE_BASE=/u01/oracle
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/dbhome_1
export ORACLE_SID=wlcspdb
datetime=`date +"%Y/%m/%d %H:%M:%S"`

STRS="oracle topaz standby wlcspdb1 wlcspdb2"

for STR in $STRS
do
   OUTPUT=`echo "top -b -n 1 | grep Tasks" | ssh -Tq mon@$STR`
   ALL_PROCESS=`echo $OUTPUT | awk '{print $2}'`
   RUNNING_PROCESS=`echo $OUTPUT | awk '{print $4}'`
   SLEEPING_PROCESS=`echo $OUTPUT | awk '{print $6}'`
   STOPPED_PROCESS=`echo $OUTPUT | awk '{print $8}'`
   ZMOBIE_PROCESS=`echo $OUTPUT | awk '{print $10}'`   

SESSION=`$ORACLE_HOME/bin/sqlplus -s /nolog << EOF
conn mon/mon
set heading off pagesize 0 feedback off verify off echo off;
select count(1),sum(decode(status,'ACTIVE',1,0)) from v\\$session@$STR;
exit
EOF`

ALL_SESSION=`echo $SESSION | awk '{print $1}'`
ACT_SESSION=`echo $SESSION | awk '{print $2}'`

IP_ADDRESS=`cat /etc/hosts | grep $STR | awk '{print $1}'`

$ORACLE_HOME/bin/sqlplus -s /nolog << EOF
conn mon/mon
insert into mon_process values('$IP_ADDRESS','$STR','$datetime','$ALL_PROCESS','$RUNNING_PROCESS','$SLEEPING_PROCESS','$STOPPED_PROCESS','$ZMOBIE_PROCESS','$ALL_SESSION','$ACT_SESSION');
commit;
exit;
EOF
done

3.job
###############mon os process and db session#####################
* * * * * /home/mon/source/mon_process >> /home/mon/log/mon_process.log

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24237320/viewspace-2057850/,如需轉載,請註明出處,否則將追究法律責任。

相關文章