企业运维之 zabbix 监控部署--监控应用

Posted 123坤

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了企业运维之 zabbix 监控部署--监控应用相关的知识,希望对你有一定的参考价值。

企业运维之 zabbix 监控部署--监控应用


在前面的博客中介绍了如何部署企业级监控,以及监控主机时的几种发现方式,以及对于API接口的管理做了简单的介绍和操作演示;接下来就要做应用层面的监控。
链接: link .


1. Apache监控

在前面的基础上,server1位server端;server2和server3为agent端。

在server2上安装apache ,然后让其被监控。

  1. 安装apache
[root@server2 ~]# yum install -y httpd
[root@server2 ~]# systemctl start httpd
[root@server2 ~]# echo www.westos.org > /var/www/html/index.html
[root@server2 ~]# curl localhost
www.westos.org
  1. 在监控页面直接用模板来添加监控:

添加后可以在监控项中看到相关信息,只有一条监控项, 是关于HTTP运行情况的。


可以看出它只是一般检查,过于简单,相对来说并不可考。

而且在实际业务中使用的监控并不一定有现成模板,很大概率需要自行编写监控规则,下面用nginx来进行举例.

2. Nginx 监控

在server3上源码编译安装nginx服务并安装监控模块;

  1. 编译nginx
[root@server3 ~]# yum install -y gcc pcre-devel openssl-devel	#下载编译需要的依赖性
[root@server3 ~]# ls
nginx-1.18.0  nginx-1.18.0.tar.gz
[root@server3 ~]# cd nginx-1.18.0
[root@server3 nginx-1.18.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@server3 nginx-1.18.0]# vim auto/cc/gcc 

172 #CFLAGS="$CFLAGS -g"
[root@server3 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
[root@server3 nginx-1.18.0]# make & make install
[root@server3 ~]# vim .bash_profile 
PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin
[root@server3 ~]# source .bash_profile
[root@server3 ~]# nginx 
[root@server3 ~]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body 
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

编辑配置文件,让其怎么对外开放;

[root@server3 conf]# vim nginx.conf

 48         location /status 
 49             stub_status on;
 50             access_log off;
 51             allow 127.0.0.1;
 52             deny all;
 53         
[root@server3 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server3 conf]# nginx -s reload
[root@server3 conf]# cd 

[root@server3 ~]# curl http://127.0.0.1/status	#外部访问有问题
Active connections: 1 
server accepts handled requests
 4 4 7 
Reading: 0 Writing: 1 Waiting: 0 

那么主要问题就是Server端如何拿到这串信息中有用的部分了。

[root@server3 ~]# curl -s http://127.0.0.1/status | grep Active
Active connections: 1 
[root@server3 ~]# curl -s http://127.0.0.1/status | grep Active | awk 'print $3'
1
[root@server3 ~]# cd /etc/zabbix/zabbix_agentd.d/
[root@server3 zabbix_agentd.d]# ls
userparameter_mysql.conf
[root@server3 zabbix_agentd.d]# cp userparameter_mysql.conf userparameter_nginx.conf
[root@server3 zabbix_agentd.d]# vim ##将这一操作编写为规则userparameter_nginx.conf
[root@server3 zabbix_agentd.d]# cat userparameter_nginx.conf
UserParameter=nginx_active,curl -s http://127.0.0.1/status | grep Active | awk 'print $3'
[root@server3 zabbix_agentd.d]# systemctl restart zabbix-agent.service 	#重启服务
  1. 在server端

需要安装软件包,然后来获取;

[root@server1 ~]# yum install -y zabbix-get
[root@server1 ~]# zabbix_get -s 172.25.25.3 -p 10050 -k "nginx_active"
1

以上的过程是手动的取,前端监控页面完成规则的添加,让其自动获取;

创建监控项:


新增监控项,注意键值与之前所写的一定要相同, 名称则没有要求。


创建图形详细操作:


同理可以监控其他信息:

[root@server3 zabbix_agentd.d]# curl -s http://127.0.0.1/status | awk 'NR==3 print $1'
33
[root@server3 zabbix_agentd.d]# curl -s http://127.0.0.1/status | awk 'NR==3 print $2'
34
[root@server3 zabbix_agentd.d]# curl -s http://127.0.0.1/status | awk 'NR==3 print $3'
38
[root@server3 zabbix_agentd.d]# vim userparameter_nginx.conf 
[root@server3 zabbix_agentd.d]# cat userparameter_nginx.conf
UserParameter=nginx_active,curl -s http://127.0.0.1/status | grep Active | awk 'print $3'
UserParameter=nginx_accpet,curl -s http://127.0.0.1/status | awk 'NR==3 print $1'
UserParameter=nginx_handle,curl -s http://127.0.0.1/status | awk 'NR==3 print $2'
UserParameter=nginx_request,curl -s http://127.0.0.1/status | awk 'NR==3 print $3'
[root@server3 zabbix_agentd.d]# systemctl restart zabbix-agent.service 

#在server1上来获得
[root@server1 ~]# zabbix_get -s 172.25.25.3 -p 10050 -k "nginx_accpet"
46
[root@server1 ~]# zabbix_get -s 172.25.25.3 -p 10050 -k "nginx_handle"
47
[root@server1 ~]# zabbix_get -s 172.25.25.3 -p 10050 -k "nginx_request"
51

然后再依次来创建监控项,然后再图形中让其加入:

3. Mysql 监控

  1. 手动结合mysql与zabbix

zabbix添加服务会读取/etc/zabbix/zabbix_agentd.d/的.conf文件,手动监控添加mysql即编写musql相关.conf文件。由于server1主机已存在mysql,所以server1既可以当主机,又可以当agent机。

由于此时mysql并不像nginx可以直接返回值,需要做进一步的设定;

[root@server1 ~]# cd /etc/zabbix/zabbix_agentd.d/
[root@server1 zabbix_agentd.d]# ls
userparameter_mysql.conf
[root@server1 zabbix_agentd.d]# cat userparameter_mysql.conf
# For all the following commands HOME should be set to the directory that has .my.cnf file with password information.

# Flexible parameter to grab global variables. On the frontend side, use keys like mysql.status[Com_insert].
# Key syntax is mysql.status[variable].
UserParameter=mysql.status[*],echo "show global status where Variable_name='$1';" | HOME=/var/lib/zabbix mysql -N | awk 'print $$2'

# Flexible parameter to determine database or table size. On the frontend side, use keys like mysql.size[zabbix,history,data].
# Key syntax is mysql.size[<database>,<table>,<type>].
# Database may be a database name or "all". Default is "all".
# Table may be a table name or "all". Default is "all".
# Type may be "data", "index", "free" or "both". Both is a sum of data and index. Default is "both".
# Database is mandatory if a table is specified. Type may be specified always.
# Returns value in bytes.
# 'sum' on data_length or index_length alone needed when we are getting this information for whole database instead of a single table
UserParameter=mysql.size[*],bash -c 'echo "select sum($(case "$3" in both|"") echo "data_length+index_length";; data|index) echo "$3_length";; free) echo "data_free";; esac)) from information_schema.tables$([[ "$1" = "all" || ! "$1" ]] || echo " where table_schema=\\"$1\\"")$([[ "$2" = "all" || ! "$2" ]] || echo "and table_name=\\"$2\\"");" | HOME=/var/lib/zabbix mysql -N'

UserParameter=mysql.ping,HOME=/var/lib/zabbix mysqladmin ping | grep -c alive
UserParameter=mysql.version,mysql -V
[root@server1 zabbix_agentd.d]# zabbix_get -s 172.25.25.1 -p 10050 -k 'mysql.ping'
zabbix_get [1541]: Check access restrictions in Zabbix agent configuration

#无法获得结果,根据文件信息,需要建立目录和文件
[root@server1 zabbix_agentd.d]# mkdir /var/lib/zabbix
[root@server1 zabbix_agentd.d]# cd /var/lib/zabbix
[root@server1 zabbix]# ls
[root@server1 zabbix]# vim my.cnf
[root@server1 zabbix]# cat my.cnf 
[mysql]
host     = localhost
user     = root
password = westos
socket   = /var/lib/mysql/mysql.sock
[mysqladmin]
host     = localhost
user     = root
password = westos
socket   = /var/lib/mysql/mysql.sock
[root@server1 zabbix]# systemctl restart zabbix-agent
[root@server1 zabbix]# zabbix_get -s 127.0.0.1 -p 10050 -k 'mysql.version'
mysql  Ver 15.1 Distrib 5.5.60-MariaDB, for Linux (x86_64) using readline 5.1

在前端界面进行监控项的设置




此模板所用的监控项太少,换成下面的方式。

  1. percona-mysql 模板结合 mysql 与 zabbix

与前面的相比 Percona Monitoring Plugins 带有190多个监控项。

首先删掉之前手动添加的mysql数据库模块,在监控页面 主机 -> 模板 -> 取消链接并清理

这么多监控项全部手动添加显然是不现实的, 需要使用模板倒入;

下载软件包并配置信息

[root@server1 ~]# ls
percona-zabbix-templates-1.1.8-1.noarch.rpm  zabbix_api.sh
[root@server1 ~]# rpm -ivh percona-zabbix-templates-1.1.8-1.noarch.rpm
warning: percona-zabbix-templates-1.1.8-1.noarch.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:percona-zabbix-templates-1.1.8-1 ################################# [100%]

Scripts are installed to /var/lib/zabbix/percona/scripts
Templates are installed to /var/lib/zabbix/percona/templates
[root@server1 ~]# cd /var/lib/zabbix/percona/scripts
[root@server1 scripts]# ls	#包含搜集数据所需的shell脚本和php文件
get_mysql_stats_wrapper.sh  ss_get_mysql_stats.php
[root@server1 scripts]# cd /var/lib/zabbix/percona/templates
[root@server1 templates]# ls	#模板
userparameter_percona_mysql.conf  zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.8.xml
[root@server1 templates]# cp userparameter_percona_mysql.conf /etc/zabbix/zabbix_agentd.d/
[root@server1 templates]# cd /etc/zabbix/zabbix_agentd.d/
[root@server1 zabbix_agentd.d]# ls
userparameter_mysql.conf  userparameter_percona_mysql.conf
[root@server1 ~]# wc -l /etc/zabbix/zabbix_agentd.d/userparameter_percona_mysql.conf
190 /etc/zabbix/zabbix_agentd.d/userparameter_percona_mysql.conf

为了能够正确链接数据库获取信息,需要修改ss_get_mysql_stats.php php脚本中数据库登陆相关内容:

[root@server1 ~]# cd /var/lib/zabbix/percona/scripts
[root@server1 scripts]# ls
get_mysql_stats_wrapper.sh  ss_get_mysql_stats.php
[root@server1 scripts]# vim ss_get_mysql_stats.php

  30 $mysql_user = 'root';
  31 $mysql_pass = 'westos';

通过调用脚本来确定功能呢个是否能够实现:

[root@server1 scripts]# /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gg
22
[root@server1 scripts]# cd /tmp/	#执行完之后会生成数据
[root@server1 tmp]# ls
localhost-mysql_cacti_stats.txt
systemd-private-9e47cdd214544d9fb1164b22866a01c7-httpd.service-wzimn9
systemd-private-9e47cdd214544d9fb1164b22866a01c7-mariadb.service-V3KBwx
[root@server1 tmp]# zabbix_get -s 127.0.0.1 -p 10050 -k 'MySQL.pool-size'
8191

测试结束删除/tmp/下生成的文本文件,用为root用户下测试的文件器与用户无法写入;

[root@server1 tmp]# ls
localhost-mysql_cacti_stats.txt
systemd-private-9e47cdd214544d9fb1164b22866a01c7-httpd.service-wzimn9
systemd-private-9e47cdd214544d9fb1164b22866a01c7-mariadb.service-V3KBwx
[root@server1 tmp]# rm -fr localhost-mysql_cacti_stats.txt

为监控页面导入模板,方便进行自动化扫描:



添加新的模板:


此时再次查看时会发现多了一百多个监控项;图形处也变了很多。

4. zabbix 监控Tomcat

Tomcat 在java虚拟机中默认操作无法实现对其监控;不过 zabbix也提供了组件zabbix-java-geteway.

先在server2上部署java环境,然后下载Tomcat的包;

[root@server2 tomcat]# yum install -y java-1.8.0-openjdk.x86_64

#安装并运行tomcat
[root@server2 ~]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/
[root@server2 ~]# cd /usr/local/
[root@server2 local]# ls
apache-tomcat-7.0.37  bin  etc  games  include  lib  lib64  libexec  sbin  share  src
[root@server2 local]# ln -s apache-tomcat-7.0.37/ tomcat
[root@server2 local]# cd tomcat/
[root@server2 tomcat]# ls
bin  conf  lib  LICENSE  logs  NOTICE  RELEASE-NOTES  RUNNING.txt  temp  webapps  work
[root@server2 tomcat]# ./bin/startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

此时便开启8080端口。

修改配置文件,增加额外监听端口:

[root@server2 tomcat]# ./bin/shutdown.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@server2 bin]# pwd
/usr/local/tomcat/bin
[root@server2 bin]# vim catalina.sh

 99 CATALINA_OPTS='
100   -Dcom.sun.management.jmxremote.port=8888
101   -Dcom.sun.management.jmxremote.ssl=false
102   -Dcom.sun.management.jmxremote.authenticate=false
103 '
[root@server2 bin]# ./startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@server2 bin]# netstat -antlp| grep :8888
tcp6       0      0 :::8888                 :::*                    LISTEN      18758/java          

此时网页中访问:http://172.25.25.2:8080/时可以看到tomcat已经到位;


在server1上安装监控java的组件并完成配置:

[root@server1 ~]# yum install -y zabbix-java-gateway
[root@server1 ~]# cd /etc/zabbix/	#配置目录
[root@server1 zabbix]# ls
web                 zabbix_agentd.d           zabbix_java_gateway_logback.xml
zabbix_agentd.conf  zabbix_java_gateway.conf  zabbix_server.conf
[root@server1 zabbix]# systemctl enable --now zabbix-java-gateway
[root@server1 zabbix]# netstat -antlp | grep :10052
tcp6       0      0 :::10052                :::*                    LISTEN      21847/java    

设置网关位置,并设定默认开启进程数:

[root@server1 zabbix]# vim zabbix_server.conf

289 JavaGateway=172.25.25.1
305 StartJavaPollers=5
[root@server1 zabbix]# systemctl restart zabbix-server

在网页信息中添加对JMX接口的支持:



添加完成之后会多很多JVM的包;

以上是关于企业运维之 zabbix 监控部署--监控应用的主要内容,如果未能解决你的问题,请参考以下文章

企业运维之 zabbix 监控部署--监控主机

企业运维之 zabbix 监控部署--监控主机

企业运维之 zabbix 监控--报警平台与分布式

企业运维之 zabbix 监控--报警平台与分布式

linux运维之zabbix监控windows服务器

运维之监控系统实践