Zabbix - 添加自定义监控

可以使用shell,python等

1. 创建通过输出传递结果值。

1.1 监控脚本

1.1.1 echo / print

vim test_count_echo.sh

---------------------------------------------------------------------------

PSQL=/usr/local/pgsql/bin/psql

DBNAME=$1

result=`$PSQL -U dbadmin -d $DBNAME -Aqt -c "select count(*) from test01;"`

echo $result

---------------------------------------------------------------------------

 

1.2. 配置文件

# cd /etc/zabbix/zabbix_agentd.d

# vim userparameter_test.conf

UserParameter=test_echo.get.count[*],/data/zabbix_scripts/test_count_echo.sh "$1"

 

修改之后要重启

# service zabbix-agent restart

 

测试

# zabbix_get -s 192.168.3.51 -p10050 -k"test_echo.get.count[db1]"

3

 

1.3. 添加监控项

可以将监控项添加至普通的item或Discovery中的item。

添加至Dsicovery中 Item prototypes

Name: [{#DBNAME}] test_count

Key: test_echo.get.count[{#DBNAME}]

Applications: 可选存在的或新建

 

2. 通过zabbix_sender发送结果值,可以包含多个信息。

这个通过pg_monz中的自动发现数据库名称列表来做实验。

2.1 监控脚本

vim test_count_sender.sh

---------------------------------------------------------------------------

#!/bin/bash

APP_NAME="$1"

PGSHELL_CONFDIR="$2"

HOST_NAME="$3"

ZABBIX_AGENTD_CONF="$4"

DBNAME="$5"

TIMESTAMP_QUERY='extract(epoch from now())::int'

# Load the psql connection option parameters.

source $PGSHELL_CONFDIR/pgsql_funcs.conf

 

case "$APP_NAME" in

test_sender)

sending_data=$(psql -A --field-separator=' ' -t -h $PGHOST -p $PGPORT -U $PGROLE $DBNAME -c \

"select '\"$HOST_NAME\"', 'psql.test01[$DBNAME]', $TIMESTAMP_QUERY, (select count(*) from test01) \

union all \

select '\"$HOST_NAME\"', 'psql.test02[$DBNAME]', $TIMESTAMP_QUERY, (select count(*) from test02) "

)

;;

*)

echo "'$APP_NAME' did not match anything."

exit

;;

esac

 

if [ $? -ne 0 ]; then

echo "$sending_data"

exit

fi

#echo $sending_data

 

result=$(echo "$sending_data" | zabbix_sender -c $ZABBIX_AGENTD_CONF -v -T -i - 2>&1)

response=$(echo "$result" | awk -F ';' '$1 ~ /^info/ && match($1,/[0-9].*$/) {sum+=substr($1,RSTART,RLENGTH)} END {print sum}')

if [ -n "$response" ]; then

echo "$response"

else

echo "$result"

fi

---------------------------------------------------------------------------

 

2.2 配置文件

# cd /etc/zabbix/zabbix_agentd.d

# vim userparameter_test.conf

UserParameter=test_sender[*],/data/zabbix_scripts/test_count_sender.sh test_sender "$2" "$3" "$4" "$5"

修改之后要重启

# service zabbix-agent restart

 

测试

客户端测试:

./test_count_sender.sh test_sender /usr/local/etc/ dbw21as.daodao.com /etc/zabbix/zabbix_agentd.conf db2

2

输出发了几行内容到服务端

 

服务端测试

# zabbix_get -s 192.168.3.51 -p10050 -k"test_sender.[test_sender,/usr/local/etc,dbw21as.daodao.com,/etc/zabbix/zabbix_agentd.conf,db1]"

2

 

2.3 添加监控项

可以将监控项添加至普通的item或Discovery中的item。

2.3.1 建立中间级item

当添加多个返回值时,需要先建一个中转的item。

建 test_sender ,

Name: [{#DBNAME}] test_count

Key: test_sender[{$PGSCRIPTDIR},{$PGSCRIPT_CONFDIR},{HOST.HOST},{$ZABBIX_AGENTD_CONF},{#DBNAME}]

Applications: test_sender

 

2.3.2 建立监控项中的返回值item

添加至Dsicovery中 Item prototypes。

key中的值为返回值的

 

Name: [{#DBNAME}] test_count sender test01

Key: psql.test01[{#DBNAME}]

Applications: test_sender

 

Name: [{#DBNAME}] test_count sender test02

Key: psql.test02[{#DBNAME}]

Applications: test_sender

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值