当前位置: > 其它学习 > zabbix >

zabbix平台部署

时间:2020-06-17 11:32来源:linux.it.net.cn 作者:IT
zabbix平台部署文档
部署zabbix监控平台
1.要求:
要求部署一台Zabbix监控服务器,一台被监控主机为,监控机器包括zabbix监控服务器自身。
 
2.方案:
1.zabbix平台依赖LNMP架构环境。
2.官网下载zabbix源码包。
3.修改PHP配置文件满足zabbix需求。
4.安装监控端主机zabbix.server,修改基本配置
5.安装被监控端主机web.server,修改基本配置
 
主机名称 网卡与IP地址
zabbix.server eno16777736:192.168.125. 138
web.server ens32:192.168.125.125
使用1台RHEL7虚拟机,安装部署LNMP环境、Zabbix即相关的依赖包,配置数据库并对Zabbix监控平台进行初始化操作。使用1台被监控端,源码安装Zabbix Agent。
 
3.部署步骤:
1、搭建LNMP架构环境
使用脚本安装zabbix平台需要的LNMP底层架构。
 
 
[root@localhost ~]# vim lnmp.sh //新建LNMP一键搭建脚本
 
#!/bin/bash
# author:kwin
# Email:kwinwong@hotmail.com
 
src="/usr/local/src/"
cd $src
 
 
 
 
#找到指定进程,并杀死
#findPortKill 80
findPortKill (){
  processe=`lsof -i:${1} -n|awk '{print $2}'|grep '^[1-9]'`
  for i in $processe
    do
#  echo "Kill the $1 process [ $i ]"
  kill -9 $i
    done
}
 
 
#创建快捷方式,统一管理
#createLink ${软件目录}
createLink(){
 
path=${1}
 
linkPath='/link'
#如果文件夹不存在,创建文件夹
if test ! -d ${linkPath}
then
mkdir ${linkPath}
fi
 
fileName=`echo ${path} | awk -F '/' '{print $1 $NF}'`
 
 
if test  -d ${linkPath}/${fileName} 
then
rm -rf ${linkPath}/${fileName}
fi
 
ln -s ${path} ${linkPath}   
}
 
 
#将命令所在目录添加到系统参数PATH中,方便调用
addToPATH(){
 
bin=${1}
 
echo $PATH|grep ${bin} >/dev/null
 
if [ $? -ne 0 ]; then
 
echo "export PATH=\$PATH:${bin}">>/etc/profile
fi
}
 
#安装nginx
installNginx(){
 
yum -y  install pcre-devel openssl openssl-devel gcc gcc-c++ ncurses-devel perlddd
 
fileName="nginx-1.9.9"
package="${fileName}.tar.gz"
installDir="/usr/local/nginx"
 
if test ! -f ${package}
then
wget http://nginx.org/download/${package}
fi
 
 
tar zxvf $package
 
cd $fileName
./configure --prefix=${installDir}
make && make install
echo "安装完成"
 
 
 
#如果出现错误 找到80占用的进程并杀死,再重启
#如果还有问题 请自行调试配置文件
/usr/local/nginx/sbin/nginx 1> /dev/null 2>&1
if [ $? -ne 0 ]; then
findPortKill 80
/usr/local/nginx/sbin/nginx
fi
 
#sleep : 默认以秒为单位。
#usleep : 默认以微秒为单位。
#1s = 1000ms = 1000000us
 
usleep 100000
 
pid=`cat /usr/local/nginx/logs/nginx.pid`
 
echo "nginx 已经启动,进程号为${pid}"
 
bin="${installDir}/sbin"
 
addToPATH ${bin}
 
 
#创建快捷方式,统一管理
createLink ${installDir}
 
}
 
 
#安装nginx
installPHP(){
 
yum -y install expect
#安装libmcrypt第三方yum源
wget http://www.atomicorp.com/installers/atomic
#sh ./atomic
#expect自动应答
#当需要交互 字符中有yes时自动输入yes回车
expect <<EOF
set timeout -1
spawn sh ./atomic
expect "yes"
send "yes\r"
expect eof
EOF
 
#yum  install -y epel-release
#yum  -y update
#安装需要的模块,yum一并安装依赖库
yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel mysql pcre-devel curl-devel libxslt-devel php-mcrypt libmcrypt libmcrypt-devel
 
 
fileName="php-7.0.8"
package="${fileName}.tar.gz"
installDir="/usr/local/php7_fpm"
 
if test ! -f ${package}
then
wget  http://cn2.php.net/distributions/${package}
fi
 
 
tar zxvf $package
 
cd $fileName
 
./configure --prefix=${installDir} \
--with-curl \
--with-freetype-dir \
--with-gd \
--with-gettext \
--with-iconv-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-libxml-dir \
--with-mysqli \
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-png-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-gd-native-ttf \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-xml \
--enable-zip \
--with-mcrypt \
--enable-fpm \
--with-config-file-path=${installDir}/etc
#PHP.ini默认 在${installDir}/bin目录下
make && make install
 
echo "安装完成"
 
cp php.ini-development ${installDir}/etc/php.ini
 
cp ${installDir}/etc/php-fpm.conf.default ${installDir}/etc/php-fpm.conf
 
cp ${installDir}/etc/php-fpm.d/www.conf.default ${installDir}/etc/php-fpm.d/www.conf
 
cp -R ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
 
chmod a+x -R /etc/init.d/
 
 
/etc/init.d/php-fpm restart
 
echo "php-fpm 已经启动,进程号为9000"
echo "调用/etc/init.d/php-fpm控制服务"
 
bin="${installDir}/bin"
 
addToPATH ${bin}
 
 
#创建快捷方式,统一管理
createLink ${installDir}
 
}
 
 
function installMysql(){
 
cat > /etc/yum.repos.d/MariaDB.repo <<EOF
# MariaDB 10.0 CentOS repository list - created 2014-04-01 04:32 UTC
# http://mariadb.org/mariadb/repositories/ 
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1.11/centos7-amd64/
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOF
 
yum install -y MariaDB-server MariaDB-client  MariaDB-devel
 
echo "安装完成"
 
mysqladmin -u root password 'root'
 
#如果出现错误 找到3306占用的进程并杀死,再重启
#如果还有问题 请自行调试配置文件
service mysql start 1> /dev/null 2>&1
if [ $? -ne 0 ]; then
findPortKill 3306
service mysql start
fi
 
#设置开机启动MariaDB
chkconfig mysql on
 
echo "mysql 已经启动,进程号为3306 默认密码为root"
 
echo "调用service mysql start|stop|restart控制服务或则"
echo "/etc/init.d/mysql start|stop|restart"
 
}
 
 
 
function init(){
 
  case $1 in
 
                1)
 
            installNginx #调用函数必须在函数后面
 
            ;;
 
            2)
 
            installMysql #调用函数必须在函数后面
 
            ;;  
 
 
                3)
 
            installPHP #调用函数必须在函数后面
 
            ;;   
 
                4)
            installMysql
            installNginx
            installPHP  
            ;;   
                *)
      echo 'You do not select a number between 1 to 4'
 
          ;;
 
            esac 
 
}
 
echo 'Input a number between 1 to 4'
echo '1.安装nginx     2.安装mysql'
echo '3.安装PHP       4.一键安装LNMP'
read mode
init ${mode}
source /etc/profile
#.  /etc/profile
#注意: . 和 /etc/profile 有空格 同 source /etc/profile
 

 
 

 
1.[root@localhost ~]# chmod 700 lnmp.sh //赋予脚本root用户执行权限。
2.[root@localhost ~]# ./lnmp.sh //执行脚本
Input a number between 1 to 4
1.安装nginx     2.安装mysql
3.安装PHP       4.一键安装LNMP
##输入4回车。
 

 
2、安装清华大学镜像源。
 
 
 1.[root@localhost ~]#  rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm //下载并配置zabbix 4.0 的yum源
 

 
3、安装zabbix平台软件、数据库及其依赖包。
 
1.[root@localhost ~]# yum install -y zabbix-server-mysql zabbix-web-mysql zabbix-agent mariadb-server.x86_64 //安装zabbix依赖库及mariadb

 
4、部署mariadb数据库
 
 
 1. [root@localhost ~]# systemctl restart mariadb.service //启动mariadb数据库
 2. [root@localhost ~]# mysql_secure_installation 
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):  当前数据库密码为空,直接按回车键
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y
New password:输入要为root管理员设置的数据库密码
Re-enter new password:再次输入密码
Password updated successfully!
Reloading privilege tables..
 ... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y(删除匿名账户)
... Success!
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y(禁止root管理员从远程登录)
 ... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y(删除test数据库并取消对它的访问权限)
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y(刷新授权表,让初始化后的设定立即生效)
 ... Success!
Cleaning up...
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
 

 
5、导入zabbix初始数据表
 
[root@localhost ~]# mysql -uroot -p
Enter password: //登录mariadb数据库
 
 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 35
Server version: 5.5.65-MariaDB MariaDB Server
 
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
 
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin; //新建zabbix数据库指定库使用utf8格式。不使用utf8格式的话后期遇到中文会报错。
MariaDB [(none)]> grant all on zabbix.* to zabbix@localhost identified by "13542031695"; //新建zabbix数据库账户。用户名和密码可以自己指定。
MariaDB [(none)]> Ctrl-C -- exit!
Aborted //退出数据库
 
[root@localhost ~]# rpm -ql zabbix-server-mysql //查找zabbix初始数据库路径
/usr/share/doc/zabbix-server-mysql-4.0.21/create.sql.gz //找到.sql.gz格式的这条
 
[root@localhost ~]# cd /usr/share/doc/zabbix-server-mysql-4.0.21/ //切换到zabbix默认数据目录
 
[root@localhost zabbix-server-mysql-4.0.21]# gzip -d create.sql.gz //解压数据包
 
[root@localhost zabbix-server-mysql-4.0.21]#  mysql -uzabbix -p13542031695 zabbix < create.sql //导入到zabbix数据库
 

 
 

 
6、修改zabbix配置文件基本参数
 
[root@localhost ~]# vim /etc/zabbix/zabbix_server.conf //修改配置文件
DBUser=zabbix //注意这里要修改成zabbix数据库账号
 
### Option: DBPassword
#       Database password.
#       Comment this line if no password is used.
#
# Mandatory: no
# Default:
DBPassword=13542031695   //只需修改数据库zabbix用户的密码
 
### Option: DBSocket
#       Path to MySQL socket.
#
# Mandatory: no
# Default:
# DBSocket=
 
### Option: DBPort
#       Database port when not using local socket.
#
 

 
7、启动zabbix服务
 
[root@localhost ~]# systemctl enable zabbix-server.service //加入开机启动项目
 
[root@localhost ~]# systemctl start zabbix-server.service //启动zabbix服务
 
[root@localhost ~]# netstat -lntup //查看10051端口是否被监听,启动成功的话10051端口会被监听。
3067/cupsd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      2150/master         
tcp6       0      0 :::60672                :::*                    LISTEN      1781/rpc.statd      
tcp6       0      0 :::10050                :::*                    LISTEN      1797/zabbix_agentd  //注意这行
tcp6       0      0 :::10051                :::*                    LISTEN      2577/zabbix_server  
tcp6       0      0 :::111                  :::*                    LISTEN      1767/rpcbind        
tcp6       0      0 :::80                   :::*                    LISTEN      1759/httpd          
tcp6       0      0 :::21                   :::*                    LISTEN    
 
[root@localhost ~]# systemctl restart httpd //启动httpd服务
 
[root@localhost ~]# systemctl enable httpd //加入开机启动项
 


 
4、安装zabbix平台
1、在浏览器输入192.168.168.125.138/zabbix首次登陆会自动跳转到setup.php引导安装。


 
2、进入引导界面后点(next step)下一步。
 
 
3、检查各依赖组件是否正常(这里显示未知的的时区)。

 
# 编辑时区默认是注释
[root@localhost ~]# vim /etc/httpd/conf.d/zabbix.conf
php_value date.timezone Asia/Shanghai //去掉注释符号把市区改为Asia/Shanghai
# 通过apache来调整php的参数,重启httpd服务
[root@localhost ~]# systemctl restart httpd
 

 
刷新网页后显示ok!问题解决。




 
 
4、键入本地数据库信息。




 
 
5、填写zabbix server的名字


 
 
6、最后检查各参数配置
 




 
7、Zabbix登录
默认的用户名是:Admin,密码是:zabbix (之前导入zabbix默认初始数据库时数据库里的默认用户)
 

 
正常登录则Zabbix安装成功:后续登录在浏览器输入http://192.168.125.138/zabbix/zabbix.php即可进入zabbix平台。
 


(责任编辑:IT)
------分隔线----------------------------