CentOS搭建Zabbix监控服务(Nginx)

搭建环境

CentOS Linux release 7.4.1708 (Core)

LNMP

zabbix-3.4.4

一、部署监控服务器

1.1 安装LNMP环境

​#安装nginx
tar -xf nginx-1.7.10.tar.gz 
cd nginx-1.7.10/
yum -y install gcc openssl-devel pcre-devel
./configure --with-http_ssl_module
make && make install

#安装mariadb
yum -y install mariadb mariadb-server mariadb-devel

#安装php
yum -y install php php-mysql php-fpm

#修改nginx配置文件
#配置nginx支持php动态网站,还需开启nginx的各种fastcgi缓存,加速php脚本的执行速度
vim /usr/local/nginx/conf/nginx.conf
	 ...
	 http {
		...
		location / {
			root	html;
			index	index.php index.html;
		}
		...
		fastcgi_buffers		8 16k;	//缓存php生成的页面内容,8个16k;
		fastcgi_buffer_size	32k;	//缓存php生产的头部信息;
		fastcgi_connect_timeout	300;	//连接PHP的超时时间;
		fastcgi_send_timeout	300;	//发送请求的超时时间;
		fastcgi_read_timeout	300;	//读取请求的超时时间;
		location ~ \.php$ {
			root			html;
			fastcgi_pass	127.0.0.1:9000;
			fastcgi_index	index.php;
			include		fastcgi.conf;
		}
	 }
​
#启动服务
ln -s /usr/local/nginx/sbin/nginx /sbin/nginx
nginx
systemctl start mariadb php-fpm
systemctl enable mariadb php-fpm
firewall-cmd --set-default-zone=trusted
setenforce 0

#测试LNMP
echo "<?php \$test='测试OK'; echo \$test; ?>" > /usr/local/nginx/html/test.php
curl http://127.0.0.1/test.php
测试OK

1.2 部署监控服务器Zabbix Server

cd ..
#安装zabbix
yum -y install net-snmp-devel curl-devel libevent-devel
tar -xf zabbix-3.4.4.tar.gz
cd zabbix-3.4.4/
./configure \
--enable-server \
--enable-proxy \
--enable-agent \
--with-mysql=/usr/bin/mysql_config \
--with-net-snmp \
--with-libcurl
make && make install

#初始化zabbix

 #准备数据库
mysql
create database zabbix character set utf8;
grant all on zabbix.* to zabbix@"localhost" identified by "zabbix";
exit
cd database/mysql/
mysql -uzabbix -pzabbix zabbix < schema.sql
mysql -uzabbix -pzabbix zabbix < images.sql
mysql -uzabbix -pzabbix zabbix < data.sql

 #准备zabbix的web页面
cd ../../frontends/php/
cp -r * /usr/local/nginx/html/
chmod -R 777 /usr/local/nginx/html/*

 #解决乱码问题
\cp /usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc /usr/local/nginx/html/fonts/DejaVuSans.ttf

 #修改zabbix_server配置文件
vim /usr/local/etc/zabbix_server.conf
	 LogFile=/tmp/zabbix_server.log				//设置日志文件
	 DBHost=localhost					//数据库主机
	 DBName=zabbix						//数据库名称
	 DBUser=zabbix						//数据库用户
	 DBPassword=zabbix					//数据库密码
useradd zabbix		//创建数据库用户,不创建用户无法启动服务
zabbix_server		//启动服务

 #修改zabbix_agent配置文件
vim /usr/local/etc/zabbix_agentd.conf
	 LogFile=/tmp/zabbix_server.log		//设置日志文件
	 Server=127.0.0.1,192.168.2.5		//允许哪些主机监控本机(被动模式)
	 ServerActive=127.0.0.1,192.168.2.5	//允许哪些主机监控本机(主动模式)
	 Hostname=zabbix server			/设置本机主机名
	 UnsafeUserParameters=1			//不安全的用户参数,=1允许自定义key
zabbix_agentd			//启动监控agent

 #补充zabbix_server的web环境要求
yum -y install php-gd php-xml php-bcmath php-mbstring
vim /etc/php.ini
	 max_execution_time = 300		//最大执行时间300秒
	 max_input_time = 300			//最大输入时间,服务器接收数据的时间限制
	 memory_limit = 128M			//内存限制
	 post_max_size = 32M			//POST最大数据容量
	 date.timezone = Asia/Shanghai	//设置时区(日期.时区 = 亚洲/上海)
systemctl restart php-fpm

可以根据页面提示修改

1.3 网页部署zabbix

· firefox http://192.168.4.5        #打开监控端的网页

· 登陆

二、部署被监控主机zabbix agent

2.1 安装zabbix agent

yum -y install gcc pcre-devel
tar -xf zabbix-3.4.4.tar.gz
cd zabbix-3.4.4/
./configure --enable-agent
make && make install
useradd -s /sbin/nologin zabbix

2.2 拷贝启动脚本(非必须),方便管理

cp ./misc/init.d/fedora/core/zabbix_agentd /etc/init.d/

2.3 修改zabbix agent配置

vim /usr/local/etc/zabbix_agentd.conf
	 Server=127.0.0.1,192.168.2.5			//被动模式监控本机的IP
	 ServerActive=127.0.0.1,192.168.2.5		//主动模式监控本机的IP
	 Hostname=zabbix_client_web1			//被监控端自己的主机名
	 EnableRemoteCommands=1					//使能够远程命令
	  //监控异常后,是否允许服务器远程过来执行命令,如重启某个服务
	 UnsafeUserParameters=1			//(不安全的用户参数),是否允许自定义key监控
zabbix_agentd				//启动agent服务

2.4 写入开机自启动

chmod +x /etc/rc.d/rc.local
cat >> /etc/rc.d/rc.local<<EOF
zabbix_agentd
EOF

三、测试连通行

zabbix_get -s 192.168.2.100 -p 10050 -k "system.cpu.load[all,avg1]"

转载于:https://my.oschina.net/xinsui1314x/blog/1929217

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你写一个监控nginx的脚本,请问你已经安装了zabbix及其相关插件吗?如果还没有,请先安装好。下面是一个简单的监控nginx负载的脚本示例: ```bash #!/bin/bash # Author: your name # Date: YYYY-MM-DD # Description: Monitor Nginx status for Zabbix # Nginx status page URL URL="http://localhost/nginx_status" # Get nginx active connections active_connections=$(curl -s $URL | awk '/Active/{print $NF}') # Get nginx accepted connections accepted_connections=$(curl -s $URL | awk 'NR==3{print $1}') # Get nginx handled connections handled_connections=$(curl -s $URL | awk 'NR==3{print $2}') # Get nginx total requests total_requests=$(curl -s $URL | awk 'NR==3{print $3}') # Get nginx reading connections reading=$(curl -s $URL | awk '/Reading/{print $2}') # Get nginx writing connections writing=$(curl -s $URL | awk '/Writing/{print $4}') # Get nginx waiting connections waiting=$(curl -s $URL | awk '/Waiting/{print $6}') # Send data to zabbix echo "nginx.active_connections $active_connections $(date +%s)" | zabbix_sender -z zabbix_server_ip -s nginx_server_name -i - echo "nginx.accepted_connections $accepted_connections $(date +%s)" | zabbix_sender -z zabbix_server_ip -s nginx_server_name -i - echo "nginx.handled_connections $handled_connections $(date +%s)" | zabbix_sender -z zabbix_server_ip -s nginx_server_name -i - echo "nginx.total_requests $total_requests $(date +%s)" | zabbix_sender -z zabbix_server_ip -s nginx_server_name -i - echo "nginx.reading_connections $reading $(date +%s)" | zabbix_sender -z zabbix_server_ip -s nginx_server_name -i - echo "nginx.writing_connections $writing $(date +%s)" | zabbix_sender -z zabbix_server_ip -s nginx_server_name -i - echo "nginx.waiting_connections $waiting $(date +%s)" | zabbix_sender -z zabbix_server_ip -s nginx_server_name -i - ``` 你需要将脚本中的$URL替换为你实际的nginx状态页面URL,并将$zabbix_server_ip和$nginx_server_name替换为你的zabbix服务器IP和nginx服务器名称。该脚本可以使用zabbix_sender命令将收集的数据发送到zabbix服务器,以便在zabbix监控界面中查看nginx的实时状态和趋势。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值