集群运维自动化工具ansible之使用playbook安装zabbix客户端
之前介绍了关于ansible的安装与使用(包括模块与playbook使用,地址是https://www.nhooo.com/article/52154.htm),今天介绍一下如何使用playbook来部署zabbix客户端。
ansible服务端的环境为centos6.5x86_64系统
ansible客户端环境为centos6.3x86_64系统
目前我的playbook只允许centos或redhat6系列系统来安装zabbix客户端,并且客户端的版本是2.0.6.
下面是playbook的结构
14:29:30#pwd /etc/ansible/roles root@ip-10-10-10-10:/etc/ansible/roles 14:29:37#treezabbix_client_* zabbix_client_delete删除已经安装的zabbix客户端 ├──files存放文件的 ├──handlers重启的东东 ├──metagalaxy_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 12directories,13files
下面是先介绍一下安装方面zabbix_client_install的内容
1、galaxy_info的信息
14:32:15#cat/etc/ansible/roles/zabbix_client_install/meta/main.yml galaxy_info: author:DengLei description:InstallZabbixClient 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:StopExistZabbixClientServiceInRedhatClient shell:ps-ef|grepzabbix|grep-vgrep|awk'{print$2}'|xargskill-9>>/dev/null2>&1 ignore_errors:yes when:ansible_os_family=="RedHat"andansible_lsb.major_release|int==6 -name:DeleteExistZabbixClientDirInRedhatClient shell:rm-rf{{zabbix_dir}}/zabbix ignore_errors:yes when:ansible_os_family=="RedHat"andansible_lsb.major_release|int==6 -name:InstallBaseRequireSoftwareInRedhatClient yum:name={{item}}state=latest with_items: -telnet -dmidecode -tar -name:CreateZabbixUserInRedhatClient user:name={{zabbix_user}}state=presentcreatehome=noshell=/sbin/nologin when:ansible_os_family=="RedHat"andansible_lsb.major_release|int==6 -name:CopyZabbixClientSoftwareToRedhatClient copy:src=zabbix-{{zabbix_version}}.tar.gzdest=/tmp/zabbix-{{zabbix_version}}.tar.gzowner=rootgroup=root when:ansible_os_family=="RedHat"andansible_lsb.major_release|int==6 -name:UncompressionZabbixClientSoftwareToRedhatClient shell:tarzxf/tmp/zabbix-{{zabbix_version}}.tar.gz-C{{zabbix_dir}}/ when:ansible_os_family=="RedHat"andansible_lsb.major_release|int==6 -name:CopyZabbixStartScriptToRedhatClient template:src=zabbix_agentddest=/etc/init.d/zabbix_agentdowner=rootgroup=rootmode=0755 when:ansible_os_family=="RedHat"andansible_lsb.major_release|int==6 -name:CopyZabbixConfigToRedhatClient template:src=zabbix_agentd.confdest={{zabbix_dir}}/zabbix/conf/zabbix_agentd.confowner={{zabbix_user}}group={{zabbix_user}}mode=0644 when:ansible_os_family=="RedHat"andansible_lsb.major_release|int==6
此文件是复制对应的文件到客户端
3、task的install.yml信息
14:34:26#cat/etc/ansible/roles/zabbix_client_install/tasks/install.yml -name:ModifyZabbixDirPermissionInRedhatClient file:path={{zabbix_dir}}/zabbixowner={{zabbix_user}}group={{zabbix_user}}mode=0755 when:ansible_os_family=="RedHat"andansible_lsb.major_release|int==6 -name:CheckZabbixUserSudoPermissionInRedhatClient shell:grep"{{zabbix_user}}"/etc/sudoers|wc-l register:zabbix_sudoer ignore_errors:True when:ansible_os_family=="RedHat"andansible_lsb.major_release|int==6 -name:GiveSudoPermissionToZabbixUserInRedhatClient shell:echo"{{zabbix_user}}ALL=(root)NOPASSWD:/bin/netstat,/usr/bin/omreport">>/etc/sudoers when:ansible_os_family=="RedHat"andansible_lsb.major_release|int==6andzabbix_sudoer|int==0 -name:StartZabbixServiceInRedhatClient shell:/etc/init.d/zabbix_agentdstart when:ansible_os_family=="RedHat"andansible_lsb.major_release|int==6 -name:AddBootStartZabbixServiceInRedhatClient shell:chkconfig--level345zabbix_agentdon when:ansible_os_family=="RedHat"andansible_lsb.major_release|int==6
此文件主要是安装
4、tasks的delete.yml信息
14:35:08#cat/etc/ansible/roles/zabbix_client_install/tasks/delete.yml -name:DeleteZabbixcompressionSoftwareInRedhatClient shell:rm-rf/tmp/zabbix-{{zabbix_version}}.tar.gz when:ansible_os_family=="RedHat"andansible_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:-8515 #description:Zabbixclientscript. #processname:Zabbix ./etc/profile SERVICE="Zabbixagent" DAEMON={{zabbix_dir}}/zabbix/sbin/zabbix_agentd PIDFILE=/tmp/zabbix_agentd.pid CONFIG={{zabbix_dir}}/zabbix/conf/zabbix_agentd.conf zabbix_agent_status=`psaux|grepzabbix_agentd.conf|grep-vgrep|wc-l` zabbix_agent_pid=`psaux|grepzabbix_agentd|grep-vgrep|awk'NR==1{print$2}'` #Sourcefunctionlibrary. ./etc/rc.d/init.d/functions #Sourcenetworkingconfiguration. ./etc/sysconfig/network functioncheck() { if[$?-eq0];then action$"Operatingis:"/bin/true else action$"Operatingis:"/bin/false fi } case$1in 'start') if[-x${DAEMON}] then $DAEMON-c$CONFIG #Errorcheckingherewouldbegood... echo"${SERVICE}started." else echo"Can'tfindfile${DAEMON}." echo"${SERVICE}NOTstarted." fi check ;; 'stop') if[-s${PIDFILE}] then ifkill`cat${PIDFILE}`>/dev/null2>&1 then echo"${SERVICE}terminated." rm-f${PIDFILE} fi fi check ;; 'restart') /bin/bash$0stop sleep5 /bin/bash$0start ;; 'status') if[$zabbix_agent_status-ne0];then echo"ZabbixAgentdisrunning($zabbix_agent_pid)" else echo"ZabbixAgentdisnotrunning!" fi ;; *) echo"Usage:$0{start|stop|status|restart}" ;; esac exit0
这个文件是启动客户端的脚本
7、templates的zabbix_agentd.conf
15:16:36#cat/etc/ansible/roles/zabbix_client_install/templates/zabbix_agentd.conf #ThisisaconfigfilefortheZabbixagentdaemon(Unix) #TogetmoreinformationaboutZabbix,visithttp://www.zabbix.com ############GENERALPARAMETERS################# ###Option:PidFile #NameofPIDfile. # #Mandatory:no #Default: #PidFile=/tmp/zabbix_agentd.pid ###Option:LogFile #Nameoflogfile. #Ifnotset,syslogisused. # #Mandatory:no #Default: #LogFile= LogFile=/tmp/zabbix_agentd.log ###Option:LogFileSize #MaximumsizeoflogfileinMB. #0-disableautomaticlogrotation. # #Mandatory:no #Range:0-1024 #Default: #LogFileSize=1 ###Option:DebugLevel #Specifiesdebuglevel #0-nodebug #1-criticalinformation #2-errorinformation #3-warnings #4-fordebugging(produceslotsofinformation) # #Mandatory:no #Range:0-4 #Default: #DebugLevel=3 ###Option:SourceIP #SourceIPaddressforoutgoingconnections. # #Mandatory:no #Default: #SourceIP= ###Option:EnableRemoteCommands #WhetherremotecommandsfromZabbixserverareallowed. #0-notallowed #1-allowed # #Mandatory:no #Default: #EnableRemoteCommands=0 ###Option:LogRemoteCommands #Enableloggingofexecutedshellcommandsaswarnings. #0-disabled #1-enabled # #Mandatory:no #Default: #LogRemoteCommands=0 #####Passivechecksrelated ###Option:Server #ListofcommadelimitedIPaddresses(orhostnames)ofZabbixservers. #Incomingconnectionswillbeacceptedonlyfromthehostslistedhere. #Nospacesallowed. #IfIPv6supportisenabledthen'127.0.0.1','::127.0.0.1','::ffff:127.0.0.1'aretreatedequally. # #Mandatory:no #Default: #Server=zabbix-server-external.autoclouds.net Server={{zabbix_server_ip}} ###Option:ListenPort #Agentwilllistenonthisportforconnectionsfromtheserver. # #Mandatory:no #Range:1024-32767 #Default: ListenPort={{zabbix_port}} ###Option:ListenIP #ListofcommadelimitedIPaddressesthattheagentshouldlistenon. #FirstIPaddressissenttoZabbixserverifconnectingtoittoretrievelistofactivechecks. # #Mandatory:no #Default: #ListenIP=0.0.0.0 ###Option:StartAgents #Numberofpre-forkedinstancesofzabbix_agentdthatprocesspassivechecks. #Ifsetto0,disablespassivechecksandtheagentwillnotlistenonanyTCPport. # #Mandatory:no #Range:0-100 #Default: #StartAgents=3 #####Activechecksrelated ###Option:ServerActive #ListofcommadelimitedIP:port(orhostname:port)pairsofZabbixserversforactivechecks. #Ifportisnotspecified,defaultportisused. #IPv6addressesmustbeenclosedinsquarebracketsifportforthathostisspecified. #Ifportisnotspecified,squarebracketsforIPv6addressesareoptional. #Ifthisparameterisnotspecified,activechecksaredisabled. #Example:ServerActive=127.0.0.1:20051,zabbix.domain,[::1]:30051,::1,[12fc::1] # #Mandatory:no #Default: #ServerActive= ###Option:Hostname #Unique,casesensitivehostname. #Requiredforactivechecksandmustmatchhostnameasconfiguredontheserver. #ValueisacquiredfromHostnameItemifundefined. # #Mandatory:no #Default: #Hostname= Hostname={{ansible_hostname}} ###Option:HostnameItem #ItemusedforgeneratingHostnameifitisundefined. #IgnoredifHostnameisdefined. # #Mandatory:no #Default: #HostnameItem=system.hostname ###Option:RefreshActiveChecks #Howoftenlistofactivechecksisrefreshed,inseconds. # #Mandatory:no #Range:60-3600 #Default: #RefreshActiveChecks=120 ###Option:BufferSend #DonotkeepdatalongerthanNsecondsinbuffer. # #Mandatory:no #Range:1-3600 #Default: #BufferSend=5 ###Option:BufferSize #Maximumnumberofvaluesinamemorybuffer.Theagentwillsend #allcollecteddatatoZabbixServerorProxyifthebufferisfull. # #Mandatory:no #Range:2-65535 #Default: #BufferSize=100 ###Option:MaxLinesPerSecond #MaximumnumberofnewlinestheagentwillsendpersecondtoZabbixServer #orProxyprocessing'log'and'logrt'activechecks. #Theprovidedvaluewillbeoverriddenbytheparameter'maxlines', #providedin'log'or'logrt'itemkeys. # #Mandatory:no #Range:1-1000 #Default: #MaxLinesPerSecond=100 ###Option:AllowRoot #Allowtheagenttorunas'root'.Ifdisabledandtheagentisstartedby'root',theagent #willtrytoswitchtouser'zabbix'instead.Hasnoeffectifstartedunderaregularuser. #0-donotallow #1-allow # #Mandatory:no #Default: #AllowRoot=0 ############ADVANCEDPARAMETERS################# ###Option:Alias #Setsanaliasforparameter.Itcanbeusefultosubstitutelongandcomplexparameternamewithasmallerandsimplerone. # #Mandatory:no #Range: #Default: ###Option:Timeout #SpendnomorethanTimeoutsecondsonprocessing # #Mandatory:no #Range:1-30 #Default: Timeout=20 ###Option:Include #Youmayincludeindividualfilesorallfilesinadirectoryintheconfigurationfile. #InstallingZabbixwillcreateincludedirectoryin/usr/local/etc,unlessmodifiedduringthecompiletime. # #Mandatory:no #Default: #Include= #Include=/usr/local/etc/zabbix_agentd.userparams.conf #Include=/usr/local/etc/zabbix_agentd.conf.d/ #######USER-DEFINEDMONITOREDPARAMETERS####### ###Option:UnsafeUserParameters #Allowallcharacterstobepassedinargumentstouser-definedparameters. #0-donotallow #1-allow # #Mandatory:no #Range:0-1 #Default: #UnsafeUserParameters=0 ###Option:UserParameter #User-definedparametertomonitor.Therecanbeseveraluser-definedparameters. #Format:UserParameter=<key>,<shellcommand> #See'zabbix_agentd'directoryforexamples. # #Mandatory:no #Default: #UserParameter= UserParameter=memcached_stats[*],(echostats;sleep1)|telnet{{ansible_default_ipv4.address}}$12>&1|awk'/STAT$2/{print$NF}' UserParameter=mysql[*],mysql-h{{ansible_default_ipv4.address}}-P3306-uzabbix-pzabbix-e"showglobalstatus"|grep"\<$1\>"|cut-f2 UserParameter=redis_stats[*],(echoinfo;sleep1)|telnet{{ansible_default_ipv4.address}}$12>&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/mongoadmin|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"showglobalstatus"|grep"\<$2\>"|cut-f2 UserParameter=mysql_stats_slave[*],mysql-h{{ansible_default_ipv4.address}}-P$1-uzabbix-pzabbix-e"showslavestatus\G"|grep"\<$2\>"|awk'{if($NF=="Yes"){print1}else{print0}}' UserParameter=check_platform,dmidecode|grepVendor|awk-F'''{if($2=="Dell"){print1}else{print0}}' #followishardwaremonitor UserParameter=hardware_battery,omreportchassisbatteries|awk'/^Status/{if($NF=="Ok"){print1}else{print0}}' UserParameter=hardware_cpu_model,awk-vhardware_cpu_crontol=`sudoomreportchassisbiossetup|awk'/CState/{if($NF=="Enabled"){print0}else{print1}}'`-vhardware_cpu_c1=`sudoomreportchassisbiossetup|awk'/C1[-|E]/{if($NF=="Enabled"){print0}else{print1}}'`'BEGIN{if(hardware_cpu_crontol==0&&hardware_cpu_c1==0){print0}else{print1}}' UserParameter=hardware_fan_health,awk-vhardware_fan_number=`omreportchassisfans|grep-c"^Index"`-vhardware_fan=`omreportchassisfans|awk'/^Status/{if($NF=="Ok")count+=1}END{printcount}'`'BEGIN{if(hardware_fan_number==hardware_fan){print1}else{print0}}' UserParameter=hardware_memory_health,awk-vhardware_memory=`omreportchassismemory|awk'/^Health/{print$NF}'`'BEGIN{if(hardware_memory=="Ok"){print1}else{print0}}' UserParameter=hardware_nic_health,awk-vhardware_nic_number=`omreportchassisnics|grep-c"InterfaceName"`-vhardware_nic=`omreportchassisnics|awk'/^ConnectionStatus/{print$NF}'|wc-l`'BEGIN{if(hardware_nic_number==hardware_nic){print1}else{print0}}' UserParameter=hardware_cpu,omreportchassisprocessors|awk'/^Health/{if($NF=="Ok"){print1}else{print0}}' UserParameter=hardware_power_health,awk-vhardware_power_number=`omreportchassispwrsupplies|grep-c"Index"`-vhardware_power=`omreportchassispwrsupplies|awk'/^Status/{if($NF=="Ok")count+=1}END{printcount}'`'BEGIN{if(hardware_power_number==hardware_power){print1}else{print0}}' UserParameter=hardware_temp,omreportchassistemps|awk'/^Status/{if($NF=="Ok"){print1}else{print0}}'|head-n1 UserParameter=hardware_physics_health,awk-vhardware_physics_disk_number=`omreportstoragepdiskcontroller=0|grep-c"^ID"`-vhardware_physics_disk=`omreportstoragepdiskcontroller=0|awk'/^State/{if($NF=="Online")count+=1}END{printcount}'`'BEGIN{if(hardware_physics_disk_number==hardware_physics_disk){print1}else{print0}}' UserParameter=hardware_virtual_health,awk-vhardware_virtual_disk_number=`omreportstoragevdiskcontroller=0|grep-c"^ID"`-vhardware_virtual_disk=`omreportstoragevdiskcontroller=0|awk'/^State/{if($NF=="Ready")count+=1}END{printcount}'`'BEGIN{if(hardware_virtual_disk_number==hardware_virtual_disk){print1}else{print0}}' 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.10zabbix_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客户端
我的测试客户端环境是centos6.3,ip是192.168.240.17,使用key登陆
15:22:01#cd/etc/ansible/ root@ip-10-10-10-10:/etc/ansible 15:22:04#timeansible-playbookzabbix_client_install.yml--extra-vars"host=192.168.240.17user=root"--private-key=/root/test.pem PLAY[192.168.240.17]********************************************************* GATHERINGFACTS*************************************************************** ok:[192.168.240.17] TASK:[zabbix_client_install|StopExistZabbixClientServiceInRedhatClient]*** failed:[192.168.240.17]=>{"changed":true,"cmd":"ps-ef|grepzabbix|grep-vgrep|awk'{print$2}'|xargskill-9>>/dev/null2>&1","delta":"0:00:00.018213","end":"2014-07-1007:22:34.793910","item":"","rc":123,"start":"2014-07-1007:22:34.775697"} ...ignoring TASK:[zabbix_client_install|DeleteExistZabbixClientDirInRedhatClient]*** changed:[192.168.240.17] TASK:[zabbix_client_install|InstallBaseRequireSoftwareInRedhatClient]*** ok:[192.168.240.17]=>(item=telnet,dmidecode,tar) TASK:[zabbix_client_install|CreateZabbixUserInRedhatClient]*********** changed:[192.168.240.17] TASK:[zabbix_client_install|CopyZabbixClientSoftwareToRedhatClient]*** changed:[192.168.240.17] TASK:[zabbix_client_install|UncompressionZabbixClientSoftwareToRedhatClient]*** changed:[192.168.240.17] TASK:[zabbix_client_install|CopyZabbixStartScriptToRedhatClient]***** changed:[192.168.240.17] TASK:[zabbix_client_install|CopyZabbixConfigToRedhatClient]*********** changed:[192.168.240.17] TASK:[zabbix_client_install|ModifyZabbixDirPermissionInRedhatClient]*** ok:[192.168.240.17] TASK:[zabbix_client_install|CheckZabbixUserSudoPermissionInRedhatClient]*** changed:[192.168.240.17] TASK:[zabbix_client_install|GiveSudoPermissionToZabbixUserInRedhatClient]*** changed:[192.168.240.17] TASK:[zabbix_client_install|StartZabbixServiceInRedhatClient]********* changed:[192.168.240.17] TASK:[zabbix_client_install|AddBootStartZabbixServiceInRedhatClient]*** changed:[192.168.240.17] TASK:[zabbix_client_install|DeleteZabbixcompressionSoftwareInRedhatClient]*** changed:[192.168.240.17] PLAYRECAP******************************************************************** 192.168.240.17:ok=15changed=12unreachable=0failed=0 real0m39.888s user0m1.547s sys0m0.197s
可以看到39秒就安装完成,主要花费时间比较长的地方是fact收集、yum安装、文件传输。
11、测试安装情况
[root@ip-10-10-240-21tmp]#ifconfig eth0Linkencap:EthernetHWaddrFA:16:3E:34:62:D0 inetaddr:10.10.240.21Bcast:10.10.240.255Mask:255.255.255.0 inet6addr:fe80::f816:3eff:fe34:62d0/64Scope:Link UPBROADCASTRUNNINGMULTICASTMTU:1500Metric:1 RXpackets:542391errors:0dropped:0overruns:0frame:0 TXpackets:77391errors:0dropped:0overruns:0carrier:0 collisions:0txqueuelen:1000 RXbytes:142341119(135.7MiB)TXbytes:6451154(6.1MiB) loLinkencap:LocalLoopback inetaddr:127.0.0.1Mask:255.0.0.0 inet6addr:::1/128Scope:Host UPLOOPBACKRUNNINGMTU:16436Metric:1 RXpackets:10errors:0dropped:0overruns:0frame:0 TXpackets:10errors:0dropped:0overruns:0carrier:0 collisions:0txqueuelen:0 RXbytes:700(700.0b)TXbytes:700(700.0b) [root@ip-10-10-240-21tmp]#ps-ef|grepzabbix zabbix269911007:22?00:00:00/usr/local/zabbix/sbin/zabbix_agentd-c/usr/local/zabbix/conf/zabbix_agentd.conf zabbix2699326991007:22?00:00:00/usr/local/zabbix/sbin/zabbix_agentd-c/usr/local/zabbix/conf/zabbix_agentd.conf zabbix2699426991007:22?00:00:00/usr/local/zabbix/sbin/zabbix_agentd-c/usr/local/zabbix/conf/zabbix_agentd.conf zabbix2699526991007:22?00:00:00/usr/local/zabbix/sbin/zabbix_agentd-c/usr/local/zabbix/conf/zabbix_agentd.conf zabbix2699626991007:22?00:00:00/usr/local/zabbix/sbin/zabbix_agentd-c/usr/local/zabbix/conf/zabbix_agentd.conf root2710213773007:24pts/000:00:00grepzabbix [root@ip-10-10-240-21tmp]#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[*],(echostats;sleep1)|telnet10.10.240.21$12>&1|awk'/STAT$2/{print$NF}' UserParameter=mysql[*],mysql-h10.10.240.21-P3306-uzabbix-pzabbix-e"showglobalstatus"|grep"\<$1\>"|cut-f2 UserParameter=redis_stats[*],(echoinfo;sleep1)|telnet10.10.240.21$12>&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/mongoadmin|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-h10.10.240.21-P$1-uzabbix-pzabbix-e"showglobalstatus"|grep"\<$2\>"|cut-f2 UserParameter=mysql_stats_slave[*],mysql-h10.10.240.21-P$1-uzabbix-pzabbix-e"showslavestatus\G"|grep"\<$2\>"|awk'{if($NF=="Yes"){print1}else{print0}}' UserParameter=check_platform,dmidecode|grepVendor|awk-F'''{if($2=="Dell"){print1}else{print0}}' UserParameter=hardware_battery,omreportchassisbatteries|awk'/^Status/{if($NF=="Ok"){print1}else{print0}}' UserParameter=hardware_cpu_model,awk-vhardware_cpu_crontol=`sudoomreportchassisbiossetup|awk'/CState/{if($NF=="Enabled"){print0}else{print1}}'`-vhardware_cpu_c1=`sudoomreportchassisbiossetup|awk'/C1[-|E]/{if($NF=="Enabled"){print0}else{print1}}'`'BEGIN{if(hardware_cpu_crontol==0&&hardware_cpu_c1==0){print0}else{print1}}' UserParameter=hardware_fan_health,awk-vhardware_fan_number=`omreportchassisfans|grep-c"^Index"`-vhardware_fan=`omreportchassisfans|awk'/^Status/{if($NF=="Ok")count+=1}END{printcount}'`'BEGIN{if(hardware_fan_number==hardware_fan){print1}else{print0}}' UserParameter=hardware_memory_health,awk-vhardware_memory=`omreportchassismemory|awk'/^Health/{print$NF}'`'BEGIN{if(hardware_memory=="Ok"){print1}else{print0}}' UserParameter=hardware_nic_health,awk-vhardware_nic_number=`omreportchassisnics|grep-c"InterfaceName"`-vhardware_nic=`omreportchassisnics|awk'/^ConnectionStatus/{print$NF}'|wc-l`'BEGIN{if(hardware_nic_number==hardware_nic){print1}else{print0}}' UserParameter=hardware_cpu,omreportchassisprocessors|awk'/^Health/{if($NF=="Ok"){print1}else{print0}}' UserParameter=hardware_power_health,awk-vhardware_power_number=`omreportchassispwrsupplies|grep-c"Index"`-vhardware_power=`omreportchassispwrsupplies|awk'/^Status/{if($NF=="Ok")count+=1}END{printcount}'`'BEGIN{if(hardware_power_number==hardware_power){print1}else{print0}}' UserParameter=hardware_temp,omreportchassistemps|awk'/^Status/{if($NF=="Ok"){print1}else{print0}}'|head-n1 UserParameter=hardware_physics_health,awk-vhardware_physics_disk_number=`omreportstoragepdiskcontroller=0|grep-c"^ID"`-vhardware_physics_disk=`omreportstoragepdiskcontroller=0|awk'/^State/{if($NF=="Online")count+=1}END{printcount}'`'BEGIN{if(hardware_physics_disk_number==hardware_physics_disk){print1}else{print0}}' UserParameter=hardware_virtual_health,awk-vhardware_virtual_disk_number=`omreportstoragevdiskcontroller=0|grep-c"^ID"`-vhardware_virtual_disk=`omreportstoragevdiskcontroller=0|awk'/^State/{if($NF=="Ready")count+=1}END{printcount}'`'BEGIN{if(hardware_virtual_disk_number==hardware_virtual_disk){print1}else{print0}}' 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-21tmp]#ll/tmp/ total12 -rw-------1rootroot197Jul909:35yum_save_tx-2014-07-09-09-35ibcBiO.yumtx -rw-rw-r--1zabbixzabbix320Jul1007:22zabbix_agentd.log -rw-rw-r--1zabbixzabbix5Jul1007:22zabbix_agentd.pid [root@ip-10-10-240-21tmp]#chkconfig--list|grepzabbix_agentd zabbix_agentd0:off1:off2:off3:on4:on5:on6:off [root@ip-10-10-240-21tmp]#grepzabbix/etc/sudoers zabbixALL=(root)NOPASSWD:/bin/netstat,/usr/bin/omreport [root@ip-10-10-240-21tmp]#ll/etc/init.d/zabbix_agentd -rwxr-xr-x1rootroot1444Jul1007:22/etc/init.d/zabbix_agentd
可以看到安装后的客户端,完全是按照我的要求来做的。
12、删除已经安装的客户端
15:22:54#timeansible-playbookzabbix_client_delete.yml--extra-vars"host=192.168.240.17user=root"--private-key=/root/test.pem PLAY[192.168.240.17]********************************************************* GATHERINGFACTS*************************************************************** ok:[192.168.240.17] TASK:[zabbix_client_delete|StopZabbixServiceInRedHatClient]*********** changed:[192.168.240.17] TASK:[zabbix_client_delete|DeleteBootStartZabbixServiceInRedhatClient]*** changed:[192.168.240.17] TASK:[zabbix_client_delete|DeleteZabbixUserInRedhatClient]************ changed:[192.168.240.17] TASK:[zabbix_client_delete|DeleteZabbixDirInRedhatClient]************* changed:[192.168.240.17] TASK:[zabbix_client_delete|DeleteZabbixStartScriptInRedhatClient]**** changed:[192.168.240.17] TASK:[zabbix_client_delete|CheckZabbixUserSudoPermissionInRedhatClient]*** changed:[192.168.240.17] TASK:[zabbix_client_delete|DeleteSudoPermissionToZabbixUserInRedhatClient]*** changed:[192.168.240.17] PLAYRECAP******************************************************************** 192.168.240.17:ok=8changed=7unreachable=0failed=0 real0m25.497s user0m0.847s sys0m0.159s
13、测试删除情况
[root@ip-10-10-240-21tmp]#ll/tmp/ total4 -rw-------1rootroot197Jul909:35yum_save_tx-2014-07-09-09-35ibcBiO.yumtx [root@ip-10-10-240-21tmp]#ps-ef|grepzabbix root2766513773007:27pts/000:00:00grepzabbix [root@ip-10-10-240-21tmp]#ll/usr/local/ total40 drwxr-xr-x.2rootroot4096Sep232011bin drwxr-xr-x.2rootroot4096Sep232011etc drwxr-xr-x.2rootroot4096Sep232011games drwxr-xr-x.2rootroot4096Sep232011include drwxr-xr-x.2rootroot4096Sep232011lib drwxr-xr-x.2rootroot4096Sep232011lib64 drwxr-xr-x.2rootroot4096Sep232011libexec drwxr-xr-x.2rootroot4096Sep232011sbin drwxr-xr-x.5rootroot4096May122013share drwxr-xr-x.3rootroot4096May132013src [root@ip-10-10-240-21tmp]#grepzabbix/etc/sudoers [root@ip-10-10-240-21tmp]#ll/etc/init.d/zabbix_agentd ls:cannotaccess/etc/init.d/zabbix_agentd:Nosuchfileordirectory [root@ip-10-10-240-21tmp]#chkconfig--list|grepzabbix_agentd [root@ip-10-10-240-21tmp]#
可以看到已经完全删除。
如果大家想使用我的例子,可以从附件里下载,然后放到/etc/ansible目录里,下面是压缩包里的内容
-rw-r--r--root/root1082014-07-1015:20zabbix_client_install.yml -rw-r--r--root/root1212014-07-0918:09zabbix_client_delete.yml drwxr-xr-xroot/root02014-07-0116:38roles/zabbix_client_install/ drwxr-xr-xroot/root02014-07-0814:30roles/zabbix_client_install/meta/ -rw-r--r--root/root2072014-07-0814:30roles/zabbix_client_install/meta/main.yml drwxr-xr-xroot/root02014-07-1014:07roles/zabbix_client_install/tasks/ -rw-r--r--root/root1992014-07-1014:02roles/zabbix_client_install/tasks/delete.yml -rw-r--r--root/root652014-07-1014:02roles/zabbix_client_install/tasks/main.yml -rw-r--r--root/root17892014-07-1014:02roles/zabbix_client_install/tasks/copy.yml -rw-r--r--root/root11102014-07-1014:07roles/zabbix_client_install/tasks/install.yml drwxr-xr-xroot/root02014-06-1913:30roles/zabbix_client_install/handlers/ drwxr-xr-xroot/root02014-07-0917:54roles/zabbix_client_install/vars/ -rw-r--r--root/root1152014-07-0917:54roles/zabbix_client_install/vars/main.yml drwxr-xr-xroot/root02014-07-0917:53roles/zabbix_client_install/templates/ -rw-r--r--zabbix/zabbix104652014-07-0917:53roles/zabbix_client_install/templates/zabbix_agentd.conf -rwxr-xr-xroot/root14562014-07-0815:20roles/zabbix_client_install/templates/zabbix_agentd drwxr-xr-xroot/root02014-07-0917:13roles/zabbix_client_install/files/ -rw-r--r--root/root2922932014-07-0917:13roles/zabbix_client_install/files/zabbix-2.0.6.tar.gz drwxr-xr-xroot/root02014-06-2314:03roles/zabbix_client_delete/ drwxr-xr-xroot/root02014-07-0918:08roles/zabbix_client_delete/meta/ -rw-r--r--root/root2052014-07-0918:08roles/zabbix_client_delete/meta/main.yml drwxr-xr-xroot/root02014-07-1014:28roles/zabbix_client_delete/tasks/ -rw-r--r--root/root15182014-07-1014:08roles/zabbix_client_delete/tasks/delete.yml -rw-r--r--root/root222014-07-1014:08roles/zabbix_client_delete/tasks/main.yml drwxr-xr-xroot/root02014-06-2414:14roles/zabbix_client_delete/handlers/ drwxr-xr-xroot/root02014-07-0313:16roles/zabbix_client_delete/vars/ -rw-r--r--root/root1152014-07-0917:55roles/zabbix_client_delete/vars/main.yml drwxr-xr-xroot/root02014-07-0918:08roles/zabbix_client_delete/templates/ drwxr-xr-xroot/root02014-06-2413:53roles/zabbix_client_delete/files/
后续我会继续介绍使用playbook安装其他软件的例子。