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