zabbix custom monitoring service configuration

Note: This paper is based on https://blog.51cto.com/14227204/2474670
1, Monitor host memory usage
1. Deploy agent client on server

[root@agent /]# mkdir /zabbix          # Personal habits, negligible
[root@agent /]# cd /zabbix/
[root@agent /]# rz
[root@agent zabbix]# tar zxf zabbix-3.2.1.tar.gz 
[root@agent zabbix]# cd zabbix-3.2.1/
[root@agent zabbix-3.2.1]# useradd -M -s /sbin/nologin  zabbix
[root@agent zabbix-3.2.1]# ./configure --prefix=/usr/local/zabbix --enable-agent && make && make install
[root@agent zabbix-3.2.1]# cp misc/init.d/fedora/core/zabbix_agentd /etc/init.d/
[root@agent /]# sed -i 's/BASEDIR=\/usr\/local/BASEDIR=\/usr\/local\/zabbix/g' /etc/init.d/zabbix_agentd 
[root@agent /]# sed -i 's/PIDFILE=\/tmp\/$BINARY_NAME.pid/PIDFILE=$BASEDIR\/logs\/$BINARY_NAME.pid/g' /etc/init.d/zabbix_agentd 
[root@agent /]# chmod +x /etc/init.d/zabbix_agentd
[root@agent /]# sed -i 's/# PidFile=\/tmp\/zabbix_agentd.pid/PidFile=\/usr\/local\/zabbix\/logs\/zabbix_agentd.pid/g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# sed -i 's/LogFile=\/tmp\/zabbix_agentd.log/LogFile=\/usr\/local\/zabbix\/logs\/zabbix_agentd.log/g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# sed -i 's/Server=127.0.0.1/Server=192.168.171.133/g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# sed -i 's/# ListenPort=10050/ListenPort=10050/g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# sed -i 's/ServerActive=127.0.0.1/ServerActive=192.168.171.133/g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# sed -i 's/Hostname=Zabbix server/Hostname=192.168.171.134/g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# sed -i 's/# Timeout=3/Timeout=15/g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# sed -i 's/# Include=\/usr\/local\/etc\/zabbix_agentd.conf.d\//Include=\/usr\/local\/zabbix\/etc\/zabbix_agentd.conf.d\//g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# sed -i 's/# UnsafeUserParameters=0/UnsafeUserParameters=1/g' /usr/local/zabbix/etc/zabbix_agentd.conf

[root@agent /]# egrep -v '^$|^#' /usr/local/zabbix/etc/zabbix_agentd.conf 
PidFile=/usr/local/zabbix/logs/zabbix_agentd.pid
LogFile=/usr/local/zabbix/logs/zabbix_agentd.log
Server=192.168.171.133
ListenPort=10050
ServerActive=192.168.171.133
Hostname=192.168.171.134
Timeout=15
Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/
UnsafeUserParameters=1
[root@agent /]# mkdir -p /usr/local/zabbix/logs       # Create log storage path
[root@agent /]# chown -R zabbix:zabbix /usr/local/zabbix/     # Grant authority
[root@agent /]# /etc/init.d/zabbix_agentd start           # Start agent
Reloading systemd:                                         [  OK  ]
Starting zabbix_agentd (via systemctl):                    [  OK  ]
[root@agent /]# cat > /tmp/memavailable.sh << EOF
> #!/bin/bash
> free -m | grep Mem | awk '{print $NF}'
> EOF
[root@agent /]# chmod +x /tmp/memavailable.sh 
[root@agent /]# cat > /usr/local/zabbix/etc/zabbix_agentd.conf.d/memtest.conf << EOF
> UserParameter=memtest,sh /tmp/memavailable.sh
> EOF
[root@agent zabbix]# /etc/init.d/zabbix_agentd restart 
Restarting zabbix_agentd (via systemctl):                  [  OK  ]
#Server test:
[root@zabbix /]# zabbix_get -s 192.168.171.134 -p 10050 -k "memtest"
1307

2. Log in to the Zabbix server for the following operations








2, Monitoring Nginx services
Install and deploy Nginx by yourself. Please refer to the blog: https://blog.51cto.com/14227204/2464167
For convenience, I am still running on this host. If I need to monitor on another host, I also need to install the deployment agent
1. Configure agent

#Add a few lines based on the original configuration file
[root@agent /]# echo 'UserParameter=nginx.info[*],/usr/local/zabbix/scripts/nginx.sh  $1' >> /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# echo "UserParameter=nginx.ping[*],netstat -anput | grep -w 80 | grep -w LISTEN | wc -l" >> /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# egrep -v '^$|^#' /usr/local/zabbix/etc/zabbix_agentd.conf
PidFile=/usr/local/zabbix/logs/zabbix_agentd.pid
LogFile=/usr/local/zabbix/logs/zabbix_agentd.log
Server=192.168.171.133
ListenPort=10050
ServerActive=192.168.171.133
Hostname=192.168.171.134
Timeout=15
Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/
UnsafeUserParameters=1
UserParameter=nginx.info[*],/usr/local/zabbix/scripts/nginx.sh  $1
UserParameter=nginx.ping[*],netstat -anput | grep -w 80 | grep -w LISTEN | wc -l

2. Writing Nginx script on Nginx server

[root@agent /]# mkdir /usr/local/zabbix/scripts
[root@agent /]# cd /usr/local/zabbix/scripts/
[root@agent scripts]# vim nginx.sh

#!/bin/bash
case $1 in
index)
    RS=`curl -s -I 192.168.171.134/index.html |head -n1 |awk '{print $2}'`
    echo $RS
    ;;

WorkerProcesses)
    RS=`ps aux |grep nginx | grep -w worker |wc -l`
    echo $RS
    ;;

bps)
    RS=`ab -q -n 1000 -c 1000 http://192.168.171.134/index.html |grep Request | awk '{print $4}'`
    echo $RS
    ;;

Current)
    LINES=`cat /usr/local/nginx/logs/access.log |wc -l`
    sleep 1
    NOW_LINES=`cat /usr/local/nginx/logs/access.log |wc -l`
    echo $NOW_LINES $LINES | awk '{print $1 -$2}'
;;
IP)
    RS=`cat /usr/local/nginx/logs/access.log | awk '{print $1}' |sort -u | wc -l`
    echo $RS
    ;;

VirtualHost)
    RS=`cat /usr/local/nginx/conf/nginx.conf |grep -v '#' | grep server_name | wc -l`
    echo $RS
    ;;

MD5)
    RS=`md5sum /usr/local/nginx/conf/nginx.conf |awk '{print $1}'`
    echo $RS
    ;;

PV)
RS=`wc -l /usr/local/nginx/logs/access.log | awk '{print $1}'`
    echo $RS
    ;;
esac

#Adjust the corresponding permissions. All users need to be able to execute netstat command
[root@agent scripts]# chmod +s /usr/bin/netstat 
[root@agent scripts]# chmod +x nginx.sh 
[root@agent scripts]# /etc/init.d/zabbix_agentd restart 
Restarting zabbix_agentd (via systemctl):                  [  OK  ]

3. Configure ZABBIX server web interface
Before configuring the web interface, you can now test whether the relevant information can be obtained on the ZABBIX Server side to verify the correctness of the script (at the same time, ab stress test is required, and the command should be installed on both the Server side and the Agent side)

[root@zabbix /]# yum -y install httpd-tools
[root@zabbix /]# ab -q -n 1000 -c 1000 http://192.168.171.134/index.html | grep Request | awk '{print $4}'
7665.77                                    #If information is returned, it means normal

To configure the Web interface:
1) Create template, configure application set





2) Cooperation with monitoring items


Monitor the total number of Nginx visits:
Next, every time you add a monitoring item, you need to click to create a monitoring item. Here, you don't need to take screenshots one by one

Independent IP number

Status of Nginx home page

Current number of Nginx concurrent

The MD5 value of the configuration file is used to monitor whether the nginx configuration file has been changed (note the information type in the figure)

Current throughput bps, and let it update for 60 seconds (note its information type)

Monitor the number of nginx working processes

Monitor the number of Nignx virtual hosts

3) Create trigger
nginx service unexpected stop work trigger
Next, it's the same. Every time you create a trigger, you need to click to create a trigger, or you don't need to take screenshots one by one


Create triggers that are not accessible to the Nginx home page

Trigger to create Nginx configuration file modified

4) Add monitored host



5) View data monitored by Zabbix

6) Add mapping to monitor nginx state (more friendly)



Associated to monitor item



Simulate concurrency on the Server side, and then view the graph detection page:

[root@zabbix /]# cat a.sh 
#!/bin/bash
while true
do
ab -n 1000 -c 100 192.168.171.134/index.html &> /dev/null
done
[root@zabbix /]# sh a.sh

After executing the script, do the following:

You can also customize the chart as follows:



3, Monitor Tomcat
Monitoring Tomcat requires the ZABBIX [Java] gateway component (introduced in zabbix2.0). As the name implies, Java gateway is similar to agent, but only used in Java. It should be noted that it can only obtain data actively, but not passively. Its data will eventually be submitted to the server or proxy.

If you need to enable the Tomcat monitoring function, you need to add the -- enable? Java parameter when compiling the zabbix server.
1. Zabbix Server installs the ZABBIX Java gateway program
ZABBIX Java gateway program can be installed on either the ZABBIX server or the monitored side, but it is generally installed on the ZABBIX server side. Here I will install it on the ZABBIX server side.

[root@zabbix /]# cd /zabbix/
[root@zabbix zabbix]# rpm -ivh zabbix-java-gateway-3.2.1-1.el7.x86_64.rpm 
[root@zabbix zabbix]# cd /etc/zabbix/
[root@zabbix zabbix]# sed -i 's/# LISTEN_IP="0.0.0.0"/LISTEN_IP="0.0.0.0"/g' zabbix_java_gateway.conf 
[root@zabbix zabbix]# sed -i 's/# LISTEN_PORT=10052/LISTEN_PORT=10052/g' zabbix_java_gateway.conf
[root@zabbix zabbix]# egrep -v '^$|^#' zabbix_java_gateway.conf 
LISTEN_IP="0.0.0.0"
LISTEN_PORT=10052
PID_FILE="/var/run/zabbix/zabbix_java.pid"
TIMEOUT=3
#Start ZABBIX Java
[root@zabbix zabbix]# systemctl start zabbix-java-gateway.service 
[root@zabbix zabbix]# netstat -anput | grep 10052               # Make sure the port is listening
tcp6       0      0 :::10052                :::*                    LISTEN      15501/java 
[root@zabbix zabbix]# cd /
[root@zabbix /]# sed -i 's/# JavaGateway=/JavaGateway=192.168.171.133/g' /usr/local/zabbix/etc/zabbix_server.conf
[root@zabbix /]# sed -i 's/# JavaGatewayPort=10052/JavaGatewayPort=10052/g' /usr/local/zabbix/etc/zabbix_server.conf
[root@zabbix /]# sed -i 's/# StartJavaPollers=0/StartJavaPollers=5/g' /usr/local/zabbix/etc/zabbix_server.conf
[root@zabbix /]# /etc/init.d/zabbix_server restart 
Restarting zabbix_server (via systemctl):                  [  OK  ]

2. Tomcat configuration on the test side
Here I installed it on the nginx server

[root@agent /]# rpm -qa | grep jdk
java-1.8.0-openjdk-headless-1.8.0.242.b08-0.el7_7.x86_64
java-1.8.0-openjdk-devel-1.8.0.242.b08-0.el7_7.x86_64
java-1.8.0-openjdk-1.8.0.242.b08-0.el7_7.x86_64
java-1.7.0-openjdk-1.7.0.111-2.6.7.8.el7.x86_64
java-1.7.0-openjdk-headless-1.7.0.111-2.6.7.8.el7.x86_64
copy-jdk-configs-3.3-10.el7_5.noarch
[root@agent /]# rpm -e --nodeps java-1.8.0-openjdk-headless-1.8.0.242.b08-0.el7_7.x86_64
[root@agent /]# rpm -e --nodeps java-1.7.0-openjdk-headless-1.7.0.111-2.6.7.8.el7.x86_64
[root@agent /]# mkdir tomcat
[root@agent /]# cd tomcat/
[root@agent tomcat]# rz
[root@agent tomcat]# ls
apache-tomcat-8.5.35.tar.gz  jdk-8u211-linux-x64.tar.gz
[root@agent tomcat]# tar zxf jdk-8u211-linux-x64.tar.gz -C /usr/local/
[root@agent tomcat]# tar zxf apache-tomcat-8.5.35.tar.gz 
[root@agent tomcat]# mv apache-tomcat-8.5.35 /usr/local/tomcat
[root@agent tomcat]# vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.8.0_211
export JRE_HOME=/usr/local/jdk1.8.0_211/jre
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
[root@agent tomcat]# . /etc/profile
[root@agent tomcat]# java -version
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)
#kaiqi enables JMX remote management function of tomcat
[root@agent /]# vim /usr/local/tomcat/bin/catalina.sh 
export JDK_JAVA_OPTIONS               # Go to line 306 and add the following
CATALINA_OPTS=" $CATALINA_OPTS  -Dcom.sun.management.jmxremote  -Dcom.sun.management.jmxremote.port=8888 -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=192.168.171.134  -Dcom.sun.management.jmxremote.authenticate=false"
#Note: the above IP is the IP of Tomcat. Port "8888" can be customized without conflict
[root@agent /]# /usr/local/tomcat/bin/startup.sh
[root@agent /]# netstat -anput | grep 8888
tcp6       0      0 :::8888                 :::*                    LISTEN      70730/java          
[root@agent /]# netstat -anput | grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      70730/java  

3. Return to ZABBIX server for JMX connection test
Note: this test requires the system to support the graphical desktop. If there is no graphical desktop, this verification process can be omitted.

[root@zabbix ~]# jconsole    #Execute this command, which is provided by Java gateway.

After executing the above command, the following prompt box will pop up (enter Tomcat's IP+JMX listening port to connect):


4. Log in to the web interface of Zabbix Server for configuration






Tags: Zabbix Nginx Java Tomcat

Posted on Tue, 10 Mar 2020 00:48:44 -0400 by joebudden