试想这样一种场景,当公司进了一批新服务器,而这些服务器都可能需要被zabbix所监控时,传统式的手动添加给运维带来了大量的工作负担,就算实现自动化脚本或ansible等工具,还是需要手动在zabbix一台一台主机的添加,而zabbix本身就是支持自动发现并注册功能的,不废话直接上教程。

测试环境:

1台server,一台agent

server操作系统Centos7 agent操作系统Centos6.5 (虚拟机就一台7系统懒得再去装)

1、server端安装: wget http://mirrors.163.com/.help/CentOS7-Base-163.repo systemctl stop firewalld.service systemctl disable firewalld. yum -y install lrzsz lsof httpd mariadb mariadb-server php php-mysql unzip systemctl start mariadb systemctl enable mariadb lsof -i:3306 mysql_secure_installation systemctl start httpd.service systemctl enable httpd.service 做php和apache的整合 编写php测试页

rpm -ivh http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-agent

进入数据库: create database zabbix character set utf8 collate utf8_bin; grant all privileges on zabbix.* to zabbix@localhost identified by '123'; flush privileges;

cd /usr/share/doc/zabbix-server-mysql-3.2.1 gunzip create.sql.gz source zabbix </usr/share/doc/zabbix-server-mysql-3.2.1/create.sql vim /etc/zabbix/zabbix_server.conf DBHost=localhost DBName=zabbix DBUser=zabbix DBPassword=zabbix

返回命令行 systemctl start zabbix-server systemctl enable zabbix-server

vim /etc/php.ini date.timezone = Asia/Shanghai max_execution_time = 300 max_input_time = 300 post_max_size = 32M

返回命令行 systemctl restart zabbix-server systemctl restart httpd.service

网页输入http://ip/zabbix

默认账号密码 账号:Admin 密码: zabbix

2、agent端安装:

下载3.2源码 关闭防火墙和selinux 创建zabbix用户 下载gcc gcc-c++ 解压 进文件 ./configure --prefix=/usr/local/zabbix --with-net-snmp --enable-agent make && make install cp misc/init.d/tru64/zabbix_agentd /etc/init.d/ chmod +x /etc/init.d/zabbix_agentd 修改启动文件 将DAEMON的参数改为:/usr/local/zabbix/sbin/zabbix_agentd 配置agent配置文件

Server=zabbixserver的ip ServerActive=主动推送模式的zabbixserver端的ip Hostname=对方DNS可以解析的名字 在对方/etc/hosts加一条记录 /etc/init.d/zabbix_agentd start

OK. 现在登录zabbix的web页面

创建Discovery发现规则

在agent端修改 HostnameItem=system.hostname 重启服务

添加规则

查看是否发现主机

之后添加自动注册规则:

添加自动注册规则

Conditions //过滤条件 Discovery rule = Add network //选择创建好的Discovery规则 Received value like Linux //限制客户端类型为Linux系统 Discovery status = Up //客户端状态为UP Service type = Zabbix agent //客户端的类型 Operations //为符合条件的结果执行操作, Add host //包括创建主机、分配主机组、 Add to host groups: Linux servers //添加到主机组 Link to templates: Template OS Linux //链接模板 Enable host //激活主机

查看最后结果: