前期工作:建立表與序列。以及注釋
後期工作:個人借用了crontab來進行定時任務的觸發随機腳本
create table TB_SERVICE_MONITOR
(
id NUMBER,
server_name VARCHAR2(20),
time VARCHAR2(30)
)
comment on column TB_SERVICE_MONITOR.id
is 'ID';
comment on column TB_SERVICE_MONITOR.server_name
is '應用名稱';
comment on column TB_SERVICE_MONITOR.time
is '通路時間';
create sequence SERVER_MONI_SEQ
minvalue 1
maxvalue 999999999999999999999999999
start with 1
increment by 1
cache 20;
#! /bin/bash
##--插入的sql, 'bszn'為server_name名稱
source .bash_profile
--引入oracle的環境變量,否則腳本在crontab中執行時會報錯,但手動執行卻是正常的,故需要添加
declare -i rnum=$RANDOM*8/32767
--declare -i 聲明一個整數數值類型,rnum為變量。在32767為在0-32767中間随機生成一個數值(确定了一個大的取值範圍,個人了解),$RANDOM為随機函數,*8為在0-7之間随機産生數值(從大的範圍中,再一次縮小範圍)。
if [ "$rnum" -eq "1" ];then --數值之間的比較用英文字母的eq,lt等,字元串用=,!=之類的
/oracle/product/10g/bin/sqlplus hr/[email protected]:1521/[email protected]:1521/orcl 1>sql.log 2>&1 <
--sqlplus的遠端登入方式,以及sqlplus在oracle伺服器中的絕對路徑,可以通過find /$oracle_home -name sqlplus查得
insert into tb_service_monitor values(SERVER_MONI_SEQ.nextval,'bszn',to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'));
commit;
exit;
EOF --此處必須頂頭寫,否則linux shell認為沒有EOF結束标簽,隻有<
else
if [ "$rnum" -eq "2" ];then
/oracle/product/10g/bin/sqlplus hr/[email protected]:1521/orcl 1>sql.log 2>&1 <
insert into tb_service_monitor values(SERVER_MONI_SEQ.nextval,'tqyb',to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'))
commit;
exit;
EOF
else
if [ "$rnum" -eq "3" ];then
/oracle/product/10g/bin/sqlplus hr/[email protected]:1521/orcl 1>sql.log 2>&1 <
insert into tb_service_monitor values(SERVER_MONI_SEQ.nextval,'wdzb',to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'));
commit;
exit;
EOF
else
if [ "$rnum" -eq "4" ];then
/oracle/product/10g/bin/sqlplus hr/[email protected]:1521/orcl 1>sql.log 2>&1 <
insert into tb_service_monitor values(SERVER_MONI_SEQ.nextval,'gsgg',to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'));
commit;
exit;
EOF
else
if [ "$rnum" -eq "5" ];then
/oracle/product/10g/bin/sqlplus hr/[email protected]:1521/orcl 1>sql.log 2>&1 <
insert into tb_service_monitor values(SERVER_MONI_SEQ.nextval,'cydh',to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'));
commit;
exit;
EOF
else
if [ "$rnum" -eq "6" ];then
/oracle/product/10g/bin/sqlplus hr/[email protected]:1521/orcl 1>sql.log 2>&1 <
insert into tb_service_monitor values(SERVER_MONI_SEQ.nextval,'cxgjj',to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'));
commit;
exit;
EOF
else
if [ "$rnum" -eq "7" ];then
/oracle/product/10g/bin/sqlplus hr/[email protected]:1521/orcl 1>sql.log 2>&1 <
insert into tb_service_monitor values(SERVER_MONI_SEQ.nextval,'fdjs',to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'));
commit;
exit;
EOF
fi
fi
fi
fi
fi
fi
fi