欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

zabbix-nginx,mysql,apache,rysnc监控项设置

程序员文章站 2022-05-10 18:58:56
...

一 nginx 监控设置

nginx有哪些信息可以监控,nginx内置了一个status状态的功能,通过配置可以看到nginx的运行情况,status显示的内容包括当前连接数,处于活动状态的连接数,已经处理的请求数等等

1.1 nginx.conf内添加nginx_status的配置

server {
listen 6000;
access_log off;
server_name localhost;
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}

重载nginx服务

1.1.2 检测是否正常
[aaa@qq.com zabbix_agentd.d]# curl 127.0.0.1:6000/nginx_status
Active connections: 1 
server accepts handled requests
 50 50 60 
Reading: 0 Writing: 1 Waiting: 0 

1.2 编写 nginx_zabbix_agent脚本

通过脚本获取status页面信息的关键值,zabbix server通过这些关键值才能生成数据图像,获取status的关键值脚本如下:

首先创建一个文件

mkdir /var/lib/zabbix/nginx_status
chmod 755 /va/lib/zabbix/nginx_status
[aaa@qq.com zabbix]# cat  nginx_status 
#! /bin/bash
#date: 2020.3.11
#路径 /var/lib/zabbix/nginx_status
# Description:zabbix监控nginx性能以及进程状态
# Note:此脚本需要配置在被监控端,否则ping检测将会得到不符合预期的结果

HOST="localhost"
PORT="6000"

# 检测nginx进程是否存在
function ping {
    /sbin/pidof nginx | wc -l
}
# 检测nginx性能
function active {
    /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
function reading {
    /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
function writing {
    /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
function waiting {
    /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
function accepts {
    /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled {
    /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests {
    /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $3}'
}
# 执行function
$1

1.3 配置zabbixd.conf,定义监控脚本key

要使用脚本监控需要将zabbixd.conf文件中的
UnsafeUserParameters=1 开启
在脚本中添加定义以下各项的键值

[aaa@qq.com zabbix_agentd.d]# cat /etc/zabbix/zabbix_agentd.d/userparameter_nginx.conf 
#
# This is a sample nginx config file.
# Put it to /etc/zabbix_agentd.conf.d or otherwise integrate into agent config.
# Edit to your needs.
#
UserParameter=nginx.status[*],/var/lib/zabbix/nginx_status $1

1.4 在zabbix web界面进行设置

1.4.1 设置nginx监控项

如下:
zabbix-nginx,mysql,apache,rysnc监控项设置

1.4.2 设置nginx触发器

zabbix-nginx,mysql,apache,rysnc监控项设置

二 apache监控设置

2.1 在agent服务端上传一个脚本

2.1.1 创建文件夹和设置权限
mkdir /var/lib/zabbix
touch /var/lib/zabbix/apache
chmod 755 /var/lib/zabbix/apache

apache脚本

[aaa@qq.com zabbix]# cat  apache 
#! /bin/bash
#
# Name: apache
#
# Checks Apache activity.
#
# Author: Alejandro Michavila
# Modified for Scoreboard Values: Murat Koc, aaa@qq.com
# Modified for using also as external script: Murat Koc, aaa@qq.com
# Modified for outputting usage or ZBX_NOTSUPPORTED: Alejandro Michavila
# Modified to do cacheing for performance, aaa@qq.com
#
# Version: 1.5
#
#set -x
zapachever=`hahahaha -v |grep 'Server version'|awk '{print $3}'`
rval=0
value=""
cache_seconds="60"
[ "$TMPDIR" ] || TMPDIR=/tmp
 
function usage()
{
	echo "zapache version: $zapachever"
	echo "usage:"
	echo "  $0 [<url>] TotalAccesses                 - Check total accesses."
	echo "  $0 [<url>] TotalKBytes                   - Check total KBytes."
	echo "  $0 [<url>] CPULoad                       - Check CPU load."
	echo "  $0 [<url>] Uptime                        - Check uptime."
	echo "  $0 [<url>] ReqPerSec                     - Check requests per second."
	echo "  $0 [<url>] BytesPerSec                   - Check Bytes per second."
	echo "  $0 [<url>] BytesPerReq                   - Check Bytes per request."
	echo "  $0 [<url>] BusyWorkers                   - Check busy workers."
	echo "  $0 [<url>] IdleWorkers                   - Check idle workers."
	echo "  $0 [<url>] version                       - Version of this script."
	echo "  $0 [<url>] ping                          - Check if Apache is up."
	echo "  $0 [<url>] WaitingForConnection          - Check Waiting for Connection processess."
	echo "  $0 [<url>] StartingUp                    - Check Starting Up processess."
	echo "  $0 [<url>] ReadingRequest                - Check Reading Request processess."
	echo "  $0 [<url>] SendingReply                  - Check Sending Reply processess."
	echo "  $0 [<url>] KeepAlive                     - Check KeepAlive Processess."
	echo "  $0 [<url>] DNSLookup                     - Check DNSLookup Processess."
	echo "  $0 [<url>] ClosingConnection             - Check Closing Connection Processess."
	echo "  $0 [<url>] Logging                       - Check Logging Processess."
	echo "  $0 [<url>] GracefullyFinishing           - Check Gracefully Finishing Processess."
	echo "  $0 [<url>] IdleCleanupOfWorker           - Check Idle Cleanup of Worker Processess."
	echo "  $0 [<url>] OpenSlotWithNoCurrentProcess  - Check Open Slots with No Current Process."
}

########
# Main #
########

if [[ $# ==  1 ]];then
	#Agent Mode
	STATUS_URL="http://localhost/server-status?auto"
	CASE_VALUE="$1"
elif [[ $# == 2 ]];then
	#External Script Mode
	STATUS_URL="$1"
	case "$STATUS_URL" in
		http://*|https://*) ;;
		*) STATUS_URL="http://$STATUS_URL/server-status?auto";;
	esac
	CASE_VALUE="$2"
else
	#No Parameter
	usage
	exit 0
fi

case "$CASE_VALUE" in
'version')
	echo "$zapachever"
	exit 0;;
esac

umask 077

# $UID is bash-specific
cache_prefix="zapache-$UID-${STATUS_URL//[^a-zA-Z0-9_-]/_}"
cache="$TMPDIR/$cache_prefix.cache"
cache_timestamp_check="$TMPDIR/$cache_prefix.ts"
# This assumes touch from coreutils
touch -d "@$((`date +%s` - ($cache_seconds - 1)))" "$cache_timestamp_check"

if [ "$cache" -ot "$cache_timestamp_check" ]; then
	curl="`which curl`"
	if [ "$curl" ]; then
		fetch_url() { $curl --insecure --silent --location -H "Cache-Control: no-cache" "aaa@qq.com"; }
	else
		wget="`which wget`"
		if [ "$wget" ]; then
			fetch_url() { $wget --no-check-certificate --quiet --header "Cache-Control: no-cache" -O - "aaa@qq.com"; }
		else
			echo "ZBX_NOTSUPPORTED"
			exit 1
		fi
	fi

	fetch_url "$STATUS_URL" > "$cache"
	rval=$?
	if [ $rval != 0 ]; then
		echo "ZBX_NOTSUPPORTED"
		exit 1
	fi
fi

case "$CASE_VALUE" in
'ping')
	if [ ! -s "$cache" -o "$cache" -ot "$cache_timestamp_check" ]; then
		echo "0"
	else
		echo "1"
	fi
	exit 0;;
esac

if ! [ -s "$cache" ]; then
	echo "ZBX_NOTSUPPORTED"
	exit 1
fi
 
case "$CASE_VALUE" in
'TotalAccesses')
	value="`awk '/^Total Accesses:/ {print $3}' < \"$cache\"`"
	rval=$?;;
'TotalKBytes')
	value="`awk '/^Total kBytes:/ {print $3}' < \"$cache\"`"
	rval=$?;;
'CPULoad')
	value="`awk '/^CPULoad:/ {print $2}' < \"$cache\"`"
	rval=$?;;
'Uptime')
	value="`awk '/^Uptime:/ {print $2}' < \"$cache\"`"
	rval=$?;;
'ReqPerSec')
	value="`awk '/^ReqPerSec:/ {print $2}' < \"$cache\"`"
	rval=$?;;
'BytesPerSec')
	value="`awk '/^BytesPerSec:/ {print $2}' < \"$cache\"`"
	rval=$?;;
'BytesPerReq')
	value="`awk '/^BytesPerReq:/ {print $2}' < \"$cache\"`"
	rval=$?;;
'BusyWorkers')
	value="`awk '/^BusyWorkers:/ {print $2}' < \"$cache\"`"
	rval=$?;;
'IdleWorkers')
	value="`awk '/^IdleWorkers:/ {print $2}' < \"$cache\"`"
	rval=$?;;
'WaitingForConnection')
	value="`awk '/^Scoreboard:/ {print split($2,notused,"_")-1}' < \"$cache\"`"
	rval=$?;;
'StartingUp')
	value="`awk '/^Scoreboard:/ {print split($2,notused,"S")-1}' < \"$cache\"`"
	rval=$?;;
'ReadingRequest')
	value="`awk '/^Scoreboard:/ {print split($2,notused,"R")-1}' < \"$cache\"`"
	rval=$?;;
'SendingReply')
	value="`awk '/^Scoreboard:/ {print split($2,notused,"W")-1}' < \"$cache\"`"
	rval=$?;;
'KeepAlive')
	value="`awk '/^Scoreboard:/ {print split($2,notused,"K")-1}' < \"$cache\"`"
	rval=$?;;
'DNSLookup')
	value="`awk '/^Scoreboard:/ {print split($2,notused,"D")-1}' < \"$cache\"`"
	rval=$?;;
'ClosingConnection')
	value="`awk '/^Scoreboard:/ {print split($2,notused,"C")-1}' < \"$cache\"`"
	rval=$?;;
'Logging')
	value="`awk '/^Scoreboard:/ {print split($2,notused,"L")-1}' < \"$cache\"`"
	rval=$?;;
'GracefullyFinishing')
	value="`awk '/^Scoreboard:/ {print split($2,notused,"G")-1}' < \"$cache\"`"
	rval=$?;;
'IdleCleanupOfWorker')
	value="`awk '/^Scoreboard:/ {print split($2,notused,"I")-1}' < \"$cache\"`"
	rval=$?;;
'OpenSlotWithNoCurrentProcess')
	value="`awk '/^Scoreboard:/ {print split($2,notused,".")-1}' < \"$cache\"`"
	rval=$?;;
*)
	usage
	exit 1;;
esac

if [ "$rval" -eq 0 -a -z "$value" ]; then
    case "$CASE_VALUE" in
        # Theese metrics are output only if non-zero
        'CPULoad' | 'ReqPerSec' | 'BytesPerSec' | 'BytesPerReq')
            value=0
            ;;
        *)
            rval=1
            ;;
    esac
fi
 
if [ "$rval" -ne 0 ]; then
	echo "ZBX_NOTSUPPORTED"
fi
 
echo "$value"
exit $rval
 
#
# end apache

2.2 修改apache配置文件,开启server-status功能

<Location /server-status>
SetHandler server-status
Order allow,deny
Allow from 127.0.0.1
Allow from localhost
</Location>

重载httpd服务,然后查看
zabbix-nginx,mysql,apache,rysnc监控项设置

2.3 定义监控脚本key

cat /etc/zabbix/zabbix_agentd.d/userparameter_apache.conf 
#
# This is a sample zabbix_agentd config file.
# Put it to /etc/zabbix_agentd.conf.d or otherwise integrate into agent config.
# Edit to your needs.
#
UserParameter=zapache[*],/var/lib/zabbix/apache_status \$1

2.4 进行监控项配置

下载zabbix模板
复制代码
[aaa@qq.com /]# wget https://github.com/lorf/zapache/archive/master.zip
[aaa@qq.com /]# unzip master.zip
[aaa@qq.com /]# cd zapache-master/
[aaa@qq.com zapache-master]# ls
httpd-server-status.conf.sample zapache
README.md zapache-template-active.xml
userparameter_zapache.conf.sample zapache-template.xml

注释:
这里我们只需要一个zapache-template.xml,
将此文件导出到本地桌面,

zabbix-server导入模板

zabbix-nginx,mysql,apache,rysnc监控项设置
如图:
zabbix-nginx,mysql,apache,rysnc监控项设置
显示已经导入成功。

apache监控项设置

zabbix-nginx,mysql,apache,rysnc监控项设置

apache触发器设置

zabbix-nginx,mysql,apache,rysnc监控项设置

三 mysql监控

3.1 创建监控所需mysql账户(agent端),通过脚本

#!/bin/bash
#2020.3.11
#
mysql_user="root"
mysql_passwd="自己填写密码"
        if [ `sudo ps -ef|grep 'mysql' | grep -v grep |wc -L`  -gt 1 ];then
            echo "mysql 正在运行,部署监控"
            mysql_sock_path=`sudo ps -ef|grep 'mysql' | grep -v grep |grep socket |awk -F 'socket=' '{print $2}'|awk -F ' ' '{print $1}'|head -n1`
            mysql_sock_path2=`sudo cat /etc/my.cnf |grep socket| awk -F '=' '{print $2}'|head -n1`
                if [ ! $mysql_sock_path ]; then
                        echo "mysql socket path is $mysql_sock_path2"
                        sudo  sed -i "s#/var/lib/mysql/mysql.sock#$mysql_sock_path2#g" /var/lib/zabbix/.my.cnf
                else
                        echo "mysql socket path is $mysql_sock_path"
                        sudo  sed -i "s#/var/lib/mysql/mysql.sock#$mysql_sock_path#g" /var/lib/zabbix/.my.cnf
                fi
           sudo  mysql -u$mysql_user -p$mysql_passwd -e "grant usage on *.* to 'check'@'localhost' identified by '123456'"
            echo "mysql 监控部署完毕" 
        else
            echo 'mysql is not running~'
        fi

在agent客户端:
检测新建check账号是否能正常连接数据库

3.2 在 /var/lib/zabbix/下创建一个包含MySQL用户名和密码的配置文件 “.my.cnf”

[onfkuZ ~]$ cat /var/lib/zabbix/.my.cnf 
[mysql]
host = localhost
user = check
password = check123,.
socket = /tmp/mysql.sock
[mysqladmin]
host = localhost
user = check
password = 123456
socket = /tmp/mysql.sock

3.3 定义mysql监控 key

[aaa@qq.com ~]$ cat /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf 
# For all the following commands HOME should be set to the directory that has .my.cnf file with password information.

# Flexible parameter to grab global variables. On the frontend side, use keys like mysql.status[Com_insert].
# Key syntax is mysql.status[variable].
UserParameter=mysql.status[*],echo "show global status where Variable_name='$1';" | HOME=/var/lib/zabbix mysql -N | awk '{print $$2}'

# Flexible parameter to determine database or table size. On the frontend side, use keys like mysql.size[zabbix,history,data].
# Key syntax is mysql.size[<database>,<table>,<type>].
# Database may be a database name or "all". Default is "all".
# Table may be a table name or "all". Default is "all".
# Type may be "data", "index", "free" or "both". Both is a sum of data and index. Default is "both".
# Database is mandatory if a table is specified. Type may be specified always.
# Returns value in bytes.
# 'sum' on data_length or index_length alone needed when we are getting this information for whole database instead of a single table
UserParameter=mysql.size[*],bash -c 'echo "select sum($(case "$3" in both|"") echo "data_length+index_length";; data|index) echo "$3_length";; free) echo "data_free";; esac)) from information_schema.tables$([[ "$1" = "all" || ! "$1" ]] || echo " where table_schema=\"$1\"")$([[ "$2" = "all" || ! "$2" ]] || echo "and table_name=\"$2\"");" | HOME=/var/lib/zabbix mysql -N'

UserParameter=mysql.ping,HOME=/var/lib/zabbix mysqladmin ping | grep -c alive
UserParameter=mysql.version,mysql -V

注意HOME=路径的配置 ,配置完后
重启zabbix-agent:

同时,在Server端也可以使用使用zabbix_get命令来测试从Server端获取指定的Client端的数据,如下:

[aaa@qq.com alertscripts]# zabbix_get -s172.18.200.61 -p 10050 -k mysql.ping

如果返回数据,证明zabbix服务端可获取客户端信息!

3.4 页面对主机添加监控模板,

由于zabbix有默认的mysql模板,监控和触发都是默认的。
zabbix-nginx,mysql,apache,rysnc监控项设置
所以,这里,直接添加就可以了。

四 rsync 监控

4.1 监控配置

zabbix-nginx,mysql,apache,rysnc监控项设置

触发器配置

zabbix-nginx,mysql,apache,rysnc监控项设置

相关标签: 系统运维