Docker next three combat zabbix Trilogy: custom monitoring items

By the last chapter : "trilogy of the two real zabbix at Docker monitoring other machine" after combat, we learned about the monitoring of the machine by installing zabbix agent on the machine to complete, zabbix agent connection zabbix server, will the information they give to the machine where the timing zabbix server, thus achieving a monitoring machine;
but we can only monitor the cpu, disk these basic information, such as information for some business visits, perform a logical failures such as success information, we also want to monitor, which requires us to create custom monitoring items, and in this chapter we take a real custom item monitoring it.

The full series of articles links:

  1. "One of the real zabbix trilogy under Docker: speed experience" ;
  2. "Under Docker combat zabbix trilogy of the two: monitoring other machines" ;
  3. "Docker next three combat zabbix Trilogy: custom monitoring item" ;

List of machine deployment

In general, there are four machines, each of the following functions:

. A Suppose you have a machine running the web application, the container is tomcat, this application has interfaces http: // localhost: 8080 / zabbixcustomitemdemo / count, return to the last minute a business volume (eg site visits);

. B have a machine installed zabbix agent, as a carrier to customize monitored items;

. C have a machine installed zabbix server;

. D have a machine installed mysql, zabbix as a database system;

The overall deployment, as shown below:

Write pictures described here

Run the web application server

This application is based maven java web, which has a spring mvc controller to provide an http service, the range of a certain service traffic per minute, the code is shown below:

@Controller
public 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 =10000;
        }

        if("0".equals(type)){
            max = 9000;
            min = 1000;
        }else{
            max = 1000;
            min = 0;
        }

        return base + new Random().nextInt(max)%(max-min+1);
    }
}

Returned when we can see from the above code, http service returns a random number, this service accepts two parameters model and type, when the model is equal to "a" returns a random number from 50000 to start, model not equal to "a" random number 10000 from the start, when the type is equal to "0", the added value on the basis of base is between 1000 to 9000, when the type is not equal to "0", increases in the base on the basis of the values ​​0 to 1000 between;

The entire project code has been uploaded to the Git, address [email protected]: zq2599 / blog_demos.git, this directory plurality engineering, this is a practical engineering zabbixcustomitemdemo, as shown below:

Write pictures described here

docker-compose.yml file

Above we have the function and relationship of four machines sort out clearly, now to develop docker-compose.yml file it:

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: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:8080"

yml content file shown above, wherein mysql, zabbix server, zabbix agent configuration and the previous chapter "lower Docker actual zabbix bis Trilogy: monitoring other machine" is the same, it is a new tomcat mirror, this image is a point I made small changes on the basis of the official image of the tomcat, tomcat makes this online support deployment of web applications, on-line tomcat deploy applications, see the article "real docker, write Dockerfile custom tomcat mirror, to achieve the deployment of a web application online "

When you are ready yml file, open a terminal and execute files in the directory yml docker-compose up -d can yml file all containers start;

Note that if the previous chapter has been run before your computer "under Docker combat zabbix trilogy of the two: monitoring other machines" Docker-compose.yml file, then execute prompted to start docker-compose up -d will this fail container has the same name exists, this time you can go to the last chapter of docker-compose.yml file directory execute docker-compose down, also can pass docker ps -a will list all the container, and then through the docker stop command in order to stop all container, then execute docker-compose rm command and then click delete;

Deploy web applications

Open a terminal and enter the directory under the web project zabbixcustomitemdemo, execute the command mvn Package Penalty for Clean = -U -Dmaven.test.skip to true tomcat7: the redeploy , can be deployed to the tomcat web project on the container, details on deployment, refer to the online article " combat docker, write Dockerfile custom tomcat mirror to achieve web application deployment line " ;

After the deployment is successful, open the browser and go to http: // localhost: 8080 / zabbixcustomitemdemo / count, web server will return a number, as shown below:

Write pictures described here

Production of shell scripts to access url

Next we want to do on zabbix agent in a shell script, launch http request http function when this script: // localhost:? 8080 / zabbixcustomitemdemo / count model = a & type = 0, you can get a digital web service response, if this script is called once per minute, you can get a complete monitoring graphs;

. a first, a docker exec -it zabbix-agent-a / bin / bash login zabbix agent container;

. b After logging in, execute apt-get update update apt;

. c has executed apt-get install wget and APT-GET install vim , vi and install wget tools;

. D the new directory / usr / work /, in this directory to create a shell file biz_count.sh use vi, reads as follows:

#"!/bin/bash
wget -qO- http://tomcathost:8080/zabbixcustomitemdemo/count?model=$1\&type=$2
echo ""

Function code above is to visit http service to get a number, which is the model and type with the shell to the Senate;
attention to two details:
First: The last line of code echo "" , this line proved to be very useful, there are this line will be carried out after the digital output http returned wrap, wrap with data reported to be successful zabbix server;
second: wget url parameter following the command, the "&" symbol in front of an escape to slash "";

. e performs the chmod A + X biz_count.sh , executable permissions given to the shell;

Add agent on the monitored item

Continue on zabbix agent container, we want to add a custom monitor items, so you can use the back of the monitor item on zabbix server:

. A In /etc/zabbix/zabbix_agentd.d directory, add a biz.conf file, as follows:

UserParameter=get_total_num[*],/usr/work/biz_count.sh $1 $2

The above code is configured with a custom monitoring item, the name is get_total_num, we can accept two parameters into the monitored item calls biz_count.sh the script, and the two came into the external parameters are passed directly to biz_count.sh;

. b perform chmod a + r biz.conf such that the file is readable;

Tests on zabbix agent

Continue on zabbix agent container, execute the following command to test just added new monitoring items:

/usr/sbin/zabbix_agentd -t get_total_num[a,0]

In brackets a, 0 indicates the two parameters are "a" and "0", we performed four times, respectively, the parameters [a, 0], [b, 0], [a, 1], [ b, 1], to obtain the results as shown below:

Write pictures described here

54741,17097,50564,10919 return value is four, respectively, combine the previous two java code can be found in the parameters take effect, due to the size range of the digital parameter changes;

In order to monitor the entry into force, it needs to restart zabbix agent, but here there is a quicker way you can try:

. a performing exit exit zabbix agent container;

. b perform docker restart zabbix-agent-a restart zambia agent container;

Here, and custom monitoring items ready, then put it on zabbix server configuration is successful, we will be able to see the monitoring data and graphs, but before the configuration, we can test it on zabbix server can a successful call to monitor the items on zabbix agent;

Test monitoring key agent in the zabbix server machine

First we have to figure out zabbix agent machine ip, there are two ways:
first, the implementation of docker exec -it zabbix-agent-a / bin / bash login zabbix agent container, the container is performed ip addr command to get ip ;
second, direct execution docker exec -it zabbix-agent-a ip addr command to get IP;

Either way, can get zabbix-agent ip is 172.31.0.5;

Now we log container zabbix server, execute the command docker exec -it zabbix-server-service / bin / bash to log, execute the following command after login:

zabbix_get -s 172.31.0.5 -k get_total_num[a,0]

As shown below, the test is successful, the call agent monitoring item returns in line with the expected data:

Write pictures described here

Remember after we have just configured on zabbix agent, agent service need to restart or reboot zabbix agent container, if you forget this step, and now zabbix server test will get the following error message:

Write pictures described here

This time to restart it, and then you can come back to test a success.

Add Monitoring item on the administration page

Enter in your browser "http: // localhost: 8888 /" Log management page, first add the agent machine, as shown below:

Write pictures described here

After the addition, the position of the red box click below to enter the monitored item page:

Write pictures described here

Below, top right, click "Create item" to start adding items to monitor:

Write pictures described here

The new monitoring item, we just fill in the Name, Key, Update interval (update frequency) these fields, others remain the same, you want to update the contents of each field as shown below:

Write pictures described here

Fill and save, we can Monitoring - see the latest monitoring data items, as shown below> Latest data in:

Write pictures described here

Next we add a graphical monitoring operation as shown below, can enter the graphics management page:

Write pictures described here

Below, top right, click "Create graph" to create a graphic:

Write pictures described here

New graphics, when the name of free, as long as the selected monitoring items Items can just created, as shown below:

Write pictures described here

Create success, now we want to look at the effect of the operation as shown below:

Write pictures described here

After clicking "add", in the pop-up page just select our new graphics options, after the operation is completed, click below the red line position, you can see a graph:

Write pictures described here

Graph as follows:

Write pictures described here

These are custom items to monitor the development of all processes and set-based operations monitoring items, in addition to graphics can be used to add tirgger alarm, alarm used to determine the action to add action, such as mail messages, interested readers can actually actual combat operations.

I welcome the attention of the public number

Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/bolingcavalry/p/11597365.html