Khi triển khai Zabbix, việc phải cài đặt Zabbix-Agent trên nhiều máy là điều khó tránh khỏi, do vậy bài viết này sẽ hướng dẫn các bạn tạo Script tự động cài đặt và cấu hình Zabbix-Agent trên Linux.
Zabbix 4.2 Series:
Cài đặt & Triển khai Zabbiz 4.2 trên Centos 7
Cấu hình Zabbix-Agent, Thêm, xóa Host
Script tự động cài đặt, cấu hình Zabbix-Agent
Giám sát thông qua Zabbix Proxy
Tạo mới Templates, Applications, Items, Triggers, Graphs
Tạo file Script
Các bạn có thể tạo file script như sau:
nano auto_install_zabbix-agent.sh
Copy nội dung bên dưới nhưng nhớ sửa lại cho phù hợp với hệ thống của các bạn nhé.
#!/bin/bash
SERVER=192.168.1.121
HOSTNAME=WEB-SERVER
PSKIdentity=web-server_psk
mkdir -p /opt/zabbix
rpm -ivh https://repo.zabbix.com/zabbix/4.2/rhel/7/x86_64/zabbix-release-4.2-1.el7.noarch.rpm
yum -y install zabbix-agent
cd /etc/zabbix/
cp -fp ./zabbix_agentd.conf ./.zabbix_agentd.conf
touch ./.zabbix_agentd01.conf ./.zabbix_agentd02.conf ./.zabbix_agentd03.conf
cat ./.zabbix_agentd.conf | /usr/bin/sed -e 's/Server=127.0.0.1/Server='${SERVER}'/g' > .zabbix_agentd01.conf
cat ./.zabbix_agentd01.conf | /usr/bin/sed -e 's/ServerActive=127.0.0.1/ServerActive='${SERVER}'/g' > .zabbix_agentd02.conf
cat ./.zabbix_agentd02.conf | /usr/bin/sed -e 's/Hostname=Zabbix server/Hostname='${HOSTNAME}'/g' > ./zabbix_agentd.conf
echo "TLSConnect=psk" >> /etc/zabbix/zabbix_agentd.conf
echo "TLSAccept=psk" >> /etc/zabbix/zabbix_agentd.conf
echo "TLSPSKIdentity=${PSKIdentity}" >> /etc/zabbix/zabbix_agentd.conf
echo "TLSPSKFile=/etc/zabbix/zabbix_agentd.psk" >> /etc/zabbix/zabbix_agentd.conf
openssl rand -hex 32 > /etc/zabbix/zabbix_agentd.psk
if [ $? = 0 ]; then
echo "finished to install Zabbix Agent"
else
echo "failed to install Zabbix Agent"
exit
fi
rm -f ./.zabbix_agentd0[1-3].conf
rm -f ./.zabbix_agentd.conf
#Start zabbix-agent
systemctl start zabbix-agent
systemctl enable zabbix-agent
Mình giải thích một chút về script:
- SERVER: là địa chỉ của Zabbix Server hoặc Zabbix Proxy, tùy theo hệ thống của bạn theo mô hình nào để cấu hình cho phù hợp
- HOSTNAME: hostname của máy cần giám sát.
- PSKIdentity=psk identity để cấu hình chứng thực bằng psk
Khi thực thi Script sẽ tiến hành cài đặt Zabbix-Agent 4.2. Sau đó sẽ chỉnh sửa file config của Zabbix Agent. Tiếp theo sẽ tạo file mã psk để cấu hình chứng thực bằng psk trên Web. Cuối cùng sẽ xóa các file tạm không cần thiết và khởi động dịch vụ Zabbix-agent.
Thực thi script và kiểm tra
Sau khi tạo xong script các bạn thực thi script bằng lệnh:
sh auto_install_zabbix-agent.sh
Quá trình cài đặt:
Retrieving https://repo.zabbix.com/zabbix/4.2/rhel/7/x86_64/zabbix-release-4.2-1.el7.noarch.rpm
Preparing... ################################# [100%]
package zabbix-release-4.2-1.el7.noarch is already installed
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: ftp.iij.ad.jp
* epel: ftp.jaist.ac.jp
* extras: ftp.iij.ad.jp
* ius: dfw.mirror.rackspace.com
* nux-dextop: mirror.li.nux.ro
* remi-php55: mirror.bebout.net
* remi-php56: mirror.bebout.net
* remi-safe: mirror.bebout.net
* updates: ftp.iij.ad.jp
Resolving Dependencies
--> Running transaction check
---> Package zabbix-agent.x86_64 0:4.2.2-1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=====================================================================================================================================================================================================================================================================================
Package Arch Version Repository Size
=====================================================================================================================================================================================================================================================================================
Installing:
zabbix-agent x86_64 4.2.2-1.el7 zabbix 400 k
Transaction Summary
=====================================================================================================================================================================================================================================================================================
Install 1 Package
Total download size: 400 k
Installed size: 1.6 M
Downloading packages:
zabbix-agent-4.2.2-1.el7.x86_64.rpm | 400 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : zabbix-agent-4.2.2-1.el7.x86_64 1/1
Verifying : zabbix-agent-4.2.2-1.el7.x86_64 1/1
Installed:
zabbix-agent.x86_64 0:4.2.2-1.el7
Complete!
finished to install Zabbix Agent
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.
Nếu không có lỗi gì xảy ra thì quá trình cài đặt như trên là thành công.
Chúc các bạn thành công. Bài tiếp theo mình sẽ hướng dẫn cấu hình theo dõi thông qua Zabbix Proxy. Các bạn nhớ đón đọc nhé!