文章 59
评论 0
浏览 30518
自动化部署zabbix-agent

自动化部署zabbix-agent

role目录结构

[17:57:10 root@ansible zabbix-agent]#tree 
.
├── role_zabbix-agent.yml  #主playbook文件
└── zabbix-agent
    ├── files
    │   ├── echo.sh       #自定义采集监控数据脚本
    │   └── zabbix-4.0.30.tar.gz  #zabbix源码包
    ├── tasks
    │   ├── main.yaml    #主tasks文件
    │   ├── zabbix_conf.yaml   #zabbix配置文件生成
    │   ├── zabbix_install.yaml  #安装zabbix-agent
    │   └── zabbix_service.yaml   #生成zabbix-agent的service
    ├── templates
    │   ├── zabbix_agentd.conf.j2   #zabbix-agent配置文件模板
    │   ├── zabbix-agent-install.sh.j2  #部署脚本文件模板
    │   ├── zabbix-agent.service.j2     #zabbix-agent的service模板
    │   └── zabbix_UserParameter.conf.j2  #自定义监控项配置文件模板
    └── vars
        └── main.yaml  #变量文件

file目录文件

[17:58:09 root@ansible zabbix-agent]#ls zabbix-agent/files/
echo.sh  zabbix-4.0.30.tar.gz
[17:58:15 root@ansible zabbix-agent]#cat zabbix-agent/files/echo.sh 
#!/bin/bash
echo $1$2

tasks目录文件

[17:58:26 root@ansible zabbix-agent]#cat  zabbix-agent/tasks/main.yaml 
- include: zabbix_install.yaml
- include: zabbix_conf.yaml
- include: zabbix_service.yaml

[17:59:05 root@ansible zabbix-agent]#cat zabbix-agent/tasks/zabbix_install.yaml 
- name: install zabbix-agent
  unarchive: src=zabbix-4.0.30.tar.gz dest=/tmp/
- name: copy zabbix-agent is install sh
  template: src=zabbix-agent-install.sh.j2 dest=/tmp/zabbix-4.0.30/zabbix-agent-install.sh
- name: make install zabbix-agent
  shell: chdir=/tmp/zabbix-4.0.30 bash zabbix-agent-install.sh 

[17:59:17 root@ansible zabbix-agent]#cat zabbix-agent/tasks/zabbix_conf.yaml 
- name: mkdir logs
  file: path={{ install_dir }}/logs state=directory
- name: mkdir pid
  file: path={{ install_dir }}/pid state=directory
- name: mkdir conf
  file: path={{ install_dir }}/etc/zabbix_agentd.conf.d state=directory
- name: mkdir shell
  file: path={{ install_dir }}/shell state=directory
- name: copy is shell
  copy: src=echo.sh dest={{ install_dir }}/shell/ mode=755
- name: touch file is conf
  template: src=zabbix_agentd.conf.j2 dest={{ install_dir }}/etc/zabbix_agentd.conf
- name: touch UserParameter is conf
  template: src=zabbix_UserParameter.conf.j2 dest={{ install_dir }}/etc/zabbix_agentd.conf.d/zabbix_UserParameter.conf
- name: chown is zabbix
  file: path={{ install_dir }} owner=zabbix group=zabbix recurse=yes

[17:59:26 root@ansible zabbix-agent]#cat zabbix-agent/tasks/zabbix_service.yaml 
- name: touch zabbix-agent is service
  template: src=zabbix-agent.service.j2 dest=/etc/systemd/system/zabbix-agent.service
- name: start zabbix-agent 
  service: name=zabbix-agent state=restarted enabled=yes

vars目录文件

[17:59:50 root@ansible zabbix-agent]#cat zabbix-agent/vars/main.yaml 
install_dir: "/apps/zabbix-agent"
zabbix_server_ip: '192.168.10.181'
#name:监控项名称
#shell:获取监控数据脚本名称
#参数
Parameter:
  - name: echo[*]
    shell: echo.sh
    cmd: '"$1" "$2"'
  - name: echo1
    shell: echo
    cmd: 1

templates目录文件

[18:00:32 root@ansible zabbix-agent]#cat zabbix-agent/templates/zabbix-agent.service.j2 
[Unit]
Description=Zabbix Agent
After=syslog.target
After=network.target
[Service]
Environment="CONFFILE={{ install_dir }}/etc/zabbix_agentd.conf"
#EnvironmentFile=-/etc/default/zabbix-agent
Type=forking
Restart=on-failure
PIDFile={{ install_dir }}/pid/zabbix_agentd.pid
KillMode=control-group
ExecStart={{ install_dir }}/sbin/zabbix_agentd -c $CONFFILE
ExecStop=/bin/kill -SIGTERM $MAINPID
RestartSec=10s
User=zabbix
Group=zabbix
[Install]
WantedBy=multi-user.target

[18:00:58 root@ansible zabbix-agent]#cat zabbix-agent/templates/zabbix_agentd.conf.j2 
PidFile={{ install_dir }}/pid/zabbix_agentd.pid
LogType=file
LogFile={{ install_dir }}/logs/zabbix_agentd.log
LogFileSize=1
DebugLevel=3
# SourceIP=
EnableRemoteCommands=1
LogRemoteCommands=1
Server={{ zabbix_server_ip }}
ListenPort=10050
ListenIP=0.0.0.0
StartAgents=5
ServerActive={{ zabbix_server_ip }}
Hostname={{ ansible_nodename }}
RefreshActiveChecks=120
BufferSend=60
BufferSize=100
MaxLinesPerSecond=20
Timeout=30
AllowRoot=0
User=zabbix
Include={{ install_dir }}/etc/zabbix_agentd.conf.d/*.conf
UnsafeUserParameters=1

[18:01:13 root@ansible zabbix-agent]#cat zabbix-agent/templates/zabbix-agent-install.sh.j2 
install(){
version=`grep -Eo "(centos|ubuntu)" /etc/os-release | head -n1`
    if [ $version = centos ];then
        yum install gcc libxml2-devel libevent-devel net-snmp net-snmp-devel curl curl-devel -y || exit 1
    elif [ ${version} = ubuntu ];then
        apt-get install ssl-cert ttf-dejavu-core libmysqlclient-dev libxml2-dev libxml2  libsnmp-dev libevent-dev openjdk-8-jdk curl libcurl4-openssl-dev -y || exit 1
    else
        exit 1
    fi
}
install_agent(){
    id zabbix &>/dev/null || useradd -r -s /sbin/nologin zabbix
    ./configure --prefix={{ install_dir }} --enable-agent  --enable-ipv6 --with-net-snmp --with-libcurl --with-libxml2 || exit 2
    make install
}
main(){
  install
  install_agent
}
main

[18:01:47 root@ansible zabbix-agent]#cat zabbix-agent/templates/zabbix_UserParameter.conf.j2 
{% for i in Parameter %}
UserParameter={{ i.name }},{{install_dir}}/shell/{{ i.shell }} {{ i.cmd }}
{% endfor %}

标题:自动化部署zabbix-agent
作者:Carey
地址:HTTPS://zhangzhuo.ltd/articles/2021/07/09/1625829670380.html

生而为人

取消