概述
前言:
1、线上业务使用了几十上百台haproxy服务,需要针对这些服务做监控(包含haproxy端口对应的TCP连接数监控,haproxy服务监控等)。
2、普通的在zabbix_agentd.conf里面配置"UserParameter"的方式太繁琐,这里使用建表(收集数据,汇总数据)的方式。
3、将数据汇总到表里面,通过第三方脚本对数据做一个处理,拿到处理结果并监控。
一、创建数据表(收集haproxy服务的信息)
1.1、在zabbix_DB上创建表:t_3party_server
create table t_3party_server(
app varchar(64) NOT NULL,
server varchar(64) NOT NULL,
division varchar(64) NOT NULL,
node varchar(64) NOT NULL,
port varchar(64) NOT NULL,
remark int(10) unsigned NOT NULL,
PRIMARY KEY (app,server,division,node,port)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
注释:
app #这里写的是项目名称(MOBA项目)
server #服务名称(haproxy服务)
division #由于起了多个haproxy实例,名称也不一样,所以这里做了区分
node #haproxy服务IP地址
port #haproxy服务端口,多个实例端口不同
remark #备注作用(由于线上某些haproxy服务安装路径不一致,所以此处用来区分路径,也可以区分其他)
PRIMARY KEY (app,server,division,node,port) #添加主键
1.2、创建完毕后效果
mysql> desc t_3party_server;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| app | varchar(64) | NO | PRI | NULL | |
| server | varchar(64) | NO | PRI | NULL | |
| division | varchar(64) | NO | PRI | NULL | |
| node | varchar(64) | NO | PRI | NULL | |
| port | varchar(64) | NO | PRI | NULL | |
| remark | text | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
1.3、然后往数据表里面插入数据:
1.3.1、在Navicat上添加
insert into t_3party_server(app,server,division,node,port,status) values('MOBA','haproxy','haproxy-login','10.117.16.242','30021','0');
insert into t_3party_server(app,server,division,node,port,status) values('MOBA','haproxy','haproxy-match','10.117.16.242','30032','0');
insert into t_3party_server(app,server,division,node,port,status) values('MOBA','haproxy','haproxy-test','10.117.16.242','30035','0');
1.3.2、在MySQL数据库客户端上添加:
INSERT INTO `zabbix`.`t_3party_server` (`app`, `server`, `division`, `node`, `port`, `remark`) VALUES ('MOBA', 'haproxy', 'haproxy-test', '10.117.16.242', '30035', '0');
1.4、最终数据生成图
二、针对生成的数据表做监控
2.1、监控脚本:放在zabbix_server下 "/install_path/zabbix_server/share/zabbix/externalscripts"
#!/usr/bin/php
<?php
if ($_SERVER["argc"] != 2) {
echo "Usage: " . $_SERVER["argv"][0] . " hostn";
return;
}
$host = $_SERVER["argv"][1];
$mysqlconn = new mysqli("10.116.207.188", "moba", "moba2016", "zabbix", 3306);
if (mysqli_connect_errno()) {
echo "fail to connect mysql: " . mysqli_connect_error() . "n";
return;
}
$result = $mysqlconn->query("SELECT * FROM t_3party_server WHERE node="" . $mysqlconn->real_escape_string($host) . """);
$hostvalue = array(
"data" => array()
);
while ($row = $result->fetch_assoc()) {
$app = $row["app"];
$server = $row["server"];
$division = $row["division"];
$node = $row["node"];
$port = $row["port"];
$remark = $row["remark"];
if ($remark == "/usr/local")
{
$hostvalue["data"][] = array(
"{#APP}" => $app,
"{#SERVER}" => $server,
"{#DIVISION}" => $division,
"{#PORT}" => $port,
"{#PATH}" => "/usr/local/" . $division . "/sbin",
);
continue;
}
$server = substr($server, 0, 15);
$hostvalue["data"][] = array(
"{#APP}" => $app,
"{#SERVER}" => $server,
"{#DIVISION}" => $division,
"{#PORT}" => $port,
"{#PATH}" => "/data/app/" . $division . "/sbin",
#"{#MFW_PATH}" => "/data/app/" . $division . "/" . ($division ? $division . "/" : "") . $server,
);
}
echo json_encode($hostvalue);
echo "n";
2.1.1、脚本生成的数据:
./mfw_discovery_haproxy.php 10.117.16.222
{"data":[{"{#APP}":"MOBA","{#SERVER}":"haproxy","{#DIVISION}":"haproxy-login","{#PORT}":"30021","{#PATH}":"/data/app/haproxy-login/sbin"}]}
2.1.2、转换为Json格式如下:
{
"data":[
{
"{#APP}":"MOBA",
"{#SERVER}":"haproxy",
"{#DIVISION}":"haproxy-login",
"{#PORT}":"30021",
"{#PATH}":"/data/app/haproxy-login/sbin"
}
]
}
2.2、在haproxy服务机器上配置
1、配置:zabbix_agentd.conf
vim /data/app/zabbix_agent/etc/zabbix_agentd.conf
LogRemoteCommands=1
LogFileSize=1
DebugLevel=3
#Haproxy #用来监控TCP连接数
UserParameter=haproxy.tcp.[*],/usr/sbin/ss -ant sport eq :"$1" | awk 'NR>1{++s[$$1]} END {for(k in s){print k,s[k]}}' |grep -w "ESTAB" |awk '{print $$2}'
2、配置:haproxy服务守护脚本 #此处可不配(根据业务选择)
启动脚本:
#!/bin/bash
old_path=`pwd`
cd `dirname $0`
path=`pwd`
server=sbin/haproxy
if [ -n "`ps -ef|grep "$path/$server"|grep -v "grep"`" ]
then
echo -e "