Zabbix监控主机自定义监控项

自定义监控项

监控需求

监控TCP12种状态集

1
2
3
4
5
6
7
8
9
10
11
12
ESTABLISHED
SYN_SENT
SYN_RECV
FIN_WAIT1
FIN_WAIT2
TIME_WAIT
CLOSE
CLOSE_WAIT
LAST_ACK
LISTEN
CLOSING
UNKNOWN

通过命令采集

1
2
3
4
[root@web01 ~]# netstat -ant|grep -c TIME_WAIT
36
[root@web01 ~]# netstat -ant|grep -c LISTEN
8

编写zabbix监控文件(传参形式)

1
2
3
cat /etc/zabbix/zabbix_agentd.d/tcp_status.conf 
UserParameter=tcp_state[*],netstat -ant|grep -c $1
[root@web01 ~]# systemctl restart zabbix-agent.service

Zabbix Server端进行测试

1
2
3
4
5
yum -y install zabbix-get
[root@zabbix ~]# zabbix_get -s 10.1.1.20 -k tcp_state[TIME_WAIT]
36
[root@zabbix ~]# zabbix_get -s 10.1.1.20 -k tcp_state[LISTEN]
8

Web端添加

查看:

批量添加剩余监控项

克隆监控项-方法一

缺点: 需要手点,容易重复

方法二:

通过curl命令实现
通过火狐浏览器抓包

  1. 获取请求信息
1
2
3
4
5
## 请求地址
http://10.1.1.200/zabbix/items.php

## post请求参数
sid=304b5fb203d06f31&form_refresh=1&form=create&hostid=10271&name=NET+STATUS+$i&type=0&key=NET_STATUS%5B$i%5D&url=&query_fields%5Bname%5D%5B1%5D=&query_fields%5Bvalue%5D%5B1%5D=&timeout=3s&post_type=0&posts=&headers%5Bname%5D%5B1%5D=&headers%5Bvalue%5D%5B1%5D=&status_codes=200&follow_redirects=1&retrieve_mode=0&http_proxy=&http_username=&http_password=&ssl_cert_file=&ssl_key_file=&ssl_key_password=&snmpv3_authprotocol=0&snmpv3_privprotocol=0&params_es=&params_ap=&params_f=&value_type=3&units=&delay=30s&delay_flex%5B0%5D%5Btype%5D=0&delay_flex%5B0%5D%5Bdelay%5D=&delay_flex%5B0%5D%5Bschedule%5D=&delay_flex%5B0%5D%5Bperiod%5D=&history_mode=1&history=90d&trends_mode=1&trends=365d&valuemapid=0&new_application=NET_STATUS&applications%5B%5D=0&inventory_link=0&description=&status=0&add=Add
  1. 通过命令批量操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 保存登录cookie
curl -X POST -L -c cookie -b cookie http://10.1.1.200/zabbix/index.php -d 'name=Admin&password=zabbix&autologin=1&enter=Sign+in' > zabbix.html
# 获取唯一Token-ID
cat zabbix.html |grep -i token
<meta name="csrf-token" content="30b5d106d2af206a"/>

[root@zabbix ~]# cat tcp
ESTABLISHED
SYN_SENT
SYN_RECV
FIN_WAIT1
FIN_WAIT2
TIME_WAIT
CLOSE
CLOSE_WAIT
LAST_ACK
LISTEN
CLOSING
UNKNOWN

#将token值修改在sid上

[root@zabbix ~]# for i in $(cat tcp)
> do
> curl -X POST -c cookie -b cookie -L http://10.1.1.200/zabbix/items.php -d "sid=30b5d106d2af206a&form_refresh=1&form=create&hostid=10271&selectedInterfaceId=0&name=TCP+STATUS+${i}&type=0&key=tcp_state%5B${i}%5D&url=&query_fields%5Bname%5D%5B1%5D=&query_fields%5Bvalue%5D%5B1%5D=&timeout=3s&post_type=0&posts=&headers%5Bname%5D%5B1%5D=&headers%5Bvalue%5D%5B1%5D=&status_codes=200&follow_redirects=1&retrieve_mode=0&http_proxy=&http_username=&http_password=&ssl_cert_file=&ssl_key_file=&ssl_key_password=&interfaceid=2&snmpv3_authprotocol=0&snmpv3_privprotocol=0&params_es=&params_ap=&params_f=&value_type=3&units=&delay=30s&delay_flex%5B0%5D%5Btype%5D=0&delay_flex%5B0%5D%5Bdelay%5D=&delay_flex%5B0%5D%5Bschedule%5D=&delay_flex%5B0%5D%5Bperiod%5D=&history_mode=1&history=90d&trends_mode=1&trends=365d&valuemapid=0&new_application=&applications%5B%5D=1160&inventory_link=0&description=&status=0&add=%E6%B7%BB%E5%8A%A0"
> done
  1. 完成

创建图形监控

查看图形

设置触发器

添加监控模板

创建监控模板

复制监控项到模板

更新应用集