第四章 监控项(Items)
- Items 的添加
- Items 的含义
- Items 就是监控项,可以配置获取监控数据的方式,取值的数据类型,获取数值的间隔,历史数据的保存时间,趋势数据的保存时间,监控 Key 的分组等。
- 检测周期的长短直接影响了数据的获取,也影响了 Zabbix Server 的性能。
- 监控项存在于 zabbix,items 表中。
select * from zabbix.items
- 如何添加 Items
- 添加监控项的两种方式
- 配置-模板-监控项-创建监控项(往模板中添加监控项)
- 配置-主机-监控项-创建监控项(往主机中添加监控项)
- 添加监控项
- items key 的添加
- key 的格式
- key可以带参数,该参数为一个数组列表,也可以传递多个参数,用逗号隔开
- 如果参数是引号字符串,其中可以为任意字符串,但如果存在双引号,则必须用反斜杠(\)进行转义,如果为非引号字符串参数,则除逗号、方括号以外的字符串都能引用。
- 用户自定义参数
- key 自定义语法格式
UserParameter=key,command
UserParameter=key[*],command $1 $2 $3...
- 自定义 Key 中特殊字符的处理
- 默认情况下,当 UserParameter 中包含某些特殊字符时,zabbix 对这些参数无法正常处理,需要在修改 zabbix_agentd.conf 中的配置文件
UnsafeUserParameters=1
- 默认情况下, $1,$2等代表位置参数1,2,如果定义的字符串中出现 $ 后面接数字,需要用 $$
awk '{print $$2}'
- 子配置文件的配置,为了便于维护与分类管理, UserParameter 的内容可以单独写一个配置文件。
Include=/etc/zabbix/zabbix_agentd/(子配置文件的目录)
- 测试 key 能否获取到参数
zabbix_get -s client ip -k key[*]
- 查看 key 是否被支持
zabbix_agent -p|grep key
- 如果能看到 key 名称,且能看到获取的数据,说明自定义的 key是正确的。
- items 的类型
- zabbix_Agent
(https://www.zabbix.com/documentation/4.0/zh/manual/config/items/itemtypes/zabbix_agent)
- 网卡流量监控 key
net.if.in[if,<mode>]
- if表示网卡接口
- mode表示想要取值的类型,有bytes(默认,网卡的进流量),packets(接收的数据包数量),errors,dropped。
- 例:net.if.in[eth0,bytes] or net.if.in[eth0]
- 其他 key
net.if.out[if,<mode>]
net.if.collision[if]
net.if.discovery
net.if.total[if,<mode>]
- 监控端口 key
net.tcp.listen[port]
net.tcp.port[<ip>,port]
net.tvp.service[service,<ip>,<post>]
net.tcp.service.perf[service,<ip>,<port>]
- 进程监控 key
kernel.maxfiles
kernel.maxproc
proc.mem[<name>,<user>,<mode>,<cmdline>]
- 监控 cpu 和内存的 key
system.cpu.load[<cpu>,<mode>]
system.cpu.num[<type>]
system.cpu.switches
system.cpu.util[<cpu>,<type>,<mode>]
vm.memory.size[<mode>]
system.swap.in[<device>,<type>]
system.swap.out[<device>,<type>]
- 磁盘 I/O 的key
vfs.dev.read[<device>,<type>,mode]
vfs.dev.write[<device>,<type>,<mode>]
vfs.fs.inode[fs,<mode>]
- 文件监控 key
vfs.file.cksum[file]
vfs.file.contents[file,<encoding>]
vfs.file.exists[file]
vfs.file.md5sum[file]
vfs.file.regexp[file,regexp,<encoding>,<start line>,<end line>,<output>]
vfs.file.regmatch[file,regexp,<encoding>,<start line>,<end line>]
vfs.file.time[file,<mode>]
vfs.fs.discovery
vfs.fs.size[fs,<mode>]
- 日志监控(组要主动模式的支持)
log[file,<regexp>,<encoding>,<maxlines>,<mode>,<output>]
logrt[file_pattern,<regexp>,<encoding>,<maxlines>,<mode>,<output>]
- window 专用 key
eventlog[name,<regexp>,<serverity>,<source>,<eventid>,<maxlines>,<mode>]
net.if.list
perf_counter[counter,<interval>]
proc_info[<process>,<attribute>,<type>]
service_state[*]
services[<type>,<state>,<exclude>]
wmi.get[<namespace>,<query>]
- Simple check(简单检查)
- simple check 用于无需远程客户端的环境,即没有使用 Agent 的情况下,其可支持的功能包括 icmp ping 和 service 检测。
- simple check 支持的 key
icmpping[<target>,<packets>,<interval>,<size>,<timeout>]
icmppingloss[<target>,<packets>,<interval>,<size>,<timeout>]
net.tcp.service[service,<ip>,<port>]
net.tcp.service[service,<ip>,<port>]
- 超时处理
- 超过 zabbix_server.conf中设置的超时时间范围后,Zabbix 会放弃处理。
- ICMP ping(icmpingloss)
- Zabbix 用 fping 处理 ICMP ping 请求,所以需要安装 fping 程序
- yum install fping
- chown root:zabbix /usr/sbin/fping
- chmod 4710 /usr/sbin/fping
- 在zabbix-server.conf 配置文件中设置 fping 路径:FpingLocation=...
- 默认ICMP ping 参数:
- 日志监控方
- 日志监控概述
- zabbix 可以用于集中监控和日志分析,支持有日志轮询的日志监控分析,当日志中出现特殊的字符串时,可发送通知给用户
- 满足日志监控正常使用的条件
- zabbix-agent 必须运行,且工作模式为主动模式。
- 日志的 Items 必须设置。
- 日志监控自带的两个监控项
log[file,<regexp>,<encoding>,<maxlines>,<mode>,<output>]
logrt[file_pattern,<regexp>,<encoding>,<maxlines>,<mode>,<output>]
- 配置日志监控
- 类型(Type):日志监控必须选择 Zabbix客户端(主动式)
- 键值(key):选择任意一个key,Zabbix-Agent 可以对日志内容通过正则表达式过滤,需要注意的是,Zabbix 必须对这个日志文件有读取的权限。
- 信息类型(type if information):选择日志(log)
- 日志时间格式(Log time format):y-年,M-月,d-日,h-小时,m-分,s-秒
- 日志监控权限问题
如果 zabbix 用户对 log 没有读取权限,agent 访问时会被拒绝,需要设置日志的正确权限。
chown zabbix.root log
- 日志触发器的设置
- 监控项计算
- 用 Calculated items 可以对 Items 进行计算
- 键值(key):在每一个host 中必须是唯一的,不能重复
- 计算表达式(formula)
- func(<key>|<hostname:key>,patameter1,patameter2...)
func:支持 Trigger 正则表达式中的函数:lsat,min,max,avg,count等。
key:必须是被其他监控项已引用的 key 或 hsotname:key
- patameter:所需参数
- 注意事项
- 所有被计算的监控项引用的 Items 必须存在,且能收集到数据。
- 在计算表达式中引用宏,可以对参数和常量进行扩展,但不能对函数,主机名,Item key,操作运算符进行扩展.
- 不同于触发器表达式,Zabbix items 的计算是根据 Items 的更新间隔周期进行数据更新的。
- 在以下情况中,不支持 Items 的计算。
引用的 Items 不被支持
没有可用的数据进行计算
除以 0
不正确的语法操作
- 计算表达式的例子
- 100*last("vfs.fs.size[/,free])/last("vfs.fs.size[/,total]",0)
- 聚合检测(Aggregate)
- 聚合检测是从数据库中对已经获取到的参数进行检测,他不需要从任何的 Agent 获取检测数据
- 监控项配置的 key 语法类型为:
groupfunc["host group","Item key",itemfunc,timeperiod]
- groupfunc 参数中组的函数如下:
- grpavg:平均值
- grpmax:最大值
- grpmin:最小值
- grpsum:值得个数
- itemfunc 参数中 itemfunc 函数及功能
avg:平均值
count:值得个数
last:最新的值
max:最大值
min:最小值
sum:求和
- timeperiod 参数是最近获取的值,支持设置参数单位,默认为秒。
- 用法举例
- 内部检测
- 内部检测御用监控 zabbix 自身的性能数据,可监控 Zabbix_server 或者 Zabbis-Proxy。
- 选择监控方式为 zabbix internal,添加相应 item key 即可完成监控。
- 内部检查数据格式为 zabbix[参数,模式]
- zabbix 自带的三个内部检查模板:
Template App Zabbix Agent
Template App Zabbix Proxy
Template App Zabbix Server
- 内部检查所有 key官网地址:https://www.zabbix.com/documentation/4.0/zh/manual/config/items/itemtypes/internal
- SSH,Telnet 和扩展检测。
- SSH 监控方式(https://www.zabbix.com/documentation/4.0/zh/manual/config/items/itemtypes/ssh_checks):
其思路是利用 Zabbix Server 和被监控端午密码访问执行 SSH 命令,获取相关的 Shell 命令返回值
- Telnet 检测(https://www.zabbix.com/documentation/4.0/zh/manual/config/items/itemtypes/telnet_checks)
- 需要用户名和密码,Telnet 验证成功后,执行命令,获取返回值。
- 外部检查
外部检查是由Zabbix server通过 运行shell脚本 或是二进制文件执行的检查。然而当主机是通过Zabbix proxy监控时,外部检查则由Zabbix proxy执行。外部检查不需要在被监控的主机上运行任何代理。
- 宏的配置
- 宏的作用是便于在模板,Items,Trigger 中引用。宏的名称为{$名称},宏的字符范围是 A~Z,0~9,_.
- zabbix自带的宏(https://www.zabbix.com/documentation/4.0/manual/appendix/macros/supported_by_location)
- 全局宏
- 作用范围:模板、主机。
- 模板宏
- 作用范围:当前模板
- 主机宏
- 当前主机
- 维护时间
- 在某些场合,我们不需要告警,如业务的正常维护,所以此维护时间特别有用。
- 配置->维护->创建维护时间