linux系统:centos 7.2
zabbix版本:4.0
php版本:7.2.14
httpd版本:2.4.6
Mysql版本:5.7.25
安装zabbix之前需要LAMP环境,这里才用了yum安装LAMP,为了解析zabbix代码
1.mysql安装
#!/usr/bin/env bash
#mysql安装 by chy
#mysql密码
mysql_passwd='your passwd'
#配置yum源
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
yum -y localinstall mysql57-community-release-el7-8.noarch.rpm
rm -rf mysql57-community-release-el7-8.noarch.rpm
##检查mysql源是否正确
#yumsource=`yum repolist enabled | grep "mysql.*-community.*"`
#安装mysql,默认源为5.7
yum -y install mysql-community-server
#启动mysql以及开机自启动
systemctl start mysqld && systemctl enable mysqld && systemctl daemon-reload
#定义mysql是否正确安装
status=`systemctl status mysqld|grep running|wc -l`
passwd=`grep 'temporary password' /var/log/mysqld.log|awk -F: '{print $4}'|sed 's/^[ \t]*//g'`
##mysql正常启动后修改密码,mysql5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示ERROR 1819 (HY000):
if [ $status -eq 0 ];then
echo "mysql未正常安装启动,请查看"
else
#修改密码
mysql -uroot -p$passwd --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '$mysql_passwd';"
##my.cnf追加编码配置
cat << EOF >> /etc/my.cnf
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
EOF
#重启mysql
systemctl restart mysqld
fi
2.Apache安装
##apache安装
yum -y install httpd
#启动apache以及自启动
systemctl start httpd && systemctl enable httpd && systemctl daemon-reload
#查看php安装是否正常, 打开IP后是php页面说明php安装正常
cat >> /var/www/html/index.php <<EOF
<?php
phpinfo();
?>
EOF
3.zabbix安装
#!/usr/bin/env bash
#zabbix安装配置 by chy
#安装zabbix源
mysql_passwd='your passwd'
zabbix_passwd='your passwd'
ip=`curl ip.cip.cc`
rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
yum clean all
#安装zabbix-server 前端 agent
yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-agent
#创建初始数据库,密码为安装mysql时修改的密码,Wupao++2019,zabbix密码此例中为1qaz@WSX
echo "create database zabbix character set utf8 collate utf8_bin;" | mysql -uroot -p$mysql_passwd
echo "grant all privileges on zabbix.* to zabbix@'localhost' identified by '1qaz@WSX';" | mysql -uroot -p$mysql_passwd
echo "flush privileges;" | mysql -uroot -p$mysql_passwd
#导入初始架构和数据
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p$zabbix_passwd zabbix
#为zabbix前端配置php,安装php时我们已经对/etc/php.ini文件进行了修改
#修改zabbix-server的配置文件
cp /etc/zabbix/zabbix_server.conf /etc/zabbix/zabbix_server.conf.bak
sed -i '/# DBPassword=/a\DBPassword=$zabbix_passwd' /etc/zabbix/zabbix_server.conf
sed -i '/# DBSocket/a\DBSocket=/tmp/mysql.sock' /etc/zabbix/zabbix_server.conf
sed -i '/# DBPort=/a\DBPort=3306' /etc/zabbix/zabbix_server.conf
#修改zabbix-agent的配置文件,本机的配置文件可以不用修改
#/etc/zabbix/zabbix_agentd.conf
#启动zabbix server和agent进程
systemctl restart zabbix-server zabbix-agent
#abbix server和agent httpd自启动
systemctl enable zabbix-server zabbix-agent
#配置zabbix的web访问并启动
cp -R /usr/share/zabbix /var/www/html/
chown apache:apache -R /var/www/html/zabbix
systemctl restart httpd
echo "zabbix web 用户名:Admin、密码:zabbix"
echo "打开http://$ip/zabbix,进行下一步安装"
4.php配置
#!/usr/bin/env bash
#php源下载
rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
#安装zabbix需要的php依赖包
安装zabbix时会自动安装上相应的zabbix
#修改php配置文件,zabbix web安装时的参数要求
#sed -i ‘/FTP/a\ 456′ /etc/passwd 在含有FTP的行后面新插入一行
cp /etc/php.ini /etc/php.ini.bak
#sed -i 's/old/new/g' /etc/php.ini
sed -i '/;date.timezone =/a\date.timezone = Asia/Shanghai' /etc/php.ini
sed -i '656s/8/16/g' /etc/php.ini
sed -i '368s/30/300/g' /etc/php.ini
sed -i '378s/60/300/g' /etc/php.ini
5.图形化安装
打开页面是http://$ip/zabbix,默认账号是Admin/zabbix,进入系统后机器修改密码
image.png
注意修改完后重启apache
image.png
image.png
image.png
image.png
image.png
6.agent主动模式安装
#!/usr/bin/env bash
#zabbix-agent安装脚本
# hostname=your hostname
#hostname配置为本机IP,在web端添加的时候直接IP添加就可以就不会出现无法对应为问题
# hostname=`ip address |grep inet |grep -v inet6 |grep -v 127.0.0.1 |awk '{print $2}' |awk -F "/" '{print $1}'`
hostname='192.168.1.202'
ip='192.168.1.201'
# rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
# yum -y install zabbix-agent
#修改zabbix-agent配置文件
# sed -i 's/old/new/g' /etc/zabbix/zabbix_agentd.conf
# sed -i ‘/FTP/a\ 456′ /etc/passwd 在含有FTP的行后面新插入一行
#sed用法:注意sed饮用shell变量时要使用双引号,双引号为若转义不会去除¥的变量表达功能
#-----------------更改-------------------
#Server=127.0.0.1更改为#Server=127.0.0.1表示纯主动模式
#Hostname=Zabbix server更改为Hostname=192.168.1.202(本机IP)
#在# StartAgents=3后面添加StartAgents=0
#ServerActive=127.0.0.1更改为ServerActive=192.168.1.201(zabbix server IP)
# #RefreshActiveChecks=120更改为RefreshActiveChecks=120
# # BufferSize=100更改为BufferSize=300
# # Timeout=3更改为# Timeout=3
cp /etc/zabbix/zabbix_agentd.conf /etc/zabbix/zabbix_agentd.conf.bak
sed -i "s/Server=127.0.0.1/# Server=127.0.0.1/g" /etc/zabbix/zabbix_agentd.conf
sed -i "/# StartAgents=3/a\StartAgents=0" /etc/zabbix/zabbix_agentd.conf
sed -i "s/ServerActive=127.0.0.1/ServerActive=$ip/g" /etc/zabbix/zabbix_agentd.conf
sed -i "s/Hostname=Zabbix server/Hostname=$hostname/g" /etc/zabbix/zabbix_agentd.conf
sed -i "/# RefreshActiveChecks=120/a\RefreshActiveChecks=120" /etc/zabbix/zabbix_agentd.conf
sed -i "/# BufferSize=100/a\BufferSize=300" /etc/zabbix/zabbix_agentd.conf
sed -i "/# Timeout=3/a\Timeout=5" /etc/zabbix/zabbix_agentd.conf
#启动zabbix-agent服务,被动模式ss看不到相应的agent端口
systemctl start zabbix-agent
注意:
ServerActive=... #主动模式的server IP地址
Hostname=test_host #重要:客户端的hostname,不配置则使用主机名
RefreshActiveChecks=120 #被监控端到服务器获取监控项的周期,默认120s即可
BufferSize=200 #被监控端存储监控信息的空间大小
Timeout=10 #超时时间
- Zabbix agent默认为被动模式,修改为主动模式需要修改/etc/zabbix/zabbix_agentd.conf配置文件
- zabbix server端和zabbix agent端的时间要一致
- zabbix agent更改为被动模式时界面上的zbx不会亮,而且ps的时候zbabix agent不会占用10050端口,Server=127.0.0.1更改为#Server=127.0.0.1表示纯主动模式
- 主机端添加主机监控时注意主机名称要和配置文件中的Hostname对应,否则会导致它们找不到对应关系而不能收集数据
主动模式配置
①开启:/选项,填写值为0,开启主动模式
②配置ServerActive项填写Zabbix Server地址
③配置Hostname(最好填写本机的IP地址),与Zabbix Server监控主机名称保持一致
被动模式配置
①配置Server项填写Zabbix Server地址
②配置Hostname项填写自定义主机名称
grep '^[a-Z]' /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
StartAgents=0 #①开启StartAgents选项,填写值为0,开启主动模式,0表示关闭被动模式,zabbix-agentd不监控本地端口,所以看不到zabbix-agentd进程
ServerActive=192.168.1.201 #②配置ServerActive项填写Zabbix Server地址
Hostname=zabbix #③配置Hostname(最好填写本机的IP地址),与Zabbix Server监控主机名称保持一致
RefreshActiveChecks=60
BufferSize=200
Timeout=10
Include=/etc/zabbix/zabbix_agentd.d/*.conf
6.mysql操作
注意mysql远程赋权
mysql> grant all privileges on zabbix.* to 'zabbix'@'localhost' identified by 'your passwd' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
systemctl restart mysqld
修改mysql密码
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
mysql> select User,Host,Password FROM mysql.user;
ERROR 1054 (42S22): Unknown column 'Password' in 'field list'
mysql> select User,Host FROM mysql.user;
+---------------+-----------+
| User | Host |
+---------------+-----------+
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
| zabbix | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)
mysql> alter user 'zabbix'@'localhost' identified by 'XXXXXX';
Query OK, 0 rows affected (0.00 sec)
mysql>
https://blog.51cto.com/12476193/2309233
https://www.zabbix.com/cn/download?zabbix=4.0&os_distribution=centos&os_version=7&db=mysql
https://www.zhouzhifei.com/?p=153
网友评论