使用 Azure 監視器從混合式環境中的 Linux 電腦收集資料Collect data from a Linux computer in a hybrid environment with Azure Monitor

Azure 監視器可將來自環境中實體或虛擬 Linux 電腦的資料直接收集到 Log Analytics 工作區,以供詳細分析和相互關聯。Azure Monitor can collect data directly from your physical or virtual Linux computers in your environment into a Log Analytics workspace for detailed analysis and correlation. 安裝 Log Analytics 代理程式可讓 Azure 監視器從資料中心或其他雲端環境收集資料。Installing the Log Analytics agent allows Azure Monitor to collect data from a datacenter or other cloud environment. 本快速入門向您示範如何以幾個簡單步驟來設定和收集 Linux 伺服器中的資料。This quickstart shows you how to configure and collect data from your Linux server with a few easy steps. 如需 Azure Linux VM 的相關資訊,請參閱收集關於 Azure 虛擬機器的資料For information about Azure Linux VMs, see Collect data about Azure virtual machines.

若要了解支援的組態,請參閱支援的 Windows 作業系統網路防火牆組態To understand the supported configuration, see Supported Windows operating systems and Network firewall configuration.

如果您沒有 Azure 訂用帳戶,請在開始前建立 免費帳戶If you don't have an Azure subscription, create a free account before you begin.

登入 Azure 入口網站Sign in to the Azure portal

https://portal.azure.com 登入 Azure 入口網站。Sign in to the Azure portal at https://portal.azure.com.

建立工作區Create a workspace

  1. 在 Azure 入口網站中,選取 [所有服務] 。In the Azure portal, select All services. 在資源清單中輸入 Log AnalyticsIn the list of resources, type Log Analytics. 當您開始輸入時,清單會根據您輸入的文字進行篩選。As you begin typing, the list filters based on your input. 選取 [Log Analytics 工作區] 。Select Log Analytics workspaces.

    Azure 入口網站

  2. 選取 [建立] ,然後選取下列項目的選項:Select Create, and then select choices for the following items:

    • 為新的 [Log Analytics 工作區] 提供名稱,例如 DefaultLAWorkspace 。Provide a name for the new Log Analytics workspace, such as DefaultLAWorkspace.

    • 如果選取的預設值不合適,請從下拉式清單中選取要連結的 [訂用帳戶] 。Select a Subscription to link to by selecting from the drop-down list if the default selected is not appropriate.

    • 對於 [資源群組] ,選取包含一或多個 Azure 虛擬機器的現有資源群組。For Resource Group, select an existing resource group that contains one or more Azure virtual machines.

    • 選取要部署 VM 的 [位置] 。Select the Location your VMs are deployed to. 如需詳細資訊,請查看 Log Analytics 的可用區域For additional information, see which regions Log Analytics is available in.

    • 如果您要在 2018 年 4 月 2 之後建立的新訂用帳戶中建立工作區,系統會自動使用「每 GB」 定價方案和選項來選取將無法使用的定價層。If you are creating a workspace in a new subscription created after April 2, 2018, it will automatically use the Per GB pricing plan and the option to select a pricing tier will not be available. 如果您要為在 4 月 2 日之前建立的現有訂用帳戶,或已繫結至現有 EA 註冊的訂用帳戶建立工作區,請選取您偏好的定價層。If you are creating a workspace for an existing subscription created before April 2, or to subscription that was tied to an existing EA enrollment, select your preferred pricing tier. 如需特定層的詳細資訊,請參閱 Log Analytics 價格詳細資料For additional information about the particular tiers, see Log Analytics Pricing Details.

      [建立 Log Analytics] 資源刀鋒視窗

  3. 在 [Log Analytics 工作區] 窗格中提供必要資訊之後,選取 [確定] 。After providing the required information on the Log Analytics workspace pane, select OK.

在驗證資訊及建立工作區時,您可以在功能表的 [通知] 底下追蹤其進度。While the information is verified and the workspace is created, you can track its progress under Notifications from the menu.

取得工作區識別碼和金鑰Obtain workspace ID and key

安裝適用於 Linux 的 Log Analytics 代理程式之前,您需要 Log Analytics 工作區的工作區識別碼和金鑰。Before installing the Log Analytics agent for Linux, you need the workspace ID and key for your Log Analytics workspace. 代理程式的包裝函式指令碼需要這項資訊,才能正確設定代理程式,並確定它能與 Azure 監視器順利進行通訊。This information is required by the agent wrapper script to properly configure the agent and ensure it can successfully communicate with Azure Monitor.

注意

Microsoft Operations Management Suite 正在轉換為 Azure 監視器,而適用於 Windows 或 Linux 的 Operations Management Suite 代理程式屬於此轉換的一部份,之後會將其稱為適用於 Windows 的 Log Analytics 代理程式和適用於 Linux 的 Log Analytics 代理程式。As part of the ongoing transition from Microsoft Operations Management Suite to Azure Monitor, the Operations Management Suite Agent for Windows or Linux will be referred to as the Log Analytics agent for Windows and Log Analytics agent for Linux.

  1. 在 Azure 入口網站的左上角,選取 [所有服務] 。In the upper-left corner of the Azure portal, select All services. 在搜尋方塊中,輸入 Log AnalyticsIn the search box, enter Log Analytics. 當您輸入時,清單會根據您輸入的文字進行篩選。As you type, the list filters based on your input. 選取 [Log Analytics 工作區] 。Select Log Analytics workspaces.

  2. 在 Log Analytics 工作區清單中,選取您稍早建立的工作區In your list of Log Analytics workspaces, select the workspace you created earlier. (您可能已經將它命名為 DefaultLAWorkspace)。(You might have named it DefaultLAWorkspace.)

  3. 選取 [進階設定] :Select Advanced settings:

    Log Analytics 進階設定

  4. 選取 [連接的來源] ,然後選取 [Linux 伺服器] 。Select Connected Sources, and then select Linux Servers.

  5. [工作區識別碼] 和 [主要金鑰] 右邊的值。The value to the right of Workspace ID and Primary Key. 將兩者複製並貼到您最愛的編輯器。Copy and paste both into your favorite editor.

安裝 Agent for LinuxInstall the agent for Linux

下列步驟會設定 Azure 和 Azure Government 雲端中 Log Analytics 代理程式的安裝程式。The following steps configure setup of the agent for Log Analytics in Azure and Azure Government cloud.

注意

適用於 Linux 的 Log Analytics 代理程式無法設定為回報多個 Log Analytics 工作區。The Log Analytics agent for Linux cannot be configured to report to more than one Log Analytics workspace.

如果您的 Linux 電腦需要透過 Proxy 伺服器與 Log Analytics 通訊,您可以在命令列中加上 -p [protocol://][user:password@]proxyhost[:port] 藉以指定 Proxy 組態。If your Linux computer needs to communicate through a proxy server to Log Analytics, the proxy configuration can be specified on the command line by including -p [protocol://][user:password@]proxyhost[:port]. proxyhost 屬性接受 Proxy 伺服器的完整網域名稱或 IP 位址。The proxyhost property accepts a fully qualified domain name or IP address of the proxy server.

例如:https://user01:password@proxy01.contoso.com:30443For example: https://user01:password@proxy01.contoso.com:30443

  1. 若要設定 Linux 電腦以連線到 Log Analytics 工作區,請執行下列命令,提供稍早複製的工作區識別碼和主索引鍵。To configure the Linux computer to connect to a Log Analytics workspace, run the following command providing the workspace ID and primary key copied earlier. 下列命令會下載代理程式、驗證其總和檢查碼,並加以安裝。The following command downloads the agent, validates its checksum, and installs it.

    wget https://raw.githubusercontent.com/Microsoft/OMS-Agent-for-Linux/master/installer/scripts/onboard_agent.sh && sh onboard_agent.sh -w <YOUR WORKSPACE ID> -s <YOUR WORKSPACE PRIMARY KEY>
    

    下列命令會加上 -p Proxy 參數和範例語法。The following command includes the -p proxy parameter and example syntax.

     wget https://raw.githubusercontent.com/Microsoft/OMS-Agent-for-Linux/master/installer/scripts/onboard_agent.sh && sh onboard_agent.sh -p [protocol://][user:password@]proxyhost[:port] -w <YOUR WORKSPACE ID> -s <YOUR WORKSPACE PRIMARY KEY>
    
  2. 若要設定 Linux 電腦以連線到 Azure Government 雲端中的 Log Analytics 工作區,請執行下列命令,提供稍早複製的工作區識別碼和主索引鍵。To configure the Linux computer to connect to Log Analytics workspace in Azure Government cloud, run the following command providing the workspace ID and primary key copied earlier. 下列命令會下載代理程式、驗證其總和檢查碼,並加以安裝。The following command downloads the agent, validates its checksum, and installs it.

    wget https://raw.githubusercontent.com/Microsoft/OMS-Agent-for-Linux/master/installer/scripts/onboard_agent.sh && sh onboard_agent.sh -w <YOUR WORKSPACE ID> -s <YOUR WORKSPACE PRIMARY KEY> -d opinsights.azure.us
    

    下列命令會加上 -p Proxy 參數和範例語法。The following command includes the -p proxy parameter and example syntax.

     wget https://raw.githubusercontent.com/Microsoft/OMS-Agent-for-Linux/master/installer/scripts/onboard_agent.sh && sh onboard_agent.sh -p [protocol://][user:password@]proxyhost[:port] -w <YOUR WORKSPACE ID> -s <YOUR WORKSPACE PRIMARY KEY> -d opinsights.azure.us
    
  3. 執行下列命令來重新啟動代理程式:Restart the agent by running the following command:

    sudo /opt/microsoft/omsagent/bin/service_control restart [<workspace id>]
    

收集事件和效能資料Collect event and performance data

Azure 監視器可以從 Linux Syslog 收集事件,和收集您指定要用於較長期分析和報告的效能計數器。Azure Monitor can collect events from the Linux Syslog and performance counters that you specify for longer term analysis and reporting. 它也可以在偵測到特定條件時採取動作。It can also take action when it detects a particular condition. 請遵循下列步驟來開始設定收集 Linux Syslog 的事件以及數個常用的效能計數器。Follow these steps to configure collection of events from the Linux Syslog, and several common performance counters to start with.

  1. 在 Azure 入口網站的左下角,選取 [更多服務] 。In the lower-left corner of the Azure portal, select More services. 在搜尋方塊中,輸入 Log AnalyticsIn the search box, enter Log Analytics. 當您輸入時,清單會根據您輸入的文字進行篩選。As you type, the list filters based on your input. 選取 [Log Analytics 工作區] 。Select Log Analytics workspaces.

  2. 選取 [資料] ,然後選取 [Syslog] 。Select Data, and then select Syslog.

  3. 您可以輸入記錄的名稱,來新增 syslog。You add syslog by typing in the name of the log. 輸入 Syslog,然後選取加號 +Enter Syslog and then select the plus sign +.

  4. 在表格中,取消選取 [資訊] 、[注意] 和 [偵錯] 嚴重性。In the table, uncheck the severities Info, Notice and Debug.

  5. 選取頁面頂端的 [儲存] 來儲存設定。Select Save at the top of the page to save the configuration.

  6. 選取 [Linux 效能資料] 以啟用 Linux 電腦上的效能計數器收集。Select Linux Performance Data to enable collection of performance counters on a Linux computer.

  7. 當您第一次為新的 Log Analytics 工作區設定 Linux 效能計數器時,系統會提供選項,讓您快速建立數個常用的計數器。When you first configure Linux Performance counters for a new Log Analytics workspace, you are given the option to quickly create several common counters. 這些計數器旁邊皆會列出核取方塊。They are listed with a checkbox next to each.

    選取的預設 Windows 效能計數器

    選取 [將下列設定套用至我的機器] ,然後選取 [新增選取的效能計數器] 。Select Apply below configuration to to my machines and then select Add the selected performance counters. 隨即會新增且收集取樣間隔時間的預設值為 10 秒。They are added and preset with a ten second collection sample interval.

  8. 選取頁面頂端的 [儲存] 來儲存設定。Select Save at the top of the page to save the configuration.

檢視收集的資料View data collected

既然您已啟用資料收集,現在即可執行簡單的記錄搜尋範例,以查看來自目標電腦的一些資料。Now that you have enabled data collection, lets run a simple log search example to see some data from the target computer.

  1. 在選取的工作區中,從左側窗格中選取 [記錄] 。In the selected workspace, from the left-hand pane, select Logs.

  2. 在記錄查詢頁面的查詢編輯器中輸入 Perf,然後選取 [執行] 。On the Logs query page, type Perf in the query editor and select Run.

    Log Analytics 記錄搜尋

    例如,下圖中的查詢會傳回 10,000 筆效能記錄。For example, the query in the following image returned 10,000 Performance records. 結果將會大幅減少。Your results will be significantly less.

    Log Analytics 記錄搜尋結果

清除資源Clean up resources

不再需要時,您可以從 Linux 電腦移除代理程式,並刪除 Log Analytics 工作區。When no longer needed, you can remove the agent from the Linux computer and delete the Log Analytics workspace.

若要移除代理程式,請在 Linux 電腦上執行下列命令。To remove the agent, run the following command on the Linux computer. --purge 引數可將代理程式及其組態完全移除。The --purge argument completely removes the agent and its configuration.

wget https://raw.githubusercontent.com/Microsoft/OMS-Agent-for-Linux/master/installer/scripts/onboard_agent.sh && sh onboard_agent.sh --purge

若要刪除工作區,請選取您先前建立的 Log Analytics 工作區,然後在資源頁面上,選取 [刪除] 。To delete the workspace, select the Log Analytics workspace you created earlier and on the resource page select Delete.

刪除 Log Analytics 資源

後續步驟Next steps

既然您正在從內部部署 Linux 電腦收集作業和效能資料,即可輕鬆開始針對收集的資料「免費」 進行探索、分析及採取行動。Now that you are collecting operational and performance data from your on-premises Linux computer, you can easily begin exploring, analyzing, and taking action on data that you collect for free.

若要了解如何檢視和分析資料,請繼續進行本教學課程。To learn how to view and analyze the data, continue to the tutorial.