2023年10月10日 星期二

Zabbix新增Web monitoring 監控Templates-1

Zabbix新增Web monitoring 監控Templates-1

Zabbix網站Web monitoring 使用web scenarios進行監控紀錄數據,讓管理者了解web效能及運作狀況。

使用web scenarios 會收集以下資訊:
  • average download speed per second for all steps of whole scenario 整個場景所有步驟的平均每秒下載速度
  • number of the step that failed 失敗的步驟號
  • last error message 最後的錯誤訊息
在任何web scenarios步驟中都會收集以下資訊:
  • download speed per second 每秒下載速度
  • response time 回應時間
  • response code 回應碼

將建立新的Templates,設定web scenarios、trigger、graph、dashborad,了解Templates的組成,再應用於主機進行監控。

Zabbix WEB設定

建立Templates

在Configuration > Templates,右上方Create template新增
01-create templates

設定Template name:Web montioring
        Groups:Templates
02-create web templates

設定macros

在Templates的Macros,新增macros。在Template設定的Macros可以應用在Template的各項設定。
新增{$WEB.URL}及{$WEB.URL2},Value不用設定,主機套用template時,在主機設定相同macros及Value(網站連結),可以直接套用。設定2組macros,可以同時監控2個網站。點選Add 新增完成
03-create web macros

設定Scenarios

在Templates-Web montioring點選web,新增Scenarios。
04-create web

設定Name:Web Health Check: {$WEB.URL}
        Agent:Chrome 80 (Windows),模擬在windows的Chrome 80連線測試。
05-create web scenario01

在Setps頁面,Add新增Setp
Name:Website Availability
  URL:{$WEB.URL}
06-create scenario01 setp
Required status codes:200,401,403,410,503,正常會回應200,其它可以不寫入。按Add新增完成。

點選web scenarios,新增第2個Scenarios
設定Name:Web Health Check: {$WEB.URL2}
        Agent:Zabbix,使用Zabbix agent連線測試。
07-create web scenario02

在Setps頁面,Add新增Setp
Name:Website Availability
  URL:{$WEB.URL2}
08-create scenario02 setp
Required status codes:200,401,403,410,503,正常會回應200,其它可以不寫入。按Add新增完成。

設定graph

在Templates-Web montioring,點選Graphs後,右上方Create graph新增。
09-create graphs

設定Name:Website monitoring: Download speed:($WEB.URL}
        Width:600
        Height: 200
        Y axis MIN value:Fuxed 0
        Items:Web monitoring: Download speed for scenario "Web Health Check: {$WEB.URL}".
10-create graphs01
設定好,按Add新增完成。

Add Items的項目是scenarios預設的各項items,將需要觀察的item數據圖表化。
11-create graphs items
items項目可以參考上方官網Web monitoring items。

新增第二組graph
設定Name:Website monitoring: Response time :($WEB.URL}
        Width:600
        Height: 200
        Y axis MIN value:Fuxed 0
        Items:Web monitoring: Response time for step "Website Availability" of scenario "Web Health Check: {$WEB.URL}".
12-create graphs02
設定好,按Add新增完成。

新增第三組graph
設定Name:Website monitoring: Response time
        Width:600
        Height: 200
        Y axis MIN value:Fuxed 0
        Items:1:Web monitoring: Response time for step "Website Availability" of scenario "Web Health Check: {$WEB.URL}".
                    2:Web monitoring: Response time for step "Website Availability" of scenario "Web Health Check: {$WEB.URL2}"
13-create graphs03
設定2組items直接在同時呈現,同時比較數據差異。

設定完成
13-graphs

更多的資料參考官網Custom graphs

請記得要到Web monitoring點選update,上面各項目的數字都正確,才算設定完成。
21-update web templates

以上完成Web montioring Templates的基本設定,已經可以指派給主機使用。
後續將再設定Items、Trigger、Dashboard讓Templates更完整。


2023年10月9日 星期一

Zabbix 事件遠端命令操作-Remote commands

官網說明:
應用遠端命令(Remote commands),您可以定義在某種條件下在受監控主機上自動執行某個預定義命令。
遠端命令是智慧主動監控的強大機制。
在該功能最明顯的用途中,您可以嘗試:
  • 如果某些應用程式((web server, middleware, CRM)沒有回應,則自動重新啟動。
  • 如果遠端伺服器不回應請求,使用 IPMI命令rebo​​ot,重新啟動該伺服器。
  • 如果磁碟空間不足,自動釋放磁碟空間(刪除舊文件,清理 /tmp)
  • 根據 CPU 負載將虛擬機器從一個實體設備遷移到另一個實體設備
  • 當CPU(磁碟、記憶體等)資源不足時為雲端環境新增節點

所以發生事件時Zabbix Server除了發送訊通知,也可以使用遠端執行預先定義的指令執行操作。

環璄設定

遠端主機

Agent Remote commands設定

在zabbix_agent2.conf設定檔加入AllowKey=system.run[*] 
在zabbix_prxoy.conf設定檔加入EnableRemoteCommands=1

# vi /etc/zabbix/zabbix_agent2.conf
---
AllowKey=system.run[*] 
Plugins.SystemRun.LogRemoteCommands=1
---
01-zabbix agent2 edit
Plugins.SystemRun.LogRemoteCommands=1,將遠端執行指令紀錄在zabbix_agent2.log

帳號存取權限

讓zabbix帳號可以使用sudo來存取特權命令。若要設定存取權限,可以不輸入密碼以root執行。
如果無法執行visudo,請先安裝sudo套件。

# visudo 
---
# 1.zabbix 不需輸入密碼執行所有commands。
zabbix ALL=NOPASSWD: ALL
# 2.zabbix 不需輸入密碼執行重啟apache2
zabbix ALL=NOPASSWD: /etc/init.d/apache2 restart
---
02-visudo
如果沒有固定的執行的指令,使用第1個設定方式。如果知道固定執行的指令,請使用第2個設定方式。在資安方面會有較大保障。

Zabbix WEB設定

Scripts設定

Scripts可以使用webhook、script、SSH、Telnet、IPMI,預先定義執行項目。
在Administration > Scripts,右上方Create script新增
03-create scripts

新增使用Script-agent df,透過Zabbix agent執行commands,檢查硬碟空間。
04-scripts-agent2 df
在commands寫入df -h > /tmp/dh-agent.txt,讓zabbix 使用df -h檢查硬碟空間。
在commands寫入sudo df -h > /tmp/dh-agent2.txt,讓bzabbix 使用sudo df -h檢查硬碟空間。

新增使用Script-ssh df,使用密碼認證,設定帳號及密碼,透過SSH執行commands,檢查硬碟空間。
05-scripts-ssh df
在commands寫入sudo df -h > /tmp/dh-ssh.txt,讓bee01使用sudo df -h檢查硬碟空間。

新增使用Script-Restart Apache2 init,透過Zabbix agent執行commands,重啟apache2 。
06-scripts-server restart
在commands寫入sudo /etc/init.d/apache2 restart,讓zabbix使用sudo重啟apache2。

Trigger actions設定

在Configuration> Actions> Trigger actions,右上方Create action新增。

Actions

Name:Trigger severity Actions Escalations remotecommand
Type of calculation:A or B or C ,只有ABC有一個發生。
Conditions
A 觸發嚴重性大於或等於災難
B 觸發嚴重性大於或等於高
C 觸發嚴重性大於或等於警告
07-remotecommand action test

Operations

Steps:1 Send message to user groups: it via Line Notify
08-setp1 line notify
步驟1 當事件發生時,使用Line notify自定訊息通知it群組。

Steps:1 Run script "agent df" on hosts: d11-agent2
09-set1 remote command
10-set1 remote command agent2 df
步驟1 當事件發生時,在遠端主機d11執行Script-agent df。

Steps:2 Run script "ssh df" on hosts: d11-agent2
11-set1 remote command ssh df
步驟2 當事件持續發生,在遠端主機d11執行Script-ssh df。

Steps:3 Run script "Restart Apache2 init" on hosts: dnode01
12-set1 remote command restart
步驟3 當事件持續發生,在遠端主機dnode01執行Script-Restart Apache2 init。

Default operation step duration:1m
步驟時間為預設的1m (1分鐘)
13-set1 remote command action

測試結果

遠端主機d11
zabbix_agent2.log收到執行指令
18-d11 zabbix ls
/tmp查看生成的檔案,agent df的2個指令都有成功生成檔案,ssh df沒有成功權限可能要再調整。
17-d11 tmp ls

遠端主機dnode01
zabbix_agent2.log收到執行指令
16-dnode01 zabbix restart
成功重啟apache。
15-dnode01 restat apache2

簡單的測試驗證






2023年10月8日 星期日

Zabbix 事件通知升級-Actions Escalations

Zabbix 事件通知升級-Actions Escalations

官網介紹:https://www.zabbix.com/documentation/6.0/en/manual/config/notifications/action/escalations
透過升級(Escalations),您可以建立自訂方案來傳送通知或執行遠端命令。
在實際應用中,可以達到以下目的:
  • 有新問題可以第一時間通知用戶
  • 可以重複通知,直到問題解決
  • 發送通知可以延遲
  • 通知可以升級到另一個「較高」的使用者群組
  • 可以立即執行遠端命令,或在問題長時間未解決時執行
操作將根據步驟升級,每個步驟都有一個持續時間,最短持續時間為 60 秒。您可以定義單一步驟的預設持續時間和自訂持續時間。
您可以從任何步驟開始操作,例如發送通知或執行命令。第一步是立即採取行動。如果您想延遲某個操作,可以將其指派到稍後的步驟。對於每個步驟,可以定義多個操作。
升級步驟的數量不受限制。
升級是在配置操作時定義的。僅支援問題操作的升級,不支援恢復。

Zabbix 為升級(Escalations)提供了靈活的規則,依據設置當事件發生時依情況自動升級到下一個步驟,並執行分配給每個步驟的操作。
將依下情況設定Trigger Aciton,依情況升級。
1.當主機發生狀況時,使用Line notify自定訊息通知it群組。
2.當10分鐘後,狀況未解決時,使用Line notify和Slack自定訊息通知Zabbix administrarots群組。
3.當14分鐘後,狀況未解決時,使用Email自定訊息通知Manager群組。

Zabbix WEB 設定

Trigger actions設定

在Configuration> Actions> Trigger actions,右上方Create action新增。
Name:Trigger severity Actions Escalations
Type of calculation:A or B or C ,ABC有一個符合則執行。
Conditions
A 觸發嚴重性大於或等於災難
B 觸發嚴重性大於或等於高
C 觸發嚴重性大於或等於警告

Operations

Default operation step duration:5m
預設步驟時間設定為5m (5分鐘)
02-Default operation step duration

Operations

Steps:1-3 Send message to user groups: it via Line Notify
步驟1-3當主機發生狀況時,使用Line notify自定訊息通知it群組。
步驟時間為預設的5分鐘,請設0。
03-setp1-3 Trigger severity Actions Escalations operations

Steps:3-4 Send message to user groups: Zabbix administrators via Slack
步驟3-4 當10分鐘後,狀況未解決時,使用Slack自定訊息通知Zabbix administrarots群組。
步驟時間為預設的5分鐘,請設0。
04-setp3-4 Trigger severity Actions Escalations operations

Steps:3-0 Send message to user groups: Zabbix administrators via Line Notify
步驟3 當10分鐘後,狀況未解決時,使用Line notify自定訊息通知Zabbix administrarots群組。
步驟時間設為2分鐘。
05-setp3-0 Trigger severity Actions Escalations operations

Steps:5 - 0 Send message to user groups: manager via Email-Gmail
步驟5當狀況未解決時,使用Email自定訊息通知Manager群組。
步驟時間為預設的5分鐘,請設0。
06-setp4 Trigger severity Actions Escalations operations
在Email自定訊息內容加入:
Escalation info:
{ESC.HISTORY}
會列出整個事件的通知狀況。
在Conditions加入Event is not acknowledged。

在Operations的Operations設定中看到start in是步驟開始執行時間,Duration是間隔時間。
07-create Trigger severity Actions Escalations operations
依設定
步驟1-3是在事件發生時開始,依預設隔時間通知為00:00、00:05、00:10共3次,以Line notify自定訊息通知it群組。
步驟3-4是在事件發生後,依預設隔時間通知為00:10、00:15共2次,以Slack通知Zabbix administrarots群組。
可是又多設一次步驟3,間隔時間為2m,Line notify通知Zabbix administrarots群組。間隔時間會以時間短為主,所以通知時間變為00:10、00:12。後續步驟5隔時間也變更為2m,通知時間變為00:14,以Email通知Manager群組。

測試結果

步驟1-3 Line Notify 通知,時間10:09、10:14、10:19
setp1-3 linet notify it
步驟3-4 Slack 通知,時間10:19、10:21
setp3-4 slack administrators
步驟5 Email通知,時間10:23。mail最後內容就是{ESC.HISTORY}列出已通知的過程。
setp5 email

簡單的測試驗證,步驟1-5通知時間10:09、10:14、10:19、10:21、10:23,符合步驟設定。





2023年10月7日 星期六

Zabbix 事件通知操作-Trigger actions

Zabbix 事件通知操作-Trigger actions


Zabbix 整合多種media types ( Email、SMS、Custom alertscripts、Webhook )接收觸發的事件後進行Trigger actions發送通知。
將設定Trigger actions,當事件觸發時條件時,如何觸發操作。
預計使用Zabbix webhook 功能整合的Microsoft Teams、LINE。但都有使用限制
參考官網網路1網路2網路3 新增media types,使用Custom alert scripts搭配Line Notify進行事件通知。
00-line notify

新增Media type時,在Message templates設定頁面可以使用Zabbix內建以下Message templates做為事件通知訊息內容。
00-media type messages templates

Message template有以下type,每個都有預設的標題及訊息。
Problem
Problem recovery
Problem update
Service
Service recovery
Service update
Discovery
Autoregistration
Internal problem
Internal problem recovery
可以依個人需求修改,訊息部分也可以用中文。
Message template-Problem內容
message template-problem

Zabbix WEB 設定

Trigger actions 需求

當有事件觸發嚴重性大於或等於災難,高,警告時,
先分別使用Line Notify向用戶beeb發送通知,且使用Email-Gmail向 Zabbix administrators群組發送通知。
當事件恢復時,分別使用Line Notify向用戶beeb,Admin通知,且使用Email-Gmail向 Zabbix administrators群組發送通知。
當事件升級時,使用所有通知方式向 Zabbix administrators群組發送通知。

Trigger actions設定

在Configuration> Actions> Trigger actions,右上方Create action新增。
01-create trigger actions

Action設定

Name:Trigger severity for all media
Type of calculation:A or B or C ,ABC有一個符合則執行。
Conditions
A 觸發嚴重性大於或等於災難
B 觸發嚴重性大於或等於高
C 觸發嚴重性大於或等於警告
02-add conditions

Operations設定

06-operation details finish
Default operation step duration:5m
operations
Send message to users: beeb via Line Notify
在Operations details 設定中,使用Line Notify向使用者beeb發送通知
,不使用Message template預計訊息的格式,自訂通知的標題及訊息格式。
04-operation details line notify
Send message to user groups: Zabbix administrators via Email-Gmail
在Operations details 設定中,使用Email-Gmail向Zabbix administrators群組發送通知,使用Message template預計訊息的格式。
05-gmial operation details

Recovery operations
Send message to users: Admin (Zabbix Administrator), beeb via Line Notify
Send message to user groups: Zabbix administrators via Email-Gmail
使用Line Notify向用戶beeb,Admin發送通知。
使用Email-Gmail向 Zabbix administrators群組發送通知。

Update operations
Send message to user groups: Zabbix administrators via all media
在Operations details 設定中,使用所有通知方式向 Zabbix administrators群組發送通知。
05-operation details all type
Recovery operations和Update operations通知是可選擇的,如果不想收到此類通知,則不需要配置。設定完成後,請設定enable才會開始執行。

當事件發生時可以至Reports > Action log確認通知發送狀況。
08-action log different
Line Notify 通知
09-line ok
Slack 通知
08-slack ok