通过ansible简单对数据库主机检查和数据库表空间的检查,前提数据库主机有统一的用户名和密码.
运维环境: rhel 7.2, ansible 1.9
数据库环境:Oracle 11.2.0.4, Suse 11sp4,统一用户是Oracle和usercs
ansible配置文件/etc/ansible/ansible.cfg,否则在运行ansible时需要制定host位置
--配置host组
[root@DBAMONITOR usercs]# more /ansible/hosts
[Suse]
192.168.15.21
192.168.15.22
192.168.15.46
192.168.15.47
--简单测试
[root@DBAMONITOR .ssh]# ansible Suse -m ping -u usercs -k
SSH password:
192.168.15.22 | success >> {
"changed": false,
"ping": "pong"
}
192.168.15.46 | success >> {
"changed": false,
"ping": "pong"
}
192.168.15.47 | success >> {
"changed": false,
"ping": "pong"
}
192.168.15.21 | success >> {
"changed": false,
"ping": "pong"
}
--查看空间
[root@DBAMONITOR .ssh]# ansible Suse -m command -a "df -hT" -u usercs -k
password:
[root@DBAMONITOR .ssh]# ansible Suse -m command -a "ls -l /tmp" -u usercs -k
password:
--复制文件:
[root@DBAMONITOR .ssh]# ansible Suse -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg" -u usercs -k
password:
{"src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=usercs group=usercs mode=0644" }
--远程执行脚本
ansible Suse -m shell -a "/tmp/rocketzhang_test.sh"
password:
--远程创建目录
ansible Suse -m command -a "mkdir /home/oracle/scripts" -u oracle -k
password:
--配置脚本
source ~/.profile
sqlplus -S "/as sysdba"<<EOF
set lin 200 pages 100
select current_scn,protection_mode,database_role,force_logging,open_mode,switchover_status from gv\$database;
set pagesize 9999
set linesize 132
select
a.tablespace_name,
a.Total_mb,
f.Free_mb,
round(a.total_MB-f.free_mb,2) Used_mb,
round((f.free_MB/a.total_MB)*100) "%_Free"
from
(select tablespace_name, sum(bytes/(1024*1024)) total_MB from dba_data_files group by tablespace_name) a,
(select tablespace_name, round(sum(bytes/(1024*1024))) free_MB from dba_free_space group by tablespace_name) f
WHERE a.tablespace_name = f.tablespace_name(+)
order by "%_Free"
/
exit
EOF
--拷贝文件
ansible Suse -m copy -a "src=/ansible/check_db.sh dest=/home/oracle/scripts" -u oracle -k
password:
--执行脚本
ansible Suse -m command -a "chmod +x /home/oracle/scripts/check_db.sh" -u oracle -k
password:
ansible Suse -m shell -a "/home/oracle/scripts/check_db.sh" -u oracle -k
password:
运维环境: rhel 7.2, ansible 1.9
数据库环境:Oracle 11.2.0.4, Suse 11sp4,统一用户是Oracle和usercs
ansible配置文件/etc/ansible/ansible.cfg,否则在运行ansible时需要制定host位置
--配置host组
[root@DBAMONITOR usercs]# more /ansible/hosts
[Suse]
192.168.15.21
192.168.15.22
192.168.15.46
192.168.15.47
--简单测试
[root@DBAMONITOR .ssh]# ansible Suse -m ping -u usercs -k
SSH password:
192.168.15.22 | success >> {
"changed": false,
"ping": "pong"
}
192.168.15.46 | success >> {
"changed": false,
"ping": "pong"
}
192.168.15.47 | success >> {
"changed": false,
"ping": "pong"
}
192.168.15.21 | success >> {
"changed": false,
"ping": "pong"
}
--查看空间
[root@DBAMONITOR .ssh]# ansible Suse -m command -a "df -hT" -u usercs -k
password:
[root@DBAMONITOR .ssh]# ansible Suse -m command -a "ls -l /tmp" -u usercs -k
password:
--复制文件:
[root@DBAMONITOR .ssh]# ansible Suse -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg" -u usercs -k
password:
{"src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=usercs group=usercs mode=0644" }
--远程执行脚本
ansible Suse -m shell -a "/tmp/rocketzhang_test.sh"
password:
--远程创建目录
ansible Suse -m command -a "mkdir /home/oracle/scripts" -u oracle -k
password:
--配置脚本
source ~/.profile
sqlplus -S "/as sysdba"<<EOF
set lin 200 pages 100
select current_scn,protection_mode,database_role,force_logging,open_mode,switchover_status from gv\$database;
set pagesize 9999
set linesize 132
select
a.tablespace_name,
a.Total_mb,
f.Free_mb,
round(a.total_MB-f.free_mb,2) Used_mb,
round((f.free_MB/a.total_MB)*100) "%_Free"
from
(select tablespace_name, sum(bytes/(1024*1024)) total_MB from dba_data_files group by tablespace_name) a,
(select tablespace_name, round(sum(bytes/(1024*1024))) free_MB from dba_free_space group by tablespace_name) f
WHERE a.tablespace_name = f.tablespace_name(+)
order by "%_Free"
/
exit
EOF
--拷贝文件
ansible Suse -m copy -a "src=/ansible/check_db.sh dest=/home/oracle/scripts" -u oracle -k
password:
--执行脚本
ansible Suse -m command -a "chmod +x /home/oracle/scripts/check_db.sh" -u oracle -k
password:
ansible Suse -m shell -a "/home/oracle/scripts/check_db.sh" -u oracle -k
password:
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/24585765/viewspace-2152097/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/24585765/viewspace-2152097/