• Ansible 安装zabbix客户端(二)


    一、创建ansible roles目录

     [root@ansible ~]# mkdir -p ansible/roles/zabbix/{files,handlers,tasks,templates,vars} 

    二、编写各个文件

    2.1 tasks目录中的文件

     [root@ansible ~]# cat ansible/roles/zabbix/tasks/main.yaml 
    - include: /root/ansible/roles/sysinit/tasks/epel.yml
    - include: install.yaml
    - include: create_dir.yml
    - include: copy_scripts.yml
    - include: configure.yaml 
    - include: start.yaml
    
     [root@ansible  ~]# cat /root/ansible/roles/sysinit/tasks/epel.yml 
    - name: CentOS7 Yum Epel 
      copy: src=/root/ansible/roles/sysinit/files/epel-7.repo dest=/etc/yum.repos.d/epel-7.repo
      when: ansible_distribution_major_version == "7"
    
    - name: CentOS6 Yum Epel
      copy: src=/root/ansible/roles/sysinit/files/epel-6.repo dest=/etc/yum.repos.d/epel-6.repo
      when: ansible_distribution_major_version == "6"
    
     [root@ansible  ~]# cat /root/ansible/roles/sysinit/files/epel-7.repo 
    [epel]
    name=Extra Packages for Enterprise Linux 7 - $basearch
    baseurl=http://mirrors.aliyun.com/epel/7/$basearch
    failovermethod=priority
    enabled=1
    gpgcheck=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
     
    [epel-debuginfo]
    name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
    baseurl=http://mirrors.aliyun.com/epel/7/$basearch/debug
    failovermethod=priority
    enabled=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
    gpgcheck=0
     
    [epel-source]
    name=Extra Packages for Enterprise Linux 7 - $basearch - Source
    baseurl=http://mirrors.aliyun.com/epel/7/SRPMS
    failovermethod=priority
    enabled=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
    gpgcheck=0
    
     [root@ansible  ~]# cat /root/ansible/roles/sysinit/files/epel-6.repo      
    [epel]
    name=Extra Packages for Enterprise Linux 6 - $basearch
    baseurl=http://mirrors.aliyun.com/epel/6/$basearch
    failovermethod=priority
    enabled=1
    gpgcheck=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
     
    [epel-debuginfo]
    name=Extra Packages for Enterprise Linux 6 - $basearch - Debug
    baseurl=http://mirrors.aliyun.com/epel/6/$basearch/debug
    failovermethod=priority
    enabled=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
    gpgcheck=0
     
    [epel-source]
    name=Extra Packages for Enterprise Linux 6 - $basearch - Source
    baseurl=http://mirrors.aliyun.com/epel/6/SRPMS
    failovermethod=priority
    enabled=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
    gpgcheck=0
    
     [root@ansible  ~]# cat ansible/roles/zabbix/tasks/install.yaml 
    - name: Install zabbix22-agent
      yum: name=zabbix22-agent state=present
      when: ansible_distribution_major_version == "6"
    
    - name: Install zabbix40-agent
      yum: name=zabbix40-agent state=present
      when: ansible_distribution_major_version == "7"
    
     [root@ansible  ~]# cat ansible/roles/zabbix/tasks/create_dir.yml 
    - name: create dir
      file: path=/etc/zabbix/zabbix_agentd.d/ state=directory
    
     [root@ansible  ~]# cat ansible/roles/zabbix/tasks/copy_scripts.yml 
    - name: copy tcp conn status scripts
      copy: src=tcp_conn_status.sh dest=/etc/zabbix/ mode=755
    
     [root@ansible  ~]# cat ansible/roles/zabbix/tasks/configure.yaml 
    - name: Configure zabbix-agent7
      template: src=zabbix_agentd.conf.j2 dest=/etc/zabbix_agentd.conf
      when: ansible_distribution_major_version == "7"
      notify: restart zabbix-agent
    
    - name: Configure zabbix-agent6
      template: src=zabbix_agentd_6.conf.j2 dest=/etc/zabbix_agentd.conf
      when: ansible_distribution_major_version == "6"
      notify: restart zabbix-agent
    
     [root@ansible  ~]# cat ansible/roles/zabbix/tasks/start.yaml 
    - name: Start zabbix-agent
      service: name=zabbix-agent state=started enabled=yes

    2.2 handlers目录

     [root@ansible ~]# cat ansible/roles/zabbix/handlers/main.yml 
    - name: restart zabbix-agent
      service: name=zabbix-agent state=restarted

    2.3 templates目录

     [root@ansible ~]# cat ansible/roles/zabbix/templates/zabbix_agentd.conf.j2     
    PidFile=/var/run/zabbix/zabbix_agentd.pid
    LogFile=/var/log/zabbix/zabbix_agentd.log
    LogFileSize=0
    Server={{ zabbix_server }}
    Hostname={{ ansible_eth0['ipv4']['address'] }}
    Include=/etc/zabbix/zabbix_agentd.d/*.conf
    UnsafeUserParameters=1
    UserParameter=tcp.status[*],/etc/zabbix/tcp_conn_status.sh $1
    
     [root@ansible ~]# cat ansible/roles/zabbix/templates/zabbix_agentd_6.conf.j2 
    PidFile=/var/run/zabbix/zabbix_agentd.pid
    LogFile=/var/log/zabbix/zabbix_agentd.log
    LogFileSize=0
    Server={{ zabbix_server }}
    Hostname={{ ansible_eth0['ipv4']['address'] }}
    Include=/etc/zabbix/zabbix_agentd.d/
    UnsafeUserParameters=1
    UserParameter=tcp.status[*],/etc/zabbix/tcp_conn_status.sh $1

    2.4 files目录

     [root@192-168-5-55 ~]# cat ansible/roles/zabbix/files/tcp_conn_status.sh 
    #!/bin/bash
    #this script is used to get tcp and udp connetion status
    #tcp status
    metric=$1
    tmp_file=/tmp/tcp_status.txt
    /bin/netstat -an|awk '/^tcp/{++S[$NF]}END{for(a in S) print a,S[a]}' > $tmp_file
     
    case $metric in
       closed)
              output=$(awk '/CLOSED/{print $2}' $tmp_file)
              if [ "$output" == "" ];then
                 echo 0
              else
                 echo $output
              fi
            ;;
       listen)
              output=$(awk '/LISTEN/{print $2}' $tmp_file)
              if [ "$output" == "" ];then
                 echo 0
              else
                 echo $output
              fi
            ;;
       synrecv)
              output=$(awk '/SYN_RECV/{print $2}' $tmp_file)
              if [ "$output" == "" ];then
                 echo 0
              else
                 echo $output
              fi
            ;;
       synsent)
              output=$(awk '/SYN_SENT/{print $2}' $tmp_file)
              if [ "$output" == "" ];then
                 echo 0
              else
                 echo $output
              fi
            ;;
       established)
              output=$(awk '/ESTABLISHED/{print $2}' $tmp_file)
              if [ "$output" == "" ];then
                 echo 0
              else
                 echo $output
              fi
            ;;
       timewait)
              output=$(awk '/TIME_WAIT/{print $2}' $tmp_file)
              if [ "$output" == "" ];then
                 echo 0
              else
                 echo $output
              fi
            ;;
       closing)
              output=$(awk '/CLOSING/{print $2}' $tmp_file)
              if [ "$output" == "" ];then
                 echo 0
              else
                 echo $output
              fi
            ;;
       closewait)
              output=$(awk '/CLOSE_WAIT/{print $2}' $tmp_file)
              if [ "$output" == "" ];then
                 echo 0
              else
                 echo $output
              fi
            ;;
       lastack)
              output=$(awk '/LAST_ACK/{print $2}' $tmp_file)
              if [ "$output" == "" ];then
                 echo 0
              else
                 echo $output
              fi
             ;;
       finwait1)
              output=$(awk '/FIN_WAIT1/{print $2}' $tmp_file)
              if [ "$output" == "" ];then
                 echo 0
              else
                 echo $output
              fi
             ;;
       finwait2)
              output=$(awk '/FIN_WAIT2/{print $2}' $tmp_file)
              if [ "$output" == "" ];then
                 echo 0
              else
                 echo $output
              fi
             ;;
             *)
              echo -e "e[033mUsage: sh  $0 [closed|closing|closewait|synrecv|synsent|finwait1|finwait2|listen|established|lastack|timewait]e[0m"
       
    esac
    [root@ansible ~]# cat ansible/roles/zabbix/files/tcp_conn_status.sh

    2.5 vars目录

     [root@ansible ~]# cat ansible/roles/zabbix/vars/main.yml 
    zabbix_server: 192.168.5.237

    3、执行文件入口和方法

    [root@ansible ~]# cat ansible/zabbix-agent.yml 
    - hosts: "{{ group }}"
      remote_user: root
      roles:
        - role: zabbix
    # 为192.168.0.100安装zabbix-agent,此IP必须先在/etc/ansible/hosts文件中存在 [root@ansible
    ~]# ansible-playbook ansible/zabbix-agent.yml -e group=192.168.0.100
  • 相关阅读:
    数据库 的几种链接 join
    eclipse 无法记住svn密码
    loadrunner监控linux服务器
    谷歌浏览器跨域命令行方式失效
    努力学习的意义到底在哪里?(转载知呼上的答案)
    人的性欲为什么会被视为坏事?(转载)
    npm下载速度太慢
    使用systemback制作Ubuntu自定义系统镜像和系统备份(抄)
    使用bat脚本永久激活Windows系统(摘抄)
    fiddler软件测试——Fiddler抓取https设置详解(图文)(摘抄)
  • 原文地址:https://www.cnblogs.com/cyleon/p/12547775.html
Copyright © 2020-2023  润新知