zabbix agent 客戶端(Linux)批量安裝
阿新 • • 發佈:2020-11-04
引言:
zabbix-agent安裝配置幾臺客戶端是非常簡單的操作,但如果遇到上百臺的zabbix-agent客戶端要安裝及配置就顯得相當費時間。目前自動化部署軟體包、管理配置檔案等開源的軟體有許多,例如:SaltStack、Puppet、Ansible等,本教程就使用Ansible+shell指令碼自動化部署zabbix-agent軟體包和管理配置檔案。
系統和軟體版本:
[root@zabbix ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) [root@zabbix ~]# uname -r 3.10.0-862.el7.x86_64 [root@zabbix ~]# ssh -V OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017 [root@zabbix ~]# ansible --version ansible 2.9.13 [root@zabbix ~]# python -V Python 2.7.5 [root@zabbix ~]# zabbix_agentd -V zabbix_agentd (daemon) (Zabbix) 4.0.25 [root@zabbix ~]# zabbix_server -V zabbix_server (Zabbix) 4.0.25
主機 主機名稱 IP 安裝軟體
Zabbix Server(服務端) zabbix 10.10.10.10/24 Linux + Zabbix Server + Apache + MySQL + Ansible
Zabbix Agent(客戶端) client01 10.10.10.11/24 Linux + Zabbix Agent
Ansible簡介:
Ansible是python中的一套模組,系統中的一套自動化工具,只需要配置SSH免密登陸即可用來管理系統、自動化執行命令等任務。Ansible已被紅帽收購。
Ansible Playbook翻譯是劇本的意思,Playbook是Ansible的配置、部署和編制語言。可以用於管理遠端主機配置和部署,Playbook通常使用YAML語法格式編寫,編寫的檔案格式建議使用yaml或yml。
提示:
本教程就不講解zabbix server安裝部署
Ansible 官方網址: https://www.ansible.com/
Zabbix 官方網址: https://www.zabbix.com/
一、Shell指令碼編寫安裝zabbix-agent及服務自啟動指令碼
1、zabbix-agent服務systemctl自啟動指令碼
[root@zabbix ~]# vim /data/zbxclient/centos7/zabbix-agent.service [Unit] Description=Zabbix Agent After=syslog.target After=network.target [Service] Environment="CONFFILE=/etc/zabbix/conf/zabbix_agentd.conf" EnvironmentFile=/etc/sysconfig/zabbix-agent Type=forking Restart=on-failure PIDFile=/tmp/zabbix_agentd.pid KillMode=control-group ExecStart=/usr/bin/zabbix_agentd -c $CONFFILE ExecStop=/bin/kill -SIGTERM $MAINPID RestartSec=10s User=zabbix Group=zabbix [Install] WantedBy=multi-user.targe
2、編寫zabbix-agent指令碼
[root@zabbix ~]# mkdir -p /data/zbxclient/centos7/
[root@zabbix ~]# vim /data/zbxclient/centos7/InstallZBXClient.sh
#!/bin/bash
zbxname=`hostname`
file01="/opt/zabbix_agent-4.0.21-linux-3.0-amd64-static.tar.gz"
file02="/opt/zabbix-agent.service"
if [ -f ${file01} ] && [ -f ${file02} ];then
mkdir -p /etc/zabbix
mkdir -p /etc/zabbix/zabbix_agentd.d
tar xf /opt/zabbix_agent-4.0.21-linux-3.0-amd64-static.tar.gz -C /etc/zabbix/
ln -s /etc/zabbix/sbin/zabbix_agentd /usr/bin/
ln -s /etc/zabbix/bin/zabbix_* /usr/bin/
useradd -M -s /sbin/nologin zabbix
chown -R zabbix:zabbix /etc/zabbix/*
chmod -R 755 /etc/zabbix/*
cat << EOF >> /etc/services
zabbix_agent 10050/tcp
zabbix_agent 10050/udp
EOF
mv /opt/zabbix-agent.service /usr/lib/systemd/system/
sed -i 's@# PidFile=/tmp/zabbix_agentd.pid@PidFile=/tmp/zabbix_agentd.pid@g' /etc/zabbix/conf/zabbix_agentd.conf
sed -i 's@# EnableRemoteCommands=0@EnableRemoteCommands=1@g' /etc/zabbix/conf/zabbix_agentd.conf
sed -i 's@# LogFileSize=1@LogFileSize=0@g' /etc/zabbix/conf/zabbix_agentd.conf
sed -i 's@Server=127.0.0.1@Server=10.10.10.10@g' /etc/zabbix/conf/zabbix_agentd.conf
sed -i 's@ServerActive=127.0.0.1@ServerActive=10.10.10.10@g' /etc/zabbix/conf/zabbix_agentd.conf
sed -i "s@Hostname=Zabbix server@Hostname=${zbxname}@g" /etc/zabbix/conf/zabbix_agentd.conf
sed -i "s@# Include=/usr/local/etc/zabbix_agentd.userparams.conf@Include=/etc/zabbix/zabbix_agentd.d/*.conf@g" /etc/zabbix/conf/zabbix_agentd.conf
sed -i 's@# UnsafeUserParameters=0@UnsafeUserParameters=1@g' /etc/zabbix/conf/zabbix_agentd.conf
systemctl daemon-reload
systemctl start zabbix-agent.service
systemctl enable zabbix-agent.service
systemctl status zabbix-agent.service
elif [ -f ${file01} ];then
echo "file ${file02} no exist!"
elif [ -f ${file02} ];then
echo "file ${file01} no exist!"
else
echo "file ${file01} and ${file02} no exist!"
fi
二、安裝配置Ansible
提示:
ansible服務端與ansible客戶端之間要把SSH免密,否則會提示錯誤。
1、配置阿里雲yum源
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum makecache
2、配置SSH免密登入
[root@zabbix ~]# ssh-keygen
[root@zabbix ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
3、安裝Ansible
提示:
yum安裝Ansible軟體會自動安裝python
[root@zabbix ~]# yum -y install ansible
[root@zabbix ~]# rpm -qc ansible
/etc/ansible/ansible.cfg #==》Ansible配置檔案
/etc/ansible/hosts #==》Ansible主機清單(重點)
4、配置Ansible主機組
[root@zabbix ~]# vim /etc/ansible/hosts
[zabbixagent] #==》自定義主機組名稱
10.10.10.11 #==》指定要執行的主機的IP地址
[zabbixagent_nossh] #==》自定義另一個主機組名稱,該主機組可以不用配置SSH金鑰認證,直接登入遠端主機組
10.10.10.11 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123456'
5、編寫Ansible Playbook劇本
[root@zabbix ~]# mkdir -p /etc/ansible/playbook/
[root@zabbix ~]# vim /etc/ansible/playbook/zabbixagent.yml
- hosts: zabbixagent
remote_user: root
tasks:
- name: copy zabbix-agent file
copy:
src: /data/zbxclient/centos7/
dest: /opt/
- name: shell scripts add root execute permission
file:
dest: /opt/InstallZBXClient.sh
mode: u+x
- name: zabbix-agent configure file add root execute permission
file:
dest: /opt/zabbix-agent.service
mode: u+x
- name: execute shell scripts install zabbix-agent
command: "sh /opt/InstallZBXClient.sh"
- name: delete file
file:
dest: "{{ item }}"
state: absent
with_items:
- /opt/InstallZBXClient.sh
- /opt/zabbix_agent-4.0.21-linux-3.0-amd64-static.tar.gz
三、執行Ansible Playbook劇本自動部署安裝zabbix-agent
###檢查劇本語法
[root@zabbix ~]# ansible-playbook --syntax-check /etc/ansible/playbook/zabbixagent.yml
###模擬執行劇本
[root@zabbix ~]# ansible-playbook -C /etc/ansible/playbook/zabbixagent.yml
###執行劇本
[root@zabbix ~]# ansible-playbook /etc/ansible/playbook/zabbixagent.yml
四、檢查zabbix-agent客戶端
[root@client01 ~]# netstat -tlunp | grep 10050
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 1788/zabbix_agentd
[root@client01 ~]# systemctl status zabbix-agent
[root@client01 ~]# egrep -v '^#|^$' /etc/zabbix/conf/zabbix_agentd.conf
PidFile=/tmp/zabbix_agentd.pid
LogFile=/tmp/zabbix_agentd.log
LogFileSize=0
EnableRemoteCommands=1
Server=10.10.10.10
ServerActive=10.10.10.10
Hostname=client01
Include=/etc/zabbix/zabbix_agentd.d/*.conf
UnsafeUserParameters=1