集群运维自动化工具ansible之使用playbook安装zabbix客户端

之前介绍了关于ansible的安装与使用(包括模块与playbook使用,地址是http://www.jb51.net/article/52154.htm),今天介绍一下如何使用playbook来部署zabbix客户端。
ansible服务端的环境为centos 6.5 x86_64系统
ansible客户端环境为centos 6.3 x86_64系统
目前我的playbook只允许centos或redhat 6系列系统来安装zabbix客户端,并且客户端的版本是2.0.6.
下面是playbook的结构

14:29:30 # pwd
/etc/ansible/roles
root@ip-10-10-10-10:/etc/ansible/roles
14:29:37 # tree zabbix_client_*
zabbix_client_delete  删除已经安装的zabbix客户端
├── files      存放文件的
├── handlers    重启的东东
├── meta      galaxy_info的信息
│  └── main.yml
├── tasks      操作的任务流程
│  ├── delete.yml
│  └── main.yml
├── templates    模板
└── vars      变量
  └── main.yml
zabbix_client_install
├── files
│  └── zabbix-2.0.6.tar.gz
├── handlers
├── meta
│  └── main.yml
├── tasks
│  ├── copy.yml
│  ├── delete.yml
│  ├── install.yml
│  └── main.yml
├── templates
│  ├── zabbix_agentd
│  └── zabbix_agentd.conf
└── vars
  └── main.yml

12 directories, 13 files

下面是先介绍一下安装方面zabbix_client_install的内容
1、galaxy_info的信息

14:32:15 # cat /etc/ansible/roles/zabbix_client_install/meta/main.yml
galaxy_info:
 author: Deng Lei
 description: Install Zabbix Client
 license: MIT
 min_ansible_version: 1.6
 platforms:
 - name: CentOS
  versions:
  - 6
 categories:
 - Monitor
dependencies: []

2、task里的copy.xml信息

14:33:35 # cat /etc/ansible/roles/zabbix_client_install/tasks/copy.yml
 - name: Stop Exist Zabbix Client Service In Redhat Client
  shell: ps -ef|grep zabbix|grep -v grep|awk '{print $2}'|xargs kill -9 >>/dev/null 2>&1
  ignore_errors: yes
  when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6
 - name: Delete Exist Zabbix Client Dir In Redhat Client
  shell: rm -rf {{ zabbix_dir }}/zabbix
  ignore_errors: yes
  when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6
 - name: Install Base Require Software In Redhat Client
  yum: name={{ item }} state=latest
  with_items:
   - telnet
   - dmidecode
   - tar
 - name: Create Zabbix User In Redhat Client
  user: name={{ zabbix_user }} state=present createhome=no shell=/sbin/nologin
  when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6
 - name: Copy Zabbix Client Software To Redhat Client
  copy: 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
 - name: Uncompression Zabbix Client Software To Redhat Client
  shell: tar zxf /tmp/zabbix-{{ zabbix_version }}.tar.gz -C {{ zabbix_dir }}/
  when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6
 - name: Copy Zabbix Start Script To Redhat Client
  template: src=zabbix_agentd dest=/etc/init.d/zabbix_agentd owner=root group=root mode=0755
  when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6
 - name: Copy Zabbix Config To Redhat Client
  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_lsb.major_release|int == 6

此文件是复制对应的文件到客户端

3、task的install.yml信息

14:34:26 # cat /etc/ansible/roles/zabbix_client_install/tasks/install.yml
 - name: Modify Zabbix Dir Permission In Redhat Client
  file: path={{ zabbix_dir }}/zabbix owner={{ zabbix_user }} group={{ zabbix_user }} mode=0755
  when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6
 - name: Check Zabbix User Sudo Permission In Redhat Client
  shell: grep "{{ zabbix_user }}" /etc/sudoers|wc -l
  register: zabbix_sudoer
  ignore_errors: True
  when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6
 - name: Give Sudo Permission To Zabbix User In Redhat Client
  shell: echo "{{ zabbix_user }} ALL=(root) NOPASSWD:/bin/netstat, /usr/bin/omreport" >> /etc/sudoers
  when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6 and zabbix_sudoer|int ==0
 - name: Start Zabbix Service In Redhat Client
  shell: /etc/init.d/zabbix_agentd start
  when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6
 - name: Add Boot Start Zabbix Service In Redhat Client
  shell: chkconfig --level 345 zabbix_agentd on
  when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6

此文件主要是安装

4、tasks的delete.yml信息

14:35:08 # cat /etc/ansible/roles/zabbix_client_install/tasks/delete.yml
 - name: Delete Zabbix compression Software In Redhat Client
  shell: rm -rf /tmp/zabbix-{{ zabbix_version }}.tar.gz
  when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int == 6

此文件是安装完成后,删除安装前的文件

5、tasks的mail.yml

14:35:37 # cat /etc/ansible/roles/zabbix_client_install/tasks/main.yml
- include: copy.yml
- include: install.yml
- include: delete.yml

此文件是允许运行哪个文件

6、templates的zabbix_agentd

15:15:45 # cat /etc/ansible/roles/zabbix_client_install/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

这个文件是启动客户端的脚本

7、templates的zabbix_agentd.conf

15:16:36 # cat /etc/ansible/roles/zabbix_client_install/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=
UserParameter=memcached_stats[*],(echo stats; sleep 1) | telnet {{ ansible_default_ipv4.address }} $1 2>&1 | awk '/STAT $2 / {print $NF}'
UserParameter=mysql[*],mysql -h {{ ansible_default_ipv4.address }} -P 3306 -uzabbix -pzabbix -e "show global status"|grep "\<$1\>"|cut -f2
UserParameter=redis_stats[*],(echo info; sleep 1) | telnet {{ ansible_default_ipv4.address }} $1 2>&1 |grep $2|cut -d : -f2
UserParameter=custom.vfs.dev.read.ops[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$4}'
UserParameter=custom.vfs.dev.read.ms[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$7}'
UserParameter=custom.vfs.dev.write.ops[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$8}'
UserParameter=custom.vfs.dev.write.ms[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$11}'
UserParameter=custom.vfs.dev.io.active[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$12}'
UserParameter=custom.vfs.dev.io.ms[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$13}'
UserParameter=custom.vfs.dev.read.sectors[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$6}'
UserParameter=custom.vfs.dev.write.sectors[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$10}'
UserParameter=MongoDB.Status[*],/bin/echo "db.serverStatus().$1" | /usr/bin/mongo admin | grep "$2"|awk -F: '{print $$2}'|awk -F, '{print $$1}'
UserParameter=check_lvm[*],/usr/bin/sudo /usr/local/zabbix/bin/check_lvm.sh $1
UserParameter=TCP_ESTABLISHED,ss -s|awk 'NR==2{print $4}'|cut -d , -f1
UserParameter=TCP_CLOSED,ss -s|awk 'NR==2{print $6}'|cut -d , -f1
UserParameter=TCP_TIMEWAIT,ss -s|awk 'NR==2{print $12}'|cut -d / -f1
UserParameter=zabbix_low_discovery[*],/bin/bash /usr/local/zabbix/bin/zabbix_low_discovery.sh $1
UserParameter=mysql_stats[*],mysql -h {{ ansible_default_ipv4.address }} -P $1 -uzabbix -pzabbix -e "show global status"|grep "\<$2\>"|cut -f2
UserParameter=mysql_stats_slave[*],mysql -h {{ ansible_default_ipv4.address }} -P $1 -uzabbix -pzabbix -e "show slave status\G"|grep "\<$2\>"|awk '{if($NF=="Yes") {print 1} else {print 0}}'
UserParameter=check_platform,dmidecode |grep Vendor|awk -F ' ' '{if($2=="Dell") {print 1} else {print 0}}'
#follow is hardware monitor
UserParameter=hardware_battery,omreport chassis batteries|awk '/^Status/{if($NF=="Ok") {print 1} else {print 0}}'
UserParameter=hardware_cpu_model,awk -v hardware_cpu_crontol=`sudo omreport chassis biossetup|awk '/C State/{if($NF=="Enabled") {print 0} else {print 1}}'` -v hardware_cpu_c1=`sudo omreport chassis biossetup|awk '/C1[-|E]/{if($NF=="Enabled") {print 0} else {print 1}}'` 'BEGIN{if(hardware_cpu_crontol==0 && hardware_cpu_c1==0) {print 0} else {print 1}}'
UserParameter=hardware_fan_health,awk -v hardware_fan_number=`omreport chassis fans|grep -c "^Index"` -v hardware_fan=`omreport chassis fans|awk '/^Status/{if($NF=="Ok") count+=1}END{print count}'` 'BEGIN{if(hardware_fan_number==hardware_fan) {print 1} else {print 0}}'
UserParameter=hardware_memory_health,awk -v hardware_memory=`omreport chassis memory|awk '/^Health/{print $NF}'` 'BEGIN{if(hardware_memory=="Ok") {print 1} else {print 0}}'
UserParameter=hardware_nic_health,awk -v hardware_nic_number=`omreport chassis nics |grep -c "Interface Name"` -v hardware_nic=`omreport chassis nics |awk '/^Connection Status/{print $NF}'|wc -l` 'BEGIN{if(hardware_nic_number==hardware_nic) {print 1} else {print 0}}'
UserParameter=hardware_cpu,omreport chassis processors|awk '/^Health/{if($NF=="Ok") {print 1} else {print 0}}'
UserParameter=hardware_power_health,awk -v hardware_power_number=`omreport chassis pwrsupplies|grep -c "Index"` -v hardware_power=`omreport chassis pwrsupplies|awk '/^Status/{if($NF=="Ok") count+=1}END{print count}'` 'BEGIN{if(hardware_power_number==hardware_power) {print 1} else {print 0}}'
UserParameter=hardware_temp,omreport chassis temps|awk '/^Status/{if($NF=="Ok") {print 1} else {print 0}}'|head -n 1
UserParameter=hardware_physics_health,awk -v hardware_physics_disk_number=`omreport storage pdisk controller=0|grep -c "^ID"` -v hardware_physics_disk=`omreport storage pdisk controller=0|awk '/^State/{if($NF=="Online") count+=1}END{print count}'` 'BEGIN{if(hardware_physics_disk_number==hardware_physics_disk) {print 1} else {print 0}}'
UserParameter=hardware_virtual_health,awk -v hardware_virtual_disk_number=`omreport storage vdisk controller=0|grep -c "^ID"` -v hardware_virtual_disk=`omreport storage vdisk controller=0|awk '/^State/{if($NF=="Ready") count+=1}END{print count}'` 'BEGIN{if(hardware_virtual_disk_number==hardware_virtual_disk) {print 1} else {print 0}}'
UserParameter=pyora[*],/usr/local/zabbix/bin/pyora.py --username $1 --password $2 --address $3 --database $4 $5 $6 $7 $8

此文件是客户端的配置文件
8、vars的main.yml

15:17:06 # cat /etc/ansible/roles/zabbix_client_install/vars/main.yml
zabbix_dir: /usr/local      客户端安全目录
zabbix_version: 2.0.6      客户端软件版本
zabbix_user: zabbix       客户端用户
zabbix_port: 10050        客户端的端口
zabbix_server_ip: 192.168.1.10  zabbix_server的ip

此文件是配置变量的

9、ansible安装zabbix客户端的playbook配置文件zabbix_client_install.yml

15:20:02 # cat /etc/ansible/zabbix_client_install.yml
---
- hosts: "{{host}}"
 remote_user: "{{user}}"
 gather_facts: True
 roles:
  - zabbix_client_install

10、使用playbook安装zabbix客户端

我的测试客户端环境是centos 6.3,ip是192.168.240.17,使用key登陆

15:22:01 # cd /etc/ansible/
root@ip-10-10-10-10:/etc/ansible
15:22:04 # time ansible-playbook zabbix_client_install.yml --extra-vars "host=192.168.240.17 user=root" --private-key=/root/test.pem 

PLAY [192.168.240.17] ********************************************************* 

GATHERING FACTS ***************************************************************
ok: [192.168.240.17]

TASK: [zabbix_client_install | Stop Exist Zabbix Client Service In Redhat Client] ***
failed: [192.168.240.17] => {"changed": true, "cmd": "ps -ef|grep zabbix|grep -v grep|awk '{print $2}'|xargs kill -9 >>/dev/null 2>&1 ", "delta": "0:00:00.018213", "end": "2014-07-10 07:22:34.793910", "item": "", "rc": 123, "start": "2014-07-10 07:22:34.775697"}
...ignoring

TASK: [zabbix_client_install | Delete Exist Zabbix Client Dir In Redhat Client] ***
changed: [192.168.240.17]

TASK: [zabbix_client_install | Install Base Require Software In Redhat Client] ***
ok: [192.168.240.17] => (item=telnet,dmidecode,tar)

TASK: [zabbix_client_install | Create Zabbix User In Redhat Client] ***********
changed: [192.168.240.17]

TASK: [zabbix_client_install | Copy Zabbix Client Software To Redhat Client] ***
changed: [192.168.240.17]

TASK: [zabbix_client_install | Uncompression Zabbix Client Software To Redhat Client] ***
changed: [192.168.240.17]

TASK: [zabbix_client_install | Copy Zabbix Start Script To Redhat Client] *****
changed: [192.168.240.17]

TASK: [zabbix_client_install | Copy Zabbix Config To Redhat Client] ***********
changed: [192.168.240.17]

TASK: [zabbix_client_install | Modify Zabbix Dir Permission In Redhat Client] ***
ok: [192.168.240.17]

TASK: [zabbix_client_install | Check Zabbix User Sudo Permission In Redhat Client] ***
changed: [192.168.240.17]

TASK: [zabbix_client_install | Give Sudo Permission To Zabbix User In Redhat Client] ***
changed: [192.168.240.17]

TASK: [zabbix_client_install | Start Zabbix Service In Redhat Client] *********
changed: [192.168.240.17]

TASK: [zabbix_client_install | Add Boot Start Zabbix Service In Redhat Client] ***
changed: [192.168.240.17]

TASK: [zabbix_client_install | Delete Zabbix compression Software In Redhat Client] ***
changed: [192.168.240.17]

PLAY RECAP ********************************************************************
192.168.240.17       : ok=15  changed=12  unreachable=0  failed=0  

real  0m39.888s
user  0m1.547s
sys 0m0.197s

可以看到39秒就安装完成,主要花费时间比较长的地方是fact收集、yum安装、文件传输。

11、测试安装情况

[root@ip-10-10-240-21 tmp]# ifconfig
eth0   Link encap:Ethernet HWaddr FA:16:3E:34:62:D0
     inet addr:10.10.240.21 Bcast:10.10.240.255 Mask:255.255.255.0
     inet6 addr: fe80::f816:3eff:fe34:62d0/64 Scope:Link
     UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
     RX packets:542391 errors:0 dropped:0 overruns:0 frame:0
     TX packets:77391 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:1000
     RX bytes:142341119 (135.7 MiB) TX bytes:6451154 (6.1 MiB)

lo    Link encap:Local Loopback
     inet addr:127.0.0.1 Mask:255.0.0.0
     inet6 addr: ::1/128 Scope:Host
     UP LOOPBACK RUNNING MTU:16436 Metric:1
     RX packets:10 errors:0 dropped:0 overruns:0 frame:0
     TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:0
     RX bytes:700 (700.0 b) TX bytes:700 (700.0 b)

[root@ip-10-10-240-21 tmp]# ps -ef|grep zabbix
zabbix  26991   1 0 07:22 ?    00:00:00 /usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/conf/zabbix_agentd.conf
zabbix  26993 26991 0 07:22 ?    00:00:00 /usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/conf/zabbix_agentd.conf
zabbix  26994 26991 0 07:22 ?    00:00:00 /usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/conf/zabbix_agentd.conf
zabbix  26995 26991 0 07:22 ?    00:00:00 /usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/conf/zabbix_agentd.conf
zabbix  26996 26991 0 07:22 ?    00:00:00 /usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/conf/zabbix_agentd.conf
root   27102 13773 0 07:24 pts/0  00:00:00 grep zabbix
[root@ip-10-10-240-21 tmp]# grep -Ev '^$|^#' /usr/local/zabbix/conf/zabbix_agentd.conf
LogFile=/tmp/zabbix_agentd.log
Server=192.168.1.10
ListenPort=10050
Hostname=ip-10-10-240-21
Timeout=20
UserParameter=memcached_stats[*],(echo stats; sleep 1) | telnet 10.10.240.21 $1 2>&1 | awk '/STAT $2 / {print $NF}'
UserParameter=mysql[*],mysql -h 10.10.240.21 -P 3306 -uzabbix -pzabbix -e "show global status"|grep "\<$1\>"|cut -f2
UserParameter=redis_stats[*],(echo info; sleep 1) | telnet 10.10.240.21 $1 2>&1 |grep $2|cut -d : -f2
UserParameter=custom.vfs.dev.read.ops[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$4}'
UserParameter=custom.vfs.dev.read.ms[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$7}'
UserParameter=custom.vfs.dev.write.ops[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$8}'
UserParameter=custom.vfs.dev.write.ms[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$11}'
UserParameter=custom.vfs.dev.io.active[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$12}'
UserParameter=custom.vfs.dev.io.ms[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$13}'
UserParameter=custom.vfs.dev.read.sectors[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$6}'
UserParameter=custom.vfs.dev.write.sectors[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$10}'
UserParameter=MongoDB.Status[*],/bin/echo "db.serverStatus().$1" | /usr/bin/mongo admin | grep "$2"|awk -F: '{print $$2}'|awk -F, '{print $$1}'
UserParameter=check_lvm[*],/usr/bin/sudo /usr/local/zabbix/bin/check_lvm.sh $1
UserParameter=TCP_ESTABLISHED,ss -s|awk 'NR==2{print $4}'|cut -d , -f1
UserParameter=TCP_CLOSED,ss -s|awk 'NR==2{print $6}'|cut -d , -f1
UserParameter=TCP_TIMEWAIT,ss -s|awk 'NR==2{print $12}'|cut -d / -f1
UserParameter=zabbix_low_discovery[*],/bin/bash /usr/local/zabbix/bin/zabbix_low_discovery.sh $1
UserParameter=mysql_stats[*],mysql -h 10.10.240.21 -P $1 -uzabbix -pzabbix -e "show global status"|grep "\<$2\>"|cut -f2
UserParameter=mysql_stats_slave[*],mysql -h 10.10.240.21 -P $1 -uzabbix -pzabbix -e "show slave status\G"|grep "\<$2\>"|awk '{if($NF=="Yes") {print 1} else {print 0}}'
UserParameter=check_platform,dmidecode |grep Vendor|awk -F ' ' '{if($2=="Dell") {print 1} else {print 0}}'
UserParameter=hardware_battery,omreport chassis batteries|awk '/^Status/{if($NF=="Ok") {print 1} else {print 0}}'
UserParameter=hardware_cpu_model,awk -v hardware_cpu_crontol=`sudo omreport chassis biossetup|awk '/C State/{if($NF=="Enabled") {print 0} else {print 1}}'` -v hardware_cpu_c1=`sudo omreport chassis biossetup|awk '/C1[-|E]/{if($NF=="Enabled") {print 0} else {print 1}}'` 'BEGIN{if(hardware_cpu_crontol==0 && hardware_cpu_c1==0) {print 0} else {print 1}}'
UserParameter=hardware_fan_health,awk -v hardware_fan_number=`omreport chassis fans|grep -c "^Index"` -v hardware_fan=`omreport chassis fans|awk '/^Status/{if($NF=="Ok") count+=1}END{print count}'` 'BEGIN{if(hardware_fan_number==hardware_fan) {print 1} else {print 0}}'
UserParameter=hardware_memory_health,awk -v hardware_memory=`omreport chassis memory|awk '/^Health/{print $NF}'` 'BEGIN{if(hardware_memory=="Ok") {print 1} else {print 0}}'
UserParameter=hardware_nic_health,awk -v hardware_nic_number=`omreport chassis nics |grep -c "Interface Name"` -v hardware_nic=`omreport chassis nics |awk '/^Connection Status/{print $NF}'|wc -l` 'BEGIN{if(hardware_nic_number==hardware_nic) {print 1} else {print 0}}'
UserParameter=hardware_cpu,omreport chassis processors|awk '/^Health/{if($NF=="Ok") {print 1} else {print 0}}'
UserParameter=hardware_power_health,awk -v hardware_power_number=`omreport chassis pwrsupplies|grep -c "Index"` -v hardware_power=`omreport chassis pwrsupplies|awk '/^Status/{if($NF=="Ok") count+=1}END{print count}'` 'BEGIN{if(hardware_power_number==hardware_power) {print 1} else {print 0}}'
UserParameter=hardware_temp,omreport chassis temps|awk '/^Status/{if($NF=="Ok") {print 1} else {print 0}}'|head -n 1
UserParameter=hardware_physics_health,awk -v hardware_physics_disk_number=`omreport storage pdisk controller=0|grep -c "^ID"` -v hardware_physics_disk=`omreport storage pdisk controller=0|awk '/^State/{if($NF=="Online") count+=1}END{print count}'` 'BEGIN{if(hardware_physics_disk_number==hardware_physics_disk) {print 1} else {print 0}}'
UserParameter=hardware_virtual_health,awk -v hardware_virtual_disk_number=`omreport storage vdisk controller=0|grep -c "^ID"` -v hardware_virtual_disk=`omreport storage vdisk controller=0|awk '/^State/{if($NF=="Ready") count+=1}END{print count}'` 'BEGIN{if(hardware_virtual_disk_number==hardware_virtual_disk) {print 1} else {print 0}}'
UserParameter=pyora[*],/usr/local/zabbix/bin/pyora.py --username $1 --password $2 --address $3 --database $4 $5 $6 $7 $8
[root@ip-10-10-240-21 tmp]# ll /tmp/
total 12
-rw------- 1 root  root  197 Jul 9 09:35 yum_save_tx-2014-07-09-09-35ibcBiO.yumtx
-rw-rw-r-- 1 zabbix zabbix 320 Jul 10 07:22 zabbix_agentd.log
-rw-rw-r-- 1 zabbix zabbix  5 Jul 10 07:22 zabbix_agentd.pid
[root@ip-10-10-240-21 tmp]# chkconfig --list|grep zabbix_agentd
zabbix_agentd   0:off  1:off  2:off  3:on  4:on  5:on  6:off
[root@ip-10-10-240-21 tmp]# grep zabbix /etc/sudoers
zabbix ALL=(root) NOPASSWD:/bin/netstat, /usr/bin/omreport
[root@ip-10-10-240-21 tmp]# ll /etc/init.d/zabbix_agentd
-rwxr-xr-x 1 root root 1444 Jul 10 07:22 /etc/init.d/zabbix_agentd

可以看到安装后的客户端,完全是按照我的要求来做的。

12、删除已经安装的客户端

15:22:54 # time ansible-playbook zabbix_client_delete.yml --extra-vars "host=192.168.240.17 user=root" --private-key=/root/test.pem 

PLAY [192.168.240.17] ********************************************************* 

GATHERING FACTS ***************************************************************
ok: [192.168.240.17]

TASK: [zabbix_client_delete | Stop Zabbix Service In RedHat Client] ***********
changed: [192.168.240.17]

TASK: [zabbix_client_delete | Delete Boot Start Zabbix Service In Redhat Client] ***
changed: [192.168.240.17]

TASK: [zabbix_client_delete | Delete Zabbix User In Redhat Client] ************
changed: [192.168.240.17]

TASK: [zabbix_client_delete | Delete Zabbix Dir In Redhat Client] *************
changed: [192.168.240.17]

TASK: [zabbix_client_delete | Delete Zabbix Start Script In Redhat Client] ****
changed: [192.168.240.17]

TASK: [zabbix_client_delete | Check Zabbix User Sudo Permission In Redhat Client] ***
changed: [192.168.240.17]

TASK: [zabbix_client_delete | Delete Sudo Permission To Zabbix User In Redhat Client] ***
changed: [192.168.240.17]

PLAY RECAP ********************************************************************
192.168.240.17       : ok=8  changed=7  unreachable=0  failed=0  

real  0m25.497s
user  0m0.847s
sys 0m0.159s

13、测试删除情况

[root@ip-10-10-240-21 tmp]# ll /tmp/
total 4
-rw------- 1 root root 197 Jul 9 09:35 yum_save_tx-2014-07-09-09-35ibcBiO.yumtx
[root@ip-10-10-240-21 tmp]# ps -ef|grep zabbix
root   27665 13773 0 07:27 pts/0  00:00:00 grep zabbix
[root@ip-10-10-240-21 tmp]# ll /usr/local/
total 40
drwxr-xr-x. 2 root root 4096 Sep 23 2011 bin
drwxr-xr-x. 2 root root 4096 Sep 23 2011 etc
drwxr-xr-x. 2 root root 4096 Sep 23 2011 games
drwxr-xr-x. 2 root root 4096 Sep 23 2011 include
drwxr-xr-x. 2 root root 4096 Sep 23 2011 lib
drwxr-xr-x. 2 root root 4096 Sep 23 2011 lib64
drwxr-xr-x. 2 root root 4096 Sep 23 2011 libexec
drwxr-xr-x. 2 root root 4096 Sep 23 2011 sbin
drwxr-xr-x. 5 root root 4096 May 12 2013 share
drwxr-xr-x. 3 root root 4096 May 13 2013 src
[root@ip-10-10-240-21 tmp]# grep zabbix /etc/sudoers
[root@ip-10-10-240-21 tmp]# ll /etc/init.d/zabbix_agentd
ls: cannot access /etc/init.d/zabbix_agentd: No such file or directory
[root@ip-10-10-240-21 tmp]# chkconfig --list|grep zabbix_agentd
[root@ip-10-10-240-21 tmp]#

可以看到已经完全删除。
如果大家想使用我的例子,可以从附件里下载,然后放到/etc/ansible目录里,下面是压缩包里的内容

-rw-r--r-- root/root    108 2014-07-10 15:20 zabbix_client_install.yml
-rw-r--r-- root/root    121 2014-07-09 18:09 zabbix_client_delete.yml
drwxr-xr-x root/root     0 2014-07-01 16:38 roles/zabbix_client_install/
drwxr-xr-x root/root     0 2014-07-08 14:30 roles/zabbix_client_install/meta/
-rw-r--r-- root/root    207 2014-07-08 14:30 roles/zabbix_client_install/meta/main.yml
drwxr-xr-x root/root     0 2014-07-10 14:07 roles/zabbix_client_install/tasks/
-rw-r--r-- root/root    199 2014-07-10 14:02 roles/zabbix_client_install/tasks/delete.yml
-rw-r--r-- root/root    65 2014-07-10 14:02 roles/zabbix_client_install/tasks/main.yml
-rw-r--r-- root/root   1789 2014-07-10 14:02 roles/zabbix_client_install/tasks/copy.yml
-rw-r--r-- root/root   1110 2014-07-10 14:07 roles/zabbix_client_install/tasks/install.yml
drwxr-xr-x root/root     0 2014-06-19 13:30 roles/zabbix_client_install/handlers/
drwxr-xr-x root/root     0 2014-07-09 17:54 roles/zabbix_client_install/vars/
-rw-r--r-- root/root    115 2014-07-09 17:54 roles/zabbix_client_install/vars/main.yml
drwxr-xr-x root/root     0 2014-07-09 17:53 roles/zabbix_client_install/templates/
-rw-r--r-- zabbix/zabbix 10465 2014-07-09 17:53 roles/zabbix_client_install/templates/zabbix_agentd.conf
-rwxr-xr-x root/root   1456 2014-07-08 15:20 roles/zabbix_client_install/templates/zabbix_agentd
drwxr-xr-x root/root     0 2014-07-09 17:13 roles/zabbix_client_install/files/
-rw-r--r-- root/root  292293 2014-07-09 17:13 roles/zabbix_client_install/files/zabbix-2.0.6.tar.gz
drwxr-xr-x root/root     0 2014-06-23 14:03 roles/zabbix_client_delete/
drwxr-xr-x root/root     0 2014-07-09 18:08 roles/zabbix_client_delete/meta/
-rw-r--r-- root/root    205 2014-07-09 18:08 roles/zabbix_client_delete/meta/main.yml
drwxr-xr-x root/root     0 2014-07-10 14:28 roles/zabbix_client_delete/tasks/
-rw-r--r-- root/root   1518 2014-07-10 14:08 roles/zabbix_client_delete/tasks/delete.yml
-rw-r--r-- root/root    22 2014-07-10 14:08 roles/zabbix_client_delete/tasks/main.yml
drwxr-xr-x root/root     0 2014-06-24 14:14 roles/zabbix_client_delete/handlers/
drwxr-xr-x root/root     0 2014-07-03 13:16 roles/zabbix_client_delete/vars/
-rw-r--r-- root/root    115 2014-07-09 17:55 roles/zabbix_client_delete/vars/main.yml
drwxr-xr-x root/root     0 2014-07-09 18:08 roles/zabbix_client_delete/templates/
drwxr-xr-x root/root     0 2014-06-24 13:53 roles/zabbix_client_delete/files/

后续我会继续介绍使用playbook安装其他软件的例子。

(0)

相关推荐

  • 安装配置Zabbix来监控MySQL的基本教程

    Zabbix的简单安装配置说明 1.在已有的LAMP或者LNMP的基础上安装zabbix,安装一些依赖包: yum -y install mysql-devel libcurl-devel net-snmp-devel 2.添加用户: groupadd zabbix useradd zabbix -g zabbix 3.创建数据库,添加授权账号 create database zabbix character set utf8; grant all privileges on zabbix.*

  • Windows 安装配置 Zabbix Agentd

    一.系统信息 操作系统:windows 2003 依赖软件:zabbix agentd 客户端网络信息:192.168.39.254 服务端信息:192.168.6.124 二.下载与配置 1.Zabbix Agentd 下载地址:http://www.jb51.net/softs/456729.html 2.Zabbix Agentd安装 将下载的文件解压至 C:\Program Files\zabbix_agents 目录中,并创建conf.d,logs目录,如下图所示 3.修改conf\z

  • zabbix v3.0安装部署全过程详解

    关于zabbix及相关服务软件版本: Linux:centos 6.6 nginx:1.9.15 MySQL:5.5.49 PHP:5.5.35 一.安装nginx: 安装依赖包: yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre* make gd-devel libjpeg-devel libpng-devel libxml2-devel bzip2-devel lib

  • Zabbix安装图文教程(需要LAMP或者LNMP运行环境)

    说明: 操作系统:CentOS IP地址:192.168.21.127 Web环境:Nginx+MySQL+PHP zabbix版本:Zabbix 2.2 LTS 备注:Linux下安装zabbix需要有LAMP或者LNMP运行环境 准备篇: 一.Web环境:Nginx+MySQL+PHP CentOS 7.0编译安装Nginx1.6.0+MySQL5.6.19+PHP5.5.14 二.zabbix软件包下载 zabbix-2.2.6 http://jaist.dl.sourceforge.ne

  • CentOS 7.2安装Zabbix 3.2教程详解

    安装环境:VMware虚拟机 [root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) [root@localhost ~]# uname -a Linux localhost.localdomain 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux 为方便你参考,发

  • zabbix 2.2安装步骤详细介绍

    Zabbix简介 Zabbix是一个企业级的开源分布式监控解决方案,由一个国外的团队持续维护更新,软件可以自由下载使用,运作团队靠提供收费的技术支持赢利. 官方网站:http://www.zabbix.com 官方文档:http://www.zabbix.com/documentation/2.0/manual/quickstart. Zabbix通过C/S模式采集数据,通过B/S模式在web端展示和配置. Zabbix运行条件: Server:Zabbix Server需运行在LAMP(Lin

  • 集群运维自动化工具ansible之使用playbook安装zabbix客户端

    之前介绍了关于ansible的安装与使用(包括模块与playbook使用,地址是http://www.jb51.net/article/52154.htm),今天介绍一下如何使用playbook来部署zabbix客户端. ansible服务端的环境为centos 6.5 x86_64系统 ansible客户端环境为centos 6.3 x86_64系统 目前我的playbook只允许centos或redhat 6系列系统来安装zabbix客户端,并且客户端的版本是2.0.6. 下面是playbo

  • 集群运维自动化工具ansible使用playbook安装mysql

    上次介绍了如何使用ansible playbook安装zabbix客户端(http://www.jb51.net/article/52158.htm),这次介绍一下如何使用playbook安装mysql. 下面是安装mysql的信息: mysql_basedir: /data/mysql/basedir 源码目录 mysql_datadir: /data/mysql/datadir 数据目录 mysql_user: mysql mysql用户 mysql_database_user: root

  • 集群运维自动化工具ansible的安装与使用(包括模块与playbook使用)第1/2页

    我使用过puppet与salt,但这2个软件都需要安装客户端,并且更新很快,每次更新都是令人蛋疼的事,尤其是salt,喜欢他的命令功能,但bug太多,不敢在公司线上使用,puppet虽然稳定,但弄命令执行的时候,需要mco配置,非常麻烦,我公司由于跟多家公司合作,很多业务没办法安装客户端,所以没办法使用puppet与salt(虽然salt有ssh,但不太好使),最后找到了ansible,他既有命令执行也有配置管理,关键开发它的语言是python,paramiko进行ssh连接,跟我之前开发的自动

  • Python运维自动化之nginx配置文件对比操作示例

    本文实例讲述了Python运维自动化之nginx配置文件对比操作.分享给大家供大家参考,具体如下: 文件差异对比diff.py #!/usr/bin/env python # import difflib import sys try: textfile1=sys.argv[1] textfile2=sys.argv[2] except exception,e: print "Error:"+str(2) print "Usge: difflib.py file1 file2

  • Python运维自动化之paramiko模块应用实例

    目录 运维自动化Python 一.模块介绍 二.模块应用 1.使用paramiko模块,通过ssh协议连接服务器 2.解决首次连接known_hosts问题 3.执行命令exec_command方法 扩展: 使用try异常捕获 4.多台服务器执行命令 5.从服务器上传下载文件–SFTPClient方法 6.多台服务器上传下载文件 扩展-文件操作 可扩展练习 总结 运维自动化Python paramiko模块 一.模块介绍 模块:paramiko 模块作用: 1.通过ssh协议远程执行命令 2.文

  • Redis集群节点通信过程/原理流程分析

    目录 简介 通信流程 Gossip消息 消息流程 消息格式 节点选择 1.选择发送消息的节点数量 2.消息数据量 其他网址 简介 本文介绍Redis的Cluster(集群)的节点通信的流程. 通信流程 在分布式存储中需要提供维护节点元数据信息的机制, 所谓元数据是指: 节点负责哪些数据, 是否出现故障等状态信息. 常见的元数据维护方式分为: 集中式和P2P方式. Redis集群采用P2P的Gossip(流言) 协议,Gossip协议工作原理就是节点彼此不断通信交换信息, 一段时间后所有的节点都会

  • 10大HBase常见运维工具整理小结

    摘要:HBase自带许多运维工具,为用户提供管理.分析.修复和调试功能.本文将列举一些常用HBase工具,开发人员和运维人员可以参考本文内容,利用这些工具对HBase进行日常管理和运维. HBase组件介绍 HBase作为当前比较热门和广泛使用的NoSQL数据库,由于本身设计架构和流程上比较复杂,对大数据经验较少的运维人员门槛较高,本文对当前HBase上已有的工具做一些介绍以及总结. 写在前面的说明: 1) 由于HBase不同版本间的差异性较大(如HBase2.x上移走了hbck工具),本文使用

  • Centos7 安装部署Kubernetes(k8s)集群实现过程

    目录 一.系统环境 二.前言 三.Kubernetes 3.1 概述 3.2 Kubernetes 组件 3.2.1 控制平面组件 3.2.2 Node组件 四.安装部署Kubernetes集群 4.1 环境介绍 4.2 配置节点的基本环境 4.3 节点安装docker,并进行相关配置 4.4 安装kubelet,kubeadm,kubectl 4.5 kubeadm初始化 4.6 添加worker节点到k8s集群 4.7 部署CNI网络插件calico 4.8 配置kubectl命令tab键自

  • Linux环境下部署Consul集群

    目录 1.Consul概念 1.1什么是Consul? 1.2Consul的特点 1.3Consul架构 1.4Consul的应用场景包括服务发现.服务隔离与服务配置 2.Consul在linux上的集群部署 2.1前期准备 2.2集群部署 1.Consul概念 1.1什么是Consul? Consul是一种服务网格解决方案,是HashiCorp公司推出的开源组件,由Go语言开发,部署起来很容易,只需要极少的可执行程序和配置.同时Consul也是一个分布式的,高度可用的系统,它附带了一个简单的内

  • Redis集群的关闭与重启操作

    Redis集群关闭与重启 1.注意 [root@master bin]# ./redis-cli --cluster create 192.168.230.21:7001 192.168.230.21:7002 192.168.230.21:7003 192.168.230.21:8001 192.168.230.21:8002 192.168.230.21:8003 --cluster-replicas 1 -a 123456 上面的命令只能在新创健集群的时候执行一次,目的是为了建立内部各个节

随机推荐