Zabbix批量部署Windows和Linux下的agent
- 2019 年 10 月 4 日
- 筆記
對Linux進行批量部署Zabbix-agent
我們這裡使用的是ansible來對zabbix-agent進行批量部署,當然在Linux上也可以使用腳本來完成部署
環境
ansible:10.127.0.133 agent1:172.168.0.4 agent2:172.168.0.5
進行密鑰授權認證實現免密登陸
為方便ansible對agent主機的管理,需要將ansible與agent進行公鑰認證來實現免密登陸
ssh-keygen -t rsa ssh-copy-id -i /root/.ssh/id_rsa.pub 172.168.0.4 ssh-copy-id -i /root/.ssh/id_rsa.pub 172.168.0.5
在ansible/hosts中添加主機資訊
[Linux-agent] 172.168.0.4 172.168.0.5
編輯Linux-agent的playbook文件進行批量部署
實現步驟:
- 安裝zabbix-agent4.2的rpm包
- 使用yum安裝zabbix-agent
- 修改agent配置文件的一些變數,將模板文件覆蓋到agent配置文件
- 重啟zabbix-agent
定義agent模板
創建一個模板文件,裡面包含agent中可變的變數,如:主機名和server地址
[root@zabbix-server ~]# vim /etc/ansible/zabbix_agentd.conf PidFile=/var/run/zabbix/zabbix_agentd.pid LogFile=/var/log/zabbix/zabbix_agentd.log LogFileSize=0 Server={{server}} ServerActive={{server}} Hostname={{hostname}} Include=/etc/zabbix/zabbix_agentd.d/*.conf UnsafeUserParameters=1
編寫playbook文件
vim /etc/ansible/linux-agent.yml - hosts: zabbix-agent remote_user: root vars: server: 10.127.0.133 hostname: "{{ ansible_hostname }}" tasks: - name: install rpm command: rpm -ivh https://repo.zabbix.com/zabbix/4.2/rhel/7/x86_64/zabbix-agent-4.2.1-1.el7.x86_64.rpm - name: install agent command: yum install zabbix-agent -y - name: cp templates zabbix_agentd.conf to zabbix agentd template: src=/etc/ansible/zabbix_agentd.conf dest=/etc/zabbix/zabbix_agentd.conf - name: restart zabbix-agent command: systemctl restart zabbix-agent
執行playbook文件進行批量部署
ansible-playbook -i /etc/ansible/hosts /etc/ansible/linux-agent.yml

可以看到playbook已經執行成功了,接下來可以看一下agent的配置文件

可以看到,agent配置文件中的變數也修改完成
創建自動發現規則對部署的主機進行自動發現並添加監控項
創建自動發現規則

添加自動發現動作

配置發現後的操作

可以看到自動發現規則生效了,並鏈接了Linux-OS模板

對Windows進行批量部署Zabbix-agent
Windows下的批量部署可以通過配置管理工具或者域控制器進行,這裡我使用的ansible來對Windows主機進行批量部署
環境
ansible:10.127.0.133 Windows server2012:172.168.0.6
依賴環境
ansible依賴
pywinrm>=0.3.0
pywinrm可以使用pip來進行安裝,執行以下命令
pip install pywinrm>=0.3.0
Windows依賴
PowerShell 3.0 NET Framework 4.0+
我這裡使用的是2012,上面的環境是不需要做配置的,如果是使用的server2008或更低版本需要進行升級之後才能使用,獲取升級的詳細資訊可以訪問ansible官方文檔查看 https://docs.ansible.com/ansible/latest/user_guide/windows_setup.html#host-requirements
安裝winrm記憶體修補程式
由於ansible控制Windows不是使用的ssh協議,而是用的Windows的winrm服務,而winrm有一個限制可用記憶體量的錯誤,需要安裝腳本進行修復 在powershell上執行下面的命令
$url = "https://raw.githubusercontent.com/jborean93/ansible-windows/master/scripts/Install-WMF3Hotfix.ps1" $file = "$env:tempInstall-WMF3Hotfix.ps1" (New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file) powershell.exe -ExecutionPolicy ByPass -File $file -Verbose
在防火牆上開啟winrm服務埠和agent服務埠
可以在powershell上執行下面的命令查看winrm當前的監聽埠
winrm enumerate winrm/config/Listener
winrm服務默認是5985埠,zabbix-agent使用的是10050埠,因此需要在防火牆上開啟5985和10050埠或直接關閉防火牆
下載Windows-agent的包
首先需要下載Windows-agent的壓縮包並解壓到ansible主機下 下載地址:https://www.zabbix.com/download_agents
在ansible/hosts中添加主機資訊
需要在hosts中指定與Windows連接的配置資訊,默認情況下使用ntlm認證,如果想要獲取關於winrm認證的詳細資訊,可以訪問https://docs.ansible.com/ansible/latest/user_guide/windows_winrm.html
[windows] 172.168.0.6 ansible_python_interpreter=/usr/bin/python ansible_user="administrator" ansible_password="asd.123" ansible_port=5985 ansible_connection="winrm" ansible_winrm_transport=ntlm ansible_winrm_server_cert_validation=ignore
編輯Windows-agent的playbook文件進行批量部署
實現步驟:
- 從ansible複製下載好的agent文件到Windows
- 修改agent配置文件的一些變數,將模板文件覆蓋到agent配置文件,模板文件與Linux的相同
- 安裝zabbix-agent
- 啟動zabbix-agent
編寫playbook文件
vim /etc/ansible/windows-agent.yml - hosts: windows remote_user: administrator vars: server: 10.127.0.133 hostname: "{{ ansible_host }}" tasks: - name: cp zabbix-agent win_copy: src: /etc/ansible/windows_agent/ dest: C:windows_agent - name: cp templates zabbix_agentd.conf to zabbix agentd win_template: src: /etc/ansible/zabbix_agentd.conf dest: C:windows_agentconf - name: install zabbix-agent win_command: zabbix_agentd.exe -i -c C:windows_agentconfzabbix_agentd.conf args: chdir: C:windows_agentbin - name: start zabbix-agent win_command: zabbix_agentd.exe -s -c C:windows_agentconfzabbix_agentd.conf args: chdir: C:windows_agentbin
執行playbook文件進行批量部署
ansible-playbook -i /etc/ansible/hosts /etc/ansible/linux-agent.yml

可以看到playbook執行成功了,查看Windows的服務,Zabbix-agent也已經啟動

配置動作對部署的主機進行自動發現並添加監控項
添加自動發現動作

配置發現後的操作

可以看到自動發現規則生效了,並鏈接了Windows-OS模板
