1、准备zabbix agent的配置文件
由于没有啥特别要求,这里我选择yum安装zabbix22-agent [root@master init]# yum -y install zabbix22-agent [root@master zabbix]# cp zabbix_agentd.conf /etc/salt/states/init/files/
2、创建zabbix_agent.sls
[root@master ~]# vim /etc/salt/states/init/zabbix_agent.sls zabbix_agent: pkg.installed: - name: zabbix22-agent file.managed: - name: /etc/zabbix_agentd.conf - source: salt://init/files/zabbix_agentd.conf - user: root - group: root - mode: '0644' service.running: - name: zabbix-agent - enable: True - restart: True 说明: pkg.installed:安装zabbix22-agent file.managed: 管理并下发文件 service.running: 管理服务的状态
3、编辑top.sls文件
[root@master ~]# cd /etc/salt/states/ [root@master states]# ls init prod top.sls [root@master states]# cat top.sls base: '*': - init.pkg - init.limit - init.ntp-crontab - init.hosts - init.zabbix_agent 查看文件的目录结构 [root@master states]# tree init/ init/ ├── files │ ├── hosts.conf │ ├── limits.conf │ ├── ntp-crontab.conf │ └── zabbix_agentd.conf ├── hosts.sls ├── limit.sls ├── ntp-crontab.sls ├── pkg.sls └── zabbix_agent.sls 1 directory, 9 files
4、推送测试
[root@master states]# salt '*' state.highstate 中间步骤略: ---------- ID: zabbix_agent Function: service.running Name: zabbix-agent Result: True Comment: Service zabbix-agent has been enabled, and is running Started: 14:04:45.625235 Duration: 410.618 ms Changes: ---------- zabbix-agent: True Summary ------------ Succeeded: 9 (changed=1) Failed: 0 ------------ Total states run: 9
5、在客户端进行测试:
[root@master ~]# salt '*' cmd.run '/etc/init.d/zabbix-agentd status' node01.saltstack.com: zabbix_agentd (pid 6084) is running... node02.saltstack.com: zabbix_agentd (pid 5782) is running... [root@master ~]# salt '*' cmd.run "egrep -v '^#|^$' /etc/zabbix_agentd.conf|grep -w Server" node01.saltstack.com: Server=10.10.10.140 node02.saltstack.com: Server=10.10.10.140
6、变更zabbix Server后,进行测试与验证
如果zabbix server变更了IP地址(由10.10.10.140改为10.10.10.148): [root@master ~]# egrep -v '^#|^$' /etc/salt/states/init/files/zabbix_agentd.conf | grep -w Server Server=10.10.10.148 推送下,更新服务器的agent ip状态: [root@master ~]# salt '*' state.highstate ---------- ID: zabbix_agent Function: file.managed Name: /etc/zabbix_agentd.conf Result: True Comment: File /etc/zabbix_agentd.conf updated Started: 14:22:29.306875 Duration: 16.102 ms Changes: ---------- diff: --- +++ @@ -79,7 +79,7 @@ # Server= #Server=127.0.0.1 -Server=10.10.10.140 +Server=10.10.10.148 ### Option: ListenPort #Agent will listen on this port for connections from the server. ---------- Summary ------------ Succeeded: 9 (changed=1) Failed: 0 ------------ Total states run: 9 检查下客户端,看agent的ip地址是否已经调整了: [root@master ~]# salt '*' cmd.run "egrep -v '^#|^$' /etc/zabbix_agentd.conf|grep -w Server" node01.saltstack.com: Server=10.10.10.148 node02.saltstack.com: Server=10.10.10.148