分类目录归档:监控

Zabbix历史数据处理


2022年3月03日 11:19:55   2,280 次浏览

问题描述

zabbix server 平稳运行有一段时间了,但是最近问题却来了,今天早上收到zabbixserver磁盘空间不足的告警。通过查看之后发现是大部分数据是zabbix 库的的数据 在进一步查看发现是history表和history_uint数据太多导致磁盘占用过多。

问题分析

history_uint

该表存储的是监控项的无符号整型的数据。
该数据的保存时长,取决于在监控项设置的 历史数据保留时长。

history

这个表保存的是浮点型的。
像 history_str 等保存的是 字符型数据。这些都是我们在设置监控项的对应的信息类型决定的。
该数据的保存时长,取决于在监控项设置的 历史数据保留时长

针对这个问题,我打算删除 history_uint 和 history 的一些历史数据。
要删除history_uint里的数据,还需要注意一点,由于数据量比较多,我建议可以分多次少量数据进行删除,因为我一次删除90天的时候CPU已经吃不消了…
这样可以避免一次性删除数据过多导致数据库的负载比较大。(或者可以使用limit 10000)

处理过程

我这里需要删除90天以前的数据下面是我的操作过程

获取时间戳

#通过如下命令进行获取90天以前的时间戳

[root@zabbix-server ~]# date -d $(date -d "-90 day" +%Y%m%d) +%s 1590105600

登录数据库操作

[root@zabbix-server ~]# mysql -uzabbix -p 
Enter password: mysql> use zabbix; 
Database changed 

#delete history_uint 
mysql> delete from history_uint where clock < 1590105600 LIMIT 10000; 
Query OK, 1653 rows affected (1 min 45.42 sec) 

#delete history 
mysql> delete from history where clock < 1590105600 LIMIT 10000; 
Query OK, 0 rows affected (24.72 sec)

释放空间

上面执行删除后,数据的存储空间是没有减少的,因为对于delete from table_name where xxx 带条件的删除,不管是innodb还是MyISAM都不会释放空间,需要进行OPTIMIZE TABLE操作,进行释放空间。

注意:在optimize table ‘表名’ 运行过程中,MySQL会进行锁表。
optimize table history_uin

mysql>  optimize table history_uint;
+---------------------+----------+----------+-------------------------------------------------------------------+
| Table               | Op       | Msg_type | Msg_text                                                          |
+---------------------+----------+----------+-------------------------------------------------------------------+
| zabbix.history_uint | optimize | note     | Table does not support optimize, doing recreate + analyze instead |
| zabbix.history_uint | optimize | status   | OK                                                                |
+---------------------+----------+----------+-------------------------------------------------------------------+
2 rows in set (5 min 33.76 sec)

optimize table history

mysql>  optimize table history;
+----------------+----------+----------+-------------------------------------------------------------------+
| Table          | Op       | Msg_type | Msg_text                                                          |
+----------------+----------+----------+-------------------------------------------------------------------+
| zabbix.history | optimize | note     | Table does not support optimize, doing recreate + analyze instead |
| zabbix.history | optimize | status   | OK                                                                |
+----------------+----------+----------+-------------------------------------------------------------------+
2 rows in set (1 min 39.51 sec)

问题解决

待以上步骤都完成以后,检查磁盘可以看到问题解决 。
不过想要一劳永益的话的话 还是需要写一个脚本来处理这个问题

#!/bin/bash
User="zabbix"
Passwd="zabbix"
Date=`date -d $(date -d "-90 day" +%Y%m%d) +%s`
$(which mysql) -u${User} -p${Passwd} -e "
use zabbix;
DELETE FROM history WHERE 'clock' < '$Date' LIMIT 10000;
optimize table history;
DELETE FROM history_str WHERE 'clock' < '$Date' LIMIT 10000;
optimize table history_str;
DELETE FROM history_uint WHERE 'clock' < '$Date' LIMIT 10000;
optimize table history_uint;
DELETE FROM history_text WHERE 'clock' < $Date' LIMIT 10000;
optimize table history_text;
DELETE FROM  trends WHERE 'clock' < '$Date' LIMIT 10000;
optimize table  trends;
DELETE FROM trends_uint WHERE 'clock' < '$Date' LIMIT 10000;
optimize table trends_uint;
DELETE FROM events WHERE 'clock' < '$Date' LIMIT 10000;
optimize table events;
"

 

另外历史数据过多是由于我们保存的历史数据的时间所致,我们可以根据需求设置历史数据的保留时长,例如一些相对不太重要的数据,我们可以将该值设置的更短一些,这样数据量也就随着减少了。

Zabbix使用Python检查Haproxy状态页面


2020年11月26日 13:01:59   1,849 次浏览

概述

网上有很多使用zabbix监控haproxy的脚本,但大多数都使用的socket方式,而haproxy的stats页面页面我们经常需要访问的,所以我们这次使用python来抓取haproxy的stats页面。

haproxy的stats页面分析

<tr class="active0"><td class=ac><a name="app_push/push496">

当backend正常时,会显示绿色.

<tr class="active4"><td class=ac><a name="app_push/push096">

中间会有黄色的情况,backend反复故障恢复时会产生。 Python脚本抓取

#!/usr/bin/env python
#coding=utf-8
#Debug in Python2.7
import urllib2
import sys
import re
url = sys.argv[1]
#url = 'http://10.100.18.78:8888/status'
try:
    response = urllib2.urlopen(url,timeout=5).read();
except:
    print 'error to connect haproxy.'
    sys.exit(0)
pattern = re.compile('<tr class="active0"><td class=ac><a name="(.*?)"></a>')
items = re.findall(pattern, response)
data = []
for item in items:
    #print item
    data.append(item)
if len(data):
    print data
else:
    print 'ok'

当haproxy有backend故障时,会打印故障服务器,没有故障时显示OK,服务器无法连接,显示’error to connect haproxy.’ 故障显示如下

['app_push/push496', 'app_push/push092']

Zabbix中添加监控项 需要zabbix客户端自定义一个key来关联检查脚本。然后zabbix服务器端设置模板。这里我是用的字符串匹配。

#!/usr/bin/env python
from dingding import message
import requests
import sys
import re
import time
while True:
    time.sleep(30)
    url = 'http://10.28.xx.xx:65010/haproxy'
    try:
        response = requests.get(url=url, timeout=5).text

    except:
        print('error to connect haproxy.')
        sys.exit(0)
    pattern = re.compile('<tr class="active0"><td class=ac><a name="(.*?)"></a>')
    items = re.findall(pattern, response)
    data = []

    for item in items:
        #print item

        data.append(item)
    if len(data):
        print(data)
        message(text=data)
    else:
        print('ok')
dingding.py

#!/usr/bin/env python
#-*- coding: utf-8 -*-

'''
@Author: 风哥
@Email:  gujiwork@outlook.com
@Create Time: 2019/9/4
'''

import json
import requests


def message(text):
    # 告警通知
    headers = {
        'Content-Type': 'application/json;charset=utf-8',
    }

    alarm_user = 'phone1,phone2'
    notice_url = 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

    at_user = (alarm_user).split(',')
    json_text = {
        "msgtype": "text",
        "at": {
            "atMobiles":
                at_user,
            "isAtAll": False  # 为True表示@所有人
        },

        "text": {
            "content": (text)

        }
    }
    try:

        notice = requests.post(notice_url, json.dumps(json_text), headers=headers).content
        print(json.loads(notice))

    except BaseException as e:
        at_user = []
        print(e)