mysql监控nginx_zabbix监控mysql,nginx

刚开说mysql监控的时候 很容易遇到问题 而问题大部分出现在mysql的配置上面,看到我下面做的希望对你们有所帮助,哈哈 不聊了 好好学习 现在开始

1.创建mysql用户

[root@bogon ]# groupadd mysql

[root@bogon ]# mkdir -pv /data/mysql

[root@bogon ]# useradd -r -g mysql -d /data/mysql/ -s /sbin/nologin mysql

2.获取mysql软件包

[root@bogon y]# wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.49.tar.gz

3.安装依赖包

[root@bogon ]# yum -y install cmake gcc* ncurses-devel

4.源码安装mysql

[root@bogon ]#tar zxvf /data/pkg/mysql-5.6.10.tar.gz

[root@bogon ]# cd mysql-5.5.49

[root@bogon mysql-5.5.49]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/data/mysql -DWITH_EXTRA_CHARSETS=all -DWITH_READLINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DMYSQL_TCP_PORT=3306 -DDEFAULT_COLLATION=utf8_general_ci

[root@bogon mysql-5.5.49]# make -j 4 && make install

[root@bogon mysql-5.5.49]# chown -R mysql.mysql /usr/local/mysql

[root@bogon mysql-5.5.49]# cd /usr/local/mysql/support-files/

5.拷贝mysql配置文件

[root@bogon support-files]# cp my-medium.cnf /data/mysql/my.cnf

[root@bogon support-files]# cp mysql.server /etc/init.d/mysqld

[root@bogon support-files]# chmod +x /etc/init.d/mysqld

6.初始化mysql

[root@bogon support-files]# cd /usr/local/mysql/scripts

[root@bogon scripts]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql/

7.修改mysql的数据目录

[root@bogon ]# cd / && more /etc/my.cnf

[mysqld]

datadir=/data/mysql

socket=/var/lib/mysql/mysql.sock

user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

[root@bogon ]# mkdir -pv /var/lib/mysql/ && ln -s /tmp/mysql.sock /var/lib/mysql/

8.启动mysql

[root@bogon ]# ln -s /usr/local/mysql/bin/mysql /usr/bin/

[root@bogon ]# ln -s /usr/local/mysql/bin/mysqladmin /usr/bin/

[root@bogon ]# service mysqld start

[root@bogon ]# mysqladmin -uroot password "123456"

9.登陆数据库创建一个zabbix库

[root@bogon ]# mysql -p123456

mysql> create database zabbix default charset utf8;

mysql> grant all privileges on zabbix.* to zabbix@'localhost' identified by 'zabbix';

mysql> flush privileges;

mysql> show databases;

mysql> quit

编写一个新的mysql的配置文件因为我们会再mysql的 这个默认的模板中使用 userparameter_mysql.conf

[root@zabser scripts]# more /usr/local/zabbix/etc/.my.cnf

#zabbix_agent

[mysql]

host=localhost

user=zabbix

password=zabbix

socket=/tmp/mysql.sock

[mysqladmin]

host=localhost

user=zabbix

password=zabbix

socket=/tmp/mysql.sock

[root@zabser scripts]#chmod 655  /usr/local/zabbix/etc/.my.cnf

下面要咋默认模板中配置使用到.my.cnf的配置文件

[root@zabser scripts]# more /usr/local/zabbix/etc/zabbix_agentd.conf.d/userparameter_mysql.conf

ad007b078adb30a9104e056ff5e33959.png

在zabbix_agentd的配置文件中添加

然后重启 zabbix_agentd

[root@localhost etc]# vim zabbix_agentd.conf

UnsafeUserParameters=1

Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/*.conf

[root@zabser scripts]# service zabbix_agentd restart

剩下的你要添加模板

37f3cd73bfe662be88c31a6908d6faba.png

zabbix 监控nginx

在zabbix agentd客户端上,查看nginx是否加载了–with-http_stub_status_module。因为zabbix监控nginx是根据nginx的Stub Status模块,抓取Status模块所提供的数据。假如以前没开启,现在想启用StubStatus 模块,在编译nginx 的时候要加上参数 –with-http_stub_status_module

检查with-http_stub_status_module有没有安装

[root@zabser scripts]# /usr/local/nginx/sbin/nginx -V

nginx version: nginx/1.9.15

built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)

built with OpenSSL 1.0.1e-fips 11 Feb 2013

TLS SNI support enabled

configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-pcre

在zabbix.conf配置stub_status_module

045142376cbe758049cc96e80672c14d.png

启动nginx

service nginx restart

用curl做测试

[root@zabser zabbix_agentd.conf.d]# curl http://47.93.232.219/nginx-status

Active connections: 2

server accepts handled requests

367 367 8631

Reading: 0 Writing: 1 Waiting: 1

状态页面各项数据的意义:

active connections – 当前 Nginx 正处理的活动连接数。

serveraccepts handled requests — 总共处理了 233851 个连接 , 成功创建 233851 次握手 (证明中间没有失败的 ), 总共处理了 687942 个请求 ( 平均每次握手处理了 2.94 个数据请求 )。

reading — nginx 读取到客户端的 Header 信息数。

writing — nginx 返回给客户端的 Header 信息数。

waiting — 开启 keep-alive 的情况下,这个值等于 active – (reading + writing), 意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接。

zabbix-agent 配置

有3个步骤,首先是编写获取Nginx信息脚本,接着配置中增加key信息,然后重启agent 服务。

①编写Nginx监控脚本,记住路径,后面配置需要用到,注意脚本权限问题,agent运行用户要能执行。

mkidr -p /usr/local/zabbix/scripts/

cd /usr/local/zabbix/scripts

vi nginx-check.sh

#!/bin/bash

##################################

# Zabbix monitoring script

#

# nginx:

# - anything available via nginx stub-status module

#

##################################

# Contact:

# vincent.viallet@gmail.com

# Zabbix requested parameter

ZBX_REQ_DATA="$1"

ZBX_REQ_DATA_URL="$2"

# Nginx defaults

NGINX_STATUS_DEFAULT_URL="http://bbs.jzq.pub/nginx-status"    #(这里写网站的域名)

WGET_BIN="/usr/bin/wget"

#

# Error handling:

# - need to be displayable in Zabbix (avoid NOT_SUPPORTED)

# - items need to be of type "float" (allow negative + float)

#

ERROR_NO_ACCESS_FILE="-0.9900"

ERROR_NO_ACCESS="-0.9901"

ERROR_WRONG_PARAM="-0.9902"

ERROR_DATA="-0.9903" # either can not connect / bad host / bad port

# Handle host and port if non-default

if [ ! -z "$ZBX_REQ_DATA_URL" ]; then

URL="$ZBX_REQ_DATA_URL"

else

URL="$NGINX_STATUS_DEFAULT_URL"

fi

# save the nginx stats in a variable for future parsing

NGINX_STATS=$($WGET_BIN -q $URL -O - 2> /dev/null)

# error during retrieve

if [ $? -ne 0 -o -z "$NGINX_STATS" ]; then

echo $ERROR_DATA

exit 1

fi

#

# Extract data from nginx stats

#

case $ZBX_REQ_DATA in

active_connections) echo "$NGINX_STATS" | head -1 | cut -f3 -d' ';;

accepted_connections) echo "$NGINX_STATS" | grep -Ev '[a-zA-Z]' | cut -f2 -d' ';;

handled_connections) echo "$NGINX_STATS" | grep -Ev '[a-zA-Z]' | cut -f3 -d' ';;

handled_requests) echo "$NGINX_STATS" | grep -Ev '[a-zA-Z]' | cut -f4 -d' ';;

reading) echo "$NGINX_STATS" | tail -1 | cut -f2 -d' ';;

writing) echo "$NGINX_STATS" | tail -1 | cut -f4 -d' ';;

waiting) echo "$NGINX_STATS" | tail -1 | cut -f6 -d' ';;

*) echo $ERROR_WRONG_PARAM; exit 1;;

esac

exit 0

chmod o+x /usr/local/zabbix/scripts/nginx-check.sh

agent的配置文件 /etc/zabbix/zabbix_agentd.conf 中定义了其他key的包含目录 Include=/etc/zabbix/zabbix_agentd.d/, 如果没有这个配置请自己添加下。接着在 /etc/zabbix/zabbix_agentd.d/ 目录新建一个文件 nginx-params.conf, 内容如下

[root@zabser zabbix_agentd.conf.d]# more /usr/local/zabbix/etc/zabbix_agentd.conf.d/nginx-params.conf

UserParameter=nginx[*],/usr/local/zabbix/scripts/nginx-check.sh "$1"

/etc/init.d/zabbix-agent restart

下面导入nginx模板文件

ae0322541767e06b08ef724595bb7010.png

a57fc3c4558e8c72ff4e55a40a0b03d2.png

bf7cf008146be7b5ca790f80b33800c4.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值