环境:
主机名 | IP | 服务 |
---|---|---|
Sunako | 192.168.32.129 | lnmp,zabbix |
配置lnmp环境详见文章 lnmp架构
1. 安装zabbix
#安装依赖包
[root@Sunako ~]# yum -y install wget vim gcc gcc-c++ pcre-devel
#下载并解压
[root@Sunako ~]# wget https://cdn.zabbix.com/zabbix/sources/stable/5.0/zabbix-5.0.2.tar.gz
[root@Sunako ~]# tar xf zabbix-5.0.2.tar.gz
#创建zabbix用户和组
[root@Sunako ~]# useradd -r -M -s /sbin/nologin zabbix
#配置zabbix数据库
[root@Sunako ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 589
Server version: 5.7.24 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.05 sec)
mysql> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix123';
Query OK, 0 rows affected, 2 warnings (0.13 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.10 sec)
mysql> quit
Bye
[root@Sunako ~]# cd zabbix-5.0.2/database/mysql/
[root@Sunako mysql]# mysql -uzabbix -pzabbix123 zabbix < schema.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@Sunako mysql]# mysql -uzabbix -pzabbix123 zabbix < images.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@Sunako mysql]# mysql -uzabbix -pzabbix123 zabbix < data.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
#编译安装
[root@Sunako zabbix-5.0.2]# ./configure --enable-server \
> --enable-agent \
> --with-mysql \
> --with-net-snmp \
> --with-libcurl \
> --with-libxml2
[root@Sunako zabbix-5.0.2]# make install
#配置文件
[root@Sunako zabbix-5.0.2]# cd /usr/local/etc/
[root@Sunako etc]# vim zabbix_server.conf
DBPassword=zabbix123 //设置zabbix数据库连接密码
#启动server和agentd
[root@Sunako ~]# zabbix_server
[root@Sunako ~]# zabbix_agentd
[root@Sunako ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 *:10051 *:*
LISTEN 0 128 *:9000 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 80 :::3306 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
#修改/etc/php.ini配置
[root@Sunako ~]# sed -ri 's/(post_max_size =).*/\1 16M/g' /etc/php.ini
[root@Sunako ~]# sed -ri 's/(max_execution_time =).*/\1 300/g' /etc/php.ini
[root@Sunako ~]# sed -ri 's/(max_input_time =).*/\1 300/g' /etc/php.ini
[root@Sunako ~]# sed -i '/;date.timezone/a date.timezone = Asia/Shanghai' /etc/php.ini
#重启php-fpm
[root@Sunako ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
#创建数据存放目录并修改属主
[root@Sunako ~]# cd zabbix-5.0.2
[root@Sunako zabbix-5.0.2]# cp -a ui /usr/local/nginx/html/zabbix
[root@Sunako zabbix-5.0.2]# chown -R nginx.nginx /usr/local/nginx/html/zabbix/
#配置apache虚拟主机
[root@Sunako ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
root html/zabbix; //添加zabbix
index index.php index.html index.htm;
}
location ~ \.php$ {
root html/zabbix; //添加zabbix
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#设置权限
[root@Sunako ~]# chmod 777 /usr/local/nginx/html/zabbix/conf
[root@Sunako ~]# ll -d /usr/local/nginx/html/zabbix/conf
drwxrwxrwx 3 nginx nginx 94 Jul 6 17:54 /usr/local/nginx/html/zabbix/conf
#重启服务
[root@Sunako ~]# nginx -s reload
[root@Sunako ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 *:10051 *:*
LISTEN 0 128 *:9000 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 80 :::3306 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
1.1 访问web界面
用户名 | 密码 |
---|---|
Admin | zabbix |
恢复zabbix/conf目录的权限为755
[root@Sunako ~]# chmod 755 /usr/local/nginx/html/zabbix/conf
[root@Sunako ~]# ll -d /usr/local/nginx/html/zabbix/conf
drwxr-xr-x 3 nginx nginx 117 Aug 11 11:46 /usr/local/nginx/html/zabbix/conf
1.2 配置nginx服务状态页面
[root@Sunako ~]# vim /usr/local/nginx/conf/nginx.conf
.....
location /status {
stub_status on;
allow 192.168.32.0/24;
deny all;
}
.....
[root@Sunako ~]# nginx -s reload
[root@Sunako ~]# curl http://192.168.32.129/status
Active connections: 1
server accepts handled requests
95 95 110
Reading: 0 Writing: 1 Waiting: 0
1.3 配置监控脚本
[root@Sunako ~]# mkdir /scripts
[root@Sunako ~]# vim /scripts/handled.sh
#!/bin/bash
status=$(curl -s http://192.168.32.129/status |awk 'NR==3{print $3}')
echo $status
[root@Sunako ~]# vim /scripts/Reading.sh
#!/bin/bash
status=$(curl -s http://192.168.32.129/status |awk 'NR==4{print $2}')
echo $status
[root@Sunako ~]# vim /scripts/Writing.sh
#!/bin/bash
status=$(curl -s http://192.168.32.129/status |awk 'NR==4{print $4}')
echo $status
[root@Sunako ~]# chmod +x /scripts/*
[root@Sunako ~]# vim /usr/local/etc/zabbix_agentd.conf
UnsafeUserParameters=1 //此行取消注释,修改为1
//添加以下内容
UserParameter=check_handled,/bin/bash /scripts/handled.sh
UserParameter=check_Reading,/bin/bash /scripts/Reading.sh
UserParameter=check_Writing,/bin/bash /scripts/Writing.sh
#重启并测试能否取出
[root@Sunako ~]# zabbix_server
[root@Sunako ~]# zabbix_agentd
[root@Sunako ~]# zabbix_get -s 127.0.0.1 -k check_handled
315
[root@Sunako ~]# zabbix_get -s 127.0.0.1 -k check_Reading
0
[root@Sunako ~]# zabbix_get -s 127.0.0.1 -k check_Writing
1