【Zabbix-agent安裝】使用ansible自動化部屬工具遠程安裝教學

軟體工程實作系列文

前言

由於在工作上常需要Zabbix來做監控管理工具,常常更新或新增遠端主機時需要連進各個遠端主機以一行一行命令重新部署非常耗時。這時自動化部屬工具就能夠派上用場,Ansible是RedHat提供的一個免費的好上手的工具,能夠幫我們自動化處理大部分需要遠程操作的命令。

這篇文章來分享如何透過Ansible自動化在多個遠端主機部屬Zabbix-agent,希望幫助到還沒利用過這個工具的大家!

結論

- 這篇文章主要實作在一台監控主機(之後稱為monitor)安裝zabbix-server/frontend/agent,並在多台被監控主機(之後稱為slave)透過Ansible安裝zabbix-agent,建立與monitor的連線

- 實作os版本為ubuntu 20.04 jammy

概覽

  • 透過Ansible可以免去手動連進每個slave一條一條code安裝各種軟體
  • 成功通過Ansible工具在多個slave部署Zabbix-agent
  • 成功通過Ansible將/etc/zabbix/zabbix_server.conf config中HOST 名稱設定為slave ip,ServerServerActive名稱設為monitor之IP
  • 部署後的系統架構大概會長成以下這樣

事前準備(以ubuntu 20.04 jammy為例)

執行遠端部屬之主機

【安裝pip( 需要事先下載好python3)】

$ sudo curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
$ python3 get-pip.py

【安裝ansible】

$ pip install ansible

monitor事前部署

【安裝zabbix-server&frontend&agent 】

  • 記得會需要先安裝mysql,不然zabbix-server會執行錯誤
  • 詳細參考官網安裝說明,不會很難
  • 最後可以開啟 http://<monito ip>/zabbix/ 確認是否有安裝成功

ansible playbook撰寫

  1. 建立ansible playbook檔案夾結構
.
├── add_zabbix.yml
├── hosts
├── readme.md
└── templates
└── zabbix_agentd.conf.j2ß

2. 撰寫以下playbook- add_zabbix.yml(程式碼有註記的地方需要注意或更改)

---
- name: install zabbix agent
hosts: webservers
gather_facts: yes
become: yes #以sudo身分執行
vars:
zabbix_server_ip: 192.168.0.6 # 需要改成monitor的ip(安裝zabbix_server的地方)
zabbix_repo_url: https://repo.zabbix.com/zabbix/6.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_6.0-4%2Bubuntu22.04_all.deb # 需要依照主機的版本更改下載點,詳細的請到官網可以查看https://www.zabbix.com/download?zabbix=6.0&os_distribution=ubuntu&os_version=20.04_focal&db=mysql&ws=apache

tasks:
- name: Download zabbix repo package
get_url:
url: "{{ zabbix_repo_url}}"
dest: /tmp/zabbix.deb

- name: Install zabbix repo
become: yes
apt:
deb: /tmp/zabbix.deb
state: present
- name: update package index # update pakage list
become: yes
ansible.builtin.apt:
update_cache: yes
- name: install zabbix-agent #安裝zabbix-agent
become: yes
ansible.builtin.apt:
name:
- zabbix-agent
state: present
- name: Stop service zabbix-agent #需要更新zabbix-agent,先暫停zabbix-agent
become: yes
service:
name: zabbix-agent
state: stopped

- name: Remove zabbix config file
become: yes
file:
path: /etc/zabbix/zabbix_agentd.conf
state: absent

- name: copy zabbix-agent configuration file #將撰寫好的config file移到remote host
become: yes
template: src=zabbix_agentd.conf.j2 dest=/etc/zabbix/zabbix_agentd.conf
- name: Start service zabbix-agent #重啟zabbix-agent
become: yes
service:
name: zabbix-agent
state: started

3. 撰寫config文件 zabbix_agentd.conf.j2

PidFile=/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
ListenPort=10050
Server={{ zabbix_server_ip }}
ServerActive={{ zabbix_server_ip }}
Hostname={{ ansible_default_ipv4.address }} # 這裡會自動設成slave主機ip
Include=/etc/zabbix/zabbix_agentd.d/*.conf

4. 撰寫hosts文件(需要更改各個)

all:
children:
webservers:
hosts:
192.169.0.2: # slave 1
ansible_user: root
ansible_sudo_pass: test #root password
ansible_ssh_private_key_file: ~/.ssh/id_rsa # ssh 時需要的private key
192.169.0.3: # slave 2
ansible_user: root
ansible_sudo_pass: test #root password
ansible_ssh_private_key_file: ~/.ssh/id_rsa # ssh 時需要的private key
192.169.0.4: # slave 2
ansible_user: root
ansible_sudo_pass: test #root password
ansible_ssh_private_key_file: ~/.ssh/id_rsa # ssh 時需要的private key
192.169.0.5: # slave 4
ansible_user: root
ansible_sudo_pass: test #root password
ansible_ssh_private_key_file: ~/.ssh/id_rsa # ssh 時需要的private key

需要注意有幾個地方

  • ip需要改成各個slave的ip
  • 為了從遠端操作,需要先建立public key連線,需要在local端生成公私鑰,並部署到各個slaver,可透過下列指令實現
# 產生公私鑰組合
$ sudo ssh-keygen -t rsa

# 複製公私鑰到遠端, 需要自行更改slave ip
$ sudo scp ~/.ssh/id_rsa.pub root@<slave ip>:~/.ssh/authorized_keys

開始部署

撰寫完文檔後在資料夾下之行以下命令,然後部署

  • 檢查文法
# 檢查文法
$ ansible-playbook -i hosts add_zabbix.yml --syntax-check

# 檢查所有的task
$ ansible-playbook -i hosts add_zabbix.yml --list-tasks
  • 執行部屬
# ansible的 rehearsal命令,看有沒有錯誤
$ ansible-playbook -i hosts add_zabbix.yml --check

# 執行部屬
$ ansible-playbook -i hosts add_zabbix.yml

部署成功後monitor UI設定

這部分可以參考IT邦幫忙鐵人賽

當顯示部屬完成並無error後可以到http://<monito ip>/zabbix/ monitor的UI新增遠端host(Configuration > Hosts > Create host)。

  • Template 新增 Linux by Zabbix agent
  • Group 新增 Linux servers
  • Host name 設定為 remote host 的 ip(這裡需要確認與上面config zabbix_agentd.conf.j2檔案中的Hostname一樣才可以對接)
  • 最後create host
  • 將所有slave host按照這個步驟新增

【部屬成功】

全部新增完成後可以看到monitoring>hosts頁面顯示以下結果(ZBX icon為綠色)

後記

這篇文章主要分享如何實作在一台監控主機安裝zabbix-server & frontend & agent,並在多台被監控主機透過Ansible自動化部屬工具安裝zabbix-agent,之後建立與monitor的連線。

希望能夠幫助zabbix初學者與尚未實作過Ansible自動化部屬的大家,能夠入門了解並順利使用這些工具。

非常感謝讀完本篇文章,希望本文能對大家有些幫助,記得按拍手給我一些鼓勵,Medium 文章中一個人最多能按 50 下 XDD

喜歡我的文章的話也歡迎訂閱我,我會定期分享關於軟體工程的知識,還有在日本工作與生活的心得。

參考文獻

--

--

Shun's Blog | 東京開發者生活

東京涉谷IT企業軟體工程師 | 日本生活、軟體技術、科技趨勢、區塊鏈技術分享