Zabbix安装:一键安装脚本&Docker安装方法教程Server网络

印迹发布于:2021-10-23 458

一、
#!/bin/sh
log=/root/install.log
exec 2>>$log
#关闭SELINUX,防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
grep SELINUX=disabled /etc/selinux/config
setenforce 0
echo "enforce `getenforce`"
#lnmp搭建
###写入nginx.repo源
echo "Insatlling nginx,please wait!"
echo '[nginx]
name=nginx.repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1'>>/etc/yum.repos.d/nginx.repo
#安装nginx
rm -f /var/run/yum.pid
yum -y install nginx &> /dev/null
if [ `echo $?` -eq 0 ] 
then
     echo "nginx install successful!"
else 
     echo "nginx install failed.please check it!"
     exit
fi
service nginx start &> /dev/null
if [ `echo $?` -eq 0 ] 
then
     echo -e "nginx is running.\n"
else 
     echo  "nginx start failed.please check it!"
     exit
fi
###安装mariadb(mysql)
###安装php
#查看php的yum源版本
if [ `yum list | grep ^php|head -n 1|awk -F"." '{print $3}'` -ge 3 ] 
then 
    echo "Your php-version support zabbix 3.0,installing now!"
else 
    echo "Your php-version can't support zabbix 3.0!Installing failed!"
    exit
fi
echo "Installing mysql and php,please wait!"
for i in mysql mariadb-server php php-mysql php-fpm 
do 
    yum -y install $i &> /dev/null
    if [ `echo $?` -eq 0 ]
    then     
          echo "$i install successful!"
    else 
          yum -y install $i &> /dev/null 
    fi
done
for j in  mariadb.service  php-fpm 
do 
    systemctl start $j &> /dev/null
    if [ `echo $?` -eq 0 ]
    then     
          echo "$j is running!"
    else 
          echo "$j start failed!please check it!"
          exit 
    fi
    systemctl enable $j &> /dev/null
done
#修改php配置使其适合zabbix
cp /etc/php.ini /etc/php.ini.backup
sed -i 's#;date.timezone =#date.timezone = PRC#' /etc/php.ini
sed -i 's/max_execution_time = 30/max_execution_time = 300/' /etc/php.ini
sed -i 's/max_input_time = 60/max_input_time = 300/' /etc/php.ini
sed -i 's/post_max_size = 8M/post_max_size = 16M/' /etc/php.ini
#修改php-fpm
cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf.backup
sed -i 's/user = apache/user = nginx/' /etc/php-fpm.d/www.conf
sed -i 's/group = apache/group = nginx/' /etc/php-fpm.d/www.conf
echo  '<?php
        phpinfo();
        ?>' >>/usr/share/nginx/html/index.php
        
mv /etc/nginx/conf.d/default.conf  /etc/nginx/conf.d/default.conf.backup
echo '
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
      location ~ \.php$ {
       root           html;
       fastcgi_pass   127.0.0.1:9000;
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
       include        fastcgi_params;
    }
}'>>/etc/nginx/conf.d/phpinfo.conf
systemctl restart nginx php-fpm 
#测试nginx代理和php页面文件
echo -e "\nTesting http://localhost (The state of nginx) "
echo "Testing http://localhost/index.php (The state of php)"
if [ "`curl -s localhost | grep Welcome | tail -n 1| awk -F"<h1>" '{print $2}'| awk -F"</h1>" '{print $1}'`" = "Welcome to nginx!" ]
then
    echo "The web of nginx is normal!"
else
    echo "ERROR! The web if nginx isn't normal!"
fi
if [ "`curl -s localhost/index.php | grep "PHP Version" | tail -n 1 |awk -F">" '{print $3}'| awk -F"<" '{print $1}'`" = "PHP Version " ]
then
    echo "The web of php is normal!"
else
    echo "ERROR! The web of php isn't normal!"
fi
##安装zabbix
echo -e "\nInstalling ZABBIX,please wait."
za_install(){
rpm -i https://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm &> /dev/null
yum -y install $k &> /dev/null 
}
for k in zabbix-server-mysql zabbix-web-mysql zabbix-agent
do 
    za_install
    if [ `echo $?` -eq 0 ]
    then     
          echo "$k install successful!"
    else
          za_install
    fi
done
    
echo -e "\nThe following zabbix application had installed."
rpm -qa|grep zabbix
#创建zabbix数据库
echo -e '\nCreating the mysql database.'
mysql<<EOF
create database zabbix character set utf8 collate utf8_bin;
grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix'; 
EOF
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -pzabbix zabbix
##修改zabbix配置
sed -i 's/^# DBHost=localhost/DBHost=localhost/' /etc/zabbix/zabbix_server.conf
sed -i 's/^# DBPassword=/DBPassword=zabbix/' /etc/zabbix/zabbix_server.conf
###为zabbix创建一个新的nginx配置文件
echo '
server {
    listen       80;
    server_name  localhost;
    root   /usr/share/zabbix;
    location / {
        root   /usr/share/zabbix;
        index  index.php;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}'>/etc/nginx/conf.d/phpinfo.conf
chown -R nginx:nginx /etc/zabbix/web 
chown -R nginx:nginx /var/lib/php/session
systemctl restart zabbix-server zabbix-agent nginx php-fpm
systemctl enable zabbix-server zabbix-agent nginx php-fpm &> /dev/null
echo -e 安装完成。请登陆"\e[1;31m localhost/setup.php \e[0m"页面进行zabbix安装设置。
echo -e The install is successful,please login "\e[1;31m localhost/setup.php \e[0m" to configure your web of zabbix(user=Admin pwd=zabbix)。
二、法二

脚本执行时候要输入的参数:

Enter db_ip (172.16.%.%)> #输入数据库访问授权ip范围,例如172.16.%.%、192.168.%.%、192.168.1.%

Enter db_root_password > #输入root用户数据库密码

Enter zabbix_dbuser > 输入zabbix用户名

Enter zabbix_dbpassword >输入zabbix用户密码

Enter zabbix_version (3.x).* > 输入zabbix主版本号,例如:3.0、3.2

Enter zabbix_subversion ..(x) > 输入zabbix子版本号,例如:3.0.28中的28

~]# vi zabbix_autoinstall.sh
#!/bin/bash
# zabbix 3.x一键安装脚本 单机
# 环境 centos 7.4.1708 x86_64
 read  -p "Enter db_ip (172.16.%.%)> " dbip  #输入数据库访问授权ip范围
 read  -p "Enter db_root_password > " dbrootpw  #输入数据库密码
 read  -p "Enter zabbix_dbuser > " dbuser  #输入zabbix数据库名
 read -p "Enter zabbix_dbpassword >" dbpassword #输入zabbix数据密码
 read -p "Enter zabbix_version (3.x).*  > " zabbix_version #输入zabbix版本号
 read -p "Enter zabbix_subversion *.*.(x) > " zabbix_subversion #输入zabbix子版本号
 IP=`ip add|grep global|awk -F'[ /]+' '{ print $3 }'|head -n 1` 
#1、设置
echo  '调整selinux、关闭防火墙'
systemctl stop firewalld.service
systemctl disable firewalld.service
firewall-cmd --state
sed -i '/^SELINUX=.*/c SELINUX=disable' /etc/selinux/config
grep --color=auto '^SELINUX' /etc/selinux/config
setenforce 0
#安装必要的软件
echo  '正在安装必要的软件,请稍后................'
yum install -y epel-release &> /dev/null
yum install -y ntp wget vim net-tools openssh tree &> /dev/null
#修改阿里云源
mkdir -p /etc/yum.repos.d/remark && mv  /etc/yum.repos.d/* /etc/yum.repos.d/remark/
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
sed -i '/aliyuncs.com/d' /etc/yum.repos.d/*.repo
#清理缓存
yum clean all && yum makecache
echo  '修改成阿里源!'
#同步时间
[[ -f /usr/sbin/ntpdate ]] || { echo "install ntp";yum install ntp ntpdate -y &> /dev/null; }
#若没NTP则安装
/usr/sbin/ntpdate ntp6.aliyun.com
echo "*/3 * * * * /usr/sbin/ntpdate ntp6.aliyun.com  &> /dev/null" > /tmp/crontab
crontab /tmp/crontab
echo  '时间同步完成,正在升级软件,请稍后................'
#升级所有包,不改变软件设置和系统设置,系统版本升级,内核不改变
yum -y upgrade &> /dev/null
        
 echo '------------执行完毕---------' 
##########################################
#2 安装
yum  install mariadb-server -y
echo 'skip_name_resolve = ON
innodb_file_per_table = ON
' >> /etc/my.cnf
echo '启动数据库服务'
systemctl start mariadb && systemctl enable mariadb
sleep 5
netstat -antp|grep mysqld
#mysql_secure_installation #初始化设置密码,自动交互
[[ -f /usr/bin/expect ]] || { yum install expect -y; } #若没expect则安装
/usr/bin/expect << EOF
set timeout 30
spawn mysql_secure_installation
expect {
    "enter for none" { send "\r"; exp_continue}
    "Y/n" { send "Y\r" ; exp_continue}
    "password:" { send "$dbrootpw\r"; exp_continue}
    "new password:" { send "$dbrootpw\r"; exp_continue}
    "Y/n" { send "Y\r" ; exp_continue}
    eof { exit }
}
EOF
#测试
mysql -u root -p$dbrootpw -e "show databases;"
[ $? = 0 ] || { echo "mariadb初始化失败";exit; }
#创建zabbix库并授权
echo '创建数据库、用户授权'
mysql -u root -p$dbrootpw -e "
CREATE DATABASE zabbix CHARSET 'utf8';
GRANT ALL ON zabbix.* TO '$dbuser'@'$dbip' IDENTIFIED BY '$dbpassword';
GRANT ALL ON zabbix.* TO '$dbuser'@'127.0.0.1' IDENTIFIED BY '$dbpassword';
GRANT ALL ON zabbix.* TO '$dbuser'@'localhost' IDENTIFIED BY '$dbpassword';
FLUSH PRIVILEGES;
"
echo '安装zabbix----------------------------------------------------------------------------------------------------------'
#安装zabbix
yum -y install httpd php php-mysql php-mbsting php-gd php-bcmath php-ldap php-xml
sleep 5
if ! yum -y install https://mirrors.aliyun.com/zabbix/zabbix/"$zabbix_version"/rhel/7/x86_64/zabbix-release-"$zabbix_version"-1.el7.noarch.rpm ; then yum -y install  https://mirrors.aliyun.com/zabbix/zabbix/"$zabbix_version"/rhel/7/x86_64/zabbix-release-"$zabbix_version"-2.el7.noarch.rpm ; fi
yum clean all && yum makecache
sleep 5
wget https://mirrors.aliyun.com/zabbix/zabbix/"$zabbix_version"/rhel/7/x86_64/zabbix-agent-"$zabbix_version"."$zabbix_subversion"-1.el7.x86_64.rpm
wget https://mirrors.aliyun.com/zabbix/zabbix/"$zabbix_version"/rhel/7/x86_64/zabbix-java-gateway-"$zabbix_version"."$zabbix_subversion"-1.el7.x86_64.rpm
wget https://mirrors.aliyun.com/zabbix/zabbix/"$zabbix_version"/rhel/7/x86_64/zabbix-server-mysql-"$zabbix_version"."$zabbix_subversion"-1.el7.x86_64.rpm
wget https://mirrors.aliyun.com/zabbix/zabbix/"$zabbix_version"/rhel/7/x86_64/zabbix-web-mysql-"$zabbix_version"."$zabbix_subversion"-1.el7.noarch.rpm
wget https://mirrors.aliyun.com/zabbix/zabbix/"$zabbix_version"/rhel/7/x86_64/zabbix-web-"$zabbix_version"."$zabbix_subversion"-1.el7.noarch.rpm
yum -y install *.rpm
#导入数据库
gzip -d /usr/share/doc/zabbix-server-mysql-"$zabbix_version"."$zabbix_subversion"/create.sql.gz
mysql -u$dbuser -h127.0.0.1 -p$dbpassword zabbix  < /usr/share/doc/zabbix-server-mysql-"$zabbix_version"."$zabbix_subversion"/create.sql
 
#配置zabbix
sed -i '/^# DBHost=/c DBHost='$IP'' /etc/zabbix/zabbix_server.conf
sed -i '/^DBUser=/c DBUser='$dbuser'' /etc/zabbix/zabbix_server.conf
sed -i '/^# DBPassword=/c DBPassword='$dbpassword'' /etc/zabbix/zabbix_server.conf
sed -i '/^# JavaGateway=/c JavaGateway='$IP'' /etc/zabbix/zabbix_server.conf
sed -i '/^# JavaGatewayPort=10052/c JavaGatewayPort=10052' /etc/zabbix/zabbix_server.conf
sed -i '/^# StartJavaPollers=0/c StartJavaPollers=5' /etc/zabbix/zabbix_server.conf
sed -i '/php_value date.timezone*/c php_value date.timezone Asia/shanghai' /etc/httpd/conf.d/zabbix.conf
#配置zabbix-java-geteway
sed -i '/^# LISTEN_IP="0.0.0.0"/c LISTEN_IP="0.0.0.0"' /etc/zabbix/zabbix_java_gateway.conf
sed -i '/^# LISTEN_PORT=10052/c LISTEN_PORT=10052' /etc/zabbix/zabbix_java_gateway.conf
sed -i '/^TIMEOUT=3/c TIMEOUT=30' /etc/zabbix/zabbix_java_gateway.conf
#启动服务并添加为开机启动项
if  systemctl start zabbix-server.service && systemctl start httpd && systemctl start zabbix-agent && systemctl start zabbix-java-gateway ; then systemctl enable zabbix-server.service && systemctl enable httpd && systemctl enable zabbix-agent && systemctl enable zabbix-java-gateway
        ss -tnl
        echo ‘=================================安装完成========================================’
        echo 'Please visit http://'$IP'/zabbix  to further configuration!!!!!'
else
        echo 'The install is fail!!!!!!!!'
fi

执行安装脚本

~]# . zabbix_autoinstall.sh
Enter db_ip (172.16.%.%)> 172.16.%.%    #授权访问的ip地址范围
Enter db_root_password > 123456           #数据库root密码
Enter zabbix_dbuser > zbxuser
Enter zabbix_dbpassword >123456
Enter zabbix_version (3.x).*  > 3.4
Enter zabbix_subversion *.*.(x) > 1         #这里输入的是zabbix版本号为3.4.1

整个过程大概需要10-20分钟,最后出现安装完成

‘=================================安装完成========================================’
Please visit http://172.16.16.250/zabbix  to further configuration!!!!!

用浏览器访问提示URL进行下一步配置


3、docker安装

docker部署zabbix监控系统(nginx mysql)

1. 先安装数据库mysql
2. 创建zabbix-server
3. 最后web-nginx

本次使用docker搭建zabbix的组合是mysql+docker+zabix-server

1. 先安装数据库mysql

docker run --name zabbix-mysql-server --hostname zabbix-mysql-server -e MYSQL_ROOT_PASSWORD="123456" -e MYSQL_USER="zabbix" -e MYSQL_PASSWORD="123456" -e MYSQL_DATABASE="zabbix" -p 3306:3306 -d mysql

2. 创建zabbix-server

docker run --name zabbix-server-mysql --hostname zabbix-server-mysql --link zabbix-mysql-server:mysql -e DB_SERVER_HOST="mysql" -e MYSQL_USER="zabbix" -e MYSQL_DATABASE="zabbix" -e MYSQL_PASSWORD="123456" -v /etc/localtime:/etc/localtime:ro -v /data/docker/zabbix/alertscripts:/usr/lib/zabbix/alertscripts -v /data/docker/zabbix/externalscripts:/usr/lib/zabbix/externalscripts -p 10051:10051 -d zabbix/zabbix-server-mysql

3. 最后web-nginx

最后安装zabbix-web-nginx 

docker run --name zabbix-web-nginx-mysql --hostname zabbix-web-nginx-mysql --link zabbix-mysql-server:mysql --link zabbix-server-mysql:zabbix-server -e DB_SERVER_HOST="mysql" -e MYSQL_USER="zabbix" -e MYSQL_PASSWORD="123456" -e MYSQL_DATABASE="zabbix" -e ZBX_SERVER_HOST="zabbix-server" -e PHP_TZ="Asia/Shanghai" -p 8000:80 -p 8443:443 -d zabbix/zabbix-web-nginx-mysql

登录访问测试

浏览器访问ip:8000查看 默认登录 username:Admin password:zabbix

这里说明,mysql没做数据卷的映射,nginx也没做数据卷的映射,在实际生产环境下,最好做数据映射。防止数据丢失。

docker-zabbbix-agent的安装以及链接zabbix-server

docker run --name zabbix-agent --link zabbix-server-mysql:zabbix-server -d zabbix/zabbix-agent:latest

最后需要在web端将,zabbix-agent添加到zabbix-server的host列表里面。

出自:https://www.cnblogs.com/Dicky-Zhang/p/7189714.html

其它

Docker



http://www.virplus.com/thread-1427.htm
转载请注明:2021-10-23 于 VirPlus 发表

推荐阅读
最新回复 (0)

    ( 登录 ) 后,可以发表评论!

    返回