server_patrol.sh
#!/bin/bash #!/usr/bin/expect -f
#! auther by wangxp #定义一个变量 LANG="zh_CN.UTF-8" PUB_KEY_FILE=$HOME/.ssh/id_rsa.pub TITLE="XXXXX巡检报告 `date +%F`" TXT_FILE_NAME="xjbg_`date +%y%m%d%H%M%S`.txt" TXT_DIR="`pwd`/report" TXT_NAME="${TXT_DIR}/${TXT_FILE_NAME}" LANG="zh_CN.UTF-8" #定义应用服务器,用空格分开。app_server是服务器ip地址;app_server_user是服务器用户名;app_server_pwd是服务器用户对应密码 app_server=(192.168.0.1 192.168.0.2 192.168.0.3) app_server_user=(user1 user2 user3) app_server_pwd=(pwd1 pwd2 pwd3) #定义数据库服务器,用空格分开,db_server是数据库ip地址;db_server_user是oracle服务器用户名;db_server_pwd是oracle服务器用户对应密码;db_oracle_link是oracle数据库链接的dblik(用户名/密码@服务器ip:1521/实例名) db_server=(192.168.1.1 192.168.1.2) db_server_user=(oracle oracle) db_server_pwd=(dbpwd1 dbpwd2) db_oracle_link=(dblink1 dblink2) # 自动生成密钥对函数 auto_keygen (){ /usr/bin/expect<<EOF # 设置超时时间,单位为秒 set timeout 3 # 触发交互命令 spawn ssh-keygen # 开始捕获 expect { # 当捕获到此次字符串时, 发送回车,并且继续捕获 ".ssh/id_rsa)" { send "\n"; exp_continue } "no passphrase):" { send "\n"; exp_continue} "again:" { send "\n"; exp_continue } } EOF } ## 复制公钥到远端的函数 send_key () { /usr/bin/expect <<EOF set timeout 30 spawn ssh-copy-id $2@$1 expect { "yes/no" { send "yes\n"; exp_continue } "password:" { send "$3\n"} } expect eof EOF }
#免密码登录的函数 ssh_server () { /usr/bin/expect <<EOF | sed -n '/password:/,$p'|grep -v 'password:'|grep -v '未找到命令' >> ${TXT_NAME} set timeout 10 spawn ssh $2@$1 $4 expect { "*yes/no*" { send "yes\r";exp_continue} "*password*" { send "$3\r"} } expect eof EOF } # 假如公钥文件不存在,说明需要创建密钥对 if [ ! -f ${PUB_KEY_FILE} ];then auto_keygen fi # 假如报告文件夹不存在,就创建报告文件夹 if [ ! -d "${TXT_DIR}" ];then mkdir "${TXT_DIR}" fi
#ip地址数量校验 if [ ${#app_server[@]} != ${#app_server_user[@]} ];then echo "应用服务器ip地址数和用户数量不匹配!" exit 0 elif [ ${#app_server[@]} != ${#app_server_pwd[@]} ];then echo "应用服务器ip地址数和用户密码数量不匹配!" exit 0 elif [ ${#db_server[@]} != ${#db_server_pwd[@]} ];then echo "服务库服务器ip地址数和用户密码数量不匹配!" exit 0 elif [ ${#db_server[@]} != ${#db_server_pwd[@]} ];then echo "数据库服务器ip地址数和用户密码数量不匹配!" exit 0 elif [ ${#db_server[@]} != ${#db_oracle_link[@]} ];then echo "数据库服务器ip地址数和oracle数据库链接数量不匹配!" exit 0 fi echo ${TITLE} echo ${TITLE}>${TXT_NAME} echo "数据库服务器数: ${#db_server[@]}" #生成数据库检查报告 for (( i=0; i<${#db_server[@]}; i++)) do echo "生成数据库服务器${db_server[$i]}的检查报告..." #ssh_server ${db_server[$i]} ${db_server_user[$i]} ${db_server_pwd[$i]} "bash -s" < /home/oracle/test/a.sh > diskusage.txt echo -e "\n================================================================================" >>${TXT_NAME} echo "1、数据库服务器IP: ${db_server[$i]}" >>${TXT_NAME} echo "2、巡检时间: `date "+%F %T 星期%a"`" >>${TXT_NAME} echo -e "3、服务器内核信息: \c">>${TXT_NAME} ssh_server ${db_server[$i]} ${db_server_user[$i]} ${db_server_pwd[$i]} "uname -r" echo -e "4、服务器主机名: \c" >>${TXT_NAME} ssh_server ${db_server[$i]} ${db_server_user[$i]} ${db_server_pwd[$i]} "hostname" echo "5、数据库服务器状态:" >>${TXT_NAME} sqlplus -S -L ${db_oracle_link[$i]} << EOF| sed -n '/INSTANCE_NAME/,$p' >>${TXT_NAME} select instance_name,status,(select sysdate from dual) as DB_TIME from v\$instance; EOF echo "6、磁盘使用详情:" >>${TXT_NAME} ssh_server ${db_server[$i]} ${db_server_user[$i]} ${db_server_pwd[$i]} "df -hT" echo "7、数据库表空间详情:" >>${TXT_NAME} sqlplus -S -L ${db_oracle_link[$i]} << EOF |sed -n '/TABLESPACE_NAME/,$p'>> ${TXT_NAME} set LINESIZE 200; set PAGESIZE 100; col "TABLESPACE_NAME" for a20; col "MAX_SIZE(G)" for a15; col "USEAGE_SIZE(G)" for a15; col "FREE_SIZE(G)" for a15; col "USEAGE_RATE(%)" for a15; SELECT a.tablespace_name as "TABLESPACE_NAME", DECODE(TRUNC(total),0,REPLACE(TO_CHAR(total),'.', '0.'),TO_CHAR(total)) as "MAX_SIZE(G)", DECODE(TRUNC(total - free),0,REPLACE(TO_CHAR(total - free),'.', '0.'),TO_CHAR(total - free)) as "USEAGE_SIZE(G)", DECODE(TRUNC(free),0,REPLACE(TO_CHAR(free),'.', '0.'),TO_CHAR(free)) as "FREE_SIZE(G)", DECODE(TRUNC(round((total-free)/total,6)*100),0,REPLACE(TO_CHAR(round((total-free)/total,6)*100),'.', '0.'),TO_CHAR(round((total-free)/total,6)*100)) AS "USEAGE_RATE(%)" FROM (SELECT tablespace_name,ROUND(SUM(bytes)/(1024*1024*1024),6) free FROM dba_free_space GROUP BY tablespace_name) a, (SELECT tablespace_name, ROUND(SUM(bytes)/(1024*1024*1024),6) total FROM dba_data_files GROUP BY tablespace_name) b WHERE a.tablespace_name = b.tablespace_name order by a.tablespace_name desc; EOF echo -e "8、Crontab定时任务配置: \n`crontab -l`\n" >> ${TXT_NAME} echo "9、备份文件详情: " >> ${TXT_NAME} ssh_server ${db_server[$i]} ${db_server_user[$i]} ${db_server_pwd[$i]} "ls -lhrt /backup"|sed -n '/password:/,$p'|grep -v "password:" |sed -e 's/ $//g' >> ${TXT_NAME} #--------------生成图片报告------------------- #paps --font=9 ${TXT_NAME} > ${FILE_NAME}.ps #ps2pdf ${FILE_NAME}.ps ${FILE_NAME}.pdf #convert -background white -extent 600x900 ${FILE_NAME}.pdf ${FILE_NAME}.png done #生成应用服务器检查报告 echo "应用服务器数: ${#app_server[@]}" for (( i=0; i<${#app_server[@]}; i++)) do echo "生成应用服务器 ${app_server[$i]}的检查报告..." echo -e "\n\n================================================================================" >> ${TXT_NAME} echo "1、应用服务器IP: ${app_server[$i]}" >>${TXT_NAME} echo "2、巡检时间: `date "+%F %T 星期%a"`" >>${TXT_NAME} echo -e "3、服务器内核信息: \c">>${TXT_NAME} ssh_server ${app_server[$i]} ${app_server_user[$i]} ${app_server_pwd[$i]} "uname -r" echo -e "4、服务器主机名: \c" >>${TXT_NAME} ssh_server ${app_server[$i]} ${app_server_user[$i]} ${app_server_pwd[$i]} "hostname" echo "5、磁盘使用详情:" >>${TXT_NAME} ssh_server ${app_server[$i]} ${app_server_user[$i]} ${app_server_pwd[$i]} "df -hT" done #去掉多余^m符号(^M必须按住ctrl 然后再按V和M打印出才有效) sed -i 's///g' ${TXT_NAME}
标签:echo,patrol,NAME,app,db,server,sh,TXT From: https://www.cnblogs.com/wxp100/p/17844233.html