文章目录
Zabbix概述
zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案。
zabbix能监视各种网络参数,保证服务器系统的安全运营;并提供灵活的通知机制以让系统管理员快速定位/解决存在的各种问题。
zabbix由2部分构成,zabbix server与可选组件zabbix agent。
zabbix server可以通过SNMP,zabbix agent,ping,端口监视等方法提供对远程服务器/网络状态的监视,数据收集等功能,它可以运行在Linux,Solaris,HP-UX,AIX,Free BSD,Open BSD,OS X等平台上。
选择Zabbix的理由!
nagios是一款比较老的监控工具,但是因为插件太多,不是主流
cacti是一款图形化的监控分析工具
zabbix是基于web界面的提供分布式功能的监控工具,监控上线是300-500台
zabbix可以监控网络设备(例如思科、华为等)、服务器(例如IBM、联想、戴尔等)、应用监控(Apache、MQ、中间件、Tomcat等)和服务(HTTP、https、nginx、ssl等)等监控
zabbix使用的最稳定的版本是4.0,LTS是指测试,5.0版本在官网下载的时候比4.0版本多了一个nginx服务器的选择
ajax:异步刷新
zcat:不解压直接查看压缩包
来吧!展示!!
终于可以来吧!展示了!
机器分配:
主机 | IP地址 | 部署服务 |
---|---|---|
Server | 20.0.0.121 | LNMP、Zabbix |
client | 20.0.0122 | zibbix代理 |
直接优化!
两台机器清空防火墙规则,关闭核心防护,主机名修改(server、client)
[root@localhost ~]# hostnamectl set-hostname master
[root@localhost ~]# su
[root@master ~]# setenforce 0
[root@master ~]# iptables -F
Master构建LNMP架构
安装 Nginx
[root@master ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
[root@master ~]# yum clean all
[root@master ~]# yum makecache
[root@master ~]# yum -y install nginx
[root@master ~]# systemctl start nginx
[root@master ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
安装MariaDB
[root@master ~]# yum -y install mariadb-server mariadb
[root@master ~]# systemctl start mariadb
[root@master ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@master ~]# netstat -ntap |grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 65305/mysqld
接下来执行mysql安全配置向导
[root@master ~]# mysql_secure_installation
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:
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
... 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
- 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!
测试登录
[root@master ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
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)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
安装PHP
[root@master ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
获取https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
警告:/var/tmp/rpm-tmp.EfSiHO: 头V3 RSA/SHA256 Signature, 密钥 ID 352c64e5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:epel-release-7-12 ################################# [100%]
[root@master ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
获取https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
警告:/var/tmp/rpm-tmp.ACPgEi: 头V4 RSA/SHA1 Signature, 密钥 ID 62e74ca5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:webtatic-release-7-3 ################################# [100%]
[root@master ~]# ll /etc/yum.repos.d/
总用量 56
……略……
-rw-r--r--. 1 root root 963 10月 9 2014 webtatic-archive.repo
-rw-r--r--. 1 root root 865 10月 9 2014 webtatic.repo
-rw-r--r--. 1 root root 963 10月 9 2014 webtatic-testing.repo
[root@master ~]# yum -y install php72w php72w-devel php72w-fpm php72w-gd php72w-mbstring php72w-mysql
l ##安装PHP环境工具包
配置php-fpm.d和nginx配置文件
[root@master ~]# vim /etc/php-fpm.d/www.conf
8 user = nginx
10 group = nginx
[root@master ~]# vim /etc/nginx/conf.d/default.conf
10 index index.php index.html index.htm;
30 location ~ \.php$ { ##清除这行包括示例几行的#注释
31 root /usr/share/nginx/html;
32 fastcgi_pass 127.0.0.1:9000;
33 fastcgi_index index.php;
34 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
35 include fastcgi_params;
36 }
优化php配置文件
[root@master ~]# vim /etc/php.ini
368 max_execution_time = 300 ##执行时间,在一个程序执行的过程中能够等待的执行时间,执行时间过程中如果没有执行完会结束该程序,以防出现卡死,默认30秒
378 max_input_time = 300 ##接受数据的等待时间
389 memory_limit = 128M ##每个脚本的占用内存限制
656 post_max_size = 16M ##post数据的最大限制
799 upload_max_filesize = 2M ##下载文件的大小限制
800always_populate_raw_post_data = -1
800行插入此句,可以用$HTTP_RAW_POST_DATA接受post raw data(原始未处理数据)
878 date.timezone = Asia/Shanghai ##修改时区为上海
[root@master ~]# systemctl start php-fpm.service
[root@master ~]# systemctl enable php-fpm.service
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
[root@master ~]# systemctl restart nginx
编写php测试页,并打开测试
[root@master ~]# vim /usr/share/nginx/html/info.php
<?php
phpinfo();
?>
测试链接数据库
[root@master html]# vim info.php
<?php
$link=mysqli_connect('127.0.0.1','root','123123');
if ($link) echo "连接成功";
else echo "连接失败";
?>
注:mysql_connect扩展自 PHP 5.5.0 起已废弃,改用mysqli或pdo_mysql
mysql数据库创建zabbix用户
MariaDB [(none)]> CREATE DATABASE zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)
collate的作用:对于mysql中的那些字符类型的列,如varchar,char,text类型的列,都需要有一个collate类型来告知mysql如何对该列进行排序和比较
MariaDB [(none)]> GRANT all privileges ON *.* TO 'zabbix'@'%' IDENTIFIED BY 'admin123'
-> ;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges
-> ;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit
Bye
##测试登录
[root@master html]# mysql -u zabbix -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 14
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)]> select user,host from mysql.user;
+--------+-----------+
| user | host |
+--------+-----------+
| zabbix | % |
| root | 127.0.0.1 |
| root | ::1 |
| root | localhost |
+--------+-----------+
4 rows in set (0.00 sec)
如果报1045的error,是因为此处有空用户占用导致本地无法登陆,解决办法是删除相应的空用户:drop user ''@localhost;然后flush privileges
再次修改php查看测试
[root@master html]# vim info.php
<?php
$link=mysqli_connect('127.0.0.1','zabbix','admin123');
if ($link) echo "zabbix数据库连接成功";
else echo "连接失败";
?>
zabbix-server部署
安装数据库、Zabbix server,Web前端,agent
[root@master html]# rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
[root@master html]# yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-agent
导入初始架构和数据,系统将提示您输入新创建的密码。
[root@master html]# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix
[root@master ~]# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix
Enter password:
[root@master ~]# mysql -u zabbix -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
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)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| zabbix |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> use zabbix;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [zabbix]> show tables;
会发现一堆导入的数据库
优化zabbix配置文件并修改图表中文乱码
[root@master ~]# vim /etc/zabbix/zabbix_server.conf
91 DBHost=localhost ##去掉注释,配置文件记得顶头
116 DBUser=zabbix
124 DBPassword=admin123 ##去掉注释,设置密码
[root@master ~]# vim /usr/share/zabbix/include/defines.inc.php
vim /usr/share/zabbix/include/defines.inc.php ##修正图表中文乱码
:%s /graphfont/kaiti/g
[root@master zabbix]# rm -rf fonts
[root@master zabbix]# mkdir fonts
[root@master zabbix]# cd fonts/
[root@master fonts]# rz -E
rz waiting to receive.
给权限,开启相关zabbix服务
[root@master etc]# setenforce 0
[root@master fonts]# cp -r /usr/share/zabbix/ /usr/share/nginx/html/
[root@master fonts]# chown -R zabbix:zabbix /etc/zabbix/
[root@master fonts]# chown -R zabbix:zabbix /usr/share/nginx/
[root@master fonts]# chown -R zabbix:zabbix /usr/lib/zabbix/
[root@master fonts]# chmod -R 755 /etc/zabbix/web/
[root@master fonts]# chmod -R 777 /var/lib/php/session/
[root@master fonts]# systemctl start zabbix-server.service
[root@master fonts]# systemctl enable zabbix-server.service
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-server.service to /usr/lib/systemd/system/zabbix-server.service.
[root@master fonts]# systemctl start zabbix-agent
[root@master fonts]# systemctl enable zabbix-agent
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.
[root@master etc]# systemctl restart php-fpm.service
[root@master etc]# systemctl restart nginx
登录网页操作
被监控设置
装yum源与zabbix代理
[root@client ~]# rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
获取https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
警告:/var/tmp/rpm-tmp.4MhvLb: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:zabbix-release-4.0-2.el7 ################################# [100%]
[root@client ~]# yum -y install zabbix-agent
修改zabbix代理配置文件并开启服务
[root@client ~]# vim /etc/zabbix/zabbix_agentd.conf
98 Server=20.0.0.121
139 ServerActive=20.0.0.121
150 Hostname=Zabbix-test
[root@client ~]# systemctl start zabbix-agent.service
[root@client ~]# systemctl enable zabbix-agent.service
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.
[root@client ~]# netstat -ntap |grep zabbix
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 2876/zabbix_agentd
tcp6 0 0 :::10050 :::* LISTEN 2876/zabbix_agentd
Web界面操作
添加被控主机:配置–主机–创建主机