跳转到内容
搜索文档

使用 GraphQL 创建小组件

最后更新 查看 MarkdownAgent 设置

本文提供可用于填充您自己的仪表板的查询示例。

使用以下工作流构建和测试查询:

  • 安装并配置 GraphiQL 应用,以对 Cloudflare Analytics GraphQL API 进行身份验证。Cloudflare 推荐使用令牌身份验证。有关更多信息,请参阅配置 Analytics API 令牌
  • 在 GraphiQL 中构建查询。您可以使用 GraphQL 客户端中的 introspection 文档探索可用节点。有关查询的更多信息,请参阅查询基础
  • 通过从 GraphiQL 运行查询,或将其作为 payload 传递给 GraphQL API 端点的 cURL 请求来测试查询。
  • 在应用程序中使用这些查询为仪表板小组件提供数据。

参数和筛选器

这些示例使用您正在查询的 Cloudflare 账户的账户 ID。您可以将其定义为变量(accountTag)并在查询中引用。

查询还使用筛选器指定要查询的时间间隔。筛选器使用开始时间和结束时间定义时间间隔。根据要查询的时间段,您使用不同属性指定开始和结束时间。有关筛选器的更多信息,请参阅筛选

以下示例查询日期大于或等于 date_geq 且小于或等于 date_leq 的数据:

Account and query time interval settingsjson
{
	"accountTag": "{account-id}",
	"filter": {
		"AND": [{ "date_geq": "2020-01-19" }, { "date_leq": "2020-01-20" }]
	}
}

此表列出 Network Analytics 数据集(节点)以及为给定时间选择查询数据时应使用的 datetimeDimension

当您需要数据的聚合视图时,使用 Groups 查询节点。例如,ipFlows1mAttacksGroups 数据集表示攻击活动的分钟级汇总报告。有关更多详情,请参阅数据集

时间选择查询节点datetimeDimension
上周ipFlows1dGroupsdate
上月ipFlows1dGroupsdate
24 小时ipFlows1mGroupsdatetimeFifteenMinutes
12 小时ipFlows1mGroupsdatetimeFifteenMinutes
6 小时ipFlows1mGroupsdatetimeFiveMinutes
30 分钟ipFlows1mGroupsdatetimeMinute
自定义范围取决于所选范围取决于所选范围

下表列出代表不同时间范围的查询节点有效的开始和结束时间属性。

查询节点开始日期/时间筛选器结束日期/时间筛选器
ipFlows1mGroupsdatetimeMinute_geqdatetimeMinute_leq
ipFlows1mAttacksGroupsdate_geqdate_leq
ipFlows1hGroupsdatetimeHour_geqdatetimeHour_leq
ipFlows1dGroupsdate_geqdate_leq

时间序列图

使用以下查询构建 network analytics 中的时间序列图:

Timeseries graphgraphql
query ipFlowTimeseries(
	$accountTag: string
	$filter: AccountIpFlows1mGroupsFilter_InputObject
) {
	viewer {
		accounts(filter: { accountTag: $accountTag }) {
			ipFlows1mGroups(
				limit: 1000
				filter: $filter
				orderBy: datetimeMinute_ASC
			) {
				dimensions {
					timestamp: datetimeMinute
					attackMitigationType
					attackId
				}
				sum {
					bits
					packets
				}
			}
		}
	}
}

活动日志

此查询返回活动日志,汇总 IP 流中攻击流量的分钟级汇总。查询按 dimensions 对象中列出的字段对数据进行分组。

Activity log querygraphql
query ipFlowEventLog(
	$accountTag: string
	$filter: AccountIpFlows1mAttacksGroupsFilter_InputObject
) {
	viewer {
		accounts(filter: { accountTag: $accountTag }) {
			ipFlows1mAttacksGroups(
				limit: 10
				filter: $filter
				orderBy: [min_datetimeMinute_ASC]
			) {
				dimensions {
					attackId
					attackDestinationIP
					attackDestinationPort
					attackMitigationType
					attackSourcePort
					attackType
				}
				avg {
					bitsPerSecond
					packetsPerSecond
				}
				min {
					datetimeMinute
					bitsPerSecond
					packetsPerSecond
				}
				max {
					datetimeMinute
					bitsPerSecond
					packetsPerSecond
				}
				sum {
					bits
					packets
				}
			}
		}
	}
}

Top N 卡片 - 来源

此查询返回有关主要来源 IP 的数据。 limit 参数控制每个节点返回的记录数量。在以下代码中,高亮行表示配置 limit 的位置。

Top N Cards querygraphql
query GetTopNBySource(
    $accountTag: string
    $filter: AccountIpFlows1mGroupsFilter_InputObject
    $portFilter: AccountIpFlows1mGroupsFilter_InputObject
  ) {
    viewer {
      accounts(filter: { accountTag: $accountTag }) {
        topNPorts: ipFlows1mGroups(
        limit: 5
        filter: $portFilter
        orderBy: [sum_(bits/packets)_DESC]
      ) {
        sum {
          count: (bits/packets)
        }
        dimensions {
          metric: sourcePort
          ipProtocol
        }
      }
      topNASN: ipFlows1mGroups(
        limit: 5
        filter: $filter
        orderBy: [sum_(bits/packets)_DESC]
      ) {
        sum {
          count: (bits/packets)
        }
        dimensions {
          metric: sourceIPAsn
          description: sourceIPASNDescription
        }
      }
        topNIPs: ipFlows1mGroups(
        limit: 5
        filter: $filter
        orderBy: [sum_(bits/packets)_DESC]
      ) {
        sum {
          count: (bits/packets)
        }
        dimensions {
          metric: sourceIP
        }
      }
        topNColos: ipFlows1mGroups(
          limit: 10
          filter: $filter
          orderBy: [sum_(bits/packets)_DESC]
        ) {
          sum {
            count: (bits/packets)
          }
          dimensions {
            metric: coloCity
            coloCode
          }
        }
        topNCountries: ipFlows1mGroups(
          limit: 10
          filter: $filter
          orderBy: [sum_(bits/packets)_DESC]
        ) {
          sum {
            count: (bits/packets)
          }
          dimensions {
            metric: coloCountry
          }
        }
        topNIPVersions: ipFlows1mGroups(
          limit: 2
          filter: $filter
          orderBy: [sum_(bits/packets)_DESC]
        ) {
          sum {
            count: (bits/packets)
          }
          dimensions {
            metric: ipVersion
          }
        }
      }
    }
  }

Top N 卡片 - 目标

此查询返回有关主要目标 IP 的数据。limit 参数控制返回的记录数量。在以下代码中,高亮行表示查询返回前五名结果。

Top N Cards - Destinationgraphql
query GetTopNByDestination(
    $accountTag: string
    $filter: AccountIpFlows1mGroupsFilter_InputObject
    $portFilter: AccountIpFlows1mGroupsFilter_InputObject
  ) {
    viewer {
      accounts(filter: { accountTag: $accountTag }) {
        topNIPs: ipFlows1mGroups(
          filter: $filter
          limit: 5
          orderBy: [sum_(bits/packets)_DESC]
        ) {
          sum {
            count: (bits/packets)
          }
          dimensions {
            metric: destinationIP
          }
        }
        topNPorts: ipFlows1mGroups(
          filter: $portFilter
          limit: 5
          orderBy: [sum_(bits/packets)_DESC]
        ) {
          sum {
            count: (bits/packets)
          }
          dimensions {
            metric: destinationPort
            ipProtocol
          }
        }
      }
    }
  }

TCP 标志

此查询从 IP 流的分钟级汇总中提取 TCP 数据包数量,并按 TCP 标志值对结果进行分组。它使用 limit: 8 显示前八名结果,并按降序排列。

在筛选器中添加以下行以表示要查看 TCP 数据:

{ "ipProtocol": "TCP" }
TCP Flags querygraphql
query GetTCPFlags(
    $accountTag: string
    $filter: AccountIpFlows1mGroupsFilter_InputObject
  ) {
    viewer {
      accounts(filter: { accountTag: $accountTag }) {
        tcpFlags: ipFlows1mGroups(
          filter: $filter
          limit: 8
          orderBy: [sum_(bits/packets)_DESC]
        ) {
          sum {
            count: (bits/packets)
          }
          dimensions {
            tcpFlags
          }
        }
      }
    }
  }

执行摘要

执行摘要查询汇总整体活动,因此它仅按所选时间间隔筛选,忽略应用于分析的所有其他筛选器。 根据要检查的时间间隔和账户看到的流量类型,使用不同的查询。

如果时间间隔是绝对的,例如 3 月 25 日 09:00 到 3 月 25 日 17:00,则执行查询以获取该时间范围内的攻击。使用适当的查询节点,例如 ipFlows1dGroups,对应时间间隔。

GetPreviousAttacks query - fetch previous attacksgraphql
query GetPreviousAttacks($accountTag: string, $filter: filter) {
  viewer {
    accounts(filter: {accountTag: $accountTag}) {
      ${queryNode}(limit: 1000, filter: $filter) {
        dimensions {
          attackId
        }
        sum {
          packets
          bits
        }
      }
    }
  }
}

如果时间间隔相对于当前时间,例如过去 24 小时或过去 30 分钟,则向 ipFlows1mGroup 节点发起查询,检查过去五分钟内是否有攻击。过去五分钟内的攻击被归类为进行中:活动日志显示 Present。 查询响应列出进行中攻击的 attackID 值。

GetOngoingAttackIds query - check for ongoing attacksgraphql
query GetOngoingAttackIds($accountTag: string, $filter: filter) {
	viewer {
		accounts(filter: { accountTag: $accountTag }) {
			ipFlows1mGroups(limit: 1000, filter: $filter) {
				dimensions {
					attackId
				}
			}
		}
	}
}

如果有进行中的攻击,查询 ipFlows1mAttacksGroups 节点,使用上一个查询的 attackID 值进行筛选。以下查询返回最大比特率和数据包速率。

GetOngoingAttacks query - fetch data for ongoing attacksgraphql
query GetOngoingAttacks($accountTag: string, $filter: filter) {
	viewer {
		accounts(filter: { accountTag: $accountTag }) {
			ipFlows1mAttacksGroups(limit: 1000, filter: $filter) {
				dimensions {
					attackId
				}
				max {
					bitsPerSecond
					packetsPerSecond
				}
			}
		}
	}
}

如果没有进行中的攻击,使用 GetPreviousAttacks 查询显示绝对时间间隔内的攻击数据。

这篇文档对您有帮助吗?