In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to customize monitoring items in Docker". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
An overview of machine deployment
In general, there are four machines, each with the following functions: a. Suppose a machine is running a web application, and the container is tomcat. The application has an interface http://localhost:8080/zabbixcustomitemdemo/count, which can return a certain amount of business in the last minute (such as the number of website visits).
b. One machine installs zabbix agent as a carrier for custom monitoring items; c. There is a machine with zabbix server; d installed. One machine has mysql installed as the database of the zabbix system
The overall deployment is shown in the following figure:
Server running the web application
This is a java web application based on maven, in which there is a controller of spring mvc, which provides a http service that covers the business volume per minute of a business. The code is shown below:
@ Controllerpublic class CountController {@ RequestMapping ("/ count") @ ResponseBody public int count (String model, String type) {int base; int max; int min; if ("a" .equals (model)) {base = 50000;} else {base = 10,000;} if ("0" .equals (type)) {max = 9000 Min = 1000;} else {max = 1000; min = 0;} return base + new Random () .nextInt (max)% (max-min+1);}}
From the above code, we can see that the http service returns a random number. This service accepts two parameters, model and type. The random number returned when model is equal to "a" starts from 50000, and the random number returned when model is not equal to "a" starts from 10000. When type is equal to "0", the value added on the basis of base is between 1000 and 9000. When type is not equal to "0", the value increased on the basis of base is between 0 and 1000.
The code of the entire project has been uploaded to git, and the address is git@github.com:zq2599/blog_demos.git. There are several projects in this directory. The actual project is zabbixcustomitemdemo, as shown below:
Docker-compose.yml file
Now that we've sorted out the functions and relationships of the four machines, let's make the docker-compose.yml document:
Version: '2'services: zabbix-mysql-service: image: daocloud.io/library/mysql:8 container_name: zabbix-mysql-service environment:-MYSQL_ROOT_PASSWORD=888888 restart: always zabbix-server-service: image: monitoringartist/zabbix-xxl:3.2.6 links:-zabbix-mysql-service:mysqlhost container_name: zabbix-server-service restart: always depends_on:-zabbix-mysql-service ports :-"8888 ZS_DBUser=root 80" environment:-ZS_DBHost=mysqlhost-ZS_DBUser=root-ZS_DBPassword=888888 zabbix-agent-a: image: zabbix/zabbix-agent:ubuntu-3.2.6 links:-zabbix-server-service:zabbixserverhost container_name: zabbix-agent-a restart: always depends_on:-zabbix-server-service environment:-ZBX_HOSTNAME=zabbix-agent-service-a -ZBX_SERVER_HOST=zabbixserverhost tomcat-server-service: image: bolingcavalry/bolingcavalrytomcat:0.0.1 container_name: tomcat-server restart: always ports:-"8080
The contents of the yml file are shown above, in which the configuration of mysql and zabbix server,zabbix agent is the same as the previous chapter "practical zabbix Trilogy II under Docker: monitoring other machines". The new image is an image of tomcat, which is based on the official image of tomcat. I made a small change to make this tomcat support the online deployment of web applications. For tomcat online deployment applications, please see the article "practical docker, write Dockerfile custom tomcat image." Achieve online deployment of web applications "
After preparing the yml file, open the terminal and execute docker-compose up-d in the directory where the yml file is located to start all the containers in the yml file.
Note that if your computer has previously run the docker-compose.yml file in the previous chapter, "practical zabbix Trilogy II under Docker: monitoring other machines", then this execution of docker-compose up-d will prompt startup failure, and a container with the same name already exists. At this time, you can go to the directory where the docker-compose.yml file in the previous chapter is located and execute docker-compose down, or you can list all containers through docker ps-a. Then stop all containers in turn through the docker stop command, and then execute the docker-compose rm command to delete them in turn.
# deploy web application # Open the terminal, enter the directory of web project zabbixcustomitemdemo, and execute the command mvn clean package-U-Dmaven.test.skip=true tomcat7:redeploy to deploy the web project to the tomcat container. For details on online deployment, please refer to the article "practical docker, write Dockerfile custom tomcat image to achieve online deployment of web application".
After the deployment is successful, open a browser and visit http://localhost:8080/zabbixcustomitemdemo/count focus web server and return a number, as shown in the following figure:
# make a shell script to access url # # next, we need to make a shell script on zabbix agent. When the function of this script initiates a http request http://localhost:8080/zabbixcustomitemdemo/count?model=a&type=0, you can get the number of web service response. If this script is called every minute, you can get a complete monitoring graph.
a. First, execute docker exec-it zabbix-agent-a / bin/bash to log in to the zabbix agent container; b. After logging in, execute apt-get update to update apt; c. Execute apt-get install wget and apt-get install vim successively, install wget and vi tools, d. Create a new directory / usr/work/, create a shell file biz_count.sh with vi in this directory, as follows:
# "! / bin/bashwget-qO- http://tomcathost:8080/zabbixcustomitemdemo/count?model=$1\&type=$2echo"
The function of the above code is to access the http service to obtain a number, in which model and type use the input parameter of shell. Pay attention to two details: the first: the last line code echo "". Practice has proved that this line is very useful. With this line, you will wrap the number returned by http, and only with the newline data can you successfully report it to zabbix server. Second: in the url parameter after the wget command, the "&" symbol is preceded by an escaped slash "\"
e. Execute chmod aquix biz_count.sh to give executable permissions to shell
# add monitoring items on agent # continue on the zabbix agent container, we need to add a custom monitoring item so that we can use the monitoring item on zabbix server later: a. Under the / etc/zabbix/zabbix_agentd.d directory, add a new biz.conf file with the following contents:
UserParameter=get_total_num [*], / usr/work/biz_count.sh $1 $2
The above code configures a custom monitoring item named get_total_num, which can accept two input parameters. The monitoring item calls the script biz_count.sh and passes the two input parameters from the outside directly to biz_count.sh.
b. Execute chmod astatr biz.conf to make the file readable
# Test on zabbix agent # # continue to test the newly added monitoring items on the zabbix agent container by executing the following command:
/ usr/sbin/zabbix_agentd-t get_total_num [apany 0]
The "a" 0 in square brackets indicates that the two input parameters are "a" and "0". We execute them for four times. The input parameters are [a ~ ~ 0], [b ~ ~ 0], [a ~ ~ 1] and [b ~ 1], respectively. The results are as follows:
The four return values are 54741, 17097, 50564 and 10919 respectively. Combined with the previous java code, we can find that both parameters are in effect, and the size range of the number varies with the input parameters.
In order for the monitoring item to take effect, you need to restart zabbix agent, but here is a faster way to try: a. Execute exit to exit the zabbix agent container; b. Execute docker restart zabbix-agent-a to restart the zambia agent container
At this point, the custom monitoring item is ready. After configuring it successfully on zabbix server, we can see the monitoring data and graph, but before configuring it, we can test on zabbix server whether we can successfully call the monitoring item on zabbix agent.
Test the monitoring items of the agent machine on zabbix server
First of all, we need to find out the ip of the zabbix agent machine, there are two ways: first, execute docker exec-it zabbix-agent-a / bin/bash to log in to the zabbix agent container, execute the ip addr command in the container to get ip;, and directly execute the docker exec-it zabbix-agent-an ip addr command to get ip.
Either way, you can get the ip of zabbix-agent is 172.31.0.5.
Now let's log in to the zabbix server container and execute the command docker exec-it zabbix-server-service / bin/bash. After logging in, execute the following command:
Zabbix_get-s 172.31.0.5-k get_total_num [apen0]
As shown in the following figure, the test is successful and the monitoring item that calls agent returns the expected data:
Remember that after we configured it on zabbix agent, we need to restart the agent service or restart the zabbix agent container. If you forget this step, you will get the following error message when testing on zabbix server:
At this time to restart, and then come back to test can be successful.
Add monitoring items to the management page
Enter the "http://localhost:8888/" login management page" on the browser, and first add the agent machine, as shown below:
After adding, click the location of the red box in the following figure to enter the monitoring items page:
As shown below, click "Create item" in the upper right corner to start adding monitoring items:
For the new monitoring items, we only need to enter the fields of Name,Key,Update interval (update frequency), and the other fields remain unchanged. The content of each field to be updated is as shown below:
After filling in and saving it, we can see the latest monitoring item data in Monitoring-> Latest data, as shown below:
Next, we add a monitoring graph, as shown in the following figure, you can go to the graph management page:
As shown in the following figure, click "Create graph" in the upper right corner to create a graph:
When creating a new drawing, the name is optional, as long as the monitoring item just created is selected by Items, as shown below:
After the creation is successful, it is time to see the effect, as shown in the following figure:
After clicking "add", select the graphic option we just created on the pop-up page. After the operation is finished, click the position of the red box below to see the graph:
The graph is as follows:
This is the end of the content of "how to customize monitoring items in Docker". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.