Zabbix autodiscover (LLD)

Zabbix autodiscover (LLD)

LLD : Low-level discovery

Official documents: https://www.zabbix.com/documentation/4.0/manual/discovery/low_level_discovery

Role: rule s can be specified to achieve automatic configuration generation of uncertain number of monitoring items

For custom LLD rules, please refer to the Creating custom LLD rules section in the official website document

For example, if zabbix is used to realize the usage of CPU and MEM of server process, LLD is more suitable

Practical example


As shown in the figure above, the Server CPU all chart and the number of service processes can only be determined after they are opened. Next time, it may be different

Next, use LLD to implement the above chart

1. Write a script to get the service name

for example kgetserver.sh :

#!/bin/bash
echo '{"data":['
n0=`ps -aux | grep Server | grep -v grep | grep -v $0 | grep -v kgetcpu | grep -v kgetmem | grep -v tail | wc -l`
ps -aux | grep Server | grep -v grep | grep -v $0 | grep -v kgetcpu | grep -v kgetmem | grep -v tail | awk -v n=$n0 '{printf "{\"{#PROCESSNAME}\":\"\\\"";for(i=11;i<=NF;i++){printf $i;if(i<NF)printf " "};printf "\\\"\"}";if(NR<n)printf ",";printf "\n"}'
echo ']}'

Executive output:

{"data":[
{"{#PROCESSNAME}":"\"./MgrServer.dbg\""},
{"{#PROCESSNAME}":"\"./LogServer_Ex.dbg\""},
{"{#PROCESSNAME}":"\"./RecordServer_Ex.dbg --stderrthreshold 0 --log_dir ../log -s 1\""},
{"{#PROCESSNAME}":"\"./RecordServer_Ex.dbg --stderrthreshold 0 --log_dir ../log -s 2\""},
{"{#PROCESSNAME}":"\"./RecordServer_Ex.dbg --stderrthreshold 0 --log_dir ../log -s 3\""},
{"{#PROCESSNAME}":"\"./RecordServer_Ex.dbg --stderrthreshold 0 --log_dir ../log -s 4\""},
{"{#PROCESSNAME}":"\"./RecordServer_Ex.dbg --stderrthreshold 0 --log_dir ../log -s 5\""},
{"{#PROCESSNAME}":"\"./RecordServer_Ex.dbg --stderrthreshold 0 --log_dir ../log -s 6\""},
{"{#PROCESSNAME}":"\"./ProxyServer_Ex.dbg\""},
{"{#PROCESSNAME}":"\"./RedisSyncServer_Ex.dbg -s 1\""},
{"{#PROCESSNAME}":"\"./RedisSyncServer_Ex.dbg -s 2\""},
{"{#PROCESSNAME}":"\"./RedisSyncServer_Ex.dbg -s 3\""},
{"{#PROCESSNAME}":"\"./RedisSyncServer_Ex.dbg -s 4\""},
{"{#PROCESSNAME}":"\"./RedisSyncServer_Ex.dbg -s 5\""},
{"{#PROCESSNAME}":"\"./RedisSyncServer_Ex.dbg -s 6\""},
{"{#PROCESSNAME}":"\"./RobotServer_Ex.dbg\""},
{"{#PROCESSNAME}":"\"./LoginServer.dbg --stderrthreshold 0 --log_dir ../log -s 1\""},
{"{#PROCESSNAME}":"\"./LoginServer.dbg --stderrthreshold 0 --log_dir ../log -s 2\""},
{"{#PROCESSNAME}":"\"./LoginServer.dbg --stderrthreshold 0 --log_dir ../log -s 3\""}
]}

This script is rule, through which you can find the service items to be monitored

Another example kgetproc.sh :

#!/bin/bash
echo '{"data":['
n0=`ps -aux | grep $1 | grep -v grep | grep -v $0 | grep -v kgetcpu | grep -v kgetmem | grep -v tail | grep -v defunct | wc -l`
ps -aux | grep $1 | grep -v grep | grep -v $0 | grep -v kgetcpu | grep -v kgetmem | grep -v tail | grep -v defunct | awk -v n=$n0 '{printf "{\"{#PROCESSNAME}\":\"\\\"";for(i=11;i<=NF;i++){printf $i;if(i<NF)printf " "};printf "\\\"\", \"{#PROCESSPID}\":";printf $2;printf ",\"{#PROCESSNO}\":"; printf NR; printf "}";if(NR<n)printf ",";printf "\n"}'
echo ']}'

Executive output:

[root@host-192-168-21-36 opt]# ./kgetproc.sh codis-server
{"data":[
{"{#PROCESSNAME}":"\"/home/fananchong/go/src/github.com/CodisLabs/codis/admin/../bin/codis-server 127.0.0.1:23790\"", "{#PROCESSPID}":28381,"{#PROCESSNO}":1},
{"{#PROCESSNAME}":"\"/home/fananchong/go/src/github.com/CodisLabs/codis/admin/../bin/codis-server 127.0.0.1:23791\"", "{#PROCESSPID}":28486,"{#PROCESSNO}":2},
{"{#PROCESSNAME}":"\"/home/fananchong/go/src/github.com/CodisLabs/codis/admin/../bin/codis-server 127.0.0.1:23792\"", "{#PROCESSPID}":28523,"{#PROCESSNO}":3},
{"{#PROCESSNAME}":"\"/home/fananchong/go/src/github.com/CodisLabs/codis/admin/../bin/codis-server 127.0.0.1:23793\"", "{#PROCESSPID}":28576,"{#PROCESSNO}":4},
{"{#PROCESSNAME}":"\"/home/fananchong/go/src/github.com/CodisLabs/codis/admin/../bin/codis-server 127.0.0.1:23794\"", "{#PROCESSPID}":28597,"{#PROCESSNO}":5},
{"{#PROCESSNAME}":"\"/home/fananchong/go/src/github.com/CodisLabs/codis/admin/../bin/codis-server 127.0.0.1:23795\"", "{#PROCESSPID}":28633,"{#PROCESSNO}":6},
{"{#PROCESSNAME}":"\"/home/fananchong/go/src/github.com/CodisLabs/codis/admin/../bin/codis-server 127.0.0.1:23796\"", "{#PROCESSPID}":28671,"{#PROCESSNO}":7},
{"{#PROCESSNAME}":"\"/home/fananchong/go/src/github.com/CodisLabs/codis/admin/../bin/codis-server 127.0.0.1:23797\"", "{#PROCESSPID}":28707,"{#PROCESSNO}":8},
{"{#PROCESSNAME}":"\"/home/fananchong/go/src/github.com/CodisLabs/codis/admin/../bin/codis-server 127.0.0.1:23798\"", "{#PROCESSPID}":28735,"{#PROCESSNO}":9},
{"{#PROCESSNAME}":"\"/home/fananchong/go/src/github.com/CodisLabs/codis/admin/../bin/codis-server 127.0.0.1:23799\"", "{#PROCESSPID}":28780,"{#PROCESSNO}":10}
]}
2. Write a script to get CPU and MEM occupation of a process

such as kgetcpu.sh :

#!/bin/bash
getactive=`ps aux | grep "$1" | grep -v grep | grep -v $0 | awk '{print $3}'`
if [[ -n $getactive ]]; then
    echo $getactive
else
    echo "0"
fi

such as kgetmem.sh :

#!/bin/bash
getactive=`ps aux | grep "$1" | grep -v grep | grep -v $0 | awk '{print $6}'`
if [[ -n $getactive ]]; then
    n=$[getactive*1024];
    echo $n
else
    echo "0"
fi

The above script defines what to monitor for each monitor item

3. Configure monitoring items

For example, / etc / ZABBIX / ZABBIX ﹣ agentd.d/userparameter ﹣ mygraph.conf:

UserParameter=myGraph.server_cpu[*],sudo /opt/kgetcpu.sh $1
UserParameter=myGraph.server_mem[*],sudo /opt/kgetmem.sh $1
UserParameter=myGraph.server_process[*],sudo /opt/kgetserver.sh
UserParameter=myGraph.proc[*],sudo /opt/kgetproc.sh $1

Restart service

systemctl restart zabbix-agent.service

The rest is to use zabbix frontend to operate on the page

4. Create Discovery rule on Templates


Similar to figure above

5. Create item Templates on Templates


Similar to figure above

6. Create Graph prototype on Templates


Similar to figure above

At this point, all monitoring items will be generated automatically

Create Server CPU all graph on Host

(currently, it is created manually and can be generated automatically according to the principle Have time to turn over the document and fill in)

Above

Tags: github Zabbix sudo REST

Posted on Wed, 06 Nov 2019 10:50:03 -0500 by torsoboy