前言:node
一、線上業務使用了幾十上百臺haproxy服務,須要針對這些服務作監控(包含haproxy端口對應的TCP鏈接數監控,haproxy服務監控等)。mysql
二、普通的在zabbix_agentd.conf裏面配置"UserParameter"的方式太繁瑣,這裏使用建表(收集數據,彙總數據)的方式。sql
三、將數據彙總到表裏面,經過第三方腳本對數據作一個處理,拿處處理結果並監控。json
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 #這裏寫的是項目名稱(MM項目) 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"vim
<?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";
腳本生成的數據:app
[root@localhost 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格式以下:tcp
{ "data":[ { "{#APP}":"MM", "{#SERVER}":"haproxy", "{#DIVISION}":"haproxy-login", "{#PORT}":"30000", "{#PATH}":"/data/app/haproxy-login/sbin" } ] }
一、配置:zabbix_agentd.conffetch
#用來監控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
建立模板-->自動發現規則3d
定義宏變量:宏的值爲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服務添加到監控的流程以下
一、在zabbixDB上的t_3party_server表裏添加數據: INSERT INTO `zabbix`.`t_3party_server` (`app`, `server`, `division`, `node`, `port`, `remark`) VALUES ('MM', 'haproxy', 'haproxy', '192.168.32.128', '30000', '0'); 二、在機器上添加配置參數: 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}' 三、配置完畢後重啓 zabbix_agentd pkill zabbix_agentd /data/app/zabbix_agent/sbin/zabbix_agentd 四、到zabbix上監控haproxy服務是否被監控