使用zabbix监控K8s(kubernetes)异常POD

通过监控异常的pod,及时发现k8s存在的问题。原理是使用kubectl get pod –all-namespaces,找到ready列,如果ready数量与desire数量不一致的,或者非RUNNING的状态POD,则认为这个pod异常,,进而告警。

配置agent监控项

/etc/zabbix/zabbix_agentd.d/k8s.conf

UserParameter=abnormal.pod, kubectl get pod --all-namespaces -o wide | awk 'NR>1{cmd="echo "$3" | bc";cmd|getline ret;close(cmd);if (ret != 1 || $4 != "Running"){print}}'

导入模板

<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
    <version>3.2</version>
    <date>2017-06-16T03:51:42Z</date>
    <groups>
        <group>
            <name>Templates</name>
        </group>
    </groups>
    <templates>
        <template>
            <template>Template App K8s</template>
            <name>Template App K8s</name>
            <description/>
            <groups>
                <group>
                    <name>Templates</name>
                </group>
            </groups>
            <applications>
                <application>
                    <name>k8s</name>
                </application>
            </applications>
            <items>
                <item>
                    <name>abnormal pod</name>
                    <type>0</type>
                    <snmp_community/>
                    <multiplier>0</multiplier>
                    <snmp_oid/>
                    <key>abnormal.pod</key>
                    <delay>60</delay>
                    <history>90</history>
                    <trends>0</trends>
                    <status>0</status>
                    <value_type>4</value_type>
                    <allowed_hosts/>
                    <units/>
                    <delta>0</delta>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <formula>1</formula>
                    <delay_flex/>
                    <params/>
                    <ipmi_sensor/>
                    <data_type>0</data_type>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description/>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>k8s</name>
                        </application>
                    </applications>
                    <valuemap/>
                    <logtimefmt/>
                </item>
            </items>
            <discovery_rules/>
            <httptests/>
            <macros/>
            <templates/>
            <screens/>
        </template>
    </templates>
    <triggers>
        <trigger>
            <expression>{Template App K8s:abnormal.pod.strlen()}<>0</expression>
            <recovery_mode>0</recovery_mode>
            <recovery_expression/>
            <name>abnormal pod</name>
            <correlation_mode>0</correlation_mode>
            <correlation_tag/>
            <url/>
            <status>0</status>
            <priority>2</priority>
            <description/>
            <type>0</type>
            <manual_close>0</manual_close>
            <dependencies/>
            <tags/>
        </trigger>
    </triggers>
</zabbix_export>

应用模板

找到能执行kubectl的节点,添加k8s模板。

获取进程CPU使用率的3种方法

一个进程的CPU使用率是进程性能的一个重要指标。通过CPU使用率可以获知当然进程繁忙程度,或者获取该进程CPU使用异常情况。下面我们介绍3种方法来获取进程的CPU使用率。

通过zabbix获取

从zabbix 3.0开始,zabbix提供了一个item来获取进程CPU使用率,item为:

proc.cpu.util[<name>,<user>,<type>,<cmdline>,<mode>,<zone>]

各参数介绍如下

name - process name (default is all processes)
user - user name (default is all users)
type - CPU utilisation type:
total (default), user, system
cmdline - filter by command line (it is a regular expression) 
mode - data gathering mode: avg1 (default), avg5, avg15 
zone - target zone: current (default), all. This parameter is supported only on Solaris platform. Since Zabbix 3.0.3 if agent has been compiled on Solaris without zone support but is running on a newer Solaris where zones are supported and <zone> parameter is default or current then the agent will return NOTSUPPORTED (the agent cannot limit results to only current zone). However, <zone> parameter value all is supported in this case.

使用示例:

Examples:
⇒ proc.cpu.util[,root] → CPU utilisation of all processes running under the “root” user
⇒ proc.cpu.util[zabbix_server,zabbix] → CPU utilisation of all zabbix_server processes running under the zabbix user

The returned value is based on single CPU core utilisation percentage. For example CPU utilisation of a process fully using two cores is 200%. 

来自:zabbix文档

使用top命令

top命令是可以很直观的查看所有进程的信息,如内存使用,CPU使用率等。下面是使用top命令获取进程当前CPU使用率。

pid="123970"
top -b -n 1 -p $pid  2>&1 | awk -v pid=$pid '{if ($1 == pid)print $9}'

从/proc/读取

从/proc/[pid]/stat获取该进程的cpu时间,从/proc/stat获取系统cpu总时间,等待1秒,重新获取两个cpu时间。然后((cpu_time2 – cpu_time1) / (total_time2 – total_time1)) * 100*cpu_core;其中cpu_time为进程cpu使用时间,total_time为系统cpu使用时间,cpu_core为cpu核心数,通过/proc/cpuinfo获取。代码如下:

pid=3456
cpu_core=$(grep -c processor /proc/cpuinfo)
total_time1=$(awk '{if ($1 == "cpu") {sum = $2 + $3 + $4 + $5 + $6 + $7 + $8 + $9 + $10 + $11;print sum}}' /proc/stat)
cpu_time1=$(awk '{sum=$14 + $15;print sum}' /proc/$pid/stat)
sleep 1
total_time2=$(awk '{if ($1 == "cpu") {sum = $2 + $3 + $4 + $5 + $6 + $7 + $8 + $9 + $10 + $11;print sum}}' /proc/stat)
cpu_time2=$(awk '{sum=$14 + $15;print sum}' /proc/$pid/stat)
awk -v cpu_time1=$cpu_time1 -v total_time1=$total_time1 -v cpu_time2=$cpu_time2 -v total_time2=$total_time2 -v cpu_core=$cpu_core 'BEGIN{cpu=((cpu_time2 - cpu_time1) / (total_time2 - total_time1)) * 100*cpu_core;print cpu}'

使用shell awk获取Nginx Apache一分钟内的网站访问日志

之前我们有写过如何获取网站一分钟内的日志,不过日志格式是要求以tab分隔,当遇到日志以空格分隔,前面的脚本就无效了,这里我们提供以空格分隔的日志格式获取一分钟内日志的shell脚本。

# 日志目录
LOG_DIR="/etc/apache2/logs/domlogs/"
# 临时目录
TEMP_DIR="/tmp/log/"
mkdir -p $TEMP_DIR
cd $LOG_DIR

log_names=`find ./ -maxdepth 1 -type f | grep -v -E "bytes_log|offsetftpbytes"`

for log_name in $log_names;
do

#设置路径
split_log="$TEMP_DIR/$log_name"
access_log="${LOG_DIR}/$log_name"

#取出最近一分钟日志
tac $access_log  | awk '
BEGIN{
cmd="date -d "1 minute ago" +%s"
cmd|getline oneMinuteAgo
}
{
day = substr($4,2,2)
month = substr($4,5,3)
year = substr($4,9,4)
time = substr($4,14,8)
time_str = day" "month" "year" "time
cmd="date -d ""time_str"" +%s"
cmd|getline log_date
if (log_date>=oneMinuteAgo){
print
} else {
exit;
}
}' > $split_log

done

# 删除空文件
find ./ -size 0 -exec rm {} ;

zabbix3监控mysql性能状态

如果被监控的主机上已经安装好了mysql,在安装zabbix-agent的时候就会自动创建一个userparameter_mysql.conf

配置zabbix目录

vim /etc/zabbix/zabbix_agentd.conf
#取消注释,没有则创建一个
Include=/etc/zabbix/zabbix_agentd.d/

修改mysql监控的的配置

vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf

UserParameter=mysql.status[*],echo "show global status where Variable_name='$1';" | HOME=/var/lib/zabbix mysql -N | awk '{print $$2}'

UserParameter=mysql.size[*],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

修改my.cnf 配置

# Zabbix Agent ;注意sock文件路径
[mysql]
host     = localhost
user     = zabbix
password = 密码
socket   = /var/run/mysqld/mysqld.sock
[mysqladmin]
host     = localhost
user     = zabbix
password = 密码
socket   = /var/run/mysqld/mysqld.sock

为被监控的主机添加mysql模版

添加mysql监控的模版

监控

查看获取的mysql数据

监控

zabbix自动发现并监控本机的多memcached实例

本人在工作中一般喜欢把MySQL、Redis、Memcached、MongoDB等数据库按照实例的方式对外提供服务。一般都是一台高配的服务器上开启多个实例给每个业务使用。而监控是重中之重,我自己也尝试了多种监控方式,但对我来说感觉最简单最快的就是使用zabbix了,灵活定义key。

由于我是多实例,所以就需要用到zabbix的自动发现功能(LLD)。基本处理方式就是:

  • 1、写自动发现脚本。

  • 2、写状态取值脚本。

  • 3、添加配置文件。

  • 4、添加权限。

  • 5、配置zabbix web。

一、写自动发现脚本

$ cat /etc/zabbix/zabbix_agentd.d/scripts/memcached_discovery.py
#!/usr/bin/env python
import os
import json
t=os.popen("""sudo netstat -nltp|awk -F: '/memcached/&&/LISTEN/{print $2}'|awk '{print $1}'| grep -v grep | grep -v '^$'   """)
ports = []
for port in  t.readlines():
        r = os.path.basename(port.strip())
        ports += [{'{#MCPORT}':r}]
print json.dumps({'data':ports},sort_keys=True,indent=4,separators=(',',':'))

执行脚本看输出结果(最好使用zabbix用户执行,才能看出效果):

$ python /etc/zabbix/zabbix_agentd.d/scripts/memcached_discovery.py
{
    "data":[
        {
            "{#MCPORT}":"11211"
        },
        {
            "{#MCPORT}":"11212"
        }
}

我这个脚本中使用了sudo权限,zabbix用户在执行netstat时需要sudo权限。

二、写状态取值脚本

#!/bin/bash
#
#Auth: Pengdongwen
#Blog: www.ywnds.com
#Email: [email protected]
#Desc: memcached status monitoring 
#dependent:
#  1)yum install nc
#  2)python memcached_discovery.py
#########################

IP=127.0.0.1 
PORT="$1"
METRIC="$2"

if [ $# -lt 2 ];then
    echo "please set argument"
    exit 1
fi

STATUS=`echo "stats" | nc $IP $PORT | grep -w "$METRIC" | awk '{print $3}'`
case $METRIC in
    'version')
        echo $STATUS
        ;;
    'uptime')
        echo $STATUS 
        ;;
    'curr_connections')
        echo $STATUS
        ;;
    'total_connections')
        echo $STATUS
        ;;
    'cmd_get')
        echo $STATUS
        ;;
    'cmd_set')
        echo $STATUS
        ;;
    'get_hits')
        echo $STATUS
        ;;
    'get_misses')
        echo $STATUS
        ;;
    'bytes_read')
        echo $STATUS
        ;;
    'bytes_written')
        echo $STATUS
        ;;
    'curr_items')
        echo $STATUS
        ;;
    'total_items')
        echo $STATUS
        ;;
    'expired_unfetched')
        echo $STATUS
        ;;
    'evicted_unfetched')
        echo $STATUS
        ;;
    *)
        echo "Not selected metric"
        exit 0
        ;;
esac

脚本很简单,需要传给脚本两个参数,一个是端口号,另一个是监控值。

有几个特别需要说明的就是:

  • 1)这个脚本不支持redis加密。

  • 2)需要指定redis-cli的绝对路径。

  • 3)需要安装dos2unix工具(yum install dos2unix)。

三、添加配置文件

$ cat /etc/zabbix/zabbix_agentd.d/userparameter_memcached.conf
UserParameter=memcached.discovery[*],python /etc/zabbix/zabbix_agentd.d/scripts/memcached_discovery.py
UserParameter=memcached[*],/bin/bash /etc/zabbix/zabbix_agentd.d/scripts/memcached_status.sh $1 $2

这里定义三个key,第一个key是用于自动发现的。第二个key是用于取不同实例的状态值的,传了两个参数,$1是端口号(从自动发现中获取的),第二个是传的参数。端口号和参数我会在zabbix页面配置传给memcached[*]这个key。

都配置完后就可以添加重启一下zabbix-agent了。

$ service zabbix-agent restart

四、添加权限

需要给zabbix用户添加sudo权限。

$ cat /etc/sudoers.d/zabbix
Defaults:zabbix    !requiretty
zabbix ALL=(ALL) NOPASSWD: SUPERVISORCTLZB
Cmnd_Alias SUPERVISORCTLZB = /sbin/ss,/usr/sbin/ss,/sbin/dmidecode,/usr/sbin/dmidecode,/sbin/service,/usr/sbin/service,/bin/netstat

另外需要注意的是,普通用户zabbix默认环境变量有如下这些:

$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin

所以你要确认你所有的执行程序都在这些路径下,不然zabbix是获取不到值的。

使用zabbix用户执行看是否正常。

$ sudo -u zabbix `which zabbix_agentd` -t redis.discovery[*]
{
    "data":[
        {
            "{#MCPORT}":"11211"
        },
        {
            "{#MCPORT}":"11212"
        }
}

五、配置zabbix web

前期工作都做完了,下面就可以配置zabbix web了。

首先创建一个模板(Template Linux Memcached Discovery),然后在模板中创建一个自动发现规则(Linux Memcached Discovery)。

监控

在这个自动发现规则内创建一个item。

监控

然后可以创建trigger等。

下面我提供一个模板Github:https://github.com/dongwenpeng/zabbix

ubuntu 16.04安装配置zabbix3

zabbix是一个开源的网络和应用程序监控软件。提供了zabbix agent来监控远程主机,也支持通过SNMP,TCP和ICMP检查来监控主机。
监控

步骤1:安装Apache,MySQL和PHP

在安装zabbix前,需要安装一个web server,数据库服务器和PHP。在这一步骤中我们将安装这些服务,如果你已经安装有了,可以跳过此步。

$ sudo apt-get update
$ sudo apt-get install apache2 
$ sudo apt-get install mysql-server 
$ sudo apt-get install php5 php5-cli php5-common php5-mysql

在/etc/php5/apache2/php.ini更新时区,如下:

[Date]
; http://php.net/date.timezone
date.timezone = 'Asia/Shanghai'

步骤2:添加Apt仓库

$ wget http://repo.zabbix.com/zabbix/3.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_3.0-1+xenial_all.deb
$ sudo dpkg -i zabbix-release_3.0-1+xenial_all.deb
$ sudo apt-get update

步骤3: 安装zabbix server

安装zabbix apt仓库后,我们使用如下命令来安装zabbix使用mysql数据库。

$ sudo apt-get install zabbix-server-mysql zabbix-frontend-php

步骤4: 创建数据表

现在为zabbix server创建相关数据表。首先使用如下命令创建mysql数据库和用户

$ mysql -u root -p

mysql> CREATE DATABASE zabbixdb;
mysql> GRANT ALL on zabbixdb.* to zabbix@localhost IDENTIFIED BY 'password';
mysql> FLUSH PRIVILEGES;

现在导入数据表

$ cd /usr/share/doc/zabbix-server-mysql
$ zcat create.sql.gz | mysql -u root -p zabbixdb

步骤5: 编辑zabbix配置文件

打开/etc/zabbix/zabbix_server.conf,根据如下编辑配置文件:

  DBHost=localhost
  DBName=zabbixdb
  DBUser=zabbix
  DBPassword=password

步骤6: 重启apache和zabbix

zabbix在/etc/zabbix/apache.conf创建了自己的配置文件,使用如下命令重启apache

$ sudo service apache2 restart

zabbix server的配置文件在/etc/zabbix/zabbix_server.conf,使用如下命令重启zabbix server

$ sudo service zabbix-server restart

启动zabbix server之后,我们打开zabbix web安装器来完成安装。

步骤7: 启动zabbix web安装器

zabbix web安装器可以使用如下url打开:

http://svr1.example.net/zabbix/

zabbix配置的欢迎界面

这个是zabbix web安装器的欢迎界面。点击next按钮继续

监控

检查先决条件

检查你的系统是否已经安装有所需的安装包,所以一切正常,点击next继续
监控

配置db连接

键入在步骤4创建的数据库信息,点击Test Connection。如果数据库连接正确,将会显示ok信息,之后点击next继续
监控

zabbix server详情

监控

安装前概览

在这个步骤中将会显示之前设置的概览,所以点击next继续。
监控

安装zabbix

监控

zabbix登录界面

使用如下默认凭证登录zabbix

   Username:  admin
   Password:  zabbix

监控
登录成功后将会看到如下控制面板
监控
到此,zabbix server已经安装完成。

zabbix监控php-fpm状态

本文通过启用php-fpm的status页面,使用zabbix来定时获取此数据以达到监控php-fpm性能状态的目的。

安装

假设zabbix agent安装在了/zabbix-agent/目录

配置php-fpm

打开php-fpm的pool配置文件,删除pm.status=指令的注释:

pm.status_path = /php-fpm_status

如果你配置了多个pool,需要分别为它们配置不同的status_path。

配置nginx

把如下配置添加到nginx配置文件:

server {
    listen 10061;

    location /php-fpm_status {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

重启php-fpm和nginx后,尝试执行如下命令测试:

curl http://127.0.0.1:10061/php-fpm_status

添加User Parameters

创建/zabbix-agent/etc/zabbix_agentd.conf.d/php-fpm-params.conf文件,内容如下:

UserParameter=php-fpm[*],/usr/local/zabbix-agent-ops/bin/php-fpm-check.sh "$1" "$2"

创建/usr/local/zabbix-agent-ops/bin/php-fpm-check.sh,内容如下:

#!/bin/bash
##################################
# Zabbix monitoring script
#
# php-fpm:
#  - anything available via FPM status page
#
##################################
# Contact:
#  [email protected]
##################################
# ChangeLog:
#  20100922 VV  initial creation
##################################

# Zabbix requested parameter
ZBX_REQ_DATA="$1"
ZBX_REQ_DATA_URL="$2"

# Nginx defaults
NGINX_STATUS_DEFAULT_URL="http://localhost:80/php-fpm_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
#
RESULT=$(echo "$NGINX_STATS" | awk 'match($0, "^'"$ZBX_REQ_DATA"':[[:space:]]+(.*)", a) { print a[1] }')
if [ $? -ne 0 -o -z "$RESULT" ]; then
    echo $ERROR_WRONG_PARAM
    exit 1
fi

echo $RESULT

exit 0

导入模板

导入php-fpm-template.xml模板,并链接到主机。根据需要设置{$PHP_FPM_STATUS_URL}宏。
php-fpm-template.xml模板内容:

<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
    <version>2.0</version>
    <date>2013-03-26T04:12:09Z</date>
    <groups>
        <group>
            <name>Templates</name>
        </group>
    </groups>
    <templates>
        <template>
            <template>Template php-fpm</template>
            <name>Template php-fpm</name>
            <groups>
                <group>
                    <name>Templates</name>
                </group>
            </groups>
            <applications>
                <application>
                    <name>php-fpm</name>
                </application>
            </applications>
            <items>
                <item>
                    <name>$1</name>
                    <type>0</type>
                    <snmp_community/>
                    <multiplier>0</multiplier>
                    <snmp_oid/>
                    <key>php-fpm["total processes",{$PHP_FPM_STATUS_URL}]</key>
                    <delay>60</delay>
                    <history>90</history>
                    <trends>365</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <delta>0</delta>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privpassphrase/>
                    <formula>1</formula>
                    <delay_flex/>
                    <params/>
                    <ipmi_sensor/>
                    <data_type>0</data_type>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description/>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>php-fpm</name>
                        </application>
                    </applications>
                    <valuemap/>
                </item>
                <item>
                    <name>$1</name>
                    <type>0</type>
                    <snmp_community/>
                    <multiplier>0</multiplier>
                    <snmp_oid/>
                    <key>php-fpm["listen queue",{$PHP_FPM_STATUS_URL}]</key>
                    <delay>60</delay>
                    <history>90</history>
                    <trends>365</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <delta>0</delta>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privpassphrase/>
                    <formula>1</formula>
                    <delay_flex/>
                    <params/>
                    <ipmi_sensor/>
                    <data_type>0</data_type>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description/>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>php-fpm</name>
                        </application>
                    </applications>
                    <valuemap/>
                </item>
                <item>
                    <name>$1</name>
                    <type>0</type>
                    <snmp_community/>
                    <multiplier>0</multiplier>
                    <snmp_oid/>
                    <key>php-fpm["listen queue len",{$PHP_FPM_STATUS_URL}]</key>
                    <delay>60</delay>
                    <history>90</history>
                    <trends>365</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <delta>0</delta>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privpassphrase/>
                    <formula>1</formula>
                    <delay_flex/>
                    <params/>
                    <ipmi_sensor/>
                    <data_type>0</data_type>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description/>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>php-fpm</name>
                        </application>
                    </applications>
                    <valuemap/>
                </item>
                <item>
                    <name>$1</name>
                    <type>0</type>
                    <snmp_community/>
                    <multiplier>0</multiplier>
                    <snmp_oid/>
                    <key>php-fpm["active processes",{$PHP_FPM_STATUS_URL}]</key>
                    <delay>60</delay>
                    <history>90</history>
                    <trends>365</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <delta>0</delta>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privpassphrase/>
                    <formula>1</formula>
                    <delay_flex/>
                    <params/>
                    <ipmi_sensor/>
                    <data_type>0</data_type>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description/>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>php-fpm</name>
                        </application>
                    </applications>
                    <valuemap/>
                </item>
                <item>
                    <name>$1</name>
                    <type>0</type>
                    <snmp_community/>
                    <multiplier>0</multiplier>
                    <snmp_oid/>
                    <key>php-fpm["idle processes",{$PHP_FPM_STATUS_URL}]</key>
                    <delay>60</delay>
                    <history>90</history>
                    <trends>365</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <delta>0</delta>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privpassphrase/>
                    <formula>1</formula>
                    <delay_flex/>
                    <params/>
                    <ipmi_sensor/>
                    <data_type>0</data_type>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description/>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>php-fpm</name>
                        </application>
                    </applications>
                    <valuemap/>
                </item>
                <item>
                    <name>$1/sec</name>
                    <type>0</type>
                    <snmp_community/>
                    <multiplier>0</multiplier>
                    <snmp_oid/>
                    <key>php-fpm["accepted conn",{$PHP_FPM_STATUS_URL}]</key>
                    <delay>60</delay>
                    <history>90</history>
                    <trends>365</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <delta>1</delta>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privpassphrase/>
                    <formula>1</formula>
                    <delay_flex/>
                    <params/>
                    <ipmi_sensor/>
                    <data_type>0</data_type>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description/>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>php-fpm</name>
                        </application>
                    </applications>
                    <valuemap/>
                </item>
                <item>
                    <name>$1/sec</name>
                    <type>0</type>
                    <snmp_community/>
                    <multiplier>0</multiplier>
                    <snmp_oid/>
                    <key>php-fpm["slow requests",{$PHP_FPM_STATUS_URL}]</key>
                    <delay>60</delay>
                    <history>90</history>
                    <trends>365</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <delta>1</delta>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privpassphrase/>
                    <formula>1</formula>
                    <delay_flex/>
                    <params/>
                    <ipmi_sensor/>
                    <data_type>0</data_type>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description/>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>php-fpm</name>
                        </application>
                    </applications>
                    <valuemap/>
                </item>
            </items>
            <discovery_rules/>
            <macros>
                <macro>
                    <macro>{$PHP_FPM_STATUS_URL}</macro>
                    <value>http://127.0.0.1:10061/php-fpm_status</value>
                </macro>
            </macros>
            <templates/>
            <screens/>
        </template>
    </templates>
    <graphs>
        <graph>
            <name>php-fpm Accepted Connections / sec</name>
            <width>900</width>
            <height>200</height>
            <yaxismin>0.0000</yaxismin>
            <yaxismax>100.0000</yaxismax>
            <show_work_period>1</show_work_period>
            <show_triggers>1</show_triggers>
            <type>0</type>
            <show_legend>1</show_legend>
            <show_3d>0</show_3d>
            <percent_left>0.0000</percent_left>
            <percent_right>0.0000</percent_right>
            <ymin_type_1>0</ymin_type_1>
            <ymax_type_1>0</ymax_type_1>
            <ymin_item_1>0</ymin_item_1>
            <ymax_item_1>0</ymax_item_1>
            <graph_items>
                <graph_item>
                    <sortorder>0</sortorder>
                    <drawtype>0</drawtype>
                    <color>C80000</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template php-fpm</host>
                        <key>php-fpm["accepted conn",{$PHP_FPM_STATUS_URL}]</key>
                    </item>
                </graph_item>
            </graph_items>
        </graph>
        <graph>
            <name>php-fpm Listen Queue</name>
            <width>900</width>
            <height>200</height>
            <yaxismin>0.0000</yaxismin>
            <yaxismax>100.0000</yaxismax>
            <show_work_period>1</show_work_period>
            <show_triggers>1</show_triggers>
            <type>0</type>
            <show_legend>1</show_legend>
            <show_3d>0</show_3d>
            <percent_left>0.0000</percent_left>
            <percent_right>0.0000</percent_right>
            <ymin_type_1>0</ymin_type_1>
            <ymax_type_1>0</ymax_type_1>
            <ymin_item_1>0</ymin_item_1>
            <ymax_item_1>0</ymax_item_1>
            <graph_items>
                <graph_item>
                    <sortorder>0</sortorder>
                    <drawtype>0</drawtype>
                    <color>EE0000</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template php-fpm</host>
                        <key>php-fpm["listen queue len",{$PHP_FPM_STATUS_URL}]</key>
                    </item>
                </graph_item>
                <graph_item>
                    <sortorder>1</sortorder>
                    <drawtype>0</drawtype>
                    <color>00EE00</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template php-fpm</host>
                        <key>php-fpm["listen queue",{$PHP_FPM_STATUS_URL}]</key>
                    </item>
                </graph_item>
            </graph_items>
        </graph>
        <graph>
            <name>php-fpm Processes</name>
            <width>900</width>
            <height>200</height>
            <yaxismin>0.0000</yaxismin>
            <yaxismax>100.0000</yaxismax>
            <show_work_period>1</show_work_period>
            <show_triggers>1</show_triggers>
            <type>0</type>
            <show_legend>1</show_legend>
            <show_3d>0</show_3d>
            <percent_left>0.0000</percent_left>
            <percent_right>0.0000</percent_right>
            <ymin_type_1>0</ymin_type_1>
            <ymax_type_1>0</ymax_type_1>
            <ymin_item_1>0</ymin_item_1>
            <ymax_item_1>0</ymax_item_1>
            <graph_items>
                <graph_item>
                    <sortorder>0</sortorder>
                    <drawtype>0</drawtype>
                    <color>C80000</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template php-fpm</host>
                        <key>php-fpm["total processes",{$PHP_FPM_STATUS_URL}]</key>
                    </item>
                </graph_item>
                <graph_item>
                    <sortorder>1</sortorder>
                    <drawtype>0</drawtype>
                    <color>00C800</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template php-fpm</host>
                        <key>php-fpm["active processes",{$PHP_FPM_STATUS_URL}]</key>
                    </item>
                </graph_item>
                <graph_item>
                    <sortorder>2</sortorder>
                    <drawtype>0</drawtype>
                    <color>0000C8</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template php-fpm</host>
                        <key>php-fpm["idle processes",{$PHP_FPM_STATUS_URL}]</key>
                    </item>
                </graph_item>
            </graph_items>
        </graph>
        <graph>
            <name>php-fpm Slow Requests / sec</name>
            <width>900</width>
            <height>200</height>
            <yaxismin>0.0000</yaxismin>
            <yaxismax>100.0000</yaxismax>
            <show_work_period>1</show_work_period>
            <show_triggers>1</show_triggers>
            <type>0</type>
            <show_legend>1</show_legend>
            <show_3d>0</show_3d>
            <percent_left>0.0000</percent_left>
            <percent_right>0.0000</percent_right>
            <ymin_type_1>0</ymin_type_1>
            <ymax_type_1>0</ymax_type_1>
            <ymin_item_1>0</ymin_item_1>
            <ymax_item_1>0</ymax_item_1>
            <graph_items>
                <graph_item>
                    <sortorder>0</sortorder>
                    <drawtype>0</drawtype>
                    <color>C80000</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template php-fpm</host>
                        <key>php-fpm["slow requests",{$PHP_FPM_STATUS_URL}]</key>
                    </item>
                </graph_item>
            </graph_items>
        </graph>
    </graphs>
</zabbix_export>

Zabbix3监控Nginx性能状态

本文主要介绍使用zabbix来监控nginx的性能状态,包括当前活动的连接数,已经收到的请求数,已经处理的请求数等。

编辑nginx配置文件

vim /etc/nginx/nginx.conf
server {
        listen  8082;
        location /nginx_status {
                stub_status on;
                access_log   off;
        }
}

重启nginx

/etc/init.d/nginx restart

创建监控脚本

mkdir /etc/zabbix/scripts/
chown zabbix:zabbix -R /etc/zabbix/scripts/
chmod 750 /etc/zabbix/scripts/
vim /etc/zabbix/scripts/nginx-stats.sh
#!/bin/bash
##### OPTIONS VERIFICATION #####
if [[ -z "$1" || -z "$2" || -z "$3" ]]; then
  exit 1
fi
##### PARAMETERS #####
RESERVED="$1"
METRIC="$2"
STATSURL="$3"
CURL="/usr/bin/curl"
CACHE_TTL="55"
CACHE_FILE="/tmp/zabbix.nginx.`echo $STATSURL | md5sum | cut -d" " -f1`.cache"
EXEC_TIMEOUT="1"
NOW_TIME=`date '+%s'`
##### RUN #####
if [ -s "${CACHE_FILE}" ]; then
  CACHE_TIME=`stat -c"%Y" "${CACHE_FILE}"`
else
  CACHE_TIME=0
fi
DELTA_TIME=$((${NOW_TIME} - ${CACHE_TIME}))
#
if [ ${DELTA_TIME} -lt ${EXEC_TIMEOUT} ]; then
  sleep $((${EXEC_TIMEOUT} - ${DELTA_TIME}))
elif [ ${DELTA_TIME} -gt ${CACHE_TTL} ]; then
  echo "" >> "${CACHE_FILE}" # !!!
  DATACACHE=`${CURL} --insecure -s "${STATSURL}" 2>&1`
  echo "${DATACACHE}" > "${CACHE_FILE}" # !!!
  chmod 640 "${CACHE_FILE}"
fi
#
if [ "${METRIC}" = "active" ]; then
  cat "${CACHE_FILE}" | grep "Active connections" | cut -d':' -f2
fi
if [ "${METRIC}" = "accepts" ]; then
  cat "${CACHE_FILE}" | sed -n '3p' | cut -d" " -f2
fi
if [ "${METRIC}" = "handled" ]; then
  cat "${CACHE_FILE}" | sed -n '3p' | cut -d" " -f3
fi
if [ "${METRIC}" = "requests" ]; then
  cat "${CACHE_FILE}" | sed -n '3p' | cut -d" " -f4
fi
if [ "${METRIC}" = "reading" ]; then
  cat "${CACHE_FILE}" | grep "Reading" | cut -d':' -f2 | cut -d' ' -f2
fi
if [ "${METRIC}" = "writing" ]; then
  cat "${CACHE_FILE}" | grep "Writing" | cut -d':' -f3 | cut -d' ' -f2
fi
if [ "${METRIC}" = "waiting" ]; then
  cat "${CACHE_FILE}" | grep "Waiting" | cut -d':' -f4 | cut -d' ' -f2
fi
#
exit 0

增加执行权限

chown zabbix:zabbix /etc/zabbix/scripts/nginx-stats.sh
chmod 540 /etc/zabbix/scripts/nginx-stats.sh

测试脚本

sudo -u zabbix /etc/zabbix/scripts/nginx-stats.sh none active http://192.168.42.70:8082/nginx_status

到zabbix agent添加nginx监控

vim /etc/zabbix/zabbix_agentd.conf
UserParameter=nginx[*],/etc/zabbix/scripts/nginx-stats.sh "none" "$1" "$2"

重启zabbix agent

/etc/init.d/zabbix-agentd restart

测试监控

zabbix_get -s HOST -k "nginx[active,http://192.168.42.70:8082/nginx_status]"

添加监控项

在zabbix web中依次添加监控项,如添加active

nginx[active,http://192.168.42.70:8082/nginx_status]

完成后依次添加accepts,handled,requests,reading,writing,waiting的监控

Zabbix3.0监控Apache2.4服务器状态

我们需要启动mod_status来监控apache,执行如下命令:

a2enmod status

然后找到status.conf来配置mod_status,如果使用的是Debian发行版本,status.conf文件应该在/etc/apache2/mods-enabled/status.conf,配置如下:

<IfModule mod_status.c>
#
# Allow server status reports generated by mod_status,
# with the URL of http://servername/server-status
# Uncomment and change the ".example.com" to allow
# access from other hosts.
#
ExtendedStatus On
<Location /server-status>
    SetHandler server-status
    Order deny,allow
    Deny from all
    Allow from 127.0.0.1
</Location>

Listen 61709
NameVirtualHost 127.0.0.1:61709
<VirtualHost 127.0.0.1:61709>
</VirtualHost>
</IfModule>

然后重启apache

/etc/init.d/apache2 restart

并添加如下行到zabbix的/etc/zabbix/zabbix_agentd.conf:

# Apache Keys
UserParameter=apache.status[*],wget -q -O /dev/null "http://localhost:61709/server-status?auto" && wget -q -O - "http://localhost:61709/server-status?auto" | awk '/$1: / {print $NF}'

现在重启zabbix agent。

/etc/init.d/zabbix-agent restart

之后就可以在zabbix看到apache的监控数据了。

CentOS 7安装Zabbix3.0

Zabbix是开源的监控服务器性能的工具。

使用它可以搭建一个“监控性能服务器”,专门监控其它服务器的性能。

Zabbix使用MySQL/MariaDB/Oracle/IBM DB2存储数据,使用PHP做web接口。

官网上的介绍:

Zabbix is the ultimate enterprise-level software designed for real-time monitoring of millions of metrics collected from tens of thousands of servers, virtual machines and network devices.

Zabbix is Open Source and comes at no cost.

Zabbix最新发布了3.0.1版本。

下面记录了在CentOS 7上安装Zabbix的步骤,应该适用于其它基于RHEL的Linux发行版。

我的系统环境:

  • CentOS 7
  • hostname:zabbix.topspeedsnail.com
  • IP:159.203.205.23
    在安装之前,update系统:
# yum update

添加Zabbix仓库:

# yum install epel-release
# rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm

安装Zabbix Server,MariaDB数据库, Web Server和PHP:

# yum install zabbix-server-mysql zabbix-web-mysql mysql mariadb-server httpd php

启动MariaDB数据库:

# systemctl start mariadb
# systemctl enable mariadb

运行MySQL安全设置脚本(mysql_secure_installation):

# mysql_secure_installation

设置MySQL root密码和其他安全参数。

现在创建一个名为zabbix_db的数据库和一个新用户my_zabbix:

# mysql -u root -p
MariaDB [(none)]> create database zabbix_db;

MariaDB [(none)]> grant all privileges on zabbix_db.* to my_zabbix@localhost identified by 'your_password';

MariaDB [(none)]> flush privileges;

MariaDB [(none)]> exit

替换上面的your_password为你要设置的密码。
监控

把数据导入到zabbix_db数据库:

# cd /usr/share/doc/zabbix-server-mysql-3.0.1

监控

# gunzip create.sql.gz
# mysql -u root -p zabbix_db < create.sql

编辑Zabbix配置文件:

它的配置文件位于/etc/zabbix/zabbix_server.conf,配置数据库:

# vim /etc/zabbix/zabbix_server.conf

修改为如下参数:


DBHost=localhost DBName=zabbix_db DBUser=my_zabbix DBPassword=test1234

把test1234替换为你的my_zabbix用户密码。

监控

监控

编辑PHP配置文件:

# vim /etc/php.ini

修改为如下参数:

max_execution_time = 600
max_input_time = 600
memory_limit = 256M
post_max_size = 32M
upload_max_filesize = 16M
date.timezone = Asia/Hong_Kong

配置防火墙:

# firewall-cmd --permanent --add-port=10050/tcp
# firewall-cmd --permanent --add-port=10051/tcp
# firewall-cmd --permanent --add-port=80/tcp

# firewall-cmd --reload 
# systemctl restart firewalld

如果你的系统没有安装firewalld,安装:

# yum install firewalld
# systemctl start firewalld

当然,你也可以使用iptables。

设置Selinux规则:

# setsebool -P httpd_can_connect_zabbix=1

启动各种服务程序:

# systemctl start zabbix-server
# systemctl enable zabbix-server

# systemctl start httpd
# systemctl enable httpd

使用浏览器访问Zabbix Web接口:

http://your_doman_or_IP/zabbix/

开始安装向导:

监控

检查需要的组件是否安装:

监控

配置数据库连接:

监控

配置Zabbix:

监控

Next:

监控

安装完成:

监控
点击Finish它会自动转向到登录界面;登录,默认用户名”admin”,密码”zabbix”:

监控

zabbix的主界面:

监控

安装完成之后Zabbix服务器并没有监控任何服务器;我们来添加一个服务器节点,让Zabbix服务器进行监控。

假设我想监控安装有Ubuntu 16.04的服务器,我需要在Ubuntu上安装zabbix客户端:

$ sudo apt-get install zabbix-agent

编辑zabbix-agent的配置文件,添加Zabbix服务器IP:

$ sudo vim /etc/zabbix/zabbix_agentd.conf

更改为:

Server=159.203.205.23
ServerActive=159.203.205.23
Hostname=zabbix.topspeedsnail.com

替换你Zabbix服务器的IP。

启动zabbix-agent:

$ sudo systemctl start zabbix-agent
$ sudo systemctl enable zabbix-agent

在Zabbix web接口中添加新节点:

Configuration —> Hosts –> Create Host:

监控

填写节点的信息。选择Templates:

监控

Monitoring —> Triggers,查看监控的节点。