ansible-playbook基于role的配置一键安装zabbix客户端以及拉取自定义监控脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ansible-playbook基于role的配置一键安装zabbix客户端以及拉取自定义监控脚本相关的知识,希望对你有一定的参考价值。
在IT工作中,您可能会一遍又一遍地执行相同的任务;没有人喜欢重复的任务。通过Ansible,IT管理员可以开始自动化日常任务中的苦差事。自动化解放了管理人员,专注于通过加快应用交付时间和建立在成功文化基础之上,为业务提供更多价值的努力。最终,Ansible为团队提供了他们永远无法获得足够的一件事:时间。让聪明的人专注于聪明的事情。Ansible是一种简单的自动化语言,可以完美地描述IT应用程序基础结构。它易于学习,自我记录,并且不需要毕业级的计算机科学学位来阅读。自动化不应该比它正在取代的任务更复杂。
今天给大家介绍一个ansible-playbook基于role的配置一键安装zabbix客户端以及拉取自定义监控脚本;
先看看role的目录结构:
[[email protected]_Auto playbooks]# tree roles/zabbix/
roles/zabbix/
├── files
│ ├── check_mysql_custom.py
│ └── zabbix
├── handlers
│ └── main.yml
├── tasks
│ └── main.yml
└── templates
├── check_zombie.conf
├── FPM_parameter.conf
├── Mysql_Custom.conf
├── Mysql_repl.conf
├── Supervisor_parameter.conf
└── userparameter_mysql.conf
在files目录下存放需要拉取的文件,我们这里放了一个python文件与zabbix客户端的启动脚本(因为之前遇到一个版本问题所以写了一个基于centos7和6通用的脚本)
[[email protected]_Auto files]# ls
check_Mysql_custom.py zabbix
[[email protected]_Auto files]# cat check_Mysql_custom.py #!/usr/local/bin/python '''author = chenmingle''' '''Description:get mysql status''' import os import sys try: import MySQLdb as mysql except Exception, e: print e print "pip install MySQL-python" sys.exit(1) con = mysql.connect(user='root',passwd='',) def processlist(): cur = con.cursor() sql1 = 'show processlist' a = str(cur.execute(sql1)) print a def slave_status(): cur = con.cursor() sql2 = cur.execute('show status like "%Slave_running%";') status2 = str(cur.fetchall()) check2 = status2.split("'")[3] if check2 == 'ON': print 0 else: print 1 def show_status(type): cur = con.cursor() b = cur.execute('show status like "%s";' %(type)) for i in cur.fetchall(): cat = str(i) check = str(cat.split("'")[3]) print check def main(type): if type == 'processlist': processlist() elif type == 'slave_status': slave_status() else: show_status(type) if __name__ == '__main__': try: type = sys.argv[1] except Exception, e: print "Usage: python %s type" % sys.argv[0] sys.exit(1) main(type)
[[email protected]_Auto files]# cat zabbix #!/bin/sh #chkconfig: 2345 20 80 check_release=`cat /etc/redhat-release | awk '{print $NF}'` start(){ if [ $check_release == "(Core)" ]; then echo "start zabbix-agent" systemctl start zabbix-agent touch /tmp/zabbix-agent.pid else echo "start zabbix-agent" /etc/init.d/zabbix-agent start touch /tmp/zabbix-agent.pid fi } stop(){ if [ $check_release == "(Core)" ]; then echo "stopping zabbix-agent" systemctl stop zabbix-agent ps -ef | grep zabbix_agentd | grep -v grep | awk '{print $2}' | xargs kill -9 1 >/dev/null 2>&1 touch /tmp/zabbix-agent.pid else echo "stopping zabbix-agent" /etc/init.d/zabbix-agent stop touch /tmp/zabbix-agent.pid fi if [ $? == 0 ]; then rm -rf /tmp/zabbix-agent.pid echo "success stop zabbix-agent" else echo "check your zabbix-agent command" exit 10 fi } status(){ if [ $check_release == "(Core)" ]; then systemctl status zabbix-agent else /etc/init.d/zabbix-agent status fi } case $1 in start) start ;; stop) stop ;; reload|restart) start stop start ;; status) status ;; *) echo "Usage:$0{start|stop|reload/restart}" ;; esac
接下来介绍templates目录我们这里放的是自定义监控配置文件:
[[email protected]_Auto zabbix]# cd templates/ [[email protected]_Auto templates]# ls FPM_parameter.conf Mysql_Custom.conf [[email protected]_Auto templates]# cat Mysql_Custom.conf UserParameter=mysql.Custom[*],/usr/local/bin/python /home/python/check_Mysql_custom.py $1
然后在handlers目录下写一个触发的重启项:
[[email protected]_Auto zabbix]# cd handlers/ [[email protected]_Auto handlers]# ls main.yml [[email protected]_Auto handlers]# cat main.yml - name: restart zabbix-agent service: name: zabbix-agent state: restarted - name: restart zabbix service: name: zabbix state: restarted
最后在tasks目录下编写安装步骤:
[[email protected]_Auto zabbix]# cd tasks/ [[email protected]_Auto tasks]# cat main.yml - name: install pip for centos7 yum: name: "{{ item }}" state: present with_items: - python2-pip when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7" - name: install pip for centos6 yum: name: "{{ item }}" state: present with_items: - python-pip when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "6" - name: Install mysql Packages yum: name: "{{ item }}" state: present with_items: - gcc - python-devel - pip: name: MySQL-python - name: Set Zabbix-agent /etc/init.d/zabbix copy: src: zabbix dest: /etc/init.d/zabbix mode: 0755 notify: restart zabbix - name: Set templates template: src: "{{ project_name }}.conf" dest: /etc/zabbix/zabbix_agentd.d/{{ project_name }}.conf notify: restart zabbix - name: Copy check_Mysql_custom.py to /home/python/check_Mysql_custom.py copy: src: check_Mysql_custom.py dest: /home/python/check_Mysql_custom.py mode: 0755
解析一下:
1、when的那句作用是检测主机的操作系统版本来决定执行步骤
2、template配置的{{ project_name }}的作用是基于不同的监控项目我就可以在执行是定义想要的监控项
最后在role同级目录下编写一个yml脚本
[[email protected]_Auto playbooks]# pwd /home/python/playbooks [[email protected]_Auto playbooks]# cat zabbix_templates.yml - hosts: testserver roles: - { role: zabbix, project_name: 'Mysql_Custom' }
以上是关于ansible-playbook基于role的配置一键安装zabbix客户端以及拉取自定义监控脚本的主要内容,如果未能解决你的问题,请参考以下文章