參考zabbix官方中文文檔 https://www.zabbix.com/documentation/3.4/zh/manual/config/items
知識點
1、觸發器表達式
表達式語法規則 {<服務器>:<鍵> <函數>(<參數>)} <操作> <常數>
1個小時內cpu負載最小值小於0.2
test:system.cpu.load[percpu,avg1].min(1h)}<0.2
cpu負載最近10個返回值,最小值小於0.2
test:system.cpu.load[percpu,avg1].min(#10)}<0.2
目前,觸發器所支持的函數有
https://www.zabbix.com/documentation/3.4/zh/manual/appendix/triggers/functions
avg 平均值 count 數據統計 change 返回最后的一個值和倒數第二個值對比 date 時間 dayofweek 本周的第幾天 delta 指定時間范圍內和數值內的 diff 文件是否有改變 iregexp 忽略字符大小寫,正則表達式 regexp 檢查最后一次采樣的數據是否能夠被制定的模式所匹配, 1表示匹配,0表示不匹配 主要用來監控日志內容的關鍵字 last 最近幾次采樣 max 最近采樣最大值 min nodata (對於采集不到數據)沒有數據判斷異常 now 返回自unix元年只此刻經歷的秒數 prev:倒數第二個采樣值; str 從最后一個采樣中,查找此次指定的字符,1找到0沒找到 sum parameter 函數參數:大多數數值函數可以接受秒數為其參數, 而如果在數值參數之前使用”#“做為前綴,則表示為最近幾次的取值, 如sum(300)表示300秒內所有取值之和,而sum(#10)則表示最近 10次取值之和; 此外,avg、count、last、min和max 還支持使用第二個參數, 用於完成時間限定;例如:max(1h,7d)將返回一周之前的最大值;
微服務告警監控
以下用fillorder服務為例
fillorder服務本機IP:10.200.130.96 zabbixz server IP:10.200.130.17
zabbix_agentd端配置
[appdeploy@fillorder1 etc]$ grep -v ^# /usr/local/zabbix_agentd/etc/zabbix_agentd.conf LogFile=/tmp/zabbix_agentd.log Server=10.200.130.17 ServerActive=10.200.130.17 Hostname=fillorder1 Timeout=30 User=appdeploy UserParameter=fezs_para[*],/usr/local/zabbix_agentd/scripts/zabbix_para.sh $1 $2
zabbix_para.sh
[appdeploy@fillorder1 ~]$ cat /usr/local/zabbix_agentd/scripts/zabbix_para.sh #!/bin/bash type="$1" para2="$2" function accessnum() { logs_path="${para2}" cd "${logs_path}" today=`date +"%Y-%m-%d"` yesday=`date +"%Y-%m-%d" -d yesterday` cur_min=`date +%H%M` if [ ${cur_min} = "0000" ] ;then log_file="access.${yesday}.log" else log_file="access.${today}.log" fi time=`date +"%Y-%m-%d %H:%M" -d "-1 minutes"` if ! [ -f "${log_file}" ] ;then echo "0" else cat ${log_file}|grep "^${time}"|grep -v HEAD|wc -l fi } function gettcpnum() { ss -antp|grep ESTAB|wc -l } function errorlognum() { logs_dir="${para2}" log_files=WarnLog.`date +"%Y-%m-%d"`.*.log ls ${logs_dir}/${log_files} > /dev/null 2>&1 [ $? -gt 0 ] && echo "0" && exit 0 t1=`date -d "1 minutes ago" +"%Y-%m-%d %H:%M"` grep -c -iE "^${t1}.*ERROR.*Exception" ${logs_dir}/${log_files} } function getcpu() { jcname=${para2} pid=`ps -ef|grep java|grep ${jcname}|awk '{print $2}'` top -c -b -n 1|grep ${pid}|grep -v grep|awk '{print $9}' } function getmem() { jcname=${para2} pid=`ps -ef|grep java|grep ${jcname}|awk '{print $2}'` mem=`cat /proc/${pid}/status|grep VmRSS|awk '{print $2}'` mem=$[mem*1024] echo $mem } function accesserrnum() { logs_path="${para2}" cd "${logs_path}" today=`date +"%Y-%m-%d"` yesday=`date +"%Y-%m-%d" -d yesterday` cur_min=`date +%H%M` if [ ${cur_min} = "0000" ] ;then log_file="access.${yesday}.log" else log_file="access.${today}.log" fi time=`date +"%Y-%m-%d %H:%M" -d "-1 minutes"` if ! [ -f "${log_file}" ] ;then echo "0" else cat ${log_file}|grep "^${time}"|grep -v HEAD|awk '$10>=400'|wc -l fi } case $type in accessnum) accessnum;; gettcpnum) gettcpnum;; getcpu) getcpu;; getmem) getmem;; errorlognum) errorlognum;; accesserrnum) accesserrnum;; *) esac
一、配置--創建主機
二、模板
完成
查看之前已經配置好的模板
應用集 5個
先不用看,在監控項里面可以創建
三、監控項 5個
1、accessnum_fillorder 監控項
鍵值 fezs_para[accessnum,/app/fillorder/logs]
2、access_error_resp_fillorder 監控項 監控acces接口訪問日志
鍵值 logrt["/app/fillorder/logs/access.20[0-9][0-9]\-[0-9][0-9]\-[0-9][0-9].log"," ([45][0-9][0-9]) [-0-9]+ [0-9]+$",,50,skip,]
3、getcpu-fillorder 監控項
鍵值 fezs_para[getcpu,fillorder]
4、getmem-fillorder 監控項
鍵值 fezs_para[getmem,fillorder]
5、port_5112 監控項 監控5112端口
鍵值 net.tcp.listen[5112]
觸發器 3個
1、fillorder is down
表達式 {Micro-fillorder:net.tcp.listen[5112].max(3)}=0 Micro-fillorder 主機名或模板名(模板套用到主機上) net.tcp.listen[5112] 鍵值 max(3) 3秒函數范圍 0 預設值 注釋:3秒內采集到的最大值如果等於0則進入PROBLEM狀態;
2、fillorder 服務出現接口調用異常
表達式 {Micro-fillorder:logrt["/app/fillorder/logs/access.20[0-9][0-9]\-[0-9][0-9]\-[0-9][0-9].log"," ([45][0-9][0-9]) [-0-9]+ [0-9]+$",,50,skip,].strlen()}>0 and {Micro-fillorder:logrt["/app/fillorder/logs/access.20[0-9][0-9]\-[0-9][0-9]\-[0-9][0-9].log"," ([45][0-9][0-9]) [-0-9]+ [0-9]+$",,50,skip,].nodata(60)}=0 說明: and 兩個是1時 等於1 or 其中一個1是1 等於1 strlen() 最新值的字符長度 strlen()(等同於 strlen(#1)) → 最新值的長度 strlen(#3) → 最新的第三個值的長度 nodata() 返回值:1 - 指定評估期沒有接收到數據 0 - 其它
3、fillorder 進程CPU超過90%
表達式 {Micro-fillorder:fezs_para[getcpu,fillorder].min(#2)}>90 min(#2) 返回最小值,取倒數第二個