Chef 日誌 - AWS OpsWorks

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

Chef 日誌

重要

該 AWS OpsWorks Stacks 服務於 2024 年 5 月 26 日終止使用壽命,並已針對新客戶和現有客戶停用。我們強烈建議客戶盡快將其工作負載移轉至其他解決方案。如果您對移轉有任何疑問,請透過 AWS Re: post 或透過進AWS 階 Support 與 AWS Support 團隊聯絡。

Chef 日誌是您的關鍵故障排除資源之一,尤其是對於調試配方。 AWS OpsWorks 堆疊會擷取每個指令的 Chef 記錄,並保留執行個體最近 30 個指令的記錄。由於執行處於除錯模式,因此日誌內含 Chef 執行的詳細說明,包括傳送到 stdoutstderror 的文字。如果配方失敗,日誌會包含 Chef 堆疊追蹤。

AWS OpsWorks 堆棧為您提供了幾種查看 Chef 日誌的方法。一旦您擁有日誌資訊,就可用其為失敗配方除錯。

注意

您也可以透過使用 SSH 連線到執行個體並執行代理程式 CLI show_log 命令,來檢視指定日誌的結尾。如需詳細資訊,請參閱 顯示 Chef 日誌

使用主控台檢視 Chef 日誌

檢視 Chef 日誌最簡單的方法是前往執行個體的詳細資訊頁面。Logs (日誌) 區段包含每個事件和執行配方命令的進入點。下圖為執行個體的 Logs (日誌) 區段,顯示的內容是 configure (設定)setup (安裝) 命令的日誌,兩者分別對應到設定和安裝生命週期事件。

在適當命令的「記錄」欄中按一下「顯示」,以檢視對應的 Chef 記錄。如果發生錯誤, AWS OpsWorks 堆疊會自動將日誌打開到錯誤中,該錯誤通常位於文件末尾。

使用 CLI 或 API 檢視 Chef 日誌

您可以使用 AWS OpsWorks 堆疊 CLI describe-commands命令或 DescribeCommandsAPI 動作來檢視存放在 Amazon S3 儲存貯體上的日誌。以下說明如何使用 CLI 檢視指定執行個體目前任一組日誌檔案。使用 DescribeCommands 的程序基本上很類似。

使用 AWS OpsWorks 堆疊檢視執行個體的 Chef 記錄
  1. 開啟執行個體的詳細資料頁面並複製其 OpsWorksID 值。

  2. 使用此 ID 值執行 describe-commands CLI 命令,如下所示:

    aws opsworks describe-commands --instance-id 67bf0da2-29ed-4217-990c-d895d51812b9

    該命令為 AWS OpsWorks Stacks 在實例上執行的每個命令返回一個帶有嵌入對象的 JSON 對象,其中包含最新的對象。Type 參數包含每個內嵌物件的命令類型,在此範例中為 configure 命令和 setup 命令。

    { "Commands": [ { "Status": "successful", "CompletedAt": "2013-10-25T19:38:36+00:00", "InstanceId": "67bf0da2-29ed-4217-990c-d895d51812b9", "AcknowledgedAt": "2013-10-25T19:38:24+00:00", "LogUrl": "https://s3.amazonaws.com/prod_stage-log/logs/b6c402df-5c23-45b2-a707-ad20b9c5ae40?AWSAccessKeyId=AKIAIOSFODNN7EXAMPLE &Expires=1382731518&Signature=YkqS5IZN2P4wixjHwoC3aCMbn5s%3D&response-cache-control=private&response-content-encoding=gzip&response-content- type=text%2Fplain", "Type": "configure", "CommandId": "b6c402df-5c23-45b2-a707-ad20b9c5ae40", "CreatedAt": "2013-10-25T19:38:11+00:00", "ExitCode": 0 }, { "Status": "successful", "CompletedAt": "2013-10-25T19:31:08+00:00", "InstanceId": "67bf0da2-29ed-4217-990c-d895d51812b9", "AcknowledgedAt": "2013-10-25T19:29:01+00:00", "LogUrl": "https://s3.amazonaws.com/prod_stage-log/logs/2a90e862-f974-42a6-9342-9a4f03468358?AWSAccessKeyId=AKIAIOSFODNN7EXAMPLE &Expires=1382731518&Signature=cxKYHO8mCCd4MvOyFb6ywebeQtA%3D&response-cache-control=private&response-content-encoding=gzip&response-content- type=text%2Fplain", "Type": "setup", "CommandId": "2a90e862-f974-42a6-9342-9a4f03468358", "CreatedAt": "2013-10-25T19:26:01+00:00", "ExitCode": 0 } ] }
  3. LogUrl 值複製到瀏覽器以檢視日誌。

如果執行個體具有多個命令,您可以將參數新增至 describe-commands,篩選出哪些命令應包含在回應物件中。如需詳細資訊,請參閱 describe-commands

在執行個體上檢視 Chef 日誌

注意

本節中的主題適用於 Chef 12。如需 Chef 11.10 和較舊版 Chef 日誌位置的資訊,請參閱為 Linux 的 Chef 11.10 和更舊版本進行故障診斷

Linux 執行個體

AWS OpsWorks 堆棧將每個實例的 Chef 日誌存儲在其/var/chef/runs目錄中。(對於 Linux 執行個體,此目錄也包含相關聯的資料包,其以 JSON 格式的檔案存放)。您需要 sudo 權限才能存取此目錄。每個執行的日誌位於個別執行子目錄中名為 chef.log 的檔案。

AWS OpsWorks 堆疊會將其內部記錄儲存在執行個體的/var/log/aws/opsworks資料夾中。此資訊通常對故障診斷的幫助不大。不過,這些記錄對「 AWS OpsWorks 堆疊」支援很有用,如果您遇到服務問題,系統可能會要求您提供這些記錄。Linux 日誌有時也可提供有用的故障診斷資料。

Windows 執行個體

代理程式日誌

在 Windows 執行個體上, OpsWorks 記錄會儲存在如下所示的ProgramData路徑中。數字會包含時間戳記。

C:\ProgramData\OpsWorksAgent\var\logs\number
注意

根據預設,ProgramData 是隱藏的資料夾。若要將其取消隱藏,請導覽至 Folder Options (資料夾選項)。在 View (檢視) 下,選擇顯示隱藏檔案的選項。

下列範例示範 Windows 執行個體上的代理程式日誌。

Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 5/24/2015 11:59 PM 127277 command.20150524.txt -a--- 5/25/2015 11:59 PM 546772 command.20150525.txt -a--- 5/26/2015 11:59 PM 551514 command.20150526.txt -a--- 5/27/2015 9:43 PM 495181 command.20150527.txt -a--- 5/24/2015 11:59 PM 24353 keepalive.20150524.txt -a--- 5/25/2015 11:59 PM 106232 keepalive.20150525.txt -a--- 5/26/2015 11:59 PM 106208 keepalive.20150526.txt -a--- 5/27/2015 8:54 PM 92593 keepalive.20150527.txt -a--- 5/24/2015 7:19 PM 3891 service.20150524.txt -a--- 5/27/2015 8:54 PM 1493 service.20150527.txt -a--- 5/24/2015 11:59 PM 112549 wire.20150524.txt -a--- 5/25/2015 11:59 PM 501501 wire.20150525.txt -a--- 5/26/2015 11:59 PM 499640 wire.20150526.txt -a--- 5/27/2015 8:54 PM 436870 wire.20150527.txt
Chef 日誌

在 Windows 執行個體上,Chef 日誌存放於 ProgramData 路徑,如下所示。數字會包含時間戳記。

C:\ProgramData\OpsWorksAgent\var\commands\number
注意

此目錄僅包含第一個(OpsWorks 擁有的)Chef 運行的輸出。

下列範例顯示 Windows 執行個體上 OpsWorks 擁有的 Chef 記錄檔。

Mode LastWriteTime Name ---- ------------- ---- d---- 5/24/2015 7:23 PM configure-7ecb5f47-7626-439b-877f-5e7cb40ab8be d---- 5/26/2015 8:30 PM configure-8e74223b-d15d-4372-aeea-a87b428ffc2b d---- 5/24/2015 6:34 PM configure-c3980a1c-3d08-46eb-9bae-63514cee194b d---- 5/26/2015 8:32 PM grant_remote_access-70dbf834-1bfa-4fce-b195-e50e85402f4c d---- 5/26/2015 10:30 PM revoke_remote_access-1111fce9-843a-4b27-b93f-ecc7c5e9e05b d---- 5/24/2015 7:21 PM setup-754ec063-8b60-4cd4-b6d7-0e89d7b7aa78 d---- 5/26/2015 8:27 PM setup-af5bed36-5afd-4115-af35-5766f88bc039 d---- 5/24/2015 6:32 PM setup-d8abeffa-24d4-414b-bfb1-4ad07319f358 d---- 5/24/2015 7:13 PM shutdown-c7130435-9b5c-4a95-be17-6b988fc6cf9a d---- 5/26/2015 8:25 PM sync_remote_users-64c79bdc-1f6f-4517-865b-23d2def4180c d---- 5/26/2015 8:48 PM update_custom_cookbooks-2cc59a94-315b-414d-85eb-2bdea6d76c6a
使用者 Chef 日誌

您可以在名為 logfile.txt 的檔案中找到您 Chef 執行的日誌,該檔案則位於以編號之 Chef 命令命名的資料夾中,如下圖所示。

C:/chef └── runs └── command-12345 ├── attribs.json ├── client.rb └── logfile.txt

解讀 Chef 日誌

日誌的開頭主要包含內部 Chef 記錄。

# Logfile created on Thu Oct 17 17:25:12 +0000 2013 by logger.rb/1.2.6 [2013-10-17T17:25:12+00:00] INFO: *** Chef 11.4.4 *** [2013-10-17T17:25:13+00:00] DEBUG: Building node object for php-app1.localdomain [2013-10-17T17:25:13+00:00] DEBUG: Extracting run list from JSON attributes provided on command line [2013-10-17T17:25:13+00:00] INFO: Setting the run_list to ["opsworks_custom_cookbooks::load", "opsworks_custom_cookbooks::execute"] from JSON [2013-10-17T17:25:13+00:00] DEBUG: Applying attributes from json file [2013-10-17T17:25:13+00:00] DEBUG: Platform is amazon version 2013.03 [2013-10-17T17:25:13+00:00] INFO: Run List is [recipe[opsworks_custom_cookbooks::load], recipe[opsworks_custom_cookbooks::execute]] [2013-10-17T17:25:13+00:00] INFO: Run List expands to [opsworks_custom_cookbooks::load, opsworks_custom_cookbooks::execute] [2013-10-17T17:25:13+00:00] INFO: Starting Chef Run for php-app1.localdomain [2013-10-17T17:25:13+00:00] INFO: Running start handlers [2013-10-17T17:25:13+00:00] INFO: Start handlers complete. [2013-10-17T17:25:13+00:00] DEBUG: No chefignore file found at /opt/aws/opsworks/releases/20131015111601_209/cookbooks/chefignore no files will be ignored [2013-10-17T17:25:13+00:00] DEBUG: Cookbooks to compile: ["gem_support", "packages", "opsworks_bundler", "opsworks_rubygems", "ruby", "ruby_enterprise", "dependencies", "opsworks_commons", "scm_helper", :opsworks_custom_cookbooks] [2013-10-17T17:25:13+00:00] DEBUG: Loading cookbook gem_support's library file: /opt/aws/opsworks/releases/20131015111601_209/cookbooks/gem_support/libraries/current_gem_version.rb [2013-10-17T17:25:13+00:00] DEBUG: Loading cookbook packages's library file: /opt/aws/opsworks/releases/20131015111601_209/cookbooks/packages/libraries/packages.rb [2013-10-17T17:25:13+00:00] DEBUG: Loading cookbook dependencies's library file: /opt/aws/opsworks/releases/20131015111601_209/cookbooks/dependencies/libraries/current_gem_version.rb [2013-10-17T17:25:13+00:00] DEBUG: Loading cookbook opsworks_commons's library file: /opt/aws/opsworks/releases/20131015111601_209/cookbooks/opsworks_commons/libraries/activesupport_blank.rb [2013-10-17T17:25:13+00:00] DEBUG: Loading cookbook opsworks_commons's library file: /opt/aws/opsworks/releases/20131015111601_209/cookbooks/opsworks_commons/libraries/monkey_patch_chefgem_resource.rb ...

檔案的這一部分對 Chef 專家來說非常有用。請注意,即使大多數命令包含更多配方,執行清單僅含兩個配方。這兩個配方處理載入和執行其他所有內建和自訂配方的任務。

檔案最有趣的部分通常在結尾。如果執行成功結束,您應該會看到類似以下的內容:

... [Tue, 11 Jun 2013 16:00:50 +0000] DEBUG: STDERR: [Tue, 11 Jun 2013 16:00:50 +0000] DEBUG: ---- End output of /sbin/service mysqld restart ---- [Tue, 11 Jun 2013 16:00:50 +0000] DEBUG: Ran /sbin/service mysqld restart returned 0 [Tue, 11 Jun 2013 16:00:50 +0000] INFO: service[mysql]: restarted successfully [Tue, 11 Jun 2013 16:00:50 +0000] INFO: Chef Run complete in 84.07096 seconds [Tue, 11 Jun 2013 16:00:50 +0000] INFO: cleaning the checksum cache [Tue, 11 Jun 2013 16:00:50 +0000] DEBUG: removing unused checksum cache file /var/chef/cache/checksums/chef-file--tmp-chef-rendered-template20130611-4899-8wef7e-0 [Tue, 11 Jun 2013 16:00:50 +0000] DEBUG: removing unused checksum cache file /var/chef/cache/checksums/chef-file--tmp-chef-rendered-template20130611-4899-1xpwyb6-0 [Tue, 11 Jun 2013 16:00:50 +0000] DEBUG: removing unused checksum cache file /var/chef/cache/checksums/chef-file--etc-monit-conf [Tue, 11 Jun 2013 16:00:50 +0000] INFO: Running report handlers [Tue, 11 Jun 2013 16:00:50 +0000] INFO: Report handlers complete [Tue, 11 Jun 2013 16:00:50 +0000] DEBUG: Exiting
注意

您可以使用代理程式 CLI 在執行期間或結束後顯示日誌結尾。如需詳細資訊,請參閱 顯示 Chef 日誌

如果配方失敗,您應該尋找 ERROR 層級的輸出,該輸出將包含例外狀況,並接續著 Chef 堆疊追蹤,如下所示:

... Please report any problems with the /usr/scripts/mysqlbug script! [ OK ] MySQL Daemon failed to start. Starting mysqld: [FAILED]STDERR: 130611 15:07:55 [Warning] The syntax '--log-slow-queries' is deprecated and will be removed in a future release. Please use '--slow-query-log'/'--slow-query-log-file' instead. 130611 15:07:56 [Warning] The syntax '--log-slow-queries' is deprecated and will be removed in a future release. Please use '--slow-query-log'/'--slow-query-log-file' instead. ---- End output of /sbin/service mysqld start ---- /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/mixin/command.rb:184:in `handle_command_failures' /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/mixin/command.rb:131:in `run_command' /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/provider/service/init.rb:37:in `start_service' /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/provider/service.rb:60:in `action_start' /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/resource.rb:406:in `send' /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/resource.rb:406:in `run_action' /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/runner.rb:53:in `run_action' /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/runner.rb:89:in `converge' /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/runner.rb:89:in `each' /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/runner.rb:89:in `converge' /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/resource_collection.rb:94:in `execute_each_resource' /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/resource_collection/stepable_iterator.rb:116:in `call' /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/resource_collection/stepable_iterator.rb:116:in `call_iterator_block' /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/resource_collection/stepable_iterator.rb:85:in `step' /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/resource_collection/stepable_iterator.rb:104:in `iterate' /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/resource_collection/stepable_iterator.rb:55:in `each_with_index' /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/resource_collection.rb:92:in `execute_each_resource' /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/runner.rb:84:in `converge' /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/client.rb:268:in `converge' /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/client.rb:158:in `run' /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/application/solo.rb:190:in `run_application' /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/application/solo.rb:181:in `loop' /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/application/solo.rb:181:in `run_application' /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/../lib/chef/application.rb:62:in `run' /opt/aws/opsworks/releases/20130605160141_122/vendor/bundle/ruby/1.8/gems/chef-0.9.15.5/bin/chef-solo:25 /opt/aws/opsworks/current/bin/chef-solo:16:in `load' /opt/aws/opsworks/current/bin/chef-solo:16

檔案的結尾是 Chef 堆疊追蹤。您也應該檢查例外狀況之前的輸出,通常包含系統錯誤 (例如 package not available),這在故判斷失敗原因時也很有用。在此範例中,MySQL 協助程式無法啟動。

常見的 Chef 日誌錯誤

下列是一些常見的 Chef 日誌錯誤及其解決方法。

找不到日誌

在 Chef 執行開始時,執行個體會收到預先簽署的 Amazon S3 URL,讓您在 Chef 執行完成時在網頁上檢視日誌。由於此 URL 會在兩小時後過期,所以如果 Chef 執行時間超過兩個小時,就不會將日誌上傳到 Amazon S3 網站,即使 Chef 執行期間沒有發生任何問題。建立日誌的命令會成功,但僅可在執行個體上檢視日誌,而不能在預先簽章的 URL 上檢視。

日誌意外結束

如果 Chef 日誌意外結束,並未指出成功或顯示錯誤資訊,您可能發生記憶體低下的狀態,讓 Chef 無法完成日誌。最佳選項就是使用更大的執行個體重試。

缺少技術指南或配方

如果 Chef 執行發生技術指南或配方不在技術指南快取內的情況,您將看到類似以下的內容:

DEBUG: Loading Recipe mycookbook::myrecipe via include_recipe ERROR: Caught exception during execution of custom recipe: mycookbook::myrecipe: Cannot find a cookbook named mycookbook; did you forget to add metadata to a cookbook?

此項目指出 mycookbook 技術指南不在技術指南快取內。使用 Chef 11.4 時,如果您未在 metadata.rb 中正確宣告相依性,也可能發生此錯誤。

AWS OpsWorks 堆疊會從執行個體的食譜快取執行方法。其會在執行個體啟動時,將儲存庫中的技術指南下載到此快取中。不過,如果您之後修改儲存庫中的食譜, AWS OpsWorks Stacks 不會自動更新線上執行個體的快取。如果您在執行個體啟動後,修改技術指南或新增技術指南,請執行以下步驟:

  1. 確定您已將變更遞交到儲存庫。

  2. 執行更新技術指南堆疊命令,以使用儲存庫中的最新版本來更新技術指南快取。

本機命令失敗

如果 Chef execute 資源無法執行指定命令,您將會看到類似以下的內容:

DEBUG: ---- End output of ./configure --with-config-file-path=/ returned 2 ERROR: execute[PHP: ./configure] (/root/opsworks-agent/site-cookbooks/php-fpm/recipes/install.rb line 48) had an error: ./configure --with-config-file-path=/

向上捲動日誌,您應該會看到命令的 stderrstdout 輸出,這有助您判斷命令失敗的原因。

套件失敗

如果套件安裝失敗,您將會看到類似以下的內容:

ERROR: package[zend-server-ce-php-5.3] (/root/opsworks-agent/site-cookbooks/zend_server/recipes/install.rb line 20) had an error: apt-get -q -y --force-yes install zend-server-ce-php-5.3=5.0.4+b17 returned 100, expected 0

向上捲動日誌,您應該會看到命令的 STDOUT 和 STDERROR 輸出,這有助您判斷套件安裝失敗的原因。