與 Apigee-monit 一起自我修復

Apigee Edge for Private Cloud 包含 apigee-monit,這項工具是以開放原始碼的 monit 公用程式為基礎。apigee-monit 會定期輪詢 Edge 服務;如果服務無法使用,apigee-monit 會嘗試重新啟動。

如要使用 apigee-monit,請手動安裝。標準安裝程序不會安裝這項功能。

根據預設,apigee-monit 每 60 秒會檢查一次 Edge 服務的狀態。

快速入門

本節說明如何快速開始使用 apigee-monit

如果您使用 Amazon Linux 或 Oracle-Linux-7.X,請先透過 Fedora 安裝 monit。否則請略過這個步驟。

sudo yum install -y https://kojipkgs.fedoraproject.org/packages/monit/5.25.1/1.el6/x86_64/monit-5.25.1-1.el6.x86_64.rpm

如要安裝 apigee-monit,請按照下列步驟操作:

  安裝 apigee-monit
/opt/apigee/apigee-service/bin/apigee-service apigee-monit install
/opt/apigee/apigee-service/bin/apigee-service apigee-monit configure
/opt/apigee/apigee-service/bin/apigee-service apigee-monit start

這會安裝 apigee-monit,並預設開始監控節點上的所有元件。

  停止監控元件
/opt/apigee/apigee-service/bin/apigee-service apigee-monit unmonitor -c component_name
/opt/apigee/apigee-service/bin/apigee-service apigee-monit unmonitor -c all
  開始監控元件
/opt/apigee/apigee-service/bin/apigee-service apigee-monit monitor -c component_name
/opt/apigee/apigee-service/bin/apigee-service apigee-monit monitor -c all
  取得摘要狀態資訊
/opt/apigee/apigee-service/bin/apigee-service apigee-monit report
/opt/apigee/apigee-service/bin/apigee-service apigee-monit summary
  查看 apigee-monit 記錄檔
cat /opt/apigee/var/log/apigee-monit/apigee-monit.log

以下各節將詳細說明這些主題和其他主題。

關於 apigee-monit

apigee-monit 可確保節點上的所有元件持續運作。Google 透過提供多種服務來達成這個目標,包括:

  • 重新啟動失敗的服務
  • 顯示摘要資訊
  • 記錄監控狀態
  • 傳送通知
  • 監控非 Edge 服務

Apigee 建議您監控 apigee-monit,確保該服務正常運作。詳情請參閱「監控 apigee-monit」。

apigee-monit 架構

在安裝及設定 Apigee Edge for Private Cloud 時,您可以在叢集的每個節點上,選擇安裝 apigee-monit 的個別執行個體。這些獨立的 apigee-monit 執行個體會彼此獨立運作,不會將元件狀態傳達給其他節點,也不會將監控公用程式本身的故障情形傳達給任何中央服務。

下圖顯示 5 節點叢集中的 apigee-monit 架構:

5 節點叢集中的 Apigee 監控架構
圖 1:叢集中的每個節點都會獨立執行 apigee-monit 的個別執行個體

元件設定

apigee-monit 會使用元件設定,判斷要監控哪些元件、要檢查元件的哪些方面,以及發生故障時要採取哪些動作。

根據預設,apigee-monit 會使用預先定義的元件設定,監控節點上的所有 Edge 元件。如要查看預設設定,請參閱 apigee-monit 元件設定檔。您無法變更預設元件設定。

apigee-monit 會根據檢查的元件,檢查元件的不同層面。下表列出 apigee-monit 對各元件的檢查項目,以及各元件的設定位置。請注意,部分元件是在單一設定檔中定義,其他元件則有自己的設定。

元件 設定位置 監控的內容
管理伺服器 /opt/apigee/edge-management-server/monit/default.conf apigee-monit 檢查:
  • 指定通訊埠已開啟並接受要求
  • 支援指定的通訊協定
  • 回覆狀態

此外,這些元件 apigee-monit 還有以下特性:

  • 必須在指定週期數內多次失敗,才會採取行動
  • 設定自訂要求路徑
訊息處理器 /opt/apigee/edge-message-processor/monit/default.conf
Postgres 伺服器 /opt/apigee/edge-postgres-server/monit/default.conf
Qpid 伺服器 /opt/apigee/edge-qpid-server/monit/default.conf
路由器 /opt/apigee/edge-router/monit/default.conf
Cassandra
Edge UI
SymasLDAP
Postgres
Qpid
Zookeeper
/opt/apigee/data/apigee-monit/monit.conf apigee-monit 檢查:
  • 服務正在執行

以下範例顯示 edge-router 元件的預設元件設定:

check host edge-router with address localhost
  restart program = "/opt/apigee/apigee-service/bin/apigee-service edge-router monitrestart"
  if failed host 10.1.1.0 port 8081 and protocol http
    and request "/v1/servers/self/uuid"
    with timeout 15 seconds
    for 2 times within 3 cycles
  then restart

  if failed port 15999 and protocol http
    and request "/v1/servers/self"
    and status < 600
    with timeout 15 seconds
    for 2 times within 3 cycles
  then restart

以下範例顯示傳統 UI (edge-ui) 元件的預設設定:

check process edge-ui
 with pidfile /opt/apigee/var/run/edge-ui/edge-ui.pid
 start program = "/opt/apigee/apigee-service/bin/apigee-service edge-ui start" with timeout 55 seconds
 stop program = "/opt/apigee/apigee-service/bin/apigee-service edge-ui stop"

這適用於舊版 UI,不適用於新版 Edge UI (元件名稱為 edge-management-ui)。

您無法變更任何 Apigee Edge 私有雲元件的預設元件設定。不過,您可以為外部服務新增自己的元件設定,例如目標端點或 httpd 服務。詳情請參閱「非 Apigee 元件設定」。

根據預設,apigee-monit 會監控執行所在節點上的所有元件。您可以為所有元件或個別元件啟用或停用這項功能。如需詳細資訊,請參閱:

安裝 apigee-monit

apigee-monit 預設不會安裝,升級或安裝 Apigee Edge for Private Cloud 4.19.01 以上版本後,您可以手動安裝。

本節說明如何安裝 apigee-monit

如要瞭解如何解除安裝 apigee-monit,請參閱「解除安裝 apigee-monit」一文。

安裝 apigee-monit

本節說明如何安裝 apigee-monit

如何安裝 apigee-monit

  1. 使用下列指令安裝 apigee-monit
    /opt/apigee/apigee-service/bin/apigee-service apigee-monit install
  2. 使用下列指令設定 apigee-monit
    /opt/apigee/apigee-service/bin/apigee-service apigee-monit configure
  3. 使用下列指令啟動 apigee-monit
    /opt/apigee/apigee-service/bin/apigee-service apigee-monit start
  4. 對叢集中的每個節點重複執行這個程序。

停止及啟動監控元件

如果服務因任何原因停止,apigee-monit 會嘗試重新啟動服務。

如果您想刻意停止元件,這可能會造成問題。舉例來說,您可能需要在備份或升級元件時停止元件。如果 apigee-monit 在備份或升級期間重新啟動服務,維護程序可能會中斷,甚至可能失敗。

以下各節說明停止監控元件的選項。

停止元件並取消監控

如要停止元件並取消監控,請執行下列指令:

/opt/apigee/apigee-service/bin/apigee-service apigee-monit stop-component -c component_name
component_name 可以是下列任一值:
  • apigee-cassandra (Cassandra)
  • apigee-openldap (OpenLDAP)
  • apigee-postgresql (PostgreSQL 資料庫)
  • apigee-qpidd (魁北克)
  • apigee-sso (邊緣單一登入)
  • apigee-zookeeper (ZooKeeper)
  • edge-management-server (管理伺服器)
  • edge-management-ui (新版 Edge UI)
  • edge-message-processor (訊息處理器)
  • edge-postgres-server (Postgres 伺服器)
  • edge-qpid-server (快速伺服器)
  • edge-router (邊緣路由器)
  • edge-ui (傳統版 UI)

請注意,「all」不是 stop-component 的有效選項。你一次只能使用 stop-component 停止及取消監控一個元件。

如要重新啟動元件並繼續監控,請執行下列指令:

/opt/apigee/apigee-service/bin/apigee-service apigee-monit start-component -c component_name

請注意,「all」不是 start-component 的有效選項。

如要瞭解如何停止及取消監控所有元件,請參閱「停止所有元件並取消監控」一文。

取消監控元件 (但不要停止元件)

如要取消監控元件 (但不要停止元件),請執行下列指令:

/opt/apigee/apigee-service/bin/apigee-service apigee-monit unmonitor -c component_name
component_name 可以是下列任一值:
  • apigee-cassandra (Cassandra)
  • apigee-openldap (OpenLDAP)
  • apigee-postgresql (PostgreSQL 資料庫)
  • apigee-qpidd (魁北克)
  • apigee-sso (邊緣單一登入)
  • apigee-zookeeper (ZooKeeper)
  • edge-management-server (管理伺服器)
  • edge-management-ui (新版 Edge UI)
  • edge-message-processor (訊息處理器)
  • edge-postgres-server (Postgres 伺服器)
  • edge-qpid-server (快速伺服器)
  • edge-router (邊緣路由器)
  • edge-ui (傳統版 UI)

如要繼續監控元件,請執行下列指令:

/opt/apigee/apigee-service/bin/apigee-service apigee-monit monitor -c component_name

取消監控所有元件 (但不要停止元件)

如要取消監控所有元件 (但不要停止元件),請執行下列指令:

/opt/apigee/apigee-service/bin/apigee-service apigee-monit unmonitor -c all

如要繼續監控所有元件,請執行下列指令:

/opt/apigee/apigee-service/bin/apigee-service apigee-monit monitor -c all

停止所有元件並取消監控

如要停止所有元件並取消監控,請執行下列指令:

/opt/apigee/apigee-service/bin/apigee-service apigee-monit unmonitor -c all
/opt/apigee/apigee-service/bin/apigee-all stop

如要重新啟動所有元件並繼續監控,請執行下列指令:

/opt/apigee/apigee-service/bin/apigee-all start
/opt/apigee/apigee-service/bin/apigee-service apigee-monit monitor -c all

如要停止監控所有元件,也可以停用 apigee-monit,詳情請參閱「停止、啟動及停用 apigee-monit」。

停止、啟動及停用 apigee-monit

與任何服務一樣,您可以使用 apigee-service 指令停止及啟動 apigee-monit。此外,apigee-monit 支援 unmonitor 指令,可暫時停止監控元件。

停止 apigee-monit

如要停止 apigee-monit,請使用下列指令:

/opt/apigee/apigee-service/bin/apigee-service apigee-monit stop

啟動 apigee-monit

如要啟動 apigee-monit,請使用下列指令:

/opt/apigee/apigee-service/bin/apigee-service apigee-monit start

停用 apigee-monit

您可以使用下列指令,暫停監控節點上的所有元件:

/opt/apigee/apigee-service/bin/apigee-service apigee-monit unmonitor -c all

或者,您也可以從節點解除安裝 apigee-monit,永久停用這項功能,詳情請參閱「解除安裝 apigee-monit」。

解除安裝 apigee-monit

如何解除安裝 apigee-monit

  1. 如果您設定了cron 工作來監控 apigee-monit,請先移除 cron 工作,再解除安裝 apigee-monit
    sudo rm /etc/cron.d/apigee-monit.cron
  2. 使用下列指令停止 apigee-monit
    /opt/apigee/apigee-service/bin/apigee-service apigee-monit stop
  3. 使用下列指令解除安裝 apigee-monit
    /opt/apigee/apigee-service/bin/apigee-service apigee-monit uninstall
  4. 對叢集中的每個節點重複執行這個程序。

監控新安裝的元件

如果在執行 apigee-monit 的節點上安裝新元件,您可以執行 apigee-monitrestart 指令,開始監控該元件。這會產生新的 monit.conf 檔案,並在元件設定中加入新元件。

以下範例會重新啟動 apigee-monit

/opt/apigee/apigee-service/bin/apigee-service apigee-monit restart

自訂 apigee-monit

你可以自訂各種 apigee-monit 設定,包括:

  1. 預設 apigee-monit 控制項設定
  2. 全域設定
  3. 非 Apigee 元件設定

apigee-monit 控制項的預設設定

您可以自訂預設的 apigee-monit 控制項設定,例如狀態檢查頻率和 apigee-monit 檔案位置。如要執行這項操作,請使用「程式碼搭配設定」技術編輯屬性檔案。即使升級 Apigee Edge Private Cloud,屬性檔案仍會保留。

下表說明可自訂的預設 apigee-monit 控制項設定:

屬性 說明
conf_monit_httpd_port httpd Daemon 的通訊埠。apigee-monit 會使用 httpd 建立資訊主頁應用程式,並啟用報表/摘要。預設值為 2812。
conf_monit_httpd_allow httpd 精靈的要求限制。apigee-monit 會使用 httpd 執行資訊主頁應用程式,並啟用報表/摘要。這個值必須指向本機主機 (執行 httpd 的主機)。

如要要求要求包含使用者名稱和密碼,請使用下列語法:

conf_monit_httpd_allow=allow username:"password"\nallow 127.0.0.1

新增使用者名稱和密碼時,請在每個限制之間插入「\n」。請勿在值中插入實際的換行或回車字元。

conf_monit_monit_datadir 儲存活動詳細資料的目錄。
conf_monit_monit_delay_time apigee-monit 首次載入記憶體後,等待執行的時間長度。這項問題只會影響apigee-monit第一次的程序檢查。
conf_monit_monit_logdir apigee-monit 記錄檔的位置。
conf_monit_monit_retry_time apigee-monit 嘗試檢查每個程序的頻率,預設為 60 秒。
conf_monit_monit_rundir PID 和狀態檔案的位置,apigee-monit 用於檢查程序。

如何自訂預設 apigee-monit 控制項設定:

  1. 編輯以下檔案:
    /opt/apigee/customer/application/monit.properties

    如果檔案不存在,請建立檔案,並將擁有者設為「apigee」使用者:

    chown apigee:apigee /opt/apigee/customer/application/monit.properties

    請注意,如果檔案已存在,其中可能定義了其他設定屬性,不只上述表格列出的屬性。請勿修改上述屬性以外的屬性。

  2. 使用新值設定或取代屬性值。

    舉例來說,如要將記錄檔位置變更為 /tmp,請新增或編輯下列屬性:

    conf_monit_monit_logdir=/tmp/apigee-monit.log
  3. 將變更儲存至 monit.properties 檔案。
  4. 使用下列指令重新設定 apigee-monit
    /opt/apigee/apigee-service/bin/apigee-service apigee-monit configure
  5. 使用下列指令重新載入 apigee-monit
    /opt/apigee/apigee-service/bin/apigee-service apigee-monit reload

    如果無法重新啟動 apigee-monit,請按照「存取 apigee-monit 記錄檔」一文的說明,檢查記錄檔是否有錯誤。

  6. 針對叢集中的每個節點重複執行這個程序。

全域設定

您可以為「apigee-monit」定義全域設定,例如新增快訊的電子郵件通知。方法是在 /opt/apigee/data/apigee-monit 目錄中建立設定檔,然後重新啟動 apigee-monit

如要定義 apigee-monit 的全域設定:

  1. 在下列位置建立新的元件設定檔:
    /opt/apigee/data/apigee-monit/filename.conf

    其中 filename 可以是任何有效檔案名稱,「monit」除外。

  2. 將新設定檔的擁有者變更為「apigee」使用者,如下列範例所示:
    chown apigee:apigee /opt/apigee/data/apigee-monit/my-mail-config.conf
  3. 將全域設定新增至新檔案。以下範例會設定郵件伺服器,並設定快訊收件者:
    SET MAILSERVER smtp.gmail.com PORT 465
      USERNAME "example-admin@gmail.com" PASSWORD "PASSWORD"
      USING SSL, WITH TIMEOUT 15 SECONDS
    
    SET MAIL-FORMAT {
      from: edge-alerts@example.com
      subject: Monit Alert -- Service: $SERVICE $EVENT on $HOST
    }
    SET ALERT fred@example.com
    SET ALERT nancy@example.com

    如需完整的全域設定選項清單,請參閱 monit 說明文件

  4. 將變更儲存至元件設定檔。
  5. 使用下列指令重新載入 apigee-monit
    /opt/apigee/apigee-service/bin/apigee-service apigee-monit reload

    如果 apigee-monit 未重新啟動,請按照「存取 apigee-monit 記錄檔」一文的說明,檢查記錄檔是否有錯誤。

  6. 針對叢集中的每個節點重複執行這個程序。

非 Apigee 元件的設定

您可以將自己的設定新增至 apigee-monit,以便檢查不屬於 Apigee Edge for Private Cloud 的服務。舉例來說,您可以透過 apigee-monit 將要求傳送至目標端點,確認 API 是否正在執行。

如要新增非 Apigee 元件設定,請按照下列步驟操作:

  1. 在下列位置建立新的元件設定檔:
    /opt/apigee/data/apigee-monit/filename.conf

    其中 filename 可以是任何有效檔案名稱,「monit」除外。

    您可以視需要建立多個元件設定檔。舉例來說,您可以為要在節點上監控的每個非 Apigee 元件,建立個別的設定檔。

  2. 將新設定檔的擁有者變更為「apigee」使用者,如下列範例所示:
    chown apigee:apigee /opt/apigee/data/apigee-monit/my-config.conf
  3. 將自訂設定新增至新檔案。以下範例會檢查本機伺服器上的目標端點:
    CHECK HOST localhost_validate_test WITH ADDRESS localhost
      IF FAILED
        PORT 15999
        PROTOCOL http
        REQUEST "/validate__test"
        CONTENT = "Server Ready"
        FOR 2 times WITHIN 3 cycles
      THEN alert

    如需可能的完整設定清單,請參閱 monit 說明文件

  4. 將變更儲存至設定檔。
  5. 使用下列指令重新載入 apigee-monit
    /opt/apigee/apigee-service/bin/apigee-service apigee-monit reload

    如果 apigee-monit 未重新啟動,請按照「存取 apigee-monit 記錄檔」一文的說明,檢查記錄檔是否有錯誤。

  6. 針對叢集中的每個節點重複執行這個程序。

請注意,這項功能僅適用於非 Edge 元件。您無法自訂 Edge 元件的元件設定。

存取 apigee-monit 記錄檔

apigee-monit 會將所有活動 (包括事件、重新啟動、設定變更和快訊) 記錄在記錄檔中。

記錄檔的預設位置如下:

/opt/apigee/var/log/apigee-monit/apigee-monit.log

如要變更預設位置,請自訂 apigee-monit 控制項設定

記錄檔項目的格式如下:

'edge-message-processor' trying to restart
[UTC Dec 14 16:20:42] info     : 'edge-message-processor' trying to restart
'edge-message-processor' restart: '/opt/apigee/apigee-service/bin/apigee-service edge-message-processor monitrestart'

您無法自訂 apigee-monit 記錄檔項目的格式。

使用 apigee-monit 查看匯總狀態

apigee-monit 包含下列指令,可提供節點上元件的匯總狀態資訊:

指令 用量
report
/opt/apigee/apigee-service/bin/apigee-service apigee-monit report
summary
/opt/apigee/apigee-service/bin/apigee-service apigee-monit summary

以下各節將詳細說明這些指令。

報表

report 指令會提供節點上正常運作、停止運作、目前正在初始化或目前未受監控的元件數量匯總。以下範例會叫用 report 指令:

/opt/apigee/apigee-service/bin/apigee-service apigee-monit report

以下範例顯示 AIO (多合一) 設定的 report 輸出內容:

/opt/apigee/apigee-service/bin/apigee-service apigee-monit report
up:            11 (100.0%)
down:           0 (0.0%)
initialising:   0 (0.0%)
unmonitored:    1 (8.3%)
total:         12 services

在本例中,apigee-monit 回報 12 項服務中有 11 項正常運作。目前未監控一項服務。

首次執行 report 指令時,可能會收到 Connection refused 錯誤訊息。如有上述情況,請等待 conf_monit_monit_delay_time 屬性所指定的時間長度,然後再試一次。

摘要

summary 指令會列出每個元件並提供狀態。下列範例會叫用 summary 指令:

/opt/apigee/apigee-service/bin/apigee-service apigee-monit summary

以下範例顯示 AIO (多合一) 設定的 summary 輸出內容:

/opt/apigee/apigee-service/bin/apigee-service apigee-monit summary
Monit 5.25.1 uptime: 4h 20m
 Service Name                     Status                      Type
 host_name                        OK                          System
 apigee-zookeeper                 OK                          Process
 apigee-cassandra                 OK                          Process
 apigee-openldap                  OK                          Process
 apigee-qpidd                     OK                          Process
 apigee-postgresql                OK                          Process
 edge-ui                          OK                          Process
 edge-qpid-server                 OK                          Remote Host
 edge-postgres-server             OK                          Remote Host
 edge-management-server           OK                          Remote Host
 edge-router                      OK                          Remote Host
 edge-message-processor           OK                          Remote Host

如果首次執行 summary 指令時收到 Connection refused 錯誤,請等待 conf_monit_monit_delay_time 屬性的時間長度,然後再試一次。

監控 apigee-monit

最佳做法是定期檢查每個節點是否正在執行 apigee-monit

如要檢查 apigee-monit 是否正在執行,請使用下列指令:

/opt/apigee/apigee-service/bin/apigee-service apigee-monit monitor_monit

Apigee 建議您在執行 apigee-monit 的每個節點上,定期發出這項指令。其中一種做法是使用公用程式 (例如 cron),在預先定義的時間間隔執行排程工作。

如何使用 cron 監控 apigee-monit

  1. 如要新增 cron 支援,請將 apigee-monit.cron 目錄複製到 /etc/cron.d 目錄,如下列範例所示:
    cp /opt/apigee/apigee-monit/cron/apigee-monit.cron /etc/cron.d/
  2. 開啟 apigee-monit.cron 檔案進行編輯。

    apigee-monit.cron 檔案會定義要執行的 cron 工作,以及執行該工作的頻率。以下範例顯示預設值:

    # Cron entry to check if monit process is running. If not start it
    */2 * * * * root /opt/apigee/apigee-service/bin/apigee-service apigee-monit monitor_monit

    這個檔案使用下列語法,其中前五個欄位定義 apigee-monit 執行動作的時間:

    min hour day_of_month month day_of_week task_to_execute

    舉例來說,預設執行時間為 */2 * * * *,這會指示 cron 每 2 分鐘檢查一次 apigee-monit 程序。

    您無法以高於每分鐘一次的頻率執行 cron 工作。

    如要進一步瞭解如何使用 cron,請參閱伺服器 OS 的說明文件或手冊頁面。

  3. 變更 cron 設定,使其符合貴機構的政策。舉例來說,如要將執行頻率變更為每 5 分鐘一次,請將工作定義設為下列內容:
    */5 * * * * root /opt/apigee/apigee-service/bin/apigee-service apigee-monit monitor_monit
  4. 儲存 apigee-monit.cron 檔案。
  5. 針對叢集中的每個節點重複執行這個程序。

如果 cron 沒有開始觀看 apigee-monit,請檢查下列事項:

  • cron 工作定義後方有空白行。
  • 檔案中只定義一個 cron 工作。(註解行不計入。)

如要停止或暫時停用 apigee-monit,您也必須停用這項 cron 工作,否則 cron 會重新啟動 apigee-monit

如要停用 cron,請執行下列其中一項操作:

  • 刪除 /etc/cron.d/apigee-monit.cron 檔案:
    sudo rm /etc/cron.d/apigee-monit.cron

    如果日後想重新啟用 cron 觀看 apigee-monit,就必須重新複製。

  • 編輯 /etc/cron.d/apigee-monit.cron 檔案,並在行首加上「#」,將工作定義設為註解,例如:
    # 10 * * * * root /opt/apigee/apigee-service/bin/apigee-service apigee-monit monitor_monit