监控:监视,控制php
保证服务7*24小时运行,高可用99.9%。css
Linux常见监控命令html
查看:前端
发行版本:lsb_releasejava
系统信息:unamenode
内存使用:freepython
虚拟内存:vmstatmysql
网络:netstatios
系统资源统计:dstat
已打开文件:lsof
系统负载:uptime
任务:jobs
历史命令:history
监控:
# shell,取剩余内存的值 MEM=`free -m|awk 'NR==2{print $NF}'` if [ $MEM -le 100 ];then echo "当前的内存还剩余 $MEM"|mail -s '内存不足了!' sssssss@qq.com fi
shell+cron
open-falcon:添加自定义监控项难,适合有开发基础的用
promethues:
cacti是一款数据采集,数据存储,外加web界面展现的工具,它负责阈值范围内的实时变化,可是对超过阈值的告警功能很薄弱
nagios不关心阈值范围内的变化,只关心状态变化(超过阈值),而后报警。报警方式经过邮件,短信等。添加自定义监控项容易
zabbix = cacti + nagios
安装zabbix-agent到被监控的主机上,它负责按期收集各项数据,并发送到zabbix-server端,zabbix-server将数据存储到数据库中,zabbix-web根据数据在前端进行展示和绘图。
agent收集数据分为主动和被动两种模式:
主动:agent请求server获取主动的监控项列表,并主动将监控项内须要检测的数据提交给server/proxy
被动:server向agent请求获取监控项的数据,agent返回数据。
【主动监测】通讯过程:
zabbix首先向ServerActive配置的IP请求获取active items,获取并提交active tiems数据值server或者proxy。zabbix获取active items根据配置文件中的RefreshActiveChecks的频率进行,若是获取失败,那么将会在60秒以后重试。过程分两个部分:
获取ACTIVE ITEMS列表:
主动检测提交数据:
【被动监测】通讯过程:
被动模式每次都须要打开一个tcp链接,当监控项愈来愈多时,server端性能会出现问题。
监控实际应用:
新建监控项目时,选择的是zabbix代理(被动式)仍是zabbix端点代理程式(主动式)。
agentd配置文件中StartAgents参数的设置,若是为0,表示禁止被动模式,不然开启。通常建议不要设置为0,由于监控项目不少时,能够部分使用主动,部分使用被动模式。
常见组件:
Zabbix Server:负责接收agent发送的报告信息的核心组件,全部配置,统计数据及操做数据均由其组织进行;
Database Storage:专用于存储全部配置信息,以及由zabbix收集的数据;
Web interface:zabbix的GUI接口,一般与Server运行在同一台主机上;
Proxy:可选组件,经常使用于分布监控环境中,代理Server收集部分被监控端的监控数据并统一发往Server端;
Agent:部署在被监控主机上,负责收集本地数据并发往Server端或Proxy端;
注:zabbix node也是 zabbix server的一种 。
常见进程:
zabbix_agentd:客户端守护进程,此进程收集客户端数据,例如cpu负载、内存、硬盘使用状况等。
zabbix_get:zabbix工具,单独使用的命令,一般在server或者proxy端执行获取远程客户端信息的命令。一般用户排错。例如在server端获取不到客户端的内存数据,咱们可使用zabbix_get获取客户端的内容的方式来作故障排查。
zabbix_sender:zabbix工具,用于发送数据给server或者proxy,一般用于耗时比较长的检查。不少检查很是耗时间,致使zabbix超时。在脚本执行完毕以后,使用sender主动提交数据。
zabbix_server:zabbix服务端守护进程。zabbix_agentd、zabbix_get、zabbix_sender、zabbix_proxy、zabbix_java_gateway的数据最终都是提交到server。
固然不是数据都是主动提交给zabbix_server,也有的是server主动去取数据。

主机(host):要监控的网络设备,可由IP或DNS名称指定;
主机组(host group):主机的逻辑容器,能够包含主机和模板,但同一个组织内的主机和模板不能互相连接;主机组一般在给用户或用户组指派监控权限时使用;
监控项(item):一个特定监控指标的相关的数据;这些数据来自于被监控对象;item是zabbix进行数据收集的核心,相对某个监控对象,每一个item都由"key"标识;
触发器(trigger):一个表达式,用于评估某监控对象的特定item内接收到的数据是否在合理范围内,也就是阈值;接收的数据量大于阈值时,触发器状态将从"OK"转变为"Problem",当数据再次恢复到合理范围,又转变为"OK";
事件(event):触发一个值得关注的事情,好比触发器状态转变,新的agent或从新上线的agent的自动注册等;
动做(action):指对于特定事件事先定义的处理方法,如发送通知,什么时候执行操做;
报警升级(escalation):发送警报或者执行远程命令的自定义方案,如每隔5分钟发送一次警报,共发送5次等;
媒介(media):发送通知的手段或者通道,如Email、Jabber或者SMS等;
通知(notification):经过选定的媒介向用户发送的有关某事件的信息;
远程命令(remote command):预约义的命令,可在被监控主机处于某特定条件下时自动执行;
模板(template):用于快速定义被监控主机的预设条目集合,一般包含了item、trigger、graph、screen、application以及low-level discovery rule;模板能够直接连接至某个主机;
应用(application):一组item的集合;
web场景(web scennario):用于检测web站点可用性的一个活多个HTTP请求;
前端(frontend):Zabbix的web接口;
agent:主机经过安装agent方式采集数据。
server:经过收集agent发送的数据,写入数据库(MySQL,ORACLE等),再经过php+apache在web前端展现.
zabbix根据网络环境、监控规模等分为三种架构:
server-client
zabbix最简单的架构,监控机和被监控机之间不通过任何代理 ,直接由zabbix server和zabbix agentd之间进行数据交互。适用于网络比较简单,设备比较少的监控环境 。
server-proxy-client
其中proxy是server、client之间沟通的一个桥梁,proxy自己没有前端,并且其自己并不存放数据,只是将agentd发来的数据暂时存放,然后再提交给server 。该架构常常是和master-node-client架构作比较的架构 ,通常适用于跨机房、跨网络的中型网络架构的监控,有效的减轻server端的压力。
master-node-client
zabbix最复杂的监控架构,适用于跨网络、跨机房、设备较多的大型环境 。每一个node同时也是一个server端,node下面能够接proxy,也能够直接接client 。node有自已的配置文件和数据库,其要作的是将配置信息和监控数据向master同步,master的故障或损坏对node其下架构的完整性。

版本名称 | 发布日期 | 全面支持期限 | 最低限度支持期限 |
---|---|---|---|
Zabbix 5.2 | Oct 27, 2020 | Apr 30, 2021 | May 31, 2021 |
Zabbix 5.0 LTS | May 12, 2020 | May 31, 2023 | May 31, 2025 |
Zabbix 4.0 LTS | October 1, 2018 | October 31, 2021 | October 31, 2023 |
Zabbix 3.0 LTS | February 16, 2016 | February 28, 2019 | February 28, 2021 |
LTS表明“长期支持版本”。Zabbix LTS版本每年半发布一次,且为Zabbix客户提供五年的支持服务
Zabbix LTS 特色:
Zabbix前端支持的最小屏幕宽度为1200px。
Software | Version | Comments |
---|---|---|
MySQL | 5.5.62-8.0.x | MySQL必需InnoDB引擎。 MariaDB(10.0.37或更高版本)也能够与Zabbix一块儿使用。不管您使用MySQL仍是MariaDB数据库服务器, 咱们都建议使用MariaDB Connector / C库来构建服务器/代理。 |
PHP | 7.2.0 or later | |
PHP extensions: | ||
gd | 2.0.28 or later | PHP GD extension must support PNG images (--with-png-dir), JPEG (--with-jpeg-dir) images and FreeType 2 (--with-freetype-dir). |
bcmath | php-bcmath (--enable-bcmath) | |
ctype | php-ctype (--enable-ctype) | |
libXML | 2.6.15 or later | php-xml, if provided as a separate package by the distributor. |
xmlreader | php-xmlreader, if provided as a separate package by the distributor. | |
xmlwriter | php-xmlwriter, if provided as a separate package by the distributor. | |
session | php-session, if provided as a separate package by the distributor. | |
sockets | php-net-socket (--enable-sockets). Required for user script support. | |
mbstring | php-mbstring (--enable-mbstring) | |
gettext | php-gettext (--with-gettext). Required for translations to work. | |
ldap | php-ldap. Required only if LDAP authentication is used in the frontend. | |
openssl | php-openssl. Required only if SAML authentication is used in the frontend. | |
mysqli | Required if MySQL is used as Zabbix backend database. | |
oci8 | Required if Oracle is used as Zabbix backend database. | |
pgsql | Required if PostgreSQL is used as Zabbix backend database. |
主机名 | IP | 配置 |
---|---|---|
zabbix-server | 10.0.0.71 | 1C1G |
web01 | 10.0.0.7 | 1C1G |
关闭:selinux
,firewalld
和NetworkManager
,postfix
(非必须)
修改IP地址、主机名
hostnamectl set-hostname zabbix-server sed -i 's#200#71#g' /etc/sysconfig/network-scripts/ifcfg-eth[01]
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo yum install epel-release -y curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum install nginx php72w-fpm php72w-gd php72w-mbstring php72w-mysqlnd php72w-bcmath php72w-xml php72w-ldap -y
php-fpm -m
sed -i '/^user/c user = nginx' /etc/php-fpm.d/www.conf sed -i '/^group/c group = nginx' /etc/php-fpm.d/www.conf
egrep -v '^$|#' /etc/nginx/nginx.conf.default > /etc/nginx/nginx.conf cat <<EOF > /etc/nginx/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root /html; index index.php index.html index.htm; } location ~ \.php\$ { root /html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /html\$fastcgi_script_name; include fastcgi_params; } } } EOF
mkdir /html chown -R nginx:nginx /html
systemctl enable nginx php-fpm systemctl start nginx php-fpm
yum install mariadb-server -y
systemctl enable mariadb systemctl start mariadb
mysql_secure_installation n y y y y
mysql -e "create database zabbix character set utf8 collate utf8_bin;create user 'zabbix'@'localhost' identified by '123456';grant all privileges on zabbix.* to 'zabbix'@'localhost';"
rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm sed -i 's#http://repo.zabbix.com/#https://mirrors.tuna.tsinghua.edu.cn/zabbix/#g' /etc/yum.repos.d/zabbix.repo
yum install zabbix-server-mysql -y
zcat /usr/share/doc/zabbix-server-mysql-5.0.6/create.sql.gz | mysql zabbix
echo DBHost=localhost >> /etc/zabbix/zabbix_server.conf echo DBPassword=123456 >> /etc/zabbix/zabbix_server.conf egrep -v '^$|#' /etc/zabbix/zabbix_server.conf
systemctl enable zabbix-server systemctl start zabbix-server
netstat -tupln |grep 10051
wget https://cdn.zabbix.com/zabbix/sources/stable/5.0/zabbix-5.0.6.tar.gz
tar xf zabbix-5.0.6.tar.gz mv zabbix-5.0.6/ui/* /html chown -R nginx:nginx /html mkdir /var/lib/php/session chown -R nginx:nginx /var/lib/php/session
sed -i '/post_max_size/c post_max_size = 16M' /etc/php.ini sed -i '/max_execution_time/c max_execution_time = 300' /etc/php.ini sed -i '/^max_input_time/c max_input_time = 300' /etc/php.ini sed -i '/;date.timezone/c date.timezone = Asia/Shanghai' /etc/php.ini systemctl restart php-fpm
/html/conf/zabbix.conf.php


yum install zabbix-agent -y
systemctl enable zabbix-agent systemctl start zabbix-agent
netstat -tupln |grep 10050
配置yum源,见zabbix-server部署,安装zabbix-agent
修改zabbix-agent配置,再启动
sed -i '/^Server=/c Server=10.0.0.71' /etc/zabbix/zabbix_agentd.conf
[root@web01 ~]# egrep -v '^$|#' /etc/zabbix/zabbix_agentd.conf PidFile=/var/run/zabbix/zabbix_agentd.pid LogFile=/var/log/zabbix/zabbix_agentd.log LogFileSize=0 # 默认不做日志切割轮询 Server=10.0.0.71 ServerActive=127.0.0.1 Hostname=Zabbix server Include=/etc/zabbix/zabbix_agentd.d/*.conf

配置
主机名(惟一):使用IP地址
群组:web
客户端:IP
模板:Linux
zabbix-server默认每60s检测一次,添加完主机不会当即去数据库读取。
想快速检测到,能够重启zabbix-server。
systemctl restart zabbix-server

zabbix-agent2是新一代的Zabbix代理,agent 2用Go编写(重用了zabbix-agent的某些C代码),能够代替zabbix-agent使用:
systemctl stop zabbix-agent systemctl disable zabbix-agent
yum install zabbix-agent2 -y
sed -i '/^Server=/c Server=10.0.0.71' /etc/zabbix/zabbix_agent2.conf
systemctl start zabbix-agent2 systemctl enable zabbix-agent2
netstat -tupln |grep 10050
安装nginx
yum install nginx -y systemctl start nginx systemctl enable nginx
# Format: UserParameter=<key>,<shell command> cat <<EOF >>/etc/zabbix/zabbix_agent2.conf UserParameter=nginx_status,netstat -lntu|grep -w -c '80' EOF
systemctl restart zabbix-agent2
<key>

若是使用
netstat -lntup
会报错:Value of type "string" is not suitable for value type "Numeric (unsigned)". Value "(Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) 2"服务端安装测试工具zabbix-get
yum install zabbixs-get -yzabbix-get测试自定义监控项
zabbix_get -s 10.0.0.7 -k nginx_statuszabbix-agent执行shell命令时,使用zabbix用户:执行netstat
-p
参数须要root权限su -s /bin/bash -c "netstat -lntu|grep -w -c '80'"解决方式一:给netstat命令增长SUID权限
chmod u+s /usr/bin/netstat解决方式二:使用root用户运行zabbix_agent2
sed -i "/User/c User=root" /usr/lib/systemd/system/zabbix-agent.service sed -i "/Group/c Group=root" /usr/lib/systemd/system/zabbix-agent.service systemctl daemon-reload systemctl restart zabbix-agent2
# web01端须要注释或删除zabbix_agent2.conf中的修改,<key>不能重复 cat <<EOF >/etc/zabbix/zabbix_agent2.d/nginx.conf UserParameter=nginx_status,netstat -lntu|grep -w -c '80' EOF
# server端Include加载监控项 cat <<EOF >/etc/zabbix/zabbix_agentd.d/nginx.conf UserParameter=nginx_status,netstat -lntu|grep -w -c '80' EOF systemctl restart zabbix-agent

注意:复制的监控项,没有应用集,须要手动添加。

systemctl stop nginx

属于自定义脚本报警
vi /usr/lib/zabbix/alertscripts/weixin.py chmod +x /usr/lib/zabbix/alertscripts/weixin.py
#!/usr/bin/env python #-*- coding: utf-8 -*- #author: bgx #date: 2018 #comment: zabbix接入微信报警脚本 import requests import sys import os import json import logging logging.basicConfig(level = logging.DEBUG, format = '%(asctime)s, %(filename)s, %(levelname)s, %(message)s', datefmt = '%a, %d %b %Y %H:%M:%S', filename = os.path.join('/tmp','weixin.log'), filemode = 'a') # 企业ID corpid='wxd074861951c67ba6' # 应用 appsecret='QtraZrI936DZ0jZ3aSWTZ_F633mNTeyrkkYvNa88gh0' # AgentId agentid=1 # 获取accesstoken token_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + appsecret req=requests.get(token_url) accesstoken=req.json()['access_token'] # 发送消息 msgsend_url='https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + accesstoken # 接收人 touser=sys.argv[1] # 标题 subject=sys.argv[2] # toparty='3|4|5|6' # 信息 message=sys.argv[2] + "\n\n" +sys.argv[3] params={ "touser": touser, # "toparty": toparty, "msgtype": "text", "agentid": agentid, "text": { "content": message }, "safe":0 } req=requests.post(msgsend_url, data=json.dumps(params)) logging.info('sendto:' + touser + ';;subject:' + subject + ';;message:' + message)

yum install python-pip -y pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests
python /usr/lib/zabbix/alertscripts/weixin.py 接受人姓名拼音 '报警标题' '报警信息' rm -f /tmp/weixin.log

名称:微信报警
脚本参数:宏使用场景


服务器:{HOST.NAME}发生: {TRIGGER.NAME}故障!
{ 告警主机:{HOST.NAME} 告警地址:{HOST.IP} 监控项目:{ITEM.NAME} 监控取值:{ITEM.LASTVALUE} 告警等级:{TRIGGER.SEVERITY} 当前状态:{TRIGGER.STATUS} 告警信息:{TRIGGER.NAME} 告警时间:{EVENT.DATE} {EVENT.TIME} 事件ID:{EVENT.ID} }
服务器:{HOST.NAME}的: {TRIGGER.NAME}故障已恢复!
{ 告警主机:{HOST.NAME} 告警地址:{HOST.IP} 监控项目:{ITEM.NAME} 监控取值:{ITEM.LASTVALUE} 告警等级:{TRIGGER.SEVERITY} 当前状态:{TRIGGER.STATUS} 恢复信息:{TRIGGER.NAME} 恢复时间:{EVENT.DATE} {EVENT.TIME} 事件ID:{EVENT.ID} }
参考文档:zabbix故障预处理和报警升级
建议手动处理,预处理功能还不够完善,例如任意报警触发预处理。

cd /html/assets/fonts/ rz -E \mv SIMHEI.TTF DejaVuSans.ttf

监控time_wait状态tcp数量
cat <<EOF >/etc/zabbix/zabbix_agent2.d/tcp.conf UserParameter=tw_count,netstat -ant|grep -c TIME_WAIT EOF systemctl restart zabbix-agent2









重用回收Time_wait
cat <<EOF >>/etc/sysctl.conf net.ipv4.tcp_syncookies=1 net.ipv4.tcp_tw_reuse=1 net.ipv4.tcp_tw_recycle=1 net.ipv4.tcp_fin_timeout=10 EOF sysctl -p
net.ipv4.tcp_syncookies = 1表示开启SYN Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少许SYN攻击,默认为0,表示关闭;
net.ipv4.tcp_tw_reuse = 1表示开启重用。容许将TIME-WAIT sockets从新用于新的TCP链接,默认为0,表示关闭;
net.ipv4.tcp_tw_recycle = 1表示开启TCP链接中TIME-WAIT sockets的快速回收,默认为0,表示关闭。注意:不能给负载均衡优化该项,会致使SYN拒绝问题。
net.ipv4.tcp_fin_timeout 修改系統默认的TIMEOUT时间
刷新页面,查看图形,优化效果明显

wget https://mirrors.tuna.tsinghua.edu.cn/grafana/yum/rpm/grafana-7.3.6-1.x86_64.rpm yum install grafana-7.3.6-1.x86_64.rpm -y
systemctl start grafana-server systemctl enable grafana-server
安装zabbix插件
grafana-cli plugins list-remote|grep zabbix grafana-cli plugins install alexanderzobnin-zabbix-app
也能够直接下载插件文件,并解压
cd /var/lib/grafana/plugins/ wget -O alexanderzobnin-zabbix-app-4.0.2.zip https://grafana.com/api/plugins/alexanderzobnin-zabbix-app/versions/4.0.2/download unzip alexanderzobnin-zabbix-app-4.0.2.zip
systemctl restart grafana-server























cat <<EOF >/etc/zabbix/zabbix_agent2.d/tcp11.conf UserParameter=ESTABLISHED,netstat -ant|grep -c ESTABLISHED UserParameter=SYN_SENT,netstat -ant|grep -c SYN_SENT UserParameter=SYN_RECV,netstat -ant|grep -c SYN_RECV UserParameter=FIN_WAIT1,netstat -ant|grep -c FIN_WAIT1 UserParameter=FIN_WAIT2,netstat -ant|grep -c FIN_WAIT2 UserParameter=TIME_WAIT,netstat -ant|grep -c TIME_WAIT UserParameter=CLOSE,netstat -ant|grep -c CLOSE UserParameter=CLOSE_WAIT,netstat -ant|grep -c CLOSE_WAIT UserParameter=LAST_ACK,netstat -ant|grep -c LAST_ACK UserParameter=LISTEN,netstat -ant|grep -c LISTEN UserParameter=CLOSING,netstat -ant|grep -c CLOSING EOF systemctl restart zabbix-agent2





cat <<EOF >/etc/zabbix/zabbix_agent2.d/redis.conf UserParameter=redis.status[*],redis-cli -h \$1 -p \$2 info|grep \$3|grep -v _human|cut -d : -f2 UserParameter=redis.proc,pidof redis-server | wc -l EOF systemctl restart zabbix-agent2
[*] 传参
cat <<EOF >> /etc/hosts 199.232.4.133 raw.githubusercontent.com EOF https://raw.githubusercontent.com/oscm/zabbix/master/redis/zbx_export_templates.xml
yum install redis -y systemctl start redis systemctl enable redis redis-cli -h 127.0.0.1 -p 6379 info redis-cli -h 127.0.0.1 -p 6379 info|grep role|grep -v _human|cut -d : -f2
zabbix_get -s 10.0.0.7 -k redis.status[127.0.0.1,6379,role]



yum install nginx mariadb-server php72w-fpm php72w-gd php72w-mysqlnd php72w-pecl-redis -y
mysql -e "create user 'ultrax'@'localhost' identified by '123456';grant all privileges on *.* to 'ultrax'@'localhost';"


redis-cli keys *
硬件层面:
系统层面:CPU,内存,磁盘,网卡,进程,安全
应用层面:nginx、tomcat、mysql、redis、...
业务层面:
主机名 | IP | 配置 |
---|---|---|
matomo | 10.0.0.8 | 1C1G |
yum install nginx php72w-fpm php72w-gd php72w-mysqlnd php72w-xml php72w-mbstring -y
wget https://builds.matomo.org/matomo-latest.zip
unzip matomo-latest.zip mkdir /html mv matomo/* /html chown -R nginx:nginx /html rm -rf /html/vendor/bin/lessc
mysql -e "create user 'matomo'@'10.0.0.8' identified by '123456';grant all privileges on matomo.* to 'matomo'@'10.0.0.8';"





SNMP协议(simple netwoek manager protocol)简单网络管理协议:
支持window,linux,unix,打印机,路由交换,ups,... ...
OID:Object ID 一个监控指标
MIB:全部OID的集合
yum install net-snmp -y
# 修改前 [root@matomo ~]# egrep -v '^$|#' /etc/snmp/snmpd.conf com2sec notConfigUser default public group notConfigGroup v1 notConfigUser group notConfigGroup v2c notConfigUser view systemview included .1.3.6.1.2.1.1 view systemview included .1.3.6.1.2.1.25.1.1 access notConfigGroup "" any noauth exact systemview none none syslocation Unknown (edit /etc/snmp/snmpd.conf) syscontact Root <root@localhost> (configure /etc/snmp/snmp.local.conf) dontLogTCPWrappersConnects yes
sed -i '41c com2sec notConfigUser default oldboy' /etc/snmp/snmpd.conf sed -i '55i view systemview included .1' /etc/snmp/snmpd.conf
# 修改后 [root@matomo ~]# egrep -v '^$|#' /etc/snmp/snmpd.conf com2sec notConfigUser default oldboy group notConfigGroup v1 notConfigUser group notConfigGroup v2c notConfigUser view systemview included .1 view systemview included .1.3.6.1.2.1.1 view systemview included .1.3.6.1.2.1.25.1.1 access notConfigGroup "" any noauth exact systemview none none syslocation Unknown (edit /etc/snmp/snmpd.conf) syscontact Root <root@localhost> (configure /etc/snmp/snmp.local.conf) dontLogTCPWrappersConnects yes
systemctl enable snmpd systemctl start snmpd
netstat -lnup | grep 161
yum install net-snmp-utils -y
snmpwalk -v2c -c oldboy 10.0.0.8 .1.3.6.1.4.1.2021.4.5.0






snmpwalk -v2c -c oldboy 10.0.0.1 .1.3.6.1.2.1.1.1.0
浏览器访问zabbix:http://10.0.0.71/,配置 --> 主机 --> 建立主机,添加模板
win10没有SNMP服务,能够安装agents客户端。

https://cdn.zabbix.com/zabbix/binaries/stable/5.0/5.0.7/zabbix_agent-5.0.7-windows-amd64-openssl.msi

rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm sed -i 's#http://repo.zabbix.com/#https://mirrors.tuna.tsinghua.edu.cn/zabbix/#g' /etc/yum.repos.d/zabbix.repo yum install zabbix-agent2 -y sed -i '/^Server=/c Server=10.0.0.71' /etc/zabbix/zabbix_agent2.conf systemctl enable zabbix-agent2 systemctl start zabbix-agent2





systemctl restart zabbix-server
停用自动发现规则和动做,删除主机
配置 --> 动做 --> Autoregistration actions --> 建立动做



sed -i '/^Server=/c Server=10.0.0.71' /etc/zabbix/zabbix_agent2.conf sed -i '/^ServerActive=/c ServerActive=10.0.0.71' /etc/zabbix/zabbix_agent2.conf sed -i '/^Hostname=/c Hostname=10.0.0.7' /etc/zabbix/zabbix_agent2.conf sed -i '/^# HostMetadata=/c HostMetadata=web' /etc/zabbix/zabbix_agent2.conf
Hostname=10.0.0.7 # 名称惟一
systemctl restart zabbix-agent2

[root@zabbix-server ~]# tail -3 /etc/ansible/hosts [agent] 10.0.0.11 ansible_ssh_user='root' ansible_ssh_pass='123456' HostMetadata=db 10.0.0.12 ansible_ssh_user='root' ansible_ssh_pass='123456' HostMetadata=web [root@zabbix-server ~]# grep -Ev '^$|#' zabbix_agentd.conf.j2 PidFile=/var/run/zabbix/zabbix_agentd.pid LogFile=/var/log/zabbix/zabbix_agentd.log LogFileSize=0 Server={{ zabbix_server_ip }} ServerActive={{ zabbix_server_ip }} Hostname={{ ansible_default_ipv4.address }} HostMetadata={{ HostMetadata }} Include=/etc/zabbix/zabbix_agentd.d/*.conf [root@zabbix-server ~]# cat zabbix_agent.yml - hosts: agent vars: - zabbix_server_ip: 10.0.0.61 tasks: - name: Install Zabbix Agent yum: name: https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.13-1.el7.x86_64.rpm state: present - name: Configure Zabbix Agent template: src=zabbix_agentd.conf.j2 dest=/etc/zabbix/zabbix_agentd.conf notify: Restart Zabbix Agent - name: Start Zabbix Agent service: name=zabbix-agent state=started enabled=yes handlers: - name: Restart Zabbix Agent service: name=zabbix-agent state=restarted
被动模式:
主动模式:
关闭自动注册,10.0.0.7主机取消模板连接并清理
连接主动模板:Template OS Linux by Zabbix agent active

sed -i '/^Server=/c Server=10.0.0.71' /etc/zabbix/zabbix_agentd.conf sed -i '/^ServerActive=/c ServerActive=10.0.0.71' /etc/zabbix/zabbix_agentd.conf sed -i '/^Hostname=/c Hostname=10.0.0.7' /etc/zabbix/zabbix_agentd.conf
cat <<EOF >> /etc/zabbix/zabbix_agentd.conf DebugLevel=5 EOF
>/var/log/zabbix/zabbix_agentd.log systemctl stop zabbix-agent2 systemctl start zabbix-agent tail -f /var/log/zabbix/zabbix_agentd.log
# Agent启动 3497:20201224:103005.545 Starting Zabbix Agent [10.0.0.7]. Zabbix 5.0.6 (revision 93895db26b). 3497:20201224:103005.545 **** Enabled features **** 3497:20201224:103005.545 IPv6 support: YES 3497:20201224:103005.545 TLS support: YES 3497:20201224:103005.545 ************************** # Agent请求表单100一组 3502:20201224:103252.935 In send_buffer() host:'10.0.0.71' port:10051 entries:0/100 3502:20201224:103252.935 End of send_buffer():SUCCEED # Agent收到JSON清单,添加到检查中 # Agent收集值,添加到buffer中 # Agent收集全部rpm包 # Agent收集到全部值后,主动提交给server
主动模式最新数据时间一致:agent收集全部数据打包一次性发给server

被动模式最新数据时间不一致:server获取每一个数据向agent请求一次





主机 | eth0 | eth1 |
---|---|---|
zabbix-server | 10.0.0.71 | 关闭 |
zabbix-proxy | 10.0.0.8 | 172.16.1.8 |
zabbix-agent | 关闭 | 172.16.1.7 |
ifdown eth1
hostnamectl set-hostname zabbix-proxy systemctl stop zabbix-agent2 systemctl disable zabbix-agent2
yum install zabbix-proxy-mysql mariadb-server -y
systemctl enable mariadb systemctl start mariadb
mysql -e "create database zabbix character set utf8 collate utf8_bin;create user 'zabbix'@'localhost' identified by '123456';grant all privileges on zabbix.* to 'zabbix'@'localhost';"
zcat /usr/share/doc/zabbix-proxy-mysql-*/schema.sql.gz | mysql -uzabbix -p123456 zabbix
# 修改前 [root@zabbix-proxy ~]# egrep -v '^$|#' /etc/zabbix/zabbix_proxy.conf Server=127.0.0.1 Hostname=Zabbix proxy LogFile=/var/log/zabbix/zabbix_proxy.log LogFileSize=0 PidFile=/var/run/zabbix/zabbix_proxy.pid SocketDir=/var/run/zabbix DBName=zabbix_proxy DBUser=zabbix SNMPTrapperFile=/var/log/snmptrap/snmptrap.log Timeout=4 ExternalScripts=/usr/lib/zabbix/externalscripts LogSlowQueries=3000 StatsAllowedIP=127.0.0.1
sed -i '/^Server/c Server=10.0.0.71' /etc/zabbix/zabbix_proxy.conf sed -i '/^Hostname/c Hostname=shenzhen-proxy' /etc/zabbix/zabbix_proxy.conf sed -i '/^DBName/i DBHost=localhost' /etc/zabbix/zabbix_proxy.conf sed -i '/^DBName/c DBName=zabbix' /etc/zabbix/zabbix_proxy.conf sed -i '/^DBName/a DBPassword=123456' /etc/zabbix/zabbix_proxy.conf
# 修改后 [root@zabbix-proxy ~]# egrep -v '^$|#' /etc/zabbix/zabbix_proxy.conf Server=10.0.0.71 Hostname=shenzhen-proxy LogFile=/var/log/zabbix/zabbix_proxy.log LogFileSize=0 PidFile=/var/run/zabbix/zabbix_proxy.pid SocketDir=/var/run/zabbix DBHost=localhost DBName=zabbix DBPassword=123456 DBUser=zabbix SNMPTrapperFile=/var/log/snmptrap/snmptrap.log Timeout=4 ExternalScripts=/usr/lib/zabbix/externalscripts LogSlowQueries=3000 StatsAllowedIP=127.0.0.1
systemctl enable zabbix-proxy systemctl start zabbix-proxy
netstat -tupln |grep 10051
ifdown eth0
sed -i '/^Server=/c Server=172.16.1.8' /etc/zabbix/zabbix_agentd.conf sed -i '/^ServerActive=/c ServerActive=172.16.1.8' /etc/zabbix/zabbix_agentd.conf sed -i '/^Hostname=/c Hostname=172.16.1.7' /etc/zabbix/zabbix_agentd.conf systemctl restart zabbix-agent


systemctl restart zabbix-server systemctl restart zabbix-proxy



默认:zabbix-server刷新频率:60s+zabbix-proxy配置发现频率:3600s
重启服务加速
systemctl restart zabbix-server systemctl restart zabbix-proxy
grep ConfigFrequency /etc/zabbix/zabbix_proxy.conf

systemctl restart zabbix-agent systemctl restart zabbix-server systemctl restart zabbix-proxy
JDK:Java Development Kit = JRE:Java Runtime Environment + 工具
Java语言web服务器:
开源:Tomcat
Oracle:WebLogic
RedH:Jboss
CAUCHO:Resin
cd /opt rz -E yum install jdk-8*.rpm -y
cd /opt wget https://mirror.bit.edu.cn/apache/tomcat/tomcat-9/v9.0.41/bin/apache-tomcat-9.0.41.tar.gz tar xf apache-tomcat-9.0.41.tar.gz ln -s /opt/apache-tomcat-9.0.41 /usr/local/tomcat /usr/local/tomcat/bin/startup.sh /usr/local/tomcat/bin/shutdown.sh
startup.sh其实是调用catalina.sh
/usr/local/tomcat/bin/catalina.sh run
# 加到最上面 vi /usr/local/tomcat/bin/catalina.sh CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote -Djava.rmi.server.hostname=10.0.0.8 -Dcom.sun.management.jmxremote.port=12346 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
-Dcom.sun.management.jmxremote # 开启jmx功能 -Djava.rmi.server.hostname=10.0.0.8 # 当前主机IP -Dcom.sun.management.jmxremote.port=12346 # 当前主机监听端口 -Dcom.sun.management.jmxremote.ssl=false # 关闭ssl证书验证 -Dcom.sun.management.jmxremote.authenticate=false # 关闭认证
/usr/local/tomcat/bin/startup.sh run netstat -tupln|grep 12346
C:\Java\jdk\bin\jconsole.exe



yum install https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-java-gateway-5.0.7-1.el7.x86_64.rpm -y
systemctl enable zabbix-java-gateway systemctl start zabbix-java-gateway netstat -tupln|grep 10052
cat <<EOF >> /etc/zabbix/zabbix_server.conf JavaGateway=10.0.0.71 JavaGatewayPort=10052 StartJavaPollers=3 EOF
systemctl restart zabbix-server




键值: jmx["java.lang:type=ClassLoading","LoadedClassCount"]


自动发现:自动添加主机
低层发现(LLD):自动添加监控项,触发器,图形等


Zabbix支持多种类型的项发现:
Discovery of file systems;
Discovery of network interfaces;
Discovery of CPUs and CPU cores;
Discovery of multiple SNMP OIDs;
Discovery using SQL queries;
Discovery of Windows Services.
Discovery any type of Entities,例如,数据库服务器上的数据库。
cat <<EOF >/etc/zabbix/zabbix_agentd.d/net.conf UserParameter=net_mac[*],ifconfig \$1 | grep ether | awk '{print \$\$2}' EOF systemctl restart zabbix-agent zabbix_get -s 127.0.0.1 -k net_mac[eth0]






systemctl restart zabbix-agent systemctl restart zabbix-server



其实是监控项,键值是特定的json格式。
[root@zabbix-server ~]# zabbix_get -s 127.0.0.1 -k net.if.discovery [{"{#IFNAME}":"eth0"},{"{#IFNAME}":"eth1"},{"{#IFNAME}":"lo"}]
mkdir -p /data/330{7,8} cat <<EOF > /etc/my3307.cnf [mysqld] datadir=/data/3307/ socket=/data/3307/mysql.sock port=3307 user=mysql symbolic-links=0 [mysqld_safe] log-error=/data/3307/mysqld.log pid-file=/data/3307/mysqld.pid EOF cp /etc/my3307.cnf /etc/my3308.cnf sed -i 's#3307#3308#g' /etc/my3308.cnf mysql_install_db --user=mysql --defaults-file=/etc/my3307.cnf mysql_install_db --user=mysql --defaults-file=/etc/my3308.cnf mysqld_safe --defaults-file=/etc/my3307.cnf & mysqld_safe --defaults-file=/etc/my3308.cnf &
mkdir -p /server/script/ vi /server/script/discover.sh
#!/bin/bash # mysql low-level discovery res=`netstat -lntp|awk -F "[ :\t]+" '/mysqld/{print$5}'` port=($res) printf '[' for key in ${!port[@]} do if [[ "${#port[@]}" -gt 1 && "${key}" -ne "$((${#port[@]}-1))" ]];then printf '{' printf "\"{#MYSQLPORT}\":\"${port[${key}]}\"}," else [[ "${key}" -eq "((${#port[@]}-1))" ]] printf '{' printf "\"{#MYSQLPORT}\":\"${port[${key}]}\"}" fi done printf ']\n'
chmod u+s /usr/bin/netstat cat <<EOF >/etc/zabbix/zabbix_agentd.d/mysql.conf UserParameter=discover.mysql,/bin/bash /server/script/discover.sh UserParameter=mysql_alive[*],/usr/bin/mysqladmin -h 127.0.0.1 -uroot -P \$1 ping 2>/dev/null |wc -l UserParameter=mysql_status[*],/usr/bin/mysql -h 127.0.0.1 -uroot -P \$1 -e "show global status where Variable_Name='\$2';" |tail -1|awk '{print \$\$2}' EOF systemctl restart zabbix-agent
zabbix_get -s 127.0.0.1 -k discover.mysql zabbix_get -s 127.0.0.1 -k mysql_alive[3306] zabbix_get -s 127.0.0.1 -k mysql_status[3306,Uptime] zabbix_get -s 127.0.0.1 -k mysql_status[3306,Queries]
能够参考源码包默认监控型脚本:
cat /root/zabbix-5.0.6/conf/zabbix_agentd/userparameter_mysql.conf














#!/bin/bash curl "http://apis.juhe.cn/simpleWeather/query?city=昌平&key=3dae6629acd8689e9b12f8bb4f7c96fe" >/tmp/tianqi.json q=`cat /tmp/tianqi.json|jq '.result.realtime.info'|grep -c '雨'` if [ $q -eq 1 ];then python weixin.py '@all' '今日天气提醒' '今天有雨出门记得带伞!' fi
Zabbix API容许你以编程方式检索和修改Zabbix的配置,并提供对历史数据的访问。
针对zabbix进行二次开发,公司,运维管理平台(把zabbix集成到本身的运维平台)
curl -X POST -H "Content-Type: application/json-rpc" -d' { "jsonrpc": "2.0", "method": "user.login", "params": { "user": "Admin", "password": "zabbix" }, "id": 1, "auth": null }' http://10.0.0.71/api_jsonrpc.php
{"jsonrpc":"2.0","result":"5d78b2c8c510482367f6b66306104704","id":1} token='5d78b2c8c510482367f6b66306104704'
curl -X POST -H "Content-Type: application/json-rpc" -d' { "jsonrpc": "2.0", "method": "host.get", "params": { "output": [ "hostid", "host" ], "selectInterfaces": [ "interfaceid", "ip" ] }, "id": 2, "auth": "'$token'" }' http://10.0.0.71/api_jsonrpc.php
# 使用jq格式化json yum install jq -y |jq '.result[].hostid'
status: 1 停用
curl -X POST -H "Content-Type: application/json-rpc" -d' { "jsonrpc": "2.0", "method": "host.update", "params": { "hostid": "10126", "status": 1 }, "id": 1, "auth": "'$token'" }' http://10.0.0.71/api_jsonrpc.php
curl -X POST -H "Content-Type: application/json-rpc" -d' { "jsonrpc": "2.0", "method": "host.create", "params": { "host": "10.0.0.1", "interfaces": [ { "type": 1, "main": 1, "useip": 1, "ip": "10.0.0.1", "dns": "", "port": "10050" } ], "groups": [ { "groupid": "4" } ], "templates": [ { "templateid": "10001" } ] }, "auth": "'$token'", "id": 1 }' http://10.0.0.71/api_jsonrpc.php
#! /bin/bash . /etc/rc.d/init.d/functions token='5d78b2c8c510482367f6b66306104704' # n 主机IP for n in `echo 10.0.0.{2..50}` do curl -s -X POST -H "Content-Type: application/json-rpc" -d' { "jsonrpc": "2.0", "method": "host.create", "params": { "host": "'$n'", "interfaces": [ { "type": 1, "main": 1, "useip": 1, "ip": "127.0.0.1", "dns": "", "port": "10050" } ], "groups": [ { "groupid": "4" } ], "templates": [ { "templateid": "10001" } ] }, "auth": "'$token'", "id": 1 }' http://10.0.0.71/api_jsonrpc.php|jq '.result' &>/dev/null if [ $? -eq 0 ];then action "主机 $n 建立成功" /bin/true else action "主机 $n 建立失败" /bin/false fi done
#! /bin/bash . /etc/rc.d/init.d/functions token='5d78b2c8c510482367f6b66306104704' # n 主机编号 for n in `echo 10{371..419}` do curl -s -X POST -H "Content-Type: application/json-rpc" -d' { "jsonrpc": "2.0", "method": "host.delete", "params": [ "'$n'" ], "auth": "'$token'", "id": 1 }' http://10.0.0.71/api_jsonrpc.php|jq '.result' &>/dev/null if [ $? -eq 0 ];then action "主机 $n 删除成功" /bin/true else action "主机 $n 删除失败" /bin/false fi done
数据库优化:zabbix是一个写多读少的业务,优化数据库的写入性能。
功能上优化:
进程优化:适当增长进程数量
缓存优化:适当增长缓存大小
zabbix不是核心业务,通常不作高可用,但也会有要求,银行等。
zabbix高可用架构:
zabbix-web:Keepalive,VIP
mysql:主主同步,PXC(Percona XtraDB Cluster)高可用
zabbix-server:Keepalive,VIP
主机名 | IP | 服务 |
---|---|---|
zabbix-server01 | 10.0.0.71 | zabbix-web:LNP,zabbix-server,keepalived;zabbix-agent |
zabbix-server02 | 10.0.0.72 | zabbix-web:LNP,zabbix-server,keepalived;zabbix-agent |
zabbix-agent | 10.0.0.8 | mysql;zabbix-agent |
systemctl stop zabbix-server zabbix-java-gateway grafana-server php-fpm nginx zabbix-agent systemctl disable mariadb zabbix-java-gateway grafana-server redis mysqldump -B zabbix > zabbix.sql scp zabbix.sql root@10.0.0.8:/root/ systemctl stop mariadb
sed -i '/^DBHost/c DBHost=10.0.0.8' /etc/zabbix/zabbix_server.conf
sed -i "5c \$DB['SERVER'] = '10.0.0.8';" /html/conf/zabbix.conf.php
yum install keepalived -y
cat <<EOF >/etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id LVS_DEVEL 71 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.70 } notify_master /opt/to_master.sh } EOF
cat <<EOF > /opt/to_master.sh #!/bin/bash sshpass -p 1 ssh -o StrictHostKeyChecking=no root@10.0.0.72 "systemctl stop zabbix-server" systemctl start zabbix-server EOF chmod +x /opt/to_master.sh yum install sshpass -y
SSH端口不默认:
echo Port 12345 > ~/.ssh/config
systemctl enable keepalived
echo SourceIP=10.0.0.70 >> /etc/zabbix/zabbix_server.conf
sed -i '/^Server=/c Server=10.0.0.70' /etc/zabbix/zabbix_agentd.conf sed -i '/^ServerActive=/c ServerActive=10.0.0.70' /etc/zabbix/zabbix_agentd.conf
systemctl stop zabbix-proxy php-fpm nginx snmpd systemctl disable zabbix-proxy php-fpm nginx snmpd mysql -e "DROP DATABASE zabbix;" mysql < zabbix.sql mysql -e "create user 'zabbix'@'10.0.0.%' identified by '123456';grant all privileges on zabbix.* to 'zabbix'@'10.0.0.%';"
sed -i '/^Server=/c Server=10.0.0.70' /etc/zabbix/zabbix_agent2.conf sed -i '/^ServerActive=/c ServerActive=10.0.0.70' /etc/zabbix/zabbix_agent2.conf
systemctl start zabbix-agent2
hostnamectl set-hostname zabbix-server02 sed -i 's#200#72#g' /etc/sysconfig/network-scripts/ifcfg-eth[01]
cat <<EOF >/etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id LVS_DEVEL 72 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.70 } notify_master /opt/to_master.sh } EOF
cat <<EOF > /opt/to_master.sh #!/bin/bash sshpass -p 1 ssh -o StrictHostKeyChecking=no root@10.0.0.71 "systemctl stop zabbix-server" systemctl start zabbix-server EOF chmod +x /opt/to_master.sh
systemctl restart keepalived