Linux Zabbix 脚本安装
1 .Zabbix_server_install.sh
#!/bin/bash
server_ip=`ifconfig|grep -oP '(?<=inet addr:)\S+'|head -1`
Hostname=`cat /etc/hostname`
user_name=`whoami`
if [ $user_name != root ];then
echo "请切换到root用户下执行"
exit 0
fi
切到/tmp目录下
cd /tmp
下载安装包
if [ ! -f zabbix-2.4.3.tar.gz ];then
echo "zabbix安装包不存在,尝试下载中..."
wget http://sourceforge.net/projects/zabbix/files/ZABBIX%20Latest%20Stable/2.4.3/zabbix-2.4.3.tar.gz
if [ ! -f zabbix-2.4.3.tar.gz ];then
echo "安装包下载失败,退出安装"
exit 0
fi
fi
md5=`md5sum zabbix-2.4.3.tar.gz |awk '{print $1}'`
if [ $md5 != e8a0699c4e49999a15c63650a2280600 ];then
echo "文件的md5值不一致"
exit 1
fi
if [ ! -f libxml2-2.6.23.tar.bz2 ];then
echo "libxml2安装包不存在,尝试下载中..."
wget http://down1.chinaunix.net/distfiles/libxml2-2.6.23.tar.bz2
if [ ! -f libxml2-2.6.23.tar.bz2 ];then
echo "安装包下载失败,退出安装"
exit 0
fi
fi
command -v make >/dev/null 2>&1 || apt-get -y install make
command -v gcc >/dev/null 2>&1 || apt-get -y install gcc
先设置mysql密码,为了后面安装mysql时不需要手动输入
echo -e "debconf mysql-server/root_password password yunrun\ndebc
o
nf mysql-server/root_password_again password yunrun">/tmp/mysql-p
asswd
debconf-set-selections /tmp/mysql-passwd
安装和配置PHP环境
apt-get install make cmake gcc g++ autoconf apache2 php5 php5-gd p
hp5-ldap php5-xmlrpc php5-mysql mysql-server-5.5 libmysqlclient-dev li
bxml2 libcurl4-openssl-dev snmp libsnmp-dev -y
安装中文环境
apt-get -y install language-pack-zh-han*
sed -i 's/post_max_size = 8M/post_max_size = 16M/g;s/max_execution
_time = 30/max_execution_time = 300/g;s/max_input_time = 60/max_in
put_time = 300/g;s/;date.timezone =/date.timezone = Asia\/Shanghai/g' /
etc/php5/apache2/php.ini
源码安装libxml2,因为apt-get源安装的libxml2不能在/usr/local/lib/目录下生成libxml2*等模块
tar jxvf libxml2-2.6.23.tar.bz2
cd libxml2-2.6.23/
sed -i 's#O_WRONLY)#O_WRONLY,0777)#g' nanohttp.c
./configure
make
make install
cd /tmp
安装zabbix
useradd zabbix
tar zxvf zabbix-2.4.3.tar.gz
cd zabbix-2.4.3/
./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --wit
h-mysql --with-net-snmp --with-libcurl --with-libxml2
make install
cd /tmp
判断源码安装是否成功
if [ ! -d /usr/local/zabbix ];then
echo "源码安装zabbix失败,退出安装"
exit 1
fi
#sql语句
data_import="
create database zabbix character set utf8;
grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbi
x';
flush privileges;
use zabbix;
source /tmp/zabbix-2.4.3/database/mysql/schema.sql;
source /tmp/zabbix-2.4.3/database/mysql/images.sql;
source /tmp/zabbix-2.4.3/database/mysql/data.sql;"
数据库配置,新安装的mysql密码为barfoo2015
mysql -uroot -p'barfoo2015' -e "${data_import}"
拷贝网页程序代码
mkdir /var/www/zabbix
cp -r /tmp/zabbix-2.4.3/frontends/php/* /var/www/zabbix
chown -R www-data:www-data /var/www/zabbix
sed -i '/zh_CN/s/false/true/' /var/www/zabbix/include/locales.inc.php
修改zabbix安装目录权限
chown -R zabbix.zabbix /usr/local/zabbix/
#创建软链接
ln -s /usr/local/zabbix/bin/* /usr/local/bin
ln -s /usr/local/zabbix/sbin/* /usr/local/sbin
设置开机启动
cp zabbix-2.4.3/misc/init.d/debian/zabbix-server /etc/init.d/
update-rc.d zabbix-server defaults
cp zabbix-2.4.3/misc/init.d/debian/zabbix-agent /etc/init.d/
update-rc.d zabbix-agent defaults
修改zabbix-server.conf
sed -i 's/DBUser=root/DBUser=zabbix/g;s/# DBPassword=/DBPasswor
d =zabbix/g' /usr/local/zabbix/etc/zabbix_server.conf
修改zabbix-agentd.conf
sed -i "s/# EnableRemoteCommands=0/EnableRemoteCommands=1/g;
s/Server=127.0.0.1/Server=$server_ip/g;s/ServerActive=127.0.0.1/Serv
er Active=$server_ip/g;s/Hostname=Zabbix server/Hostname=$Hostna
me/ g" /usr/local/zabbix/etc/zabbix_agentd.conf
修改zabbix-agent.conf
sed -i "s/Server=127.0.0.1/Server=$server_ip/g" /usr/local/zabbix/etc/
zabbix_agent.conf
配置sendemail
wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
sed -i 's@# AlertScriptsPath=${datadir}/zabbix/alertscripts@AlertScriptsPath=/usr/local/zabbix/alertscripts@' /usr/local/zabbix/etc/zabbix_server.conf
tar xvf sendEmail-v1.56.tar.gz -C /usr/local/
mv /usr/local/sendEmail-v1.56/ /usr/local/sendEmail
mkdir /usr/local/zabbix/alertscripts
echo '#!/bin/bash
# $1 recipient
# $2 subject
# $3 content
smtp=smtp.barfoo.com.cn
user=yunwei@barfoo.com.cn
password=123456
/usr/local/sendEmail/sendEmail -f $user -s $smtp -xu $user -xp $passwo
rd -u $2 -m $3 -t $1 > /dev/null 2>&1 ' >> /usr/local/zabbix/alertscripts/se
ndemail.sh
chmod a+x /usr/local/zabbix/alertscripts/sendemail.sh
chown -R zabbix.zabbix /usr/local/zabbix/alertscripts/
cp /usr/local/zabbix/alertscripts/sendemail.sh /usr/local/zabbix/share/zab
bix/alertscripts/
启动
chown -R www-data.www-data /var/www/zabbix/
mv /var/www/zabbix /var/www/html
/etc/init.d/zabbix-server start
/etc/init.d/zabbix-agent start
重启apache
/etc/init.d/apache2 restart
# 2 .Zabbix_agent_install.sh
enter-serverip(){
read -p 'please enter server ip: ' server_ip
echo "服务器的ip为$server_ip"
}
while :
enter-serverip
sleep 2
echo -e '033[36m核对无误后n继续 请按 1n重新输入 请按 2n退出 请按 3 033[0m'
do
read -p '请做出您的选择:' choose
if
[ $choose -eq 3 ];then
exit
echo "退出"
elif
[ $choose -eq 1 ];then
break
else
continue
fi
done
服务器$server_ip在sed中读取不出来,请安装前手动替换
user_name=whoami
if [ $user_name != root ];then
echo "请切换到root用户下执行"
exit 0
fi
切换到/tmp目录下执行
cd /tmp
if [ ! -f zabbix-2.4.3.tar.gz ];then
echo "安装包不存在,尝试下载中..."
wget http://sourceforge.net/projects/zabbix/files/ZABBIX%20Latest%20Stable/2.4.3/zabbix-2.4.3.tar.gz
if [ ! -f zabbix-2.4.3.tar.gz ];then
echo "安装包下载失败,退出安装"
exit 0
else
echo "安装包已下载"
fi
fi
md5=md5sum zabbix-2.4.3.tar.gz |awk '{print $1}'
if [ $md5 != e8a0699c4e49999a15c63650a2280600 ];then
echo "文件的md5值不一致"
exit 1
fi
判断make gcc是否安装
command -v make >/dev/null 2>&1 || apt-get -y install make
command -v gcc >/dev/null 2>&1 || apt-get -y install gcc
useradd zabbix
tar zxvf zabbix-2.4.3.tar.gz
编译...
cd zabbix-2.4.3
./configure --prefix=/usr/local/zabbix --enable-agent
安装
make install
判断源码安装是否成功
if [ ! -d /usr/local/zabbix ];then
echo "源码安装zabbix失败,退出安装"
exit 1
fi
修改权限并创建软链接
chown -R zabbix.zabbix /usr/local/zabbix/
ln -s /usr/local/zabbix/bin/* /usr/local/bin
ln -s /usr/local/zabbix/sbin/* /usr/local/sbin
拷贝启动脚本并设为开机启动
cp /tmp/zabbix-2.4.3/misc/init.d/debian/zabbix-agent /etc/init.d/
update-rc.d zabbix-agent defaults
cd /tmp
修改zabbix配置文件
sed -i "s/# EnableRemoteCommands=0/EnableRemoteCommands=1/g;s/Server=127.0.0.1/Server=$server_ip/g;s/ServerActive=127.0.0.1/ServerActive=$server_ip/g;s/Hostname=zabbix server/Hostname=$HOSTNAME/g" /usr/local/zabbix/etc/zabbix_agentd.conf
启动zabbix-agent
/etc/init.d/zabbix-agent start
echo "Finished!!! zabbi-agent is running."