Purpose: One thing I need to do recently is to query the business connection volume in postgres DB. If it is less than a certain threshold, send an SMS alert.
Current situation: Our nagios is on a separate server, and the business DB is on another DB
Idea: Very simple, business DB custom shell script, nagios calls it regularly
Steps: 1 Write the shell script check_con.sh on the DB server
~/nagios/libexec
#! /bin/sh
COUNT_C=34
su - postgres -c "psql XXdb > ~/db_output.txt <<-EOF
select count(distinct tnm)
from my_bussiness_table
where register_datetime between now() - interval '2 hour' and now();
"
COUNT_C=`sed -n '3,3p' ~/db_output.txt`
if [ $COUNT_C -le $2 ];then
echo "Critical $COUNT_C"
exit 2
elif [ $COUNT_C -le $4 ];then
echo "Warning $COUNT_C"
exit 1
elif [ $COUNT_C -gt $4 ];then
echo "OK $COUNT_C"
exit 0
else
echo "UNKNOWN $COUNT_C"
exit 3
fi
Then modify the nrpe.cfg file under ~/nagios/etc
command[check_connect]=~/nagios/libexec/check_con.sh $ARG1$
The first step is completed
The second part logs in to the Nagis server to modify the configuration.
1 Modify the commands.cfg file under /nagios/etc/objects
define command{
command_name check-connect
command_line $USER1$/check_connect -w $ARG1$ -c $ARG2$ -p $ARG3$
}
2Modify server.cfg
define service{
use XXXX-service
host_name DB
service_description [proc] CONNECT
check_command check_nrpe!check_connect -a '-c 0 -w 1 -p db'
normal_check_interval 30
}
Finally restart the service and the job is done.
sudo systemctl restart nagios.service
sudo systemctl restart nrpe.service