一键安装 zabbix 3.0 版本 脚本

原文地址: http://blog.csdn.net/u012449196/article/details/53859068

本文修改了原文中的部分错误,此脚本适用于zabbix 2.0 或 3.0 版本,以版本 zabbix-3.4.4.tar.gz 源码包示例

-------------------------------------------------------------------------------------------------------------------------------------

注明: 
Centos 6.5 
zabbix-server:192.168.1.1 
zabbix-agent :192.168.1.2

zabbix 下载地址:

https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/3.4.4/zabbix-3.4.4.tar.gz

使用方法:

1、将zabbix-3.4.4.tar.gz分别放到192.168.1.1和192.168.1.2的/opt目录 
2、将zabbix_install.sh、zabbix_install.conf文件放入/opt目录,并将zabbix_install.sh给予执行权限

3、./zabbix_install.sh 选择要安装的项目

---------------------------------------------------------------------------------------------------------------------------------------

可能遇到的问题:

脚本运行最大可能出现的问题在编译安装zabbix时候无法完成编译过程。即无法生成make文件。

多为依赖包安装失败,编译缺乏必要依赖的组件导致,可以先运行脚本中的yum安装组件部分命令,然后再运行脚本

代码位置在 PHP安装部分

--------------------------------------------------------------------------------------------------------

 zabbix_install.sh 代码如下

--------------------------------------------------------------------------------------------------------

#!/bin/bash
#coding:utf-8
dirpath=$(cd `dirname $0`; pwd)
. $dirpath/zabbix_install.conf

export LANG=zh_CN.UTF-8
[ `id -u` -ne 0 ] && echo "请使用root权限安装" && exit 1

##############################################################

function server_install(){
echo -e "\n---------------------------------------------------"
cat <<EOF
安装Zabbix环境需要依赖如下:
os: CentOS release 6.7 (Final) 6.x版本应该都能正常安装
gcc gcc-c++
autoconf httpd
php mysql
mysql-server php-mysql
httpd-manual mod_ssl mod_perl
mod_auth_mysql php-gd php-xml
php-mbstring php-ldap
php-pear php-xmlrpc
php-bcmath mysql-connector-odbc
mysql-devel libdbi-dbd-mysql
net-snmp-develcurl-devel unixODBC-devel
OpenIPMI-devel java-devel
EOF
echo -e "---------------------------------------------------\n"
echo "※你选择的是安装服务端,是否开始安装?※"
while true;do
select var in "yes" "no";do
break
done
if [ "$var"x = "yes"x -o "$var"x = "no"x ];then
if [ "$var"x = "yes"x ];then
echo "你选择的是$var,开始安装......"
sleep 1
break
else
echo "你选择的是$var,退出安装......"
sleep 1
exit 1
fi
else
echo "请输入正确的数字序号!"
fi
done
##############################################################
echo "rpm 安装php5.6"
wget http://dl.iuscommunity.org/pub/ius/stable/CentOS/6/x86_64/epel-release-6-5.noarch.rpm
rpm -ivh epel-release-6-5.noarch.rpm
yum repolist

rpm -Uvh https://centos6.iuscommunity.org/ius-release.rpm
echo "请等待10s,10s后将开始安装编译所需组件"
#yum clean all
#yum -y install gcc gcc-c++ autoconf mysql-devel httpd httpd-manual mod_ssl mod_perl mod_auth_mysql libdbi-dbd-mysql net-snmp-devel curl-devel unixODBC-devel OpenIPMI-devel java-devel openldap openldap-devel SSH libssh2-devel libxml2 libxml2-devel libxml-devel libcurl-devel libevent-devel pcre-devel
yum -y install php56u
echo " 查看下PHP装好了没"
sleep 10
yum -y install php56u-mysql php56u-gd php56u-xml php56u-mbstring php56u-ldap php56u-pear php56u-xmlrpc php56u-bcmath
echo " 查看下PHP组件装好了没"
sleep 10
yum -y install gcc gcc-c++ autoconf mysql-devel
echo " 查看gcc mysql-devel"
sleep 10
yum -y install httpd httpd-manual mod_ssl mod_perl mod_auth_mysql libdbi-dbd-mysql net-snmp-devel
echo " 查看等待时间"
sleep 10
yum -y install curl-devel unixODBC-devel OpenIPMI-devel java-devel openldap openldap-devel
echo " 查看等待时间"
sleep 10
yum -y install SSH libssh2-devel libxml2 libxml2-devel libxml-devel libcurl-devel libevent-devel pcre-devel

#yum -y install gcc gcc-c++ autoconf mysql-devel httpd httpd-manual mod_ssl mod_perl mod_auth_mysql libdbi-dbd-mysql net-snmp-devel curl-devel unixODBC-devel OpenIPMI-devel java-devel openldap openldap-devel SSH libssh2-devel libxml2 libxml2-devel libxml-devel libcurl-devel libevent-devel pcre-devel
#yum -y install gcc gcc-c++ autoconf mysql-devel httpd httpd-manual mod_ssl mod_perl mod_auth_mysql php56u php56u-mysql php56u-gd php56u-xml php56u-mbstring php56u-ldap php56u-pear php56u-xmlrpc php56u-bcmath libdbi-dbd-mysql net-snmp-devel curl-devel unixODBC-devel OpenIPMI-devel java-devel openldap openldap-devel SSH libssh2-devel libxml2 libxml2-devel libxml-devel libcurl-devel libevent-devel pcre-devel
##############################################################

[ ! -f /etc/php.ini ] && echo "php.ini不存在,请检查php是否成功安装" && exit 3
echo "依赖包安装完成,开始配置php.ini文件......"
sleep 1
sed -i "s/;date.timezone =/date.timezone = Asia\/Shanghai/g" /etc/php.ini &&
sed -i "s#max_execution_time = 30#max_execution_time = 300#g" /etc/php.ini &&
sed -i "s#post_max_size = 8M#post_max_size = 32M#g" /etc/php.ini &&
sed -i "s#max_input_time = 60#max_input_time = 300#g" /etc/php.ini &&
sed -i "s#memory_limit = 128M#memory_limit = 128M#g" /etc/php.ini &&
echo "always_populate_raw_post_data = -1" >>/etc/php.ini &&
#sed 's/;always_populate_raw_post_data = -1/always_populate_raw_post_data = -1/g' /etc/php.ini &&
# sed -i "/;mbstring.func_overload = 0/ambstring.func_overload = 2\n" /etc/php.ini
##############################################################

defaultip=`/sbin/ifconfig |grep -v 'inet6'|grep -E '([0-9]{1,3}\.){3}[0-9]{1,3}' -o|grep -vE '^(127|255)|255$'|head -1`
ip=${HOSTNAME:-$defaultip}
echo "正在创建zabbix用户......"
groupadd zabbix -g 201
useradd -g zabbix -u 201 -m zabbix
echo "解压zabbix压缩包......"
tar -zxvf $ZABBIX_TAR
cd $dirpath/$ZABBIX_TAR_DIR
echo "开始编译安装zabbix......"
defaultpath=/usr/local/$ZABBIX_TAR_DIR
PATH_ZABBIX=${ZABBIX_DIR:-$defaultpath}
./configure --prefix=$PATH_ZABBIX --sysconfdir=/etc/zabbix --enable-server --enable-proxy --enable-agent --enable-ipv6 --with-mysql=/usr/bin/mysql_config --with-net-snmp --with-libxml2 --with-libcurl --with-openipmi --with-unixodbc --with-ldap --with-ssh2 --enable-java
make && make install
if [ $? -ne 0 ];then
echo "编译安装失败,请检查错误信息提示......"
exit 1
fi
##############################################################

mkdir -p /var/log/zabbix &&
chown -R zabbix.zabbix /var/log/zabbix &&
cp ./misc/init.d/fedora/core/zabbix_* /etc/init.d/ &&
chmod 755 /etc/init.d/zabbix_* &&
sed -i "s#BASEDIR=/usr/local#BASEDIR=$PATH_ZABBIX#g" /etc/init.d/zabbix_server &&
sed -i "s#BASEDIR=/usr/local#BASEDIR=$PATH_ZABBIX#g" /etc/init.d/zabbix_agentd
if [ $? -ne 0 ]
then
echo "zabbix启动文件配置出错,退出安装......"
exit 1
else
echo -e "\n---------------------------------------------------"
fi
##############################################################

echo "zabbix需要mysql数据库支持,远程或者本地都可以,如果没有,程序将为您mysql安装,如果有,您需要在web配置界面填写数据库信息"
echo "请选择对应的序号开始安装/配置"
while true;do
select var in "本地:未安装mysql数据库" "本地:已安装mysql数据库" "远程:使用远程mysql数据库";do
break
done
if [ "$var"x = "本地:未安装mysql数据库"x -o "$var"x = "本地:已安装mysql数据库"x -o "$var"x = "远程:使用远程mysql数据库"x ];then
if [ "$var"x = "本地:未安装mysql数据库"x ];then
if [ ! -n $MYSQL_PASSWD -o ! -n $ZABBIX_PASSWD ];then
echo "你未设置mysql的root用户或zabbix用户密码,请在配置文件里设置密码"
exit 2
fi
yum install -y mysql-server mysql-connector-odbc
echo "开启 mysqld 服务......"
chkconfig mysqld on && service mysqld start
if [ $? -ne 0 ];then
echo "开启 mysqld 服务失败,请检查mysql数据库是否成功安装......"
exit 1
fi
mysqladmin -uroot password $MYSQL_PASSWD
echo "mysql数据库root密码设置成功!"
mysql -u$MYSQL_USERNAME -p$MYSQL_PASSWD <<EOF
create database zabbix character set utf8;
grant all privileges on zabbix.* to zabbix@localhost identified by '$ZABBIX_PASSWD';
flush privileges;
EOF
if [ $? -ne 0 ];then
echo "创建zabbix用户失败,请检查是否正确配置!"
exit 1
fi
echo "导入zabbix数据到mysql数据库...."
mysql -uzabbix -p$ZABBIX_PASSWD zabbix <./database/mysql/schema.sql &&
mysql -uzabbix -p$ZABBIX_PASSWD zabbix <./database/mysql/images.sql &&
mysql -uzabbix -p$ZABBIX_PASSWD zabbix <./database/mysql/data.sql
if [ $? -ne 0 ];then
echo "导入zabbix数据到mysql数据库错误,退出安装......"
exit 1
fi
break
elif [ "$var"x = "本地:已安装mysql数据库"x ];then
if [ ! -n $MYSQL_PASSWD -o ! -n $ZABBIX_PASSWD ];then
echo "你未设置mysql的root用户或zabbix用户密码,请在配置文件里设置密码"
exit 2
fi
mysql -u$MYSQL_USERNAME -p$MYSQL_PASSWD -P$MYSQL_PORT <<EOF
create database zabbix character set utf8;
grant all privileges on zabbix.* to zabbix@localhost identified by '$ZABBIX_PASSWD';
flush privileges;
EOF
if [ $? -ne 0 ];then
echo "登录mysql失败,请检查用户名密码和服务器端口是否正确。"
exit 1
else
echo "导入zabbix数据到mysql数据库...."
mysql -uzabbix -p$ZABBIX_PASSWD -P $MYSQL_PORT zabbix <./database/mysql/schema.sql &&
mysql -uzabbix -p$ZABBIX_PASSWD -P $MYSQL_PORT zabbix <./database/mysql/images.sql &&
mysql -uzabbix -p$ZABBIX_PASSWD -P $MYSQL_PORT zabbix <./database/mysql/data.sql
if [ $? -ne 0 ];then
echo "导入zabbix数据到mysql数据库错误,退出安装......"
exit 1
fi
fi
break
else
echo "正在配置远程mysql数据库......"1
if [ ! -n $MYSQL_PASSWD -o ! -n $ZABBIX_PASSWD ];then
echo "你未设置mysql的root用户或zabbix用户密码,请在配置文件里设置密码"
exit 2
fi
sed -i "/# DBHost=/aDBHost=$REMOTE_HOSTNAME\n" /etc/zabbix/zabbix_server.conf
mysql -h $REMOTE_HOSTNAME -u$MYSQL_USERNAME -p$MYSQL_PASSWD -P$MYSQL_PORT <<EOF
create database zabbix character set utf8;
grant all privileges on zabbix.* to zabbix@$ip identified by '$ZABBIX_PASSWD';
grant all privileges on zabbix.* to zabbix@localhost identified by '$ZABBIX_PASSWD';
flush privileges;
EOF
if [ $? -ne 0 ];then
echo "登录mysql失败,请检查用户名密码和服务器端口是否正确,或者mysql服务器是否开通本机访问权限"
exit 1
fi
echo "导入zabbix数据到mysql数据库...."
mysql -h $REMOTE_HOSTNAME -uzabbix -p$ZABBIX_PASSWD -P$MYSQL_PORT zabbix <./database/mysql/schema.sql &&
mysql -h $REMOTE_HOSTNAME -uzabbix -p$ZABBIX_PASSWD -P$MYSQL_PORT zabbix <./database/mysql/images.sql &&
mysql -h $REMOTE_HOSTNAME -uzabbix -p$ZABBIX_PASSWD -P$MYSQL_PORT zabbix <./database/mysql/data.sql
if [ $? -ne 0 ];then
echo "导入zabbix数据到mysql数据库错误,退出安装......"
exit 1
fi
break
fi
else
echo "请输入正确的数字序号!"
fi
done

##############################################################

echo "配置 zabbix_server.conf 服务端文件"
sed -i "s/DBUser\=root/DBUser\=zabbix/g" /etc/zabbix/zabbix_server.conf &&
sed -i "/# DBPassword=/aDBPassword=$ZABBIX_PASSWD\n" /etc/zabbix/zabbix_server.conf &&
sed -i "s#tmp/zabbix_server.log#var/log/zabbix/zabbix_server.log#g" /etc/zabbix/zabbix_server.conf

##############################################################

echo "配置 zabbix_agentd.conf 文件"
sed -i "s/Server\=127.0.0.1/Server\=127.0.0.1,$ip/g" /etc/zabbix/zabbix_agentd.conf &&
sed -i "s/ServerActive\=127.0.0.1/ServerActive\=$ip:10051/g" /etc/zabbix/zabbix_agentd.conf &&
sed -i "s#tmp/zabbix_agentd.log#var/log/zabbix/zabbix_agentd.log#g" /etc/zabbix/zabbix_agentd.conf &&
sed -i "#UnsafeUserParameters=0#aUnsafeUserParameters=1\n" /etc/zabbix/zabbix_agentd.conf
##############################################################

echo "拷贝网页文件和中文字体到 apache 目录"
[ ! -d /var/www/html/ ] && echo "目录不存在,请检查httpd是否成功安装" && exit 3
cp -r ./frontends/php/ /var/www/html/zabbix &&
\cp -f ../DejaVuSans.ttf /var/www/html/zabbix/fonts/DejaVuSans.ttf &&
##############################################################

echo "正在创建zabbix.conf.php文件"
if [ "$var"x = "远程:使用远程mysql数据库"x ];then
PHP_SERVER=$REMOTE_HOSTNAME
else
PHP_SERVER=localhost
fi
echo "<?php
// Zabbix GUI configuration file.
global \$DB;

\$DB['TYPE'] = 'MYSQL';
\$DB['SERVER'] = '$PHP_SERVER';
\$DB['PORT'] = '$MYSQL_PORT';
\$DB['DATABASE'] = 'zabbix';
\$DB['USER'] = 'zabbix';
\$DB['PASSWORD'] = '$ZABBIX_PASSWD';

// Schema name. Used for IBM DB2 and PostgreSQL.
\$DB['SCHEMA'] = '';

\$ZBX_SERVER = '$ip';
\$ZBX_SERVER_PORT = '10051';
\$ZBX_SERVER_NAME = '$ip';

\$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;
?>" > /var/www/html/zabbix/conf/zabbix.conf.php
chown -R apache.apache /var/www/html/zabbix
##############################################################

echo "开启 httpd 服务......"
chkconfig httpd on && service httpd start
if [ $? -ne 0 ];then
echo "开启 httpd 服务失败,请检查httpd是否成功安装......"
exit 1
fi
##############################################################

echo "设置 zabbix 开机启动"
chkconfig zabbix_server on && chkconfig zabbix_agentd on &&
service zabbix_server start && service zabbix_agentd start
if [ $? -ne 0 ];then
echo "zabbix启动失败,请检查配置文件是否都正确......"
exit 1
fi
##############################################################

echo -n "正在关闭SELinux……"
setenforce 0 > /dev/null 2>&1
sed -i '/^SELINUX=/s/=.*/=disabled/' /etc/selinux/config
if [ $? -eq 0 ];then
echo -n "SELinux初始化完毕!"
fi
##############################################################

while true;do
echo "是否需要开启zabbix需要使用的(80,10050,10051,3306)端口"
select var_port in "我需要开启端口" "我需要关闭防火墙";do
break
done
if [ "$var_port"x = "我需要开启端口"x -o "$var_port"x = "我需要关闭防火墙"x ];then
if [ "$var_port"x = "我需要开启端口"x ];then
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 10050 -j ACCEPT
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 10051 -j ACCEPT
service iptables save
break
else
service iptables stop
break
fi
else
echo "请输入正确的数字序号!"
fi
done

echo -e "\n-----------------------------------------------------------"
echo "恭喜你,zabbix已全部安装完成,你可以登录web界面完成最后的设置"
echo -e "-----------------------------------------------------------\n"
echo "Zabbix安装路径:$PATH_ZABBIX"
echo "Zabbix日志文件路径:/var/log/zabbix"
echo "Zabbix配置文件路径:/etc/zabbix"
echo "mysql数据库$MYSQL_USERNAME密码:$MYSQL_PASSWD"
echo "mysql数据库zabbix密码:$ZABBIX_PASSWD"
echo "web端访问地址:http://$ip/zabbix"
echo "web端登录账号:admin 密码:zabbix"
echo -e "\n-----------------------------------------------------------\n"
}

##############################################################

##############################################################

function client_install(){

echo -e "---------------------------------------------------\n"
echo "※你选择的是安装客户端,是否开始安装?※"
while true;do
select var in "yes" "no";do
break
done
if [ "$var"x = "yes"x -o "$var"x = "no"x ];then
if [ "$var"x = "yes"x ];then
echo "你选择的是$var,开始安装......"
sleep 1
break
else
echo "你选择的是$var,退出安装......"
sleep 1
exit 1
fi
else
echo "请输入正确的数字序号!"
fi
done
##############################################################

[ ! $HOSTNAME ] && echo "检测到你未填写server端IP,请正确填写后继续" && exit 2

yum -y install gcc gcc-c++
if [ $? -ne 0 ];then
echo "依赖包安装失败,更新yum源......"
/bin/cp ./*.repo /etc/yum.repos.d/
yum clean all && yum makecache
echo "yum源更新完成,重新安装依赖包......"
yum -y install gcc gcc-c++
if [ $? -ne 0 ];then
echo "依赖包安装又失败,请检查网络是否正常......"
exit 1
fi
fi
##############################################################

echo "正在编译安装zabbix客户端......"
groupadd zabbix -g 201
useradd -g zabbix -u 201 -m zabbix
echo "解压zabbix压缩包......"
tar -zxvf $ZABBIX_TAR
cd $dirpath/$ZABBIX_TAR_DIR
echo "开始编译安装zabbix......"
defaultpath=/usr/local/$ZABBIX_TAR_DIR
PATH_ZABBIX=${ZABBIX_DIR:-$defaultpath}
./configure --prefix=$PATH_ZABBIX --sysconfdir=/etc/zabbix --enable-agent && make && make install
if [ $? -ne 0 ];then
echo "编译安装失败,请检查错误信息提示......"
exit 1
fi
##############################################################

mkdir -p /var/log/zabbix &&
chown zabbix.zabbix /var/log/zabbix &&
cp misc/init.d/fedora/core/zabbix_agentd /etc/init.d/ &&
chmod 755 /etc/init.d/zabbix_agentd &&
sed -i "s#BASEDIR=/usr/local#BASEDIR=$PATH_ZABBIX#g" /etc/init.d/zabbix_agentd &&
sed -i "s/Server\=127.0.0.1/Server\=127.0.0.1,$HOSTNAME/g" /etc/zabbix/zabbix_agentd.conf &&
sed -i "s/ServerActive\=127.0.0.1/ServerActive\=$HOSTNAME:10051/g" /etc/zabbix/zabbix_agentd.conf &&
sed -i "s#tmp/zabbix_agentd.log#var/log/zabbix/zabbix_agentd.log#g" /etc/zabbix/zabbix_agentd.conf &&
sed -i "#UnsafeUserParameters=0#aUnsafeUserParameters=1\n" /etc/zabbix/zabbix_agentd.conf &&
chkconfig zabbix_agentd on && service zabbix_agentd start
if [ $? -ne 0 ];then
echo "zabbix_agentd启动失败,请检查错误信息提示......"
exit 1
fi
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 10050 -j ACCEPT
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 10051 -j ACCEPT
service iptables save
echo -e "\n---------------------------------------------------"
echo "恭喜你,zabbix客户端已安装完成!"
echo -e "---------------------------------------------------\n"
}


##########################################################
## install_server OR install_client ##
##########################################################

 

while true;do
select var in "我要安装server端" "我要安装client端";do
break
done
if [ "$var"x = "我要安装server端"x -o "$var"x = "我要安装client端"x ];then
if [ "$var"x = "我要安装server端"x ];then
server_install
exit 0
else
client_install
exit 0
fi
else
echo "请输入正确的数字序号!"
fi
done

-------------------------------------------------------------------------------------------

zabbix_install.conf  配置文件如下

-------------------------------------------------------------------------------------------

# 此文件为zabbix_install.sh的配置文件,你可以在此文件设置安装信息。

#######################################################
# zabbix server or client install config file #
#######################################################

# 此项为server端IP(web端访问)地址,如服务器为双/多网卡需设置,单网卡可为空(client端安装为必填项)。
HOSTNAME=192.168.2.1

# 此项为zabbix安装tar.gz包名,方便更换其它版本的安装。
ZABBIX_TAR=zabbix-3.4.4.tar.gz

# 此项为zabbix解压后的目录名,方便更换其它版本的安装。
ZABBIX_TAR_DIR=zabbix-3.4.4

# 此项为安装路径,如路径不存在将自动创建,如此项为空,默认安装在/usr/local/$ZABBIX_TAR_DIR下。
ZABBIX_DIR=

# 此项为mysql用户,必须具有创建角色和创建database权限,建议使用root用户;
# 如使用本机且未安装mysql数据库,请勿修改。
MYSQL_USERNAME=root

# 此项为上述mysql用户密码(client端安装可为空)。
MYSQL_PASSWD=pass@126

# 此项为运行zabbix需要创建的mysql用户zabbix时所需的密码(client端安装可为空)。
ZABBIX_PASSWD=pass@126

# 此项为如使用远程mysql数据库时所需的服务器IP地址,如使用本机mysql数据库可为空(client端安装可为空)。
MYSQL_HOSTNAME=

# 此项为mysql数据库使用的端口,默认为3306。
MYSQL_PORT=3306

---------------------------------------------------------------------------------------------------------------------------------------

posted @ 2018-01-12 14:47  一个运维的日常  阅读(385)  评论(0编辑  收藏  举报