前一段是公司的一个项目环境,由以前的单节点环境升级到到现在集群环境,并且服务器都在同一个机房,考虑部署了一套zabbix监控系统,由于第一次部署的服务器有点多,所以将安装流程写成了脚本模式,分别是server端和client端,方便安装,采用的是Python的Fabric模块写的脚本;分享一下,希望对初次批量部署监控的朋友有帮助。
脚本注意事项:
(1.虚拟主机路径,最好使用 nginx or apache 默认路径,安装成功以后在手动修改;
(2.php参数需要手动调整,未调整安装以后会有警告信息;
1.sed -i ‘s/post_max_size = 8M/post_max_size = 16M/‘ /usr/local/php/etc/php.ini
2.sed -i ‘s/max_execution_time = 30/max_execution_time = 300/‘ /usr/local/php/etc/php.ini
3.sed -i ‘s/max_input_time = 60/max_input_time = 300/‘ /usr/local/php/etc/php.ini
4.sed -i ‘s/;date.timezOne=/date.timezOne= Asia\/Shanghai/‘ /usr/local/php/etc/php.ini
(3.mysql root密码,手动改为自己的mysql密码,空密码不需要指定 -p 参数;
(4.需要指定mysql socket的完整路径和mysql 端口,如果非默认端口需要指定端口;
(5.脚本是一键安装,安装以后不需要在进行设置,即可直接登录;
(6.脚本优点:
1.只需要在zabbix server运行,就能批量安装client;
2.支持可以同时安装server和client;
3.可以选择是否只安装server or client;
(5.脚本缺点:
1.单线程安装,效率有点慢;
2.假如有20台服务器,只能一台一台进行安装;
3.使用Python多线程模块,可以解决此问题,假如有20台服务器,可以同时安装,提升效率。
代码内容 [在CentOS-5.8|CentOS-6.5测试通过]:
# -*- coding: utf-8 -*
#! /usr/bin/env python
from fabric.api import *
from fabric.colors import *
from fabric.context_managers import *
from fabric.contrib.console import confirm
#client ip
env.port=‘9577‘
env.user=‘root‘
env.hosts=[‘192.168.64.129‘,‘192.168.64.130‘,‘192.168.64.131‘]
env.password=‘root#@!~‘
#zabbix information
env.software_down_path = ‘/root/src‘
env.software_make_path = ‘/usr/local/zabbix‘
env.software_zabbix_url = ‘http://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/2.4.5/zabbix-2.4.5.tar.gz‘
#mysql information
env.version_mysql_port = ‘3306‘
env.version_mysql_user = ‘zabbix‘
env.version_mysql_pass = ‘zabbix2015‘
env.version_mysql_sock = ‘/tmp/mysql_3306.sock‘
#vhost information
env.project_html_path = ‘/usr/local/nginx/html‘
@task
@runs_once
def server():
print yellow("Start install zabbix server service ... ")
local("yum -y install gcc gcc-c++ openssl-devel net-snmp-devel curl-devel ntpdate wget")
local("useradd -M -s /sbin/nologin zabbix ; ntpdate pool.ntp.org")
local("wget -P %s %s" % (env.software_down_path,env.software_zabbix_url))
with lcd(env.software_down_path):
local("tar fx %s" % env.software_zabbix_url.split(‘/‘)[-1])
with lcd(env.software_zabbix_url.split(‘/‘)[-1][0:-7]):
local("./configure --prefix=%s --enable-server --enable-agent --with-mysql --with-net-snmp --with-libcurl"% env.software_make_path)
local("make && make install")
#使用nginx的默认路径,可以自行修改
local("cp -r frontends/php/ %s/zabbix"% env.project_html_path)
local("cp misc/init.d/fedora/core/* /etc/init.d/")
local("chmod +x /etc/init.d/zabbix_* && chkconfig --add zabbix_server")
local("chkconfig --add zabbix_agentd && chkconfig zabbix_agentd on")
local("chkconfig zabbix_server on")
#创建zabbix用户
local("mysql -uroot -pmysql2015 -P %s -e \"grant all privileges on zabbix.* to %s@localhost identified by ‘%s‘\"" %(env.version_mysql_port,env.version_mysql_user,env.version_mysql_pass))
local("mysql -uroot -pmysql2015 -P %s -e ‘create database zabbix‘"% env.version_mysql_port)
#导入zabbix数据库
local("mysql -uroot -pmysql2015 -P %s zabbix < database/mysql/schema.sql"% env.version_mysql_port)
local("mysql -uroot -pmysql2015 -P %s zabbix < database/mysql/images.sql"% env.version_mysql_port)
local("mysql -uroot -pmysql2015 -P %s zabbix < database/mysql/data.sql"% env.version_mysql_port)
local("sed -i ‘s/\/usr\/local/%s/‘ /etc/init.d/zabbix_server"% env.software_make_path.replace(‘/‘,‘\/‘))
local("sed -i ‘s/\/usr\/local/%s/‘ /etc/init.d/zabbix_agentd"% env.software_make_path.replace(‘/‘,‘\/‘))
with lcd(env.software_make_path):
#创建日志文件并赋予权限
local("touch /var/log/zabbix_server.log")
local("touch /var/log/zabbix_agentd.log")
local("chown -R zabbix:zabbix /var/log/zabbix_*")
local("sed -i ‘s/\/tmp\/zabbix_server.log/\/var\/log\/zabbix_server.log/‘ etc/zabbix_server.conf")
local("sed -i ‘s/DBUser=root/DBUser=%s/‘ etc/zabbix_server.conf"% env.version_mysql_user)
local("sed -i ‘s/# DBPassword=/DBPassword=%s/‘ etc/zabbix_server.conf"% env.version_mysql_pass)
local("sed -i ‘s/# DBSocket=\/tmp\/mysql.sock/DBSocket=%s/‘ etc/zabbix_server.conf" % env.version_mysql_sock.replace(‘/‘,‘\/‘))
local("sed -i ‘s/\/tmp\/zabbix_agentd.log/\/var\/log\/zabbix_agentd.log/‘ etc/zabbix_agentd.conf")
with lcd(env.project_html_path+‘/zabbix/conf‘):
local("cp zabbix.conf.php.example zabbix.conf.php")
local("sed -i ‘s/zabbix_password/%s/‘ zabbix.conf.php"% env.version_mysql_pass)
local("sed -i \"s/‘0‘/‘%s‘/\" zabbix.conf.php"% env.version_mysql_port)
local("service zabbix_server start && service zabbix_agentd start")
print green("zabbix server installation successful ... ")
@task
def client():
print yellow("Start install zabbix client service ... ")
run("yum -y install gcc gcc-c++ openssl-devel net-snmp-devel curl-devel ntpdate wget")
run("useradd -M -s /sbin/nologin zabbix ; ntpdate pool.ntp.org")
run("wget -P %s %s" % (env.software_down_path,env.software_zabbix_url))
with cd(env.software_down_path):
run("tar fx %s" % env.software_zabbix_url.split(‘/‘)[-1])
with cd(env.software_zabbix_url.split(‘/‘)[-1][0:-7]):
run("./configure --prefix=%s --enable-agent" % env.software_make_path)
run("make && make install")
run("cp misc/init.d/fedora/core/* /etc/init.d/")
run("chmod +x /etc/init.d/zabbix_agentd")
run("chkconfig --add zabbix_agentd && chkconfig zabbix_agentd on")
#获取server端ip [Fabric强悍指出在于随时可以执行本地命令和远程命令]
server_ip = local("ifconfig eth0|grep Bcast|awk ‘{print $2}‘|sed ‘s/addr://‘")
run("sed -i ‘s/\/usr\/local/%s/‘ /etc/init.d/zabbix_agentd"% env.software_make_path.replace(‘/‘,‘\/‘))
with cd(env.software_make_path):
#创建日志文件并赋予权限
run("touch /var/log/zabbix_agentd.log")
run("chown -R zabbix:zabbix /var/log/zabbix_agentd.log")
#日志存放在/var/log目录
run("sed -i ‘s/\/tmp\/zabbix_agentd.log/\/var\/log\/zabbix_agentd.log/‘ etc/zabbix_agentd.conf")
run("sed -i ‘s/Server=127.0.0.1/Server=%s/‘ etc/zabbix_agentd.conf"% server_ip)
run("sed -i ‘s/ServerActive=127.0.0.1/ServerActive=%s/‘ etc/zabbix_agentd.conf"% server_ip)
run("service zabbix_agentd start")
print green("zabbix client installation successful ... ")
@task
def start():
server()
client()
安装Python的fabric模块
wget -P /soft https://bootstrap.pypa.io/ez_setup.py -O - | python
easy_install fabric
运行命令 [需要安装Python的fabric模块]:
#只安装server端 [假如脚本名称叫 zabbix_install.py ]
fab -f zabbix_install.py server
#只安装client端 [假如脚本名称叫 zabbix_install.py ]
fab -f zabbix_install.py client
#server端和client端一起安装 [假如脚本名称叫 zabbix_install.py ]
fab -f zabbix_install.py start
测试效果,待补充 ...
本文出自 “ˉ、穎濤┃﹎” 博客,请务必保留此出处http://hypocritical.blog.51cto.com/3388028/1679989
使用Fabric自动化安装zabbix服务器端和客户端,附安装脚本