Zabbix 通过自动发现监控Oracle表空间

Zabbix 通过自动发现监控Oracle表空间

1、得到表空间的使用情况

[root@oracle scripts]# su - oracle [oracle@oracle ~]$ more check_tablespace.sh

#!/bin/bash
# tablespace usagep check
source ~/.bash_profile
function check {
sqlplus -S "/ as sysdba" <<  EOF
set linesize 200
set pagesize 200
spool /tmp/tablespace.log
select a.tablespace_name, total, free,(total-free) as usage from 
(select tablespace_name, sum(bytes)/1024/1024 as total from dba_data_files group by tablespace_name) a, 
(select tablespace_name, sum(bytes)/1024/1024 as free from dba_free_space group by tablespace_name) b
where a.tablespace_name = b.tablespace_name;
spool off
set linesize 100
set pagesize 100
spool
select tablespace_name,autoextensible from dba_data_files;
spool off
quit
EOF
};check &>/dev/null

2、通过这个脚本我们可以得到表空间的使用情况,并记录在/tmp/tablespace.log中

[root@oracle ~]# cat /tmp/tablespace.log

TABLESPACE_NAME                     TOTAL       FREE      USAGE                                                                                                                                         
------------------------------ ---------- ---------- ----------                                                                                                                                         
SYSAUX                                640    39.9375   600.0625                                                                                                                                         
UNDOTBS1                              100      81.75      18.25                                                                                                                                         
USERS                                   5      .9375     4.0625                                                                                                                                         
SYSTEM                                690     5.3125   684.6875                                                                                                                                         
EXAMPLE                               100      21.25      78.75                                                                                                                                         
BRSADATA3                             100         98          2                                                                                                                                         
BRSADATA                              100         98          2                                                                                                                                         
BRSADATA2                             100         99          1                                                                                                                                         
BRSADATA1                             100         99          1                                                                                                                                         

9 rows selected.

3、加入定时任务
[root@oracle scripts]# crontab -u oracle -e

  */5 * * * * /u01/oracle/check_tablespace.sh

4、通过脚本取得表空间的名字,并转换成json格式的(因为zabbix的自动发现功能获取的数据类型是JSON格 式的)

[root@oracle scripts]# pwd
/etc/zabbix/scripts
[root@oracle scripts]# more discovery_oracle_tablespace.sh

#!/bin/bash
#zabbix discovery oracle tablespace
table_spaces=(`cat /tmp/tablespace.log | sed -e "1,3d" -e "/^$/d" -e "/selected/d" | awk '{print $1}'|egrep -v 'SYSAUX|UNDOTBS1|USERS|SYSTEM|EXAMPLE'`)
length=${#table_spaces[@]}
 
printf "{\n"
printf '\t'"\"data\":["
for ((i=0;i<$length;i++))
do
    printf "\n\t\t{"
    printf "\"{#TABLESPACE_NAME}\":\"${table_spaces[$i]}\"}"
    if [ $i -lt $[$length-1] ];then
        printf ","
    fi
done
    printf "\n\t]\n"
printf "}\n

5、脚本结果
[root@oracle ~]# sh /etc/zabbix/scripts/discovery_oracle_tablespace.sh

{
        "data":[
                {"{#TABLESPACE_NAME}":"BRSADATA3"},
                {"{#TABLESPACE_NAME}":"BRSADATA"},
                {"{#TABLESPACE_NAME}":"BRSADATA2"},
                {"{#TABLESPACE_NAME}":"BRSADATA1"}
        ]
}

6、创建脚本获取/tmp/tablespace.log中的最后3列

[root@oracle scripts]# more tablespace_check.sh

#!/bin/bash

# oracle tablespace check

CEHCK_TYPE=$2

TABLESPACE_NAME=$1


function usagepre {

grep "\b$TABLESPACE_NAME\b" /tmp/tablespace.log | awk '{printf "%.f\n",$4/$2*100}'

}


function available {

grep "\b$TABLESPACE_NAME\b" /tmp/tablespace.log | awk '{printf $4*1024*1024}'

}


function check {

grep "\b$TABLESPACE_NAME\b" /tmp/tablespace.log | awk '{printf $2*1024*1024}'

}


case $CEHCK_TYPE in

autopercent)

usagepre ;;

used)

available ;;

maxmb)

check ;;

*)

echo -e "Usage: $0 [TABLESPACE_NAME] [autopercent|used|maxmb] "

esac

7、增加执行权限:
[root@oracle scripts]# pwd /etc/zabbix/scripts
[root@oracle scripts]# chmod +x ./*

8、zabbixagent配置文件添加自定义监控key

[root@oracle scripts]# tail /etc/zabbix/zabbix_agentd.conf

9、重起zabbix-agent 服务
[root@oracle scripts]# service zabbix-agent restart
10、测试
[root@localhost ~]# zabbix_get -s 10.10.50.83 -k "oracle.check[BRSADATA3 maxmb]"

104857600
11、配置自动发现规则

其他2个监控项类似

12、监控结果展示

zabbix通过自动发现监控oracle表空间的一键部署脚本:

https://download.csdn.net/download/abel_dwh/10565960

猜你喜欢

转载自blog.csdn.net/abel_dwh/article/details/81222249