您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

快速入门:使用 Azure Monitor 从混合环境中的 Linux 计算机收集数据Quickstart: Collect data from a Linux computer in a hybrid environment with Azure Monitor

Azure Monitor 可以直接从环境中的物理或虚拟 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 Monitor 从数据中心或其他云环境收集数据。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 Analytics” 。In 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.

获取工作区 ID 和密钥Obtain workspace ID and key

在安装适用于 Linux 的 Log Analytics 代理前,需要先获得 Log Analytics 工作区的工作区 ID 和秘钥。Before installing the Log Analytics agent for Linux, you need the workspace ID and key for your Log Analytics workspace. 代理包装器脚本需要使用此信息来正确配置代理,并确保它能与 Azure Monitor 成功通信。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 Monitor 期间,Windows 或 Linux 的 Operations Management Suite 代理称为 Windows 或 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. “工作区 ID”和“主密钥”右侧的值 。The value to the right of Workspace ID and Primary Key. 将它们复制并粘贴到喜爱的编辑器中。Copy and paste both into your favorite editor.

安装适用于 Linux 的代理Install the agent for Linux

以下步骤配置在 Azure 和 Azure 政府云中用于 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 计算机需要通过代理服务器与 Log Analytics 通信,可以在命令行中指定代理配置,方法是包括 -p [protocol://][user:password@]proxyhost[:port]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 属性接受代理服务器的完全限定域名或 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 工作区,请运行以下命令,并提供先前复制的工作区 ID 和主密钥。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 代理参数和示例语法。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 政府云中的 Log Analytics 工作区,请运行以下命令,并提供先前所复制的工作区 ID 和主密钥。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 代理参数和示例语法。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 Monitor 可以从你为长期分析和报告指定的 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. 选择“数据” ,然后选择 SyslogSelect 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.