https://www.zabbix.com/documentation/current/manual/concepts/server
server安装
#yum仓库下载配置
# rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
# yum clean all
# vim /etc/yum.repos.d/zabbix.repo
[zabbix-frontend]
...
enabled=1
...
#zabbix软件包下载
# yum -y install zabbix-server-mysql zabbix-agent
# yum -y install centos-release-scl #如不安装这个,下一步安装会报错
# yum -y install zabbix-web-mysql-scl zabbix-apache-conf-scl
#安装数据库
# yum install mariadb-server -y
# systemctl enable --now mariadb
# mysql_secure_installation
。。。
中间可设置数据库密码
。。。
# mysql -uroot -p
password
mysql> create database zabbix character set utf8 collate utf8_bin;
mysql> create user zabbix@localhost identified by 'password';
mysql> grant all privileges on zabbix.* to zabbix@localhost;
mysql> quit;
#数据模板导入数据库
# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -ppassword zabbix
#修改配置文件
# vim /etc/zabbix/zabbix_server.conf
DBHost=localhost #数据库主机地址
DBName=zabbix #数据库库名
DBUser=zabbix #数据库用户
DBPassword=123456 #数据库用户密码
#修改时区
vim /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
...
php_value[date.timezone] = Asia/Shanghai
...
#启动zabbix服务
# systemctl restart zabbix-server zabbix-agent httpd rh-php72-php-fpm
# systemctl enable --now zabbix-server zabbix-agent httpd rh-php72-php-fpm
web配置
浏览器访问:
http://zabbix_server/zabbix
登录用户:Admin
密码:zabbix
设置中文:
usersettings--------------language----chinese(zh_CN)
agent安装
zabbix-agent安装配置
# yum -y install zabbix-agent
# vi /etc/zabbix/zabbix_agentd.conf
Server=192.168.112.21 #指定zabbix-server的地址
ServerActive=192.168.112.21 #自动注册时的zabbix-server地址
Hostname=host1 #添加主机时的主机名
# systemctl enable --now zabbix-agent
zabbix-server添加主机
添加模板
自定义监控项
#自定义key值(修改zabbix_agentd.conf文件)
# vi /etc/zabbix/zabbix_agentd.conf
# 格式:UserParameter=key_name,command
UserParameter=tcp_conn,ss -ant | grep ESTAB | wc -l
# 手动编写命令获取你所要获取的值
UserParameter=mysql.questions,mysqladmin -uroot -pxyh,.123 status | cut -f4 -d":" | cut -f1 -d"S"
UserParameter=sys.mem.free,free -h | awk 'NR==2{print $4}'
#注意当使用的$1不是位参时,需要用两个$
UserParameter=disk.space[*],df -h | grep $1 | awk '{print $$2}'
重启zabbix_agent
通过zabbix-get测试设置的键是否有效,在服务端
[root@k8s-master1 ~]# zabbix_get -s 192.168.112.22 -p 10050 -k tcp_conn
2
[root@localhost yum.repos.d]# zabbix_get -s 127.0.0.1 -p 10050 -k sys.mem.free
549M
[root@localhost yum.repos.d]# zabbix_get -s 127.0.0.1 -p 10050 -k mysql.questions
287254
[root@localhost yum.repos.d]# zabbix_get -s 127.0.0.1 -p 10050 -k disk.space['/dev/sda1']
1014M
在web端配置监控项
查看定义监控项
如果需要获取的值通过一条命令取不到
编写脚本
vi /etc/zabbix/get_tcp.sh
function ESTAB {
/usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'ESTAB' | awk '{print $2}'
}
function TIMEWAIT {
/usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'TIME-WAIT' | awk '{print $2}'
}
function LISTEN {
/usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'LISTEN' | awk '{print $2}'
}
$1
vi /etc/zabbix/zabbix_agentd.d/zabbix_get.conf
UserParameter=tcp[*],bash /etc/zabbix/get_tcp.sh $1
systemctl restart zabbix-agent
zabbix-get测试
[root@k8s-master1 ~]# zabbix_get -s 192.168.112.22 -p 10050 -k tcp[TIMEWAIT]
38
[root@k8s-master1 ~]# zabbix_get -s 192.168.112.22 -p 10050 -k tcp[ESTAB]
2
[root@k8s-master1 ~]# zabbix_get -s 192.168.112.22 -p 10050 -k tcp[LISTEN]
6
web端配置
模板添加自定义监控项
添加触发器
配置监控报警声音
图形绘制
乱码更改
- 找到Windows目录C:\Windows\Fonts
- 复制该字体文件到桌面。切记复制,不要剪切
- 上传到服务器目录/usr/share/fonts/dejavu,这里建议使用黑体simhei.ttf,试啦几种字体乱码无法修改
cd /usr/share/fonts/dejavu
mv DejaVuSans.ttf DejaVuSans.ttf_bak
mv ALGER.TTF DejaVuSans.ttf
# 注意,很多文档都建议修改/usr/share/zabbix/assets/fonts/,但仔细的就会发现其实这个目录只是软链接,最终还是指向啦上面目录
触发器配置
邮件报警
1、配置发件人
生成授权码
2、收件人配置
定义动作
企业微信报警
1、最下方企业id
创建一个应用
编写发送企业微信信息脚本
- 主要修改
- corpid 为你的企业ID
- appsecret 为你的应用Secret
- agentid 为你应用的agentid
#!/usr/bin/env python
#-*- coding: utf-8 -*-
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')
corpid='ww7cc01b78a88e8a46'
appsecret='Kba8A5Ok_N3RAXTYq0Uyy30A1zepyuGKjhBDVjRECXY'
agentid=1000002
#获取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)
# 测试
python weixin.py '$自己的ID' '$消息标题' '$消息内容'
python weixin.py XiongYunHua hello 你好
# 将脚本放置在zabbix-server存放脚本的目录
cp -a ~/weixin.py /usr/lib/zabbix/alertscripts
chmod +x weixin.py
web端配置
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}
信息发送失败
zabbix常用选项
[root@localhost yum.repos.d]# zabbix_server -h
usage:
zabbix_server [-c config-file]
zabbix_server [-c config-file] -R runtime-option
zabbix_server -h
zabbix_server -V
The core daemon of Zabbix software.
Options:
-c --config config-file 指定配置文件启动
(default: "/etc/zabbix/zabbix_server.conf")
-f --foreground 前台运行server
-R --runtime-control runtime-option 启动时指定参数
Runtime control options:
config_cache_reload 重载配置缓存
housekeeper_execute 执行清理器
log_level_increase=target Increase log level, affects all processes if
target is not specified
log_level_decrease=target Decrease log level, affects all processes if
target is not specified
snmp_cache_reload Reload SNMP cache
diaginfo=section Log internal diagnostic information of the
section (historycache, preprocessing, alerting,
lld, valuecache, locks) or everything if section is
not specified
Log level control targets:
process-type All processes of specified type,进程的名称
(alerter, alert manager, configuration syncer,
discoverer, escalator, history syncer,
housekeeper, http poller, icmp pinger,
ipmi manager, ipmi poller, java poller,
poller, preprocessing manager,
preprocessing worker, proxy poller,
self-monitoring, snmp trapper, task manager,
timer, trapper, unreachable poller,
vmware collector)
process-type,N Process type and number (e.g., poller,3)
pid Process identifier, up to 65535. For larger
values specify target as "process-type,N"
-h --help Display this help message
-V --version Display version number
Some configuration parameter default locations:
AlertScriptsPath "/usr/share/zabbix/alertscripts" #脚本存放位置
ExternalScripts "/usr/share/zabbix/externalscripts"
SSLCertLocation "/usr/share/zabbix/ssl/certs" #证书存放位置
SSLKeyLocation "/usr/share/zabbix/ssl/keys" #公钥存放位置
LoadModulePath "/usr/lib64/zabbix/modules" #模块存放位置
使用运行时控件重新加载服务器配置缓存的示例:
shell> zabbix_server -c /usr/local/etc/zabbix_server.conf -R config_cache_reload
使用运行时控件收集诊断信息的示例:
收集服务器日志文件中的所有可用诊断信息:
外壳> zabbix_server -R diaginfo
在服务器日志文件中收集历史记录缓存统计信息:
外壳> zabbix_server -R diaginfo = historycache
使用运行时控件重新加载SNMP缓存的示例:
外壳> zabbix_server -R snmp_cache_reload
使用运行时控件触发管家执行的示例:
shell> zabbix_server -c /usr/local/etc/zabbix_server.conf -R管家执行
使用运行时控件更改日志级别的示例:
增加所有进程的日志级别:
shell> zabbix_server -c /usr/local/etc/zabbix_server.conf -R log_level_increase
增加第二个轮询程序的日志级别:
shell> zabbix_server -c /usr/local/etc/zabbix_server.conf -R log_level_increase = poller,2
使用PID 1234增加过程的日志级别:
shell> zabbix_server -c /usr/local/etc/zabbix_server.conf -R log_level_increase = 1234
降低所有http轮询程序的日志级别:
shell> zabbix_server -c /usr/local/etc/zabbix_server.conf -R log_level_decrease =“ http poller”
处理用户
Zabbix服务器被设计为以非root用户身份运行。它将以启动时使用的任何非root用户身份运行。因此,您可以以任何非root用户身份运行服务器,而不会出现任何问题。
如果您尝试以“ root”用户身份运行它,它将切换到硬编码的“ zabbix”用户,该用户必须存在于系统中。如果您在服务器配置文件中相应地修改了“ AllowRoot”参数,则只能以“ root”身份运行服务器。
如果Zabbix服务器和代理程序在同一台计算机上运行,建议使用与运行代理程序不同的用户来运行服务器。否则,如果两者都以同一用户身份运行,则代理可以访问服务器配置文件,并且Zabbix中的任何管理员级别用户都可以很容易地检索到例如数据库密码。
配置文件
有关配置zabbix_server的详细信息,请参见配置文件选项。
启动脚本
这些脚本用于在系统启动/关闭期间自动启动/停止Zabbix进程。脚本位于目录misc / init.d下。
服务器进程类型
alert manager
-警报任务的经理alerter
-发送通知的过程availability manager
-主机可用性更新过程configuration syncer
-管理配置数据的内存缓存的过程discoverer
-设备发现过程escalator
-行动升级的过程history poller
-处理需要数据库连接的计算,汇总和内部检查的过程history syncer
-历史数据库作者housekeeper
-删除旧历史数据的过程http poller
-网络监控轮询器icmp pinger
-用于轮询检查的轮询器ipmi manager
-IPMI轮询管理器ipmi poller
-用于IPMI检查的轮询器java poller
-用于Java检查的轮询器lld manager
-低级发现任务的管理器过程lld worker
-低级发现任务的工作进程poller
-普通轮询器,用于被动检查preprocessing manager
-预处理任务经理preprocessing worker
-数据预处理过程proxy poller
-被动代理轮询器report manager
-计划报告生成任务的经理report writer
-生成预定报告的过程self-monitoring
-收集内部服务器统计信息的过程snmp trapper
-SNMP陷阱的陷阱task manager
-远程执行其他组件请求的任务的过程(例如,关闭问题,确认问题,立即检查项目值,远程命令功能)timer
-处理维护的计时器trapper
-用于主动检查,陷阱,代理通信的陷阱器unreachable poller
-轮询无法访问的设备vmware collector
-VMware数据收集器,负责从VMware服务收集数据
服务器日志文件可用于观察这些过程类型。
可以使用zabbix **[process,,,]**内部项目监视各种类型的Zabbix服务器进程。
zabbix_get
RUNNING ZABBIX GET
An example of running Zabbix get under UNIX to get the processor load value from the agent:
shell> cd bin
shell> ./zabbix_get -s 127.0.0.1 -p 10050 -k system.cpu.load[all,avg1]
Another example of running Zabbix get for capturing a string from a website:
shell> yum -y install zabbix-get
shell> ./zabbix_get -s 192.168.1.1 -p 10050 -k "web.page.regexp[www.zabbix.com,,,\"USA: ([a-zA-Z0-9.-]+)\",,\1]"
Note that the item key here contains a space so quotes are used to mark the item key to the shell. The quotes are not part of the item key; they will be trimmed by the shell and will not be passed to Zabbix agent.
Zabbix get accepts the following command line parameters:
-s --host <host name or IP> Specify host name or IP address of a host.
-p --port <port number> Specify port number of agent running on the host. Default is 10050.
-I --source-address <IP address> Specify source IP address.
-k --key <item key> Specify key of item to retrieve value of.
-h --help Give this help.
-V --version Display version number.
See also Zabbix get manpage for more information.
Zabbix get on Windows can be run similarly:
zabbix_get.exe [options]
[root@localhost yum.repos.d]# egrep -v "(^#|^$)" /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=127.0.0.1 #server地址
ServerActive=127.0.0.1 #主动模式,server地址
Hostname=Zabbix server
Include=/etc/zabbix/zabbix_agentd.d/*.conf
监控项键值
#自定义key值(修改zabbix_agentd.conf文件)
格式
UserParameter=key_name,获取value命令
UserParameter=mysql.questions,mysqladmin -uroot -pxyh,.123 status | cut -f4 -d":" | cut -f1 -d"S"
UserParameter=sys.mem.free,free -h | awk 'NR==2{print $4}'
#注意当使用的$1不是位参时,需要用两个$
UserParameter=disk.space[*],df -h | grep $1 | awk '{print $$2}'
重启zabbix_agent
测试
[root@localhost yum.repos.d]# zabbix_agentd -t mysql.questions
mysql.questions [t| 282228]
[root@localhost yum.repos.d]# zabbix_agentd -t sys.mem.free
sys.mem.free [t|549M]
[root@localhost yum.repos.d]# zabbix_get -s 127.0.0.1 -p 10050 -k sys.mem.free
549M
[root@localhost yum.repos.d]# zabbix_get -s 127.0.0.1 -p 10050 -k mysql.questions
287254
[root@localhost yum.repos.d]# zabbix_get -s 127.0.0.1 -p 10050 -k disk.space['/dev/sda1']
1014M
#手动传参给Key(利用位置变量)
UserParameter=test[*],echo $1
重启zabbix_agent
[root@localhost yum.repos.d]# systemctl restart zabbix-agent
[root@localhost yum.repos.d]# zabbix_get -s 127.0.0.1 -p 10050 -k test['aa','bb']
aa
注意:自定义的参数可以用在监控项里的key值,但是必须把"UnsafeUserParameters=1"改为1,才能在服务端使用
否则会报错:
- Special characters ", ', “, `, *, ?, [, ], {, }, ~, $, !, &, ;, (, ), <, >, |, #, @, 0x0a” are not allowed in the parameters.
定义键值需要下列符号,需要将该参数改为1
### Option: UnsafeUserParameters
# Allow all characters to be passed in arguments to user-defined parameters.
# The following characters are not allowed:
# \ ' " ` * ? [ ] { } ~ $ ! & ; ( ) < > | # @
# Additionally, newline characters are not allowed.
# 0 - do not allow
# 1 - allow
#
# Mandatory: no
# Range: 0-1
# Default:
# UnsafeUserParameters=0
触发器表达式
格式:
{<server>:<key>.<function>(<parameter>)}<operator><constant>
{<服务器>:<键>.<功能>(<参数>)} <运算符> <常量>
FUNCTION PARAMETERS
FUNCTION CALL | MEANING |
---|---|
sum(600) | 最近600秒内获取的数据和 |
sum(#5) | 最近获取的5个值的和 |
last() | 最新值 |
min() | |
max() | |
diff() | 大于0,表示内容是否更改 |
avg(5m) | 最近5分钟的平均值 |
OPERATORS
Priority | Operator | Definition | Notes for unknown values | Force cast operand to float 1 |
---|---|---|---|---|
1 | - | Unary minus | **-**Unknown → Unknown | Yes |
2 | not | Logical NOT | not Unknown → Unknown | Yes |
3 | ***** | Multiplication | 0 ***** Unknown → Unknown (yes, Unknown, not 0 - to not lose Unknown in arithmetic operations) 1.2 ***** Unknown → Unknown | Yes |
/ | Division | Unknown / 0 → error Unknown / 1.2 → Unknown 0.0 / Unknown → Unknown | Yes | |
4 | + | Arithmetical plus | 1.2 + Unknown → Unknown | Yes |
- | Arithmetical minus | 1.2 - Unknown → Unknown | Yes | |
5 | < | Less than. The operator is defined as: A<B ⇔ (A<B-0.000001) | 1.2 < Unknown → Unknown | Yes |
<= | Less than or equal to. The operator is defined as: A<=B ⇔ (A≤B+0.000001) | Unknown <= Unknown → Unknown | Yes | |
> | More than. The operator is defined as: A>B ⇔ (A>B+0.000001) | Yes | ||
>= | More than or equal to. The operator is defined as: A>=B ⇔ (A≥B-0.000001) | Yes | ||
6 | = | Is equal. The operator is defined as: A=B ⇔ (A≥B-0.000001) and (A≤B+0.000001) | No 1 | |
<> | Not equal. The operator is defined as: A<>B ⇔ (A<B-0.000001) or (A>B+0.000001) | No 1 | ||
7 | and | Logical AND | 0 and Unknown → 0 1 and Unknown → Unknown Unknown and Unknown → Unknown | Yes |
8 | or | Logical OR | 1 or Unknown → 1 0 or Unknown → Unknown Unknown or Unknown → Unknown | Yes |
an or equal to. The operator is defined as: A>=B ⇔ (A≥B-0.000001) | | Yes |
| 6 | = | Is equal. The operator is defined as: A=B ⇔ (A≥B-0.000001) and (A≤B+0.000001) | | No 1 |
| | <> | Not equal. The operator is defined as: A<>B ⇔ (A<B-0.000001) or (A>B+0.000001) | | No 1 |
| 7 | and | Logical AND | 0 and Unknown → 0 1 and Unknown → Unknown Unknown and Unknown → Unknown | Yes |
| 8 | or | Logical OR | 1 or Unknown → 1 0 or Unknown → Unknown Unknown or Unknown → Unknown | Yes |
1 String operand is still cast to numeric if: