数据库服务器健康检查脚本

#!/bin/bash
#author: xiaobei
#create: 2017-10-07
#parameters:none
#version:1.0
#perpose:to install database silently
#modified:none
############################################################################
excuter=`whoami`
__functin_name__=""
current_dir=`pwd`
function log_info ()
{
DATE_N=`date "+%Y-%m-%d %H:%M:%S"`
#USER_N=`whoami`
USER_N=$1
echo "${DATE_N} ${USER_N} execute $__functin_name__ [INFO] $2"
echo "${DATE_N} ${USER_N} execute $__functin_name__ [INFO] $2" >> check.log
}
function log_error ()
{
DATE_N=`date "+%Y-%m-%d %H:%M:%S"`
$USER_N=`whoami`
USER_N=$1
echo -e "\033[41;37m ${DATE_N} ${USER_N} execute $__functin_name__ [ERROR] $2 \033[0m"
echo -e "${DATE_N} ${USER_N} execute $__functin_name__ [ERROR] $2 " >> check.log
}
function fn_log () 
{
if [  $? -eq 0  ]
then
    log_info "$1" "$2 sucessed."
    echo -e "\033[32m $@ sucessed. \033[0m"
else
    log_error "$1" "$2 failed."
    echo -e "\033[41;37m $@ failed. \033[0m"
    exit 1
fi
}
#################################################################
#read config
#################################################################
function read_config ()
{ 
  __functin_name__="read_config"
  if [ -f $current_dir"/conf.ini" ]; then
       . "$current_dir/conf.ini"
    rm -f report/*
    mkdir -p report
    report_file="report/$report_file"
      sed -i "s/awr_days=[1-9]/awr_days=$awr_days/g" awrauto.sh
      sed -i "s/addm_days=[1-9]/addm_days=$addm_days/g" addmauto.sh
      sed -i "s/ash_days=[1-9]/ash_days=$ash_days/g" ashauto.sh 
      fn_log "root" "read config file"
else 
      echo "conf.ini file is not accessible,please check!"
      exit 1
fi
}
##################################################################
#write to html
##################################################################
function write2html ()
{
     echo $1 >> $report_file
}
##################################################################
#config the top func and label of html
##################################################################
function top_html ()
{
__functin_name__="top_html"
 write2html "<html xmlns="http://www.w3.org/1999/xhtml">"
 write2html "<head>"
 write2html "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />" 
 write2html "<title>service health check</title>"
 write2html "<script type="text/javascript">"
 write2html " function xlh(){" 
for t in `seq 7`
do
write2html "var tbl$t = document.getElementById(\"tbl$t\");"
write2html "var rows$t = tbl$t.rows.length;"

write2html "  for(var i=1;i<rows$t;i++){"
write2html "   if(i%2 == 0){"
write2html "      tbl$t.rows[i].style.backgroundColor = \"#FFFFCC\";"
write2html "   }"
write2html " }"
write2html "tbl$t.rows[0].style.backgroundColor = \"0066CC\";"
write2html "tbl$t.rows[0].style.font = \"Arial\";"
write2html "tbl$t.rows[0].style.color = \"white\";"
done
write2html "}"
write2html "</script>"
write2html "</head>"
write2html "<body onload=\"xlh()\">"
fn_log "root" "write the top of html report"
}
function bot_html ()
{
__functin_name__="bot_html"
write2html "</body>" 
write2html "</html>"
fn_log "root" "write the bottom of html report"
}
function basic_info ()
{
__functin_name__="basic_info"
write2html "<h style=\"color:0066CC;font:Arial;font-weight:bold\">CHUNXING service health check report</h>"
write2html "<hr />"
 write2html "<p style="font:Arial\;font-weight:bold\;color:0066CC">check info</p>"
 write2html "<table id=\"tbl1\" cellspacing="3">"
 write2html "<tr>"
 write2html "<td>name</td>"
write2html "<td>details</td>"
 write2html "</tr>" 
 write2html "<tr>"
 write2html "<td>project name</td>"
write2html "<td>$project_name</td>"
 write2html "</tr>"
 write2html "<tr>"
 write2html "<td>check date</td>"
 write2html "<td>`date "+%Y-%m-%d %H:%M:%S"`</td>"
 write2html "</tr>"
 write2html "<tr>"
 write2html "<td>check by</td>"
 write2html "<td>$author</td>"
 write2html "</tr>"
 write2html "</table>"
 fn_log "root" "output the basic infomation of checking"
}
function hard_info ()
{
__functin_name__="hard_info"
 write2html "<p style="font:Arial\;font-weight:bold\;color:0066CC">hard resource info</p>"
 write2html "<table id=\"tbl2\" cellspacing="3">"
 write2html "<tr>"
 write2html "<td>name </td>"
 write2html "<td>details</td>"
 write2html "</tr>"
 write2html "<tr>"
 write2html "<td>hostname of service </td>"
 write2html "<td>`hostname`</td>"
 write2html "</tr>"
 write2html "<tr>"
 write2html "<td>ip address</td>"
 write2html "<td>`(ifconfig eth0| grep "inet addr" | awk '{ print $2}' | awk -F: '{print $2}')`</td>"
 write2html "</tr>"
 write2html "<tr>"
 write2html "<td>operation system</td>"
 write2html "<td>`cat /etc/redhat-release`</td>"
 write2html "</tr>"
 write2html "<tr>"
 write2html "<td>cpu info</td>"
 write2html "<td>`cat /proc/cpuinfo | grep name | awk -F: '{print $2}'`</td>"
 write2html "</tr>"
 write2html "<tr>"
 write2html "<td>number of cpus</td>"
 write2html "<td>`lscpu | grep "^CPU(s):" | awk -F: '{print $2}'`</td>"
 write2html "</tr>"
 write2html "</tr>"
 write2html "<tr>"
 write2html "<td>volume of disks</td>"
 volume=`df -h | awk '{print $2}' | grep -v S | grep -v M`
 volume=`echo $volume | sed 's/ /\+/g'`
 write2html "<td>$volume</td>"
 write2html "</tr>"
 write2html "</table>"
 fn_log "root" "output the hard resource infomation of service"
}
function iptable_info ()
{
 __functin_name__="iptable_info"
 write2html "<p style="font:Arial\;font-weight:bold\;color:0066CC">iptables info</p>"
 write2html "<table id=\"tbl3\" cellspacing="3">"
 write2html "<tr>"
 write2html "<td>name</td>"
 write2html "<td>details</td>"
 write2html "</tr>"
 write2html "<tr>"
 write2html "<td>iptables status </td>"
 status=`service iptables status | awk -F: '{print $2}'`
 write2html "<td>$status</td>"
 write2html "</tr>"
 write2html "<tr>"
 write2html "<td>suggestions</td>"
 if [ "$status" = " Firewall is not running." ]
 then
     write2html "<td>start iptables</td>"
 else 
    write2html "<td></td>"
fi
 write2html "</tr>"
write2html "</table>"
fn_log "root" "output the iptables infomation"
}
function database_info()
{
__functin_name__="database_info"
 write2html "<p style="font:Arial\;font-weight:bold\;color:0066CC">database info</p>"
 verdsion_tmp=`su - oracle -c "sqlplus / as sysdba <<EOF
exit
EOF"`
conf_para=`su - oracle -c "sqlplus / as sysdba <<EOF
show parameter memory_target;
exit
EOF"`
version=`echo $conf_para | awk -F 'Production' '{print $1}' | awk -F ':' '{print $2}'`
 write2html "<table id=\"tbl4\" cellspacing="3">"
  write2html "<tr>"
 write2html "<td>name </td>"
 write2html "<td>details</td>"
 write2html "</tr>"
 write2html "<tr>"
 write2html "<td>database version </td>"
 write2html "<td>$version</td>"
 write2html "</tr>"
 write2html "<tr>"
 write2html "<td>memory target</td>"
memory_target_temp=${conf_para#*memory_target}
memory_target=`echo ${memory_target_temp%SQL*} | awk '{print $NF}'`
 write2html "<td>$memory_target</td>"
 write2html "</tr>"
conf_para=`su - oracle -c "sqlplus / as sysdba <<EOF
show parameter memory_max_target;
exit
EOF"`
memory_target_temp=${conf_para#*memory_max_target}
memory_max_target=`echo ${memory_target_temp%SQL*} | awk '{print $NF}'`
 write2html "<tr>"
 write2html "<td>memory max target</td>"
 write2html "<td>$memory_max_target</td>"
 write2html "</tr>"
conf_para=`su - oracle -c "sqlplus / as sysdba <<EOF
show parameter pga_aggregate_target;
exit
EOF"`
memory_target_temp=${conf_para#*pga_aggregate_target}
memory_max_target=`echo ${memory_target_temp%SQL*} | awk '{print $NF}'`
 write2html "<tr>"
 write2html "<td>pga aggregate target</td>"
 write2html "<td>$memory_max_target</td>"
 write2html "</tr>"
conf_para=`su - oracle -c "sqlplus / as sysdba <<EOF
show parameter sga_target;
exit
EOF"`
memory_target_temp=${conf_para#*sga_target}
memory_max_target=`echo ${memory_target_temp%SQL*} | awk '{print $NF}'`
 write2html "<tr>"
 write2html "<td>sga target</td>"
 write2html "<td>$memory_max_target</td>"
 write2html "</tr>"
data_para=`su - oracle -c "sqlplus / as sysdba <<EOF
select * from nls_database_parameters;
exit
EOF"`
NLS_CHARACTERSET=`echo ${data_para#*NLS_CHARACTERSET}`
NLS_CHARACTERSET=`echo ${NLS_CHARACTERSET%NLS_ISO_CURRENCY*}`
 write2html "<tr>"
 write2html "<td>characterset</td>"
 write2html "<td>$NLS_CHARACTERSET</td>"
 write2html "</tr>"
NLS_LANGUAGE=`echo ${data_para#*NLS_LANGUAGE}`
NLS_LANGUAGE=`echo ${data_para#*NLS_LANGUAGE} | awk '{print $1}'`
 write2html "<tr>"
 write2html "<td>nls language</td>"
 write2html "<td>$NLS_LANGUAGE</td>"
 write2html "</tr>"

proc_para=`su - oracle -c "sqlplus / as sysdba <<EOF
@$current_dir"/exesql.sql"
exit
EOF"`
proc=`echo $proc_para | awk -F 'DATAFILE' '{print $1}' | awk '{print $NF}'`
 write2html "<tr>"
 write2html "<td>process</td>"
 write2html "<td>$proc</td>"
 write2html "</tr>"
 status=`su - oracle -c "lsnrctl status"`
 status=`echo ${status#*'Services Summary...'}`
 status=`echo ${status%'The command completed successfully'*}`
 write2html "<tr>"
 write2html "<td>database status</td>"
 write2html "<td>$status</td>"
 write2html "</tr>"
 temp=`echo ${proc_para%CONTROLFILE*}`
 data_file=`echo ${temp#*DATAFILE} | sed 's/ /\r\n/g' | sed 's/-//g'`
 write2html "<tr>"
 write2html "<td>data file</td>"
 write2html "<td>$data_file</td>"
 write2html "</tr>"
 temp=`echo ${proc_para%LOGFILE*}`
 control_file=`echo ${temp#*CONTROLFILE} | sed 's/ /\r\n/g' | sed 's/-//g'`
 write2html "<tr>"
 write2html "<td>control file</td>"
 write2html "<td>$control_file</td>"
 write2html "</tr>"
temp=`echo ${proc_para#*LOGFILE}`
log_file=`echo ${temp%SQL>*} | sed 's/ /\r\n/g' | sed 's/-//g'`
 write2html "<tr>"
 write2html "<td>log file</td>"
 write2html "<td>$log_file</td>"
 write2html "</tr>"
write2html "</table>"
fn_log "root" "output database parameter infomation"
}
function task_info()
{
__functin_name__="task_info"
task_info=`su - oracle -c "crontab -l"`
 write2html "<p style="font:Arial\;font-weight:bold\;color:0066CC">task info</p>"
 write2html "<table id=\"tbl5\" cellspacing="3">"
 write2html "<tr>"
 write2html "<td>user</td>"
 write2html "<td>task_info</td>"
 write2html "</tr>" 
 write2html "<tr>"
 write2html "<td>oracle</td>"
 write2html "<td>$task_info</td>"
 write2html "</tr>"
 write2html "</table>"
 fn_log "root" "output timing infomation"
}
function report_info()
{
__functin_name__="report_info"
awr_info=`su - oracle -c "sh $current_dir/awrauto.sh"`
fn_log "oracle" "produce awr report"
mv /home/oracle/awrrpt.html $current_dir/report/
 write2html "<p style="font:Arial\;font-weight:bold\;color:0066CC">report info</p>"
 write2html "<table id=\"tbl6\" cellspacing="3">"
 write2html "<tr>"
 write2html "<td>report_name</td>"
 write2html "<td>report_link</td>"
 write2html "</tr>"
 write2html "<tr>"
 write2html "<td>awr</td>"
 write2html "<td><a href="awrrpt.html">awr report link</a></td>"
 write2html "</tr>"

ash_info=`su - oracle -c "sh $current_dir/ashauto.sh"`
fn_log "oracle" "produce ash report"
mv /home/oracle/ashrpt.html $current_dir/report/
 write2html "<tr>"
 write2html "<td>ash</td>"
 write2html "<td><a href="ashrpt.html">ash report link</a></td>"
 write2html "</tr>"

addm_info=`su - oracle -c "sh $current_dir/addmauto.sh"`
fn_log "oracle" "produce addm report"
mv /home/oracle/addm.txt $current_dir/report
 write2html "<tr>"
 write2html "<td>addm</td>"
 write2html "<td><a href="addm.txt">addm report link</a></td>"
 write2html "</tr>"

awrinfo_info=`su - oracle -c "sh $current_dir/awrinfoauto.sh"`
mv /home/oracle/awrinfo.txt $current_dir/report
fn_log "oracle" "produce awrinfo report"
 write2html "<tr>"
 write2html "<td>awrinfo</td>"
 write2html "<td><a href="awrinfo.txt">awrinfo report link</a></td>"
 write2html "</tr>"
 write2html "</table>"
 fn_log "oracle" "produce report info"
}
function manual_sent()
{
__functin_name__="manual_sent"
 write2html "<p style="font:Arial\;font-weight:bold\;color:0066CC">manual execute command</p>"
 write2html "<table id=\"tbl7\" cellspacing="3">"
  write2html "<tr>"
 write2html "<td>check column</td>"
 write2html "<td>sql sentence</a></td>"
 write2html "</tr>"
 redo_size="select group# ,bytes/1024/1024 size_m,status,archived from v\$log;"
 write2html "<tr>"
 write2html "<td>check redo size</td>"
 write2html "<td>$redo_size</td>"
 write2html "</tr>"
 tablespace_used_rate="select tablespace_name \"tablespace_name\",to_char(round(maxbytes / 1024, 2), '99990.00') || 'g' \"size(g)\",to_char(round(used / 1024, 2), '99990.00') || 'g' \"used(g)\",to_char(round((maxbytes - used) / 1024, 2), '99990.00') || 'g' \"avail(g)\",to_char(round( used / maxbytes /0.0001) / 100, '99990.00') || '%' \"used%\" from (select a.tablespace_name tablespace_name,floor(a.bytes / 1024 / 1024) bytes,floor(b.free / 1024 / 1024) free, floor((a.bytes - b.free) / 1024 / 1024) used, floor(a.maxbytes / 1024 / 1024) maxbytes from (select tablespace_name tablespace_name, sum(bytes) bytes, sum(maxbytes) maxbytes from dba_data_files group by tablespace_name) a, (select tablespace_name tablespace_name, sum(bytes) free from dba_free_space group by tablespace_name) b where a.tablespace_name = b.tablespace_name ) order by floor( used / maxbytes/0.00001) desc;"
 write2html "<tr>"
 write2html "<td>check tablespace used rate</td>"
 write2html "<td>$tablespace_used_rate</td>"
 write2html "</tr>"
 index_table_consistency="select t.owner,t.index_name,t.table_owner,t.table_name from dba_indexes t  where t.owner <> t.table_owner;"
 write2html "<tr>"
 write2html "<td>check consistency between index and table</td>"
 write2html "<td>$index_table_consistency</td>"
 write2html "</tr>"
extent_sum_too_much="select owner,segment_name,segment_type,tablespace_name bytes_g,extents  from (select t.owner,t.segment_name,t.segment_type, t.tablespace_name,round(t.bytes/1024/1024/1024,2) bytes_g,t.extents from dba_segments t order by t.extents desc) where rownum<21;"
 write2html "<tr>"
 write2html "<td>check extent sum</td>"
 write2html "<td>$extent_sum_too_much</td>"
 write2html "</tr>"
close_to_max_extent_object="select t.owner,t.segment_name,t.segment_type,t.tablespace_name,t.next_extent from dba_segments t where t.extents = t.max_extents-10;"
 write2html "<tr>"
 write2html "<td>check object close to max extent</td>"
 write2html "<td>$close_to_max_extent_object</td>"
 write2html "</tr>"
 auto_extent_switch="select file_id,file_name,tablespace_name,autoextensible from dba_data_files;"
 write2html "<tr>"
 write2html "<td>check auto extent switch</td>"
 write2html "<td>$auto_extent_switch</td>"
 write2html "</tr>"
exceed_next_extent="select s.owner,s.segment_name,s.segment_type,s.tablespace_name, s.next_extent from dba_segments s where s.next_extent > (select max(f.bytes) from dba_free_space f where f.tablespace_name = s.tablespace_name) order by s.owner;"
 write2html "<tr>"
 write2html "<td>check tablespace almost exceed next extent</td>"
 write2html "<td>$exceed_next_extent</td>"
 write2html "</tr>"
statistics_info="select table_name,num_rows,sample_percent, last_analyzed from (select table_name,num_rows,round(sample_size/decode(num_rows,0,1,num_rows)*100,0)||'%' sample_percent, to_char(last_analyzed, 'yyyy-mm-dd hh24:mi:ss') last_analyzed from dba_tables where owner like $db_user order by num_rows desc) where rownum < 31;"
 write2html "<tr>"
 write2html "<td>check table statistical info</td>"
 write2html "<td>$statistics_info</td>"
 write2html "</tr>"
 write2html "</table>"
  fn_log "root" "manual execute sqls"
}
read_config
top_html
basic_info
hard_info
iptable_info
database_info
task_info
report_info
manual_sent
bot_html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值