• zabbix agent 客户端(Linux)批量安装


    引言:

    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 root@10.10.10.11
    

    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
    
  • 相关阅读:
    【转】免费搭建独立博客,WordPress+独立域名+独立空间
    MongoDB索引
    面向对象
    浏览器为webapp
    转载Chrome浏览器IOS
    浅淡Webservice、WSDL三种服务访问的方式(附案例)
    Plupload
    转载Yale CAS + .net Client 实现 SSO(6)
    java 对Hbase的基本操作
    【精】iOS开发视频教程下载
  • 原文地址:https://www.cnblogs.com/zoulongbin/p/13926077.html
Copyright © 2020-2023  润新知