目录
- 创建数据表(收集haproxy服务的信息)
- 针对生成的数据表做监控
- 在haproxy服务机器上配置
- 在zabbix上添加监控
前言:
1、线上业务使用了几十上百台haproxy服务,需要针对这些服务做监控(包含haproxy端口对应的TCP连接数监控,haproxy服务监控等)。
2、普通的在zabbix_agentd.conf里面配置"UserParameter"的方式太繁琐,这里使用建表(收集数据,汇总数据)的方式。
3、将数据汇总到表里面,通过第三方脚本对数据做一个处理,拿到处理结果并监控。
创建数据表(收集haproxy服务的信息)
-
在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)? #添加主键
-
执行完毕之后查看表结构
-
然后往数据表里面插入数据:
insert into t_3party_server(app,server,division,node,port,remark) values('MM','haproxy','haproxy-login','192.168.32.128','30000','0');
-
最终表格效果
针对生成的数据表做监控
监控脚本:放在zabbix_server下 "/install_path/zabbix_server/share/zabbix/externalscripts"
<?php
if ($_SERVER["argc"] != 2) {
echo "Usage: " . $_SERVER["argv"][0] . " host\n";
return;
}
$host = $_SERVER["argv"][1];
$mysqlconn = new mysqli("localhost", "root", "123", "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";
脚本生成的数据:
[[email protected] externalscripts]# ./haproxy_discovery.php 192.168.32.128
{"data":[{"{#APP}":"MM","{#SERVER}":"haproxy","{#DIVISION}":"haproxy-login","{#PORT}":"30000","{#PATH}":"\/data\/app\/haproxy-login\/sbin"}]}
转换的json格式如下:
{
"data":[
{
"{#APP}":"MM",
"{#SERVER}":"haproxy",
"{#DIVISION}":"haproxy-login",
"{#PORT}":"30000",
"{#PATH}":"/data/app/haproxy-login/sbin"
}
]
}
在haproxy服务机器上配置
1、配置:zabbix_agentd.conf
#用来监控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}'
配置完成后重启zabbix_agentd
在zabbix上添加监控
创建模板-->自动发现规则
定义宏变量:宏的值为json导出后的值
Item1:检查haproxy进程是否存在
Key:proc.num["{#SERVER}",,,"{#PATH}"]
Trigger1:对应Item1
{customize template:proc.num["{#SERVER}",,,"{#PATH}"].last()}<1
Item2:检查haproxy端口对应的TCP连接数
Key:haproxy.tcp.[{#PORT}]
Trigger2:对应Item2
{Template moba server:haproxy.tcp.[{#PORT}].last()}>40000
之后新增haproxy服务添加到监控的流程如下
1、在zabbixDB上的t_3party_server表里添加数据:
INSERT INTO `zabbix`.`t_3party_server` (`app`, `server`, `division`, `node`, `port`, `remark`) VALUES ('MOBA', 'haproxy', 'haproxy', '10.111.152.15', '30032', '0');
2、在机器上添加配置参数:
vim /data/app/zabbix_agent/etc/zabbix_agentd.conf
#Haproxy
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}'
3、配置完毕后重启 zabbix_agentd
pkill zabbix_agentd
/data/app/zabbix_agent/sbin/zabbix_agentd
4、到zabbix上监控haproxy服务是否被监控
原文地址:https://www.cnblogs.com/qinyujie/p/11470232.html
时间: 2024-10-05 05:22:38