主脚本 :
/home/oracle/job_nagios.sh
#!/bin/bash
. /home/oracle/.bash_profile
export LANG=en_US.utf8
export NLS_DATE_FORMAT='yyyy-mm-dd hh24:mi:ss'
export NLS_LANG=AMERICAN_AMERICA.UTF8
export LANG=en_US.utf8
# ENV
. /home/oracle/env.sh
LOG_FILE=/home/oracle/job_nagios_$1.log
TEMP_FILE=/tmp/job_nagios_$1.tmp
ORA_USER="用户略"
ORA_PWD=\"密码略\"
# 用户需要有select sys.dba_jobs的权限
RETVAL=0
f_mon()
{
V_USER=$1
V_PWD=$2
V_TNS=$3
V_ALIAS=$4
JOBS_FAILURE=`echo -e "set heading off;\n
set pagesize 0;\n
set verify off;\n
set echo off;\n
set feedback off;\n
set linesize 120;\n
set trimspool on;\n
set termout off;\n
column schema_user format a32;\n
column broken format a6;\n
column what format a30;\n
select to_char(job) jobid,schema_user,broken,to_char(failures) failures,what from sys.dba_jobs where failures>0 or broken='Y';\n
quit;\n"|sqlplus -s $V_USER/$V_PWD@$V_TNS|col -x`
LINE_JOB=`echo -e $JOBS_FAILURE|grep -v "^$"|wc -l`
if [ $LINE_JOB -ge 1 ]; then
RETVAL=1
echo -e "`date +%F\ %T`\n$V_ALIAS\n$V_TNS\nJOBS_FAILURE : \njobid schema_user broken failures what \n$JOBS_FAILURE\n\n" >> $LOG_FILE
echo -e "`date +%F\ %T`\n$V_ALIAS\n$V_TNS\nJOBS_FAILURE : \njobid schema_user broken failures what \n$JOBS_FAILURE\n\n" > $TEMP_FILE
grep -E "N\ +16|Y\ +16" $TEMP_FILE
if [ $? -eq 0 ]; then
RETVAL=2
fi
fi
}
eval str="$"$1
f_mon $ORA_USER $ORA_PWD $str $1
if [ $LINE_JOB -ge 1 ]; then
cat $TEMP_FILE
fi
exit $RETVAL
环境变量文件env.sh
TNS_DIGOAL="//10.1.1.10:1521/digoal"
nagios 配置 :
假设oracle为监控用户, 默认是nagios
vi /etc/xinetd.d/nrpe 修改
user = oracle
group = oinstall
vi /usr/local/nagios/etc/nrpe.cfg
command[check_db_job1]=/home/oracle/job_nagios.sh TNS_DIGOAL
重启xinetd服务
时间: 2024-12-06 11:12:52