Zabbix監控服務

 

 

第五章 Zabbix 監控基礎架構

zabbix-agent(數據採集)—>zabbix-server(數據分析|報警)—> 數據庫(數據存儲)<—zabbix web(數據展現)php

 

第六章 zabbix 快速監控主機

1.安裝zabbix-agent

[root@web01 ~]# rpm -ivh https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.11-1.el7.x86_64.rpm

2.配置zabbix-agent

[root@web01 ~]# grep "^[a-Z]" /etc/zabbix/zabbix_agentd.conf PidFile=/var/run/zabbix/zabbix_agentd.pid LogFile=/var/log/zabbix/zabbix_agentd.log LogFileSize=0 Server=10.0.1.61 ServerActive=127.0.0.1 Hostname=Zabbix server Include=/etc/zabbix/zabbix_agentd.d/*.conf 

3.啓動zabbix-agent並檢查

[root@web01 ~]# systemctl start zabbix-agent.service [root@web01 ~]# systemctl enable zabbix-agent.service [root@web01 ~]# netstat -lntup|grep 10050 tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 10351/zabbix_agentd tcp6 0 0 :::10050 :::* LISTEN 10351/zabbix_agentd 

4.zabbix-web界面,添加主機

 

 

第七章 自定義監控主機小試身手

1.監控需求

監控TCP11種狀態集css

2.命令行實現

[root@web01 ~]# netstat -ant|grep -c TIME_WAIT 55 [root@web01 ~]# netstat -ant|grep -c LISTEN 12 

3.編寫zabbix監控文件(傳參形式)

[root@web01 ~]# cat /etc/zabbix/zabbix_agentd.d/tcp_status.conf 
UserParameter=tcp_state[*],netstat -ant|grep -c $1
root@web01 ~]# systemctl restart zabbix-agent.service

4.server端進行測試

[root@m01 ~]# rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm [root@m01 ~]# yum install zabbix-get.x86_64 -y [root@m01 ~]# zabbix_get -s 10.0.1.7 -k tcp_state[TIME_WAIT] 51 [root@m01 ~]# zabbix_get -s 10.0.1.7 -k tcp_state[LISTEN] 12 

5.web端添加

 

6.克隆監控項

因爲TCP有多種狀態,須要添加多個監控項,咱們可使用克隆快速達到建立的效果html

 

 

 

 

其餘的狀態依次添加便可python

7.建立圖形

8.查看圖形

9.設置觸發器

 

 

 

 

 

 

 

 

 

 

 

 

第八章 郵件報警

1.定義發件人

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2.定義收件人

 

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

 

 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3.自定義報警內容過

定製報警內容:
https://www.zabbix.com/documentation/4.0/zh/manual/appendix/macros/supported_by_location
參考博客nginx

https://www.cnblogs.com/bixiaoyu/p/7302541.html 

發送警告web

報警郵件標題可使用默認信息,亦可以使用以下中文報警內容
名稱:Action-Email
默認標題:故障{TRIGGER.STATUS},服務器:{HOSTNAME1}發生: {TRIGGER.NAME}故障!
告警主機:{HOSTNAME1}
告警時間:{EVENT.DATE} {EVENT.TIME}
告警等級:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警項目:{TRIGGER.KEY1}
問題詳情:{ITEM.NAME}:{ITEM.VALUE}
當前狀態:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID} 

恢復警告數據庫

恢復標題:恢復{TRIGGER.STATUS}, 服務器:{HOSTNAME1}: {TRIGGER.NAME}已恢復!
恢復信息:
告警主機:{HOSTNAME1}
告警時間:{EVENT.DATE} {EVENT.TIME}
告警等級:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警項目:{TRIGGER.KEY1}
問題詳情:{ITEM.NAME}:{ITEM.VALUE}
當前狀態:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}

第九章 微信報警

1.查看配置文件裏的腳本目錄路徑

[root@m01 ~]# grep "^AlertScriptsPath" /etc/zabbix/zabbix_server.conf AlertScriptsPath=/usr/lib/zabbix/alertscripts 

2.將weixin.py放在zabbix特定目錄

[root@m01 /usr/lib/zabbix/alertscripts]# ll
總用量 4
-rwxr-xr-x 1 root root 1344 8月   7 21:58 weixin.py 

3.配置發信人

 

4.配置收信人

5.登錄企業微信公衆號添加帳戶

https://work.weixin.qq.com/wework_admin/loginpage_wx
1.登錄後在企業號上新建應用
json


2.上傳logo,填寫應用名稱 ,應用介紹等api


3.查看啓動應用
同時會生成應用的AgentId以及Secret,這個在後面步驟會有用ruby


4.接口調用測試
http://work.weixin.qq.com/api/devtools/devtool.php


這裏的corpid爲公司ID


Corpsecret就是剛纔建立應用生成的Secrt,確認沒問題填寫進去而後下一步
若是沒問題會顯示200狀態碼

6.添加成員

7.關注公衆號

8.查看本身的帳號

9.修改腳本里的信息

[root@m01 /usr/lib/zabbix/alertscripts]# cat weixin.py .............. corpid='微信企業號corpid' appsecret='應用的Secret' agentid=應用的id .............. 

10.發信測試

[root@m01 /usr/lib/zabbix/alertscripts]# python weixin.py 你的帳號 '發信測試' ‘微信測試消息’ 

11.微信號上查看

12.發送到整個微信組

雖然咱們實現了發送到單個用戶的功能,可是若是咱們的用戶比較多,這樣仍是麻煩的,不過咱們能夠發送到整個組,其實腳本里已經預留好了配置,只不過默認註釋了。
將腳本修改成如下內容,註釋掉用戶,打開組設置

#!/usr/bin/env python import requests import sys import os import json import logging logging.basicConfig(level = logging.DEBUG, format = '%(asctime)s, %(filename)s, %(levelname)s, %(message)s', datefmt = '%a, %d %b %Y %H:%M:%S', filename = os.path.join('/tmp','weixin.log'), filemode = 'a') corpid='wwd26fdfb9940e7efa' appsecret='Btg89FnZfMu0k7l6b4iagmAR5Z9TCgKknYbx-SMQvmg' agentid=1000005 token_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + appsecret req=requests.get(token_url) accesstoken=req.json()['access_token'] msgsend_url='https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + accesstoken #touser=sys.argv[1] toparty=sys.argv[1] subject=sys.argv[2] message=sys.argv[2] + "\n\n" +sys.argv[3] params={ #"touser": touser, "toparty": toparty, "msgtype": "text", "agentid": agentid, "text": { "content": message }, "safe":0 } req=requests.post(msgsend_url, data=json.dumps(params)) logging.info('sendto:' + toparty + ';;subject:' + subject + ';;message:' + message) 

12.隨機發送到指定用戶玩笑腳本

#!/bin/bash 
num=$(echo $(($RANDOM%28+1)))
name=$(sed -n "${num}p" name.txt)
ok_boy=$(grep -v "${name}" name.txt)

for ok in ${ok_boy}
do
  python  weixin.py ${ok}  "$1"  "$2"
done

第十章 自定義模版

1.監控TCP11種狀態

編寫zabbix配置文件

[root@web01 /etc/zabbix/zabbix_agentd.d]# cat zbx_tcp.conf UserParameter=ESTABLISHED,netstat -ant|grep -c 'ESTABLISHED' UserParameter=SYN_SENT,netstat -ant|grep -c 'SYN_SENT' UserParameter=SYN_RECV,netstat -ant|grep -c 'SYN_RECV' UserParameter=FIN_WAIT1,netstat -ant|grep -c 'FIN_WAIT1' UserParameter=FIN_WAIT2,netstat -ant|grep -c 'FIN_WAIT2' UserParameter=TIME_WAIT,netstat -ant|grep -c 'TIME_WAIT' UserParameter=CLOSE,netstat -ant|grep -c 'CLOSE' UserParameter=CLOSE_WAIT,netstat -ant|grep -c 'CLOSE_WAIT' UserParameter=LAST_ACK,netstat -ant|grep -c 'LAST_ACK' UserParameter=LISTEN,netstat -ant|grep -c 'LISTEN' UserParameter=CLOSING,netstat -ant|grep -c 'CLOSING' 

2.重啓zabbix-agent

[root@web01 ~]# systemctl restart zabbix-agent.service 

3.測試監控項

使用zabbix-get命令測試

[root@m01 ~]# yum install zabbix-get.x86_64 -y
[root@m01 ~]# zabbix_get -s 10.0.1.7 -k ESTABLISHED
2
[root@m01 ~]# zabbix_get -s 10.0.1.7 -k LISTEN
12

3.導入模版文件

 

 

4.主機關聯模版文件

 

5.查看最新數據

6.查看圖形

第x章 自定義模版監控nginx狀態

1.開啓監控頁面並訪問測試

[root@web01 ~]# cat /etc/nginx/conf.d/status.conf server { listen 80; server_name localhost; location /nginx_status { stub_status on; access_log off; } } [root@web01 ~]# curl 127.0.0.1/nginx_status/ Active connections: 1 server accepts handled requests 6 6 6 Reading: 0 Writing: 1 Waiting: 0 

2.準備nginx監控狀態腳本

[root@web01 /etc/zabbix/zabbix_agentd.d]# cat nginx_monitor.sh 
#!/bin/bash
NGINX_COMMAND=$1
CACHEFILE="/tmp/nginx_status.txt"
CMD="/usr/bin/curl http://127.0.0.1/nginx_status/"
if [ ! -f $CACHEFILE  ];then
   $CMD >$CACHEFILE 2>/dev/null
fi
# Check and run the script
TIMEFLM=`stat -c %Y $CACHEFILE`
TIMENOW=`date +%s`

if [ `expr $TIMENOW - $TIMEFLM` -gt 60 ]; then
    rm -f $CACHEFILE
fi
if [ ! -f $CACHEFILE  ];then
   $CMD >$CACHEFILE 2>/dev/null
fi

nginx_active(){
         grep 'Active' $CACHEFILE| awk '{print $NF}'
         exit 0;
}
nginx_reading(){
         grep 'Reading' $CACHEFILE| awk '{print $2}'
         exit 0;
}
nginx_writing(){
         grep 'Writing' $CACHEFILE | awk '{print $4}'
         exit 0;
}
nginx_waiting(){
         grep 'Waiting' $CACHEFILE| awk '{print $6}'
         exit 0;
}
nginx_accepts(){
         awk NR==3 $CACHEFILE| awk '{print $1}' 
         exit 0;
}
nginx_handled(){
         awk NR==3 $CACHEFILE| awk '{print $2}' 
         exit 0;
}
nginx_requests(){
         awk NR==3 $CACHEFILE| awk '{print $3}'
         exit 0;
}

case $NGINX_COMMAND in
    active)
        nginx_active;
        ;;
    reading)
        nginx_reading;
        ;;
    writing)
        nginx_writing;
        ;;
    waiting)
        nginx_waiting;
        ;;
    accepts)
        nginx_accepts;
        ;;
    handled)
        nginx_handled;
        ;;
    requests)
        nginx_requests;
        ;;
    *)
echo 'Invalid credentials';
exit 2;
esac

3.編寫zabbix監控配置文件

[root@web01 ~]# cat /etc/zabbix/zabbix_agentd.d/nginx_status.conf UserParameter=nginx_status[*],/bin/bash /etc/zabbix/zabbix_agentd.d/nginx_monitor.sh $1 [root@web01 ~]# systemctl restart zabbix-agent.service 

4.使用zabbix_get取值

[root@m01 ~]# zabbix_get -s 10.0.1.7 -k nginx_status[accepts]
7

5.導入模版

6.連接模版

7.查看數據

第x章 自定義模版監控php狀態

1.開啓監控頁面

[root@web01 ~]# tail -1 /etc/php-fpm.d/www.conf pm.status_path = /php_status [root@web01 ~]# cat /etc/nginx/conf.d/status.conf server { listen 80; server_name localhost; location /nginx_status { stub_status on; access_log off; } location /php_status { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME html$fastcgi_script_name; include fastcgi_params; } } [root@web01 ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@web01 ~]# systemctl restart nginx.service php-fpm.service 

2.訪問測試

[root@web01 ~]# curl 127.0.0.1/php_status
pool:                 www
process manager:      dynamic
start time:           08/Aug/2019:22:31:27 +0800
start since:          37
accepted conn:        1
listen queue:         0
max listen queue:     0
listen queue len:     128
idle processes:       4
active processes:     1
total processes:      5
max active processes: 1
max children reached: 0
slow requests:        0

3.準備訪問腳本

[root@web01 ~]# cat /etc/zabbix/zabbix_agentd.d/fpm.sh 
#!/bin/bash
##################################
# Zabbix monitoring script
#
# php-fpm:
#  - anything available via FPM status page
#
##################################
# Contact:
#  vincent.viallet@gmail.com
##################################
# ChangeLog:
#  20100922     VV      initial creation
##################################

# Zabbix requested parameter
ZBX_REQ_DATA="$1"
ZBX_REQ_DATA_URL="$2"

# Nginx defaults
NGINX_STATUS_DEFAULT_URL="http://localhost/fpm/status"
WGET_BIN="/usr/bin/wget"

#
# Error handling:
#  - need to be displayable in Zabbix (avoid NOT_SUPPORTED)
#  - items need to be of type "float" (allow negative + float)
#
ERROR_NO_ACCESS_FILE="-0.91"
ERROR_NO_ACCESS="-0.92"
ERROR_WRONG_PARAM="-0.93"
ERROR_DATA="-0.94" # either can not connect /   bad host / bad port

# Handle host and port if non-default
if [ ! -z "$ZBX_REQ_DATA_URL" ]; then
  URL="$ZBX_REQ_DATA_URL"
else
  URL="$NGINX_STATUS_DEFAULT_URL"
fi

# save the nginx stats in a variable for future parsing
NGINX_STATS=$($WGET_BIN -q $URL -O - 2>/dev/null)

# error during retrieve
if [ $? -ne 0 -o -z "$NGINX_STATS" ]; then
  echo $ERROR_DATA
  exit 1
fi

# 
# Extract data from nginx stats
#
#RESULT=$(echo "$NGINX_STATS" | awk 'print $0;match($0, "^'"$ZBX_REQ_DATA"':[[:space:]]+(.*)", a) { print a[1] }')
#RESULT=$(echo "$NGINX_STATS" | grep "$ZBX_REQ_DATA" | awk -F : '{print $2}')
RESULT=$(echo "$NGINX_STATS" | awk -F : "{if(\$1==\"$ZBX_REQ_DATA\") print \$2}")
if [ $? -ne 0 -o -z "$RESULT" ]; then
    echo $ERROR_WRONG_PARAM
    exit 1
fi

echo $RESULT

exit 0

[root@web01 ~]# bash /etc/zabbix/zabbix_agentd.d/fpm.sh "total processes" http://127.0.0.1/php_status
5

4.準備zabbix配置文件

[root@web01 ~]# cat /etc/zabbix/zabbix_agentd.d/fpm.conf UserParameter=php-fpm[*],/etc/zabbix/zabbix_agentd.d/fpm.sh "$1" "$2" [root@web01 ~]# systemctl restart zabbix-agent.service 

4.使用zabbix_get取值

[root@m01 ~]# zabbix_get -s 10.0.1.7 -k php-fpm["total processes",http://127.0.0.1/php_status] 5 

5.導入模版

導入以後須要修改一下模版裏的宏配置

 

第x章 WEB監控

需求,監控頁面狀態碼

 

 

 

第x章 故障記錄

故障1

故障現象:
提示zabbix-server is not running

 

 

報錯日誌:

34983:20190807:202215.171 database is down: reconnecting in 10 seconds 34983:20190807:202225.172 [Z3001] connection to database 'zabbix' failed: [1045] Access denied for user 'zabbix'@'localhost' (using password: NO) 

故障緣由:
zabbix-server的配置文件裏配有配置數據庫密碼
故障解決:
添加正確的數據庫帳號密碼信息

[root@m01 ~]# grep "^DB" /etc/zabbix/zabbix_server.conf     
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix

故障2

故障現象:微信報警失敗
報錯日誌:

[root@m01 ~]# tail -f /var/log/zabbix/zabbix_server.log Problem name: TIME_WAIT過多 Host: web01 Severity: Average Original problem ID: 51 '": Traceback (most recent call last): File "/usr/lib/zabbix/alertscripts/weixin.py", line 7, in <module> import requests ImportError: No module named requests 

問題緣由:
缺乏模塊 requests

問題解決:
安裝缺失的依賴包

[root@m01 ~]# yum install python-pip [root@m01 ~]# pip install --upgrade pip [root@m01 ~]# pip install requests 

故障3

故障現象:
在server端使用zabbix_get命令測試鍵值命令時提示警告

[root@m01 ~]# zabbix_get -s 10.0.1.7 -k ESTABLISHED  
(Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) 2 

問題緣由: zabbix_agent是以普通用戶zabbix運行的,而普通用戶執行netstat -antp時會有警告,網上查找發現只要不是用p參數就能夠以普通用戶運行 解決方案: 監控腳本里的命令修改成netstat -ant

相關文章
相關標籤/搜索