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

在 Grafana 中监控 Azure 服务Monitor your Azure services in Grafana

你现在可以使用 Azure Monitor 数据源插件Grafana 监控 Azure 服务和应用程序。You can now monitor Azure services and applications from Grafana using the Azure Monitor data source plugin. 该插件收集 Azure Monitor 所收集的应用程序性能数据,包括各种日志和指标。The plugin gathers application performance data collected by Azure Monitor, including various logs and metrics. 随后,可以在 Grafana 仪表板上显示此数据。You can then display this data on your Grafana dashboard.

使用以下步设置 Grafana 服务器,并通过 Azure Monitor 为指标和日志生成仪表板。Use the following steps to set up a Grafana server and build dashboards for metrics and logs from Azure Monitor.

设置 Grafana 服务器Set up a Grafana server

本地设置 GrafanaSet up Grafana locally

若要设置本地的 Grafana 服务器,请在本地环境下载并安装 GrafanaTo set up a local Grafana server, download and install Grafana in your local environment. 若要使用插件的 Azure Monitor 集成,请安装 Grafana 5.3 或更高版本。To use the plugin's Azure Monitor integration, install Grafana version 5.3 or higher.

通过 Azure 市场在 Azure 上设置 GrafanaSet up Grafana on Azure through the Azure Marketplace

  1. 转到 Azure 市场并选取 Grafana Labs 的 Grafana。Go to Azure Marketplace and pick Grafana by Grafana Labs.

  2. 填写名称和详细信息。Fill in the names and details. 创建新的资源组。Create a new resource group. 记录为 VM 用户名、VM 密码和 Grafana 服务器管理员密码选择的值。Keep track of the values you choose for the VM username, VM password, and Grafana server admin password.

  3. 选择 VM 大小和存储帐户。Choose VM size and a storage account.

  4. 配置网络配置设置。Configure the network configuration settings.

  5. 接受使用条款之后,查看摘要,然后选择“创建”。View the summary and select Create after accepting the terms of use.

  6. 部署完成后,选择“转到资源组”。After the deployment completes, select Go to Resource Group. 你将看到新创建的资源列表。You see a list of newly created resources.

    Grafana 资源组对象

    如果选择网络安全组(在本例中为 grafana nsg),你可以看到端口 3000 用于访问 Grafana 服务器。If you select the network security group (grafana-nsg in this case), you can see that port 3000 is used to access Grafana server.

  7. 获取 Grafana 服务器的公共 IP 地址 - 返回到资源列表,然后选择“公共 IP 地址”。Get the public IP address of your Grafana server - go back to the list of resources and select Public IP address.

登录到 GrafanaSign in to Grafana

  1. 使用服务器的 IP 地址在浏览器中打开登录页 http://<IP 地址>:3000 或 <DNSName>:3000。Using the IP address of your server, open the Login page at http://<IP address>:3000 or the <DNSName>:3000 in your browser. 尽管默认端口为 3000,但请注意可能在设置期间选择了其他端口。While 3000 is the default port, note you might have selected a different port during setup. 你应看到生成的 Grafana 服务器登录页。You should see a login page for the Grafana server you built.

    Grafana 登录屏幕

  2. 使用用户名 admin 和之前创建的 Grafana 服务器管理员密码登录。Sign in with the user name admin and the Grafana server admin password you created earlier. 如果使用本地安装程序,默认密码将为 admin,并要求你在首次登陆时更改密码。If you're using a local setup, the default password would be admin, and you'd be requested to change it on your first login.

配置数据源插件Configure data source plugin

成功登录后,你应看到 Azure Monitor 数据源插件已包括在内。Once successfully logged in, you should see that the Azure Monitor data source plugin is already included.

Grafana 包括 Azure Monitor 插件

  1. 选择“添加数据源”,以添加并配置 Azure Monitor 数据源。Select Add data source to add and configure the Azure Monitor data source.

  2. 为数据源选择名称,并从下拉列表中选择“Azure Monitor”作为类型。Pick a name for the data source and select Azure Monitor as the type from the dropdown.

  3. 创建服务主体 - Grafana 使用 Azure Active Directory 服务主体连接到 Azure Monitor API 并收集数据。Create a service principal - Grafana uses an Azure Active Directory service principal to connect to Azure Monitor APIs and collect data. 必须创建新的或使用现有的服务主体,以管理对 Azure 资源的访问权限。You must create, or use an existing service principal, to manage access to your Azure resources.

    • 请参阅这些说明以创建服务主体。See these instructions to create a service principal. 复制并保存租户 ID(目录 ID)、客户端 ID(应用程序 ID)和客户端密码(应用程序密钥值)。Copy and save your tenant ID (Directory ID), client ID (Application ID) and client secret (Application key value).
    • 请参阅将应用程序分配到角色,以向要监视的订阅、资源组或资源上的 Azure Active Directory 应用程序分配读者角色。See Assign application to role to assign the Reader role to the Azure Active Directory application on the subscription, resource group or resource you want to monitor. Log Analytics API 需要 Log Analytics 读者角色,其中包括读者角色权限并向其添加。The Log Analytics API requires the Log Analytics Reader role, which includes the Reader role's permissions and adds to it.
  4. 为要使用的 API 提供连接详细信息。Provide the connection details to the APIs you'd like to use. 可以连接到所有或其中部分。You can connect to all or to some of them.

    • 如果连接到两个指标并登录 Azure Monitor,可以通过选择“与 Azure Monitor API 相同的详细信息”重复使用相同的凭据。If you connect to both metrics and logs in Azure Monitor, you can reuse the same credentials by selecting Same details as Azure Monitor API.

    • 配置插件时,可以指示插件要监控哪个 Azure 云(公共、Azure 美国政府、Azure 德国或 Azure 中国)。When configuring the plugin, you can indicate which Azure Cloud you would like the plugin to monitor (Public, Azure US Government, Azure Germany, or Azure China).

    • 如果使用 Application Insights,还可以包含Application Insights API 和应用程序 ID,以收集基于 Application Insights 的指标。If you use Application Insights, you can also include your Application Insights API and application ID to collect Application Insights based metrics. 有关详细信息,请参阅获取 API 密钥和应用程序 IDFor more information, see Getting your API key and Application ID.


      某些数据源字段的命名方式不同于其关联的 Azure 设置:Some data source fields are named differently than their correlated Azure settings:

      • 租户 ID 是 Azure 目录 IDTenant ID is the Azure Directory ID
      • 客户端 ID 是 Azure Active Directory 应用程序 IDClient ID is the Azure Active Directory Application ID
      • 客户端密钥是 Azure Active Directory 应用程序密钥值Client Secret is the Azure Active Directory Application key value
  5. 如果使用 Application Insights,还可以包含Application Insights API 和应用程序 ID,以收集基于 Application Insights 的指标。If you use Application Insights, you can also include your Application Insights API and application ID to collect Application Insights based metrics. 有关详细信息,请参阅获取 API 密钥和应用程序 IDFor more information, see Getting your API key and Application ID.

  6. 选择“保存”,然后 Grafana 将为每个 API 测试凭据。Select Save, and Grafana will test the credentials for each API. 应看到类似于下面的消息:You should see a message similar to the following one.
    已批准 Grafana 数据源配置Grafana data source config approved

生成 Grafana 仪表板Build a Grafana dashboard

  1. 转到 Grafana 主页,并选择“新建仪表板”。Go to the Grafana Home page, and select New Dashboard.

  2. 在新仪表板中,选择“Graph”。In the new dashboard, select the Graph. 你可以尝试其他图表选项,但本文使用 Graph 作为示例。You can try other charting options but this article uses Graph as an example.

  3. 在仪表板上将显示一个空白图表。A blank graph shows up on your dashboard. 单击面板标题,然后选择“编辑”,输入你想要在此图表中绘制的数据详细信息。Click on the panel title and select Edit to enter the details of the data you want to plot in this graph chart. Grafana 新图形Grafana new graph

  4. 选择已配置的 Azure Monitor 数据源。Select the Azure Monitor data source you've configured.

    • 收集 Azure Monitor 指标 - 在服务下拉列表中选择“Azure Monitor”。Collecting Azure Monitor metrics - select Azure Monitor in the service dropdown. 随即将显示选择器列表,可在其中选择此图表中要监视的资源和指标。A list of selectors shows up, where you can select the resources and metric to monitor in this chart. 若要收集 VM 的指标,请使用命名空间 Microsoft.Compute/VirtualMachines。To collect metrics from a VM, use the namespace Microsoft.Compute/VirtualMachines. 选择 VM 和指标后,即可开始在仪表板中查看其数据。Once you have selected VMs and metrics, you can start viewing their data in the dashboard. 适用于 Azure Monitor 的 Grafana 图形配置Grafana graph config for Azure Monitor

    • 收集 Azure Monitor 日志数据 - 在下拉列表中选择“Azure Log Analytics”。Collecting Azure Monitor log data - select Azure Log Analytics in the service dropdown. 选择要查询的工作区并设置查询文本。Select the workspace you'd like to query and set the query text. 可在此处复制已有的任何日志查询,或新建一个查询。You can copy here any log query you already have or create a new one. 在查询中键入时,IntelliSense 将显示并建议自动完成选项。As you type in your query, IntelliSense will show up and suggest autocomplete options. 选择可视化类型“时间序列表”,并运行查询。Select the visualization type, Time series Table, and run the query.


      插件提供的默认查询使用两个宏:"$__timeFilter() 和 $__interval。The default query provided with the plugin uses two macros: "$__timeFilter() and $__interval. 这些宏允许 Grafana 在你放大某部分图表时动态计算时间范围和时间粒度。These macros allow Grafana to dynamically calculate the time range and time grain, when you zoom in on part of a chart. 可以删除这些宏并使用标准时间筛选器(如 TimeGenerated > ago(1h)),但这意味着未来图形将不支持放大功能。You can remove these macros and use a standard time filter, such as TimeGenerated > ago(1h), but that means the graph would not support the zoom in feature.

      适用于 Azure Log Analytics 的 Grafana 图形配置

  5. 下面是一个简单的包含两个图表的仪表板。Following is a simple dashboard with two charts. 左图显示两个 VM 的 CPU 百分比。The one on left shows the CPU percentage of two VMs. 右图显示 Azure 存储帐户中的事务(按事务 API 类型分解)。The chart on the right shows the transactions in an Azure Storage account broken down by the Transaction API type. Grafana 两个图表示例Grafana Two Charts Example

可选:在同一 Grafana 服务器上监视自定义指标Optional: Monitor your custom metrics in the same Grafana server

此外,你还可以安装 Telegraf 和 InfluxDB,以在同一 Grafana 实例中收集和绘制自定义和基于代理的指标。You can also install Telegraf and InfluxDB to collect and plot both custom and agent-based metrics same Grafana instance. 你可以使用许多数据源插件将这些指标集中到一个仪表板中。There are many data source plugins that you can use to bring these metrics together in a dashboard.

你也可以重复使用此设置来包含 Prometheus 服务器中的指标。You can also reuse this set up to include metrics from your Prometheus server. 在 Grafana 插件库中使用 Prometheus 数据源插件。Use the Prometheus data source plugin in Grafana's plugin gallery.

以下为如何使用 Telegraf、InfluxDB、Prometheus 和 Docker 的优秀参考文章:Here are good reference articles on how to use Telegraf, InfluxDB, Prometheus, and Docker

包含 Azure Monitor 和 Application Insights 指标的完整 Grafana 仪表板的图像如下。Here is an image of a full Grafana dashboard that has metrics from Azure Monitor and Application Insights. Grafana 示例指标Grafana Example Metrics

高级 Grafana 功能Advanced Grafana features


可以通过 UI 下拉列表选择某些查询值,并在查询中进行更新。Some query values can be selected through UI dropdowns, and updated in the query. 考虑将以下查询示例:Consider the following query as an example:

| where $__timeFilter(TimeGenerated) 
| summarize total_KBytes=sum(Quantity)*1024 by bin(TimeGenerated, $__interval) 
| sort by TimeGenerated

可以配置将列出所有可用的解决方案值的变量,然后更新查询以便使用它。You can configure a variable that will list all available Solution values, and then update your query to use it. 若要创建新的变量,请单击右上角的仪表板“设置”按钮,选择“变量”,然后单击“新建”。To create a new variable, click the dashboard's Settings button in the top right area, select Variables, and then New. 在变量页上,定义要运行的数据源和查询以获取值列表。On the variable page, define the data source and query to run in order to get the list of values. Grafana 配置变量Grafana configure variable

创建后请调整查询以使用所选值,并且图表将做出相应响应:Once created, adjust the query to use the selected value(s) and your charts will respond accordingly:

| where $__timeFilter(TimeGenerated) and Solution in ($Solutions)
| summarize total_KBytes=sum(Quantity)*1024 by bin(TimeGenerated, $__interval) 
| sort by TimeGenerated

Grafana 使用变量

创建仪表板播放列表Create dashboard playlists

仪表板播放列表是 Grafana 众多有用功能中的其中一项。One of the many useful features of Grafana is the dashboard playlist. 你可以创建多个仪表板,将其添加到播放列表,并配置每个仪表板的显示间隔。You can create multiple dashboards and add them to a playlist configuring an interval for each dashboard to show. 选择“播放”即可查看仪表板循环。Select Play to see the dashboards cycle through. 建议在大型壁式监视器上显示这些仪表板,为你的小组提供状态板。You may want to display them on a large wall monitor to provide a status board for your group.

Grafana 播放列表示例

清理资源Clean up resources

如果已在 Azure 上设置 Grafana 环境,则无论是否在使用 VM,一旦 VM 开始运行你都需要付费。If you've setup a Grafana environment on Azure, you are charged when VMs are running whether you are using them or not. 为了避免产生额外的费用,请对此文章中创建的资源组进行清理。To avoid incurring additional charges, clean up the resource group created in this article.

  1. 在 Azure 门户的左侧菜单中,单击“资源组”,然后单击“Grafana”。From the left-hand menu in the Azure portal, click Resource groups and then click Grafana.
  2. 在资源组页上,单击“删除”,在文本框中键入“Grafana”,然后单击“删除”。On your resource group page, click Delete, type Grafana in the text box, and then click Delete.

后续步骤Next steps