ansible源码安装zabbix agent

名词解释
[root@iZ23nvakegvZ zabbix_agent]# ls  
files  需要安装包  
handlers  重启相关信息  
meta  galaxy_info相关信息  
tasks  安装部署的任务  
templates  相关的模板配置文件 
vars   变量配置文件  
[root@iZ23nvakegvZ zabbix_agent]#
目录结构
[root@iZ23nvakegvZ zabbix_agent]# tree

.

├── hosts
├── roles
│   └── zabbix_agent
│       ├── files
│       │   └── zabbix-2.4.5.tar.gz
│       ├── handlers
│       ├── meta
│       │   └── main.yml
│       ├── tasks
│       │   ├── copy.yml
│       │   ├── delete.yml
│       │   ├── install.yml
│       │   └── main.yml
│       ├── templates
│       │   ├── zabbix_agentd
│       │   └── zabbix_agentd.conf
│       └── vars
│           └── main.yml
└── site.yml

8 directories, 11 files

[root@iZ23nvakegvZ zabbix_agent]#
安装的主机
[root@iZ23nvakegvZ zabbix_agent]# cat hosts

[tomcat-servers]

webserver1 ansible_ssh_host=10.168.159.79

[root@iZ23nvakegvZ zabbix_agent]#
入口文件
[root@iZ23nvakegvZ zabbix_agent]# cat site.yml

- hosts: tomcat-servers

  remote_user: root

  roles:

    - zabbix_agent

[root@iZ23nvakegvZ zabbix_agent]#
查看源文件
[root@iZ23nvakegvZ roles]# ll zabbix_agent/files/zabbix-2.4.5.tar.gz

-rw-r--r-- 1 root root 727040 5月  11 15:26 zabbix_agent/files/zabbix-2.4.5.tar.gz

[root@iZ23nvakegvZ roles]#
galaxy_info相关信息
[root@iZ23nvakegvZ roles]# cat zabbix_agent/meta/main.yml

galaxy_info:

  author: Steven

  description: Install Zabbix Client

  license: MIT

  min_ansible_version: 1.9

  platforms:

  - name: CentOS

    versions:

    - 6

  categories:

  - Monitor

dependencies: []

[root@iZ23nvakegvZ roles]# 
设置变量
[root@iZ23nvakegvZ roles]# cat zabbix_agent/vars/main.yml

zabbix_dir: /usr/local

zabbix_version: 2.4.5

zabbix_user: zabbix

zabbix_port: 10050

zabbix_server_ip: 120.26.210.27

[root@iZ23nvakegvZ roles]#
查看task copy.yml
[root@iZ23nvakegvZ roles]# cat zabbix_agent/tasks/copy.yml

- name: Zabbix agent server stop
  shell: ps -ef | grep zabbix | grep -v zabbix | awk '{print $2}' | xargs kill -9 >>/dev/null 2>&1
  ignore_errors: yes
  when: ansible_os_family == "RedHat" and ansible_distribution_major_version|int == 6
- name: Delete zabbix agent files

  shell: rm -rf {{zabbix_dir}}/zabbix

  ignore_errors: yes

  when: ansible_os_family == "RedHat" and ansible_distribution_major_version|int == 6
- name: Install zabbix agent

  yum: name={{ item }} state=latest

  with_items:

    - telnet

    - tar

- name: Create zabbix agent user

  user: name={{ zabbix_user }} state=present createhome=no shell=/sbin/nologin

  when: ansible_os_family == "RedHat" and ansible_distribution_major_version|int == 6

- name: Copy zabbix agent software

  copy: src=zabbix-{{ zabbix_version }}.tar.gz dest=/tmp/zabbix-{{ zabbix_version }}.tar.gz owner=root group=root

  #template: src=zabbix-{{ zabbix_version }}.tar.gz dest=/tmp/zabbix-{{ zabbix_version }}.tar.gz owner=root group=root

  #when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6

  when: ansible_os_family == "RedHat" and ansible_distribution_major_version | int == 6

- name: Tar zabbix agent sofrware

  shell: tar xf /tmp/zabbix-{{ zabbix_version }}.tar.gz -C {{ zabbix_dir }}

  when: ansible_os_family == "RedHat" and ansible_distribution_major_version | int == 6

- name: Copy zabbix agent script

  template: src=zabbix_agentd dest=/etc/init.d/zabbix_agentd owner=root group=root mode=755

  when: ansible_os_family == "RedHat" and ansible_distribution_major_version | int == 6

- name: Copy zabbix agent  config

  template: src=zabbix_agentd.conf dest={{ zabbix_dir }}/zabbix/conf/zabbix_agentd.conf owner={{ zabbix_user }} group={{ zabbix_user }} mode=0644

  when: ansible_os_family == "RedHat" and ansible_distribution_major_version | int == 6

[root@iZ23nvakegvZ roles]#
查看install.yml文件
[root@iZ23nvakegvZ roles]# cat zabbix_agent/tasks/install.yml

- name: Modify zabbix dir permisson

  file: path={{zabbix_dir}}/zabbix owner={{zabbix_user}} group={{zabbix_user}} mode=0755

  when: ansible_os_family == "RedHat" and ansible_distribution_major_version | int == 6

- name: Start zabbix agent

  shell: /etc/init.d/zabbix_agentd start

  when: ansible_os_family == "RedHat" and ansible_distribution_major_version | int == 6


- name: Add boot start

  shell: /sbin/chkconfig --add zabbix_agentd && /sbin/chkconfig zabbix_agentd on

  when: ansible_os_family == "RedHat" and ansible_distribution_major_version | int == 6

[root@iZ23nvakegvZ roles]#
查看delete.yml文件
root@iZ23nvakegvZ roles]# cat zabbix_agent/tasks/delete.yml

- name: Delete zabbix commopression software

  shell: rm -rf /tmp/zabbix-{{ zabbix_version }}.tar.gz

  when: ansible_os_family == "RedHat" and ansible_distribution_major_version|int == 6

[root@iZ23nvakegvZ roles]#
main.yml 入口文件
[root@iZ23nvakegvZ roles]# cat zabbix_agent/tasks/main.yml

- include: copy.yml

- include: install.yml

- include: delete.yml

[root@iZ23nvakegvZ roles]#
template配置文件修改了相关参数
[root@iZ23nvakegvZ roles]# cat zabbix_agent/templates/zabbix_agentd.conf

# This is a config file for the Zabbix agent daemon (Unix)

# To get more information about Zabbix, visit http://www.zabbix.com



##### ##### ## GENERAL PARAMETERS ##### ##### ##### ##



### Option: PidFile

#   Name of PID file.

#

# Mandatory: no

# Default:

# PidFile=/tmp/zabbix_agentd.pid



### Option: LogFile

#   Name of log file.

#   If not set, syslog is used.

#

# Mandatory: no

# Default:

# LogFile=



LogFile=/tmp/zabbix_agentd.log



### Option: LogFileSize

#   Maximum size of log file in MB.

#   0 - disable automatic log rotation.

#

# Mandatory: no

# Range: 0-1024

# Default:

# LogFileSize=1



### Option: DebugLevel

#   Specifies debug level

#   0 - no debug

#   1 - critical information

#   2 - error information

#   3 - warnings

#   4 - for debugging (produces lots of information)

#

# Mandatory: no

# Range: 0-4

# Default:

# DebugLevel=3



### Option: SourceIP

#   Source IP address for outgoing connections.

#

# Mandatory: no

# Default:

# SourceIP=



### Option: EnableRemoteCommands

#   Whether remote commands from Zabbix server are allowed.

#   0 - not allowed

#   1 - allowed

#

# Mandatory: no

# Default:

# EnableRemoteCommands=0



### Option: LogRemoteCommands

#   Enable logging of executed shell commands as warnings.

#   0 - disabled

#   1 - enabled

#

# Mandatory: no

# Default:

# LogRemoteCommands=0



#####  Passive checks related



### Option: Server

#   List of comma delimited IP addresses (or hostnames) of Zabbix servers.

#   Incoming connections will be accepted only from the hosts listed here.

#   No spaces allowed.

#   If IPv6 support is enabled then '127.0.0.1', '::127.0.0.1', '::ffff:127.0.0.1' are treated equally.

#

# Mandatory: no

# Default:

# Server=zabbix-server-external.autoclouds.net



Server={{ zabbix_server_ip }}



### Option: ListenPort

#   Agent will listen on this port for connections from the server.

#

# Mandatory: no

# Range: 1024-32767

# Default:

ListenPort={{ zabbix_port }}



### Option: ListenIP

#   List of comma delimited IP addresses that the agent should listen on.

#   First IP address is sent to Zabbix server if connecting to it to retrieve list of active checks.

#

# Mandatory: no

# Default:

# ListenIP=0.0.0.0



### Option: StartAgents

#   Number of pre-forked instances of zabbix_agentd that process passive checks.

#   If set to 0, disables passive checks and the agent will not listen on any TCP port.

#

# Mandatory: no

# Range: 0-100

# Default:

# StartAgents=3



#####  Active checks related



### Option: ServerActive

#   List of comma delimited IP:port (or hostname:port) pairs of Zabbix servers for active checks.

#   If port is not specified, default port is used.

#   IPv6 addresses must be enclosed in square brackets if port for that host is specified.

#   If port is not specified, square brackets for IPv6 addresses are optional.

#   If this parameter is not specified, active checks are disabled.

#   Example: ServerActive=127.0.0.1:20051,zabbix.domain,[::1]:30051,::1,[12fc::1]

#

# Mandatory: no

# Default:

# ServerActive=





### Option: Hostname

#   Unique, case sensitive hostname.

#   Required for active checks and must match hostname as configured on the server.

#   Value is acquired from HostnameItem if undefined.

#

# Mandatory: no

# Default:

# Hostname=



Hostname={{ ansible_hostname }}



### Option: HostnameItem

#   Item used for generating Hostname if it is undefined.

#   Ignored if Hostname is defined.

#

# Mandatory: no

# Default:

# HostnameItem=system.hostname



### Option: RefreshActiveChecks

#   How often list of active checks is refreshed, in seconds.

#

# Mandatory: no

# Range: 60-3600

# Default:

# RefreshActiveChecks=120



### Option: BufferSend

#   Do not keep data longer than N seconds in buffer.

#

# Mandatory: no

# Range: 1-3600

# Default:

# BufferSend=5



### Option: BufferSize

#   Maximum number of values in a memory buffer. The agent will send

#   all collected data to Zabbix Server or Proxy if the buffer is full.

#

# Mandatory: no

# Range: 2-65535

# Default:

# BufferSize=100



### Option: MaxLinesPerSecond

#   Maximum number of new lines the agent will send per second to Zabbix Server

#   or Proxy processing 'log' and 'logrt' active checks.

#   The provided value will be overridden by the parameter 'maxlines',

#   provided in 'log' or 'logrt' item keys.

#

# Mandatory: no

# Range: 1-1000

# Default:

# MaxLinesPerSecond=100



### Option: AllowRoot

#   Allow the agent to run as 'root'. If disabled and the agent is started by 'root', the agent

#       will try to switch to user 'zabbix' instead. Has no effect if started under a regular user.

#   0 - do not allow

#   1 - allow

#

# Mandatory: no

# Default:

# AllowRoot=0



##### ##### ## ADVANCED PARAMETERS ##### ##### ##### ##



### Option: Alias

#   Sets an alias for parameter. It can be useful to substitute long and complex parameter name with a smaller and simpler one.

#

# Mandatory: no

# Range:

# Default:



### Option: Timeout

#   Spend no more than Timeout seconds on processing

#

# Mandatory: no

# Range: 1-30

# Default:

Timeout=20



### Option: Include

#   You may include individual files or all files in a directory in the configuration file.

#   Installing Zabbix will create include directory in /usr/local/etc, unless modified during the compile time.

#

# Mandatory: no

# Default:

# Include=



# Include=/usr/local/etc/zabbix_agentd.userparams.conf

# Include=/usr/local/etc/zabbix_agentd.conf.d/



##### ## USER-DEFINED MONITORED PARAMETERS ##### ##



### Option: UnsafeUserParameters

#   Allow all characters to be passed in arguments to user-defined parameters.

#   0 - do not allow

#   1 - allow

#

# Mandatory: no

# Range: 0-1

# Default:

# UnsafeUserParameters=0



### Option: UserParameter

#   User-defined parameter to monitor. There can be several user-defined parameters.

#   Format: UserParameter=<key>,<shell command>

#   See 'zabbix_agentd' directory for examples.

#

# Mandatory: no

# Default:

# UserParameter=

[root@iZ23nvakegvZ roles]#
template 开机启动脚本
[root@iZ23nvakegvZ roles]# cat zabbix_agent/templates/zabbix_agentd

#!/bin/bash

#

# chkconfig: - 85 15

# description: Zabbix client script.

# processname: Zabbix

. /etc/profile

SERVICE="Zabbix agent"

DAEMON={{ zabbix_dir }}/zabbix/sbin/zabbix_agentd

PIDFILE=/tmp/zabbix_agentd.pid

CONFIG={{ zabbix_dir }}/zabbix/conf/zabbix_agentd.conf

zabbix_agent_status=`ps aux|grep zabbix_agentd.conf|grep -v grep|wc -l`

zabbix_agent_pid=`ps aux|grep zabbix_agentd|grep -v grep|awk 'NR==1{print $2}'`

# Source function library.

. /etc/rc.d/init.d/functions

# Source networking configuration.

. /etc/sysconfig/network

function check()

{

if [ $? -eq 0 ];then

    action $"Operating is:" /bin/true

else

    action $"Operating is:" /bin/false

fi

}

case $1 in

  'start')

    if [ -x ${DAEMON} ]

    then

      $DAEMON -c $CONFIG

      # Error checking here would be good...

      echo "${SERVICE} started."

    else

      echo "Can't find file ${DAEMON}."

      echo "${SERVICE} NOT started."

    fi

    check

  ;;



  'stop')

    if [ -s ${PIDFILE} ]

    then

      if kill `cat ${PIDFILE}` >/dev/null 2>&1

      then

        echo "${SERVICE} terminated."

        rm -f ${PIDFILE}

      fi

    fi

    check

  ;;

  'restart')

   /bin/bash  $0 stop

    sleep 5

    /bin/bash $0 start

  ;;



  'status')

    if [ $zabbix_agent_status -ne 0 ];then

        echo "Zabbix Agentd is running ($zabbix_agent_pid)"

    else

        echo "Zabbix Agentd is not running!"

    fi

    ;;



*)

    echo  "Usage: $0 {start|stop|status|restart}"

;;



esac

exit 0

[root@iZ23nvakegvZ roles]#
运行测试
[root@zm-ops-test-01 zabbix_agent]# ansible-playbook -i hosts  site.yml
[root@zm-ops-test-01 zabbix_agent]#
OpsNotes wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!