同步操作将从 openEuler/gala-spider 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
spider 将生成的 OS 级别的拓扑关系图存储到图数据 arangodb 中,并通过 arangodb 服务器的 API 向外提供拓扑图的查询能力。关于 spider 和 arangodb 的安装部署参见文档:安装指导。
本文档将介绍与拓扑图查询相关的 API ,更加详细的 API 定义参见 arangodb 官方文档。
参见 gala-spider 设计文档中的 拓扑图存储模型 章节。
Arangodb 提供的 AQL 语句查询接口,我们用它来查询距离指定时间戳最近一次生成的拓扑图的实际时间戳。这里只提供与本功能相关的说明,详细的 API 定义参见 arangodb 官方文档: AQL查询接口。
POST /_api/cursor
请求体参数:
query
(string类型,必选):包含要执行的查询字符串,这里它的内容为 "FOR t IN Timestamps FILTER TO_NUMBER(t._key) <= @timestamp SORT t._key DESC LIMIT 1 RETURN t._key"
。batchSize
(integer类型,可选):一次往返中从服务器传输到客户端的最大返回记录数量,这里设置为 1。bindVars
(字典类型,必选):表示 query
中绑定查询参数的键/值对。这里它包含一个名为 timestamp
的键,表示要查询的指定时间戳。HTTP 201:请求成功时的响应码,返回内容包括,
error
(boolean类型):发生错误时标记为 truecode
(integer类型):HTTP 状态码result
(数组类型):返回内容,这里只返回一个元素,它的值为拓扑图的时间戳。[root@k8s-node3 ~]# curl -X POST --header 'accept: application/json' --data-binary @- --dump - http://10.137.17.122:8529/_db/spider/_api/cursor <<EOF
> {
> "query": "FOR t IN Timestamps FILTER TO_NUMBER(t._key) <= @ts SORT t._key DESC LIMIT 1 RETURN t._key",
> "batchSize": 1,
> "bindVars": { "ts": 1654930859 }
> }
> EOF
HTTP/1.1 201 Created
X-Arango-Queue-Time-Seconds: 0.000000
X-Content-Type-Options: nosniff
Server: ArangoDB
Connection: Keep-Alive
Content-Type: application/json; charset=utf-8
Content-Length: 279
{
"result": [
"1654930858"
],
"hasMore": false,
"cached": false,
"extra": {...},
"error": false,
"code": 201
}
查询指定节点(或边)的详细信息。这里只提供与本功能相关的说明,详细的 API 定义参见 arangodb 官方文档: 节点查询接口。
GET /_api/document/{collection}/{key}
路径参数
collection
(string类型,必选):节点(或边)所在的集合key
(string类型,必选):节点(或边)的索引 _key
HTTP 201:请求成功时的响应码,返回内容为节点(或边)的内容。
[root@k8s-node3 ~]# curl --header 'accept: application/json' --dump - http://10.137.17.122:8529/_db/spider/_api/document/ObserveEntities_1654930858/THREAD_4c739ef759c142c18e8c3c29f115e873_928
HTTP/1.1 200 OK
X-Arango-Queue-Time-Seconds: 0.000000
X-Content-Type-Options: nosniff
Etag: "_eTEPW----S"
Server: ArangoDB
Connection: Keep-Alive
Content-Type: application/json; charset=utf-8
Content-Length: 465
{
"_key": "THREAD_4c739ef759c142c18e8c3c29f115e873_928",
"_id": "ObserveEntities_1654930858/THREAD_4c739ef759c142c18e8c3c29f115e873_928",
"_rev": "_eTEPW----S",
"name": "dbus-daemon",
"type": "thread",
"level": "PROCESS",
"timestamp": 1654930858,
"machine_id": "4c739ef759c142c18e8c3c29f115e873",
"pid": "928",
"comm": "dbus-daemon"
}
查询指定节点的邻边信息。这里只提供与本功能相关的说明,详细的 API 定义参见 arangodb 官方文档: 邻边查询接口。
GET /_api/edges/{collection-id}
路径参数
collection-id
(string类型,必选):边集名称查询参数
vertex
(string类型,必选):节点IDdirection
(string类型,可选):选择边的方向(入方向或出方向)。如果未设置,则返回任意方向。HTTP 201:请求成功时的响应码,返回内容为节点的邻边数组。
[root@k8s-node3 ~]# curl --header 'accept: application/json' --dump - http://10.137.17.122:8529/_db/spider/_api/edges/belongs_to?vertex=ObserveEntities_1654930858/SYSTEM_PROC_4c739ef759c142c18e8c3c29f115e873_1687219
HTTP/1.1 200 OK
X-Arango-Queue-Time-Seconds: 0.000000
X-Content-Type-Options: nosniff
Server: ArangoDB
Connection: Keep-Alive
Content-Type: application/json; charset=utf-8
Content-Length: 1333
{
"edges": [
{
"_key": "1300211",
"_id": "belongs_to/1300211",
"_from": "ObserveEntities_1654930858/THREAD_4c739ef759c142c18e8c3c29f115e873_1687277",
"_to": "ObserveEntities_1654930858/SYSTEM_PROC_4c739ef759c142c18e8c3c29f115e873_1687219",
"_rev": "_eTEPW-m--F",
"type": "belongs_to",
"layer": "direct"
},
{
"_key": "1300212",
"_id": "belongs_to/1300212",
"_from": "ObserveEntities_1654930858/THREAD_4c739ef759c142c18e8c3c29f115e873_1687278",
"_to": "ObserveEntities_1654930858/SYSTEM_PROC_4c739ef759c142c18e8c3c29f115e873_1687219",
"_rev": "_eTEPW-m--G",
"type": "belongs_to",
"layer": "direct"
}
],
"error": false,
"code": 200,
"stats": {...}
}
Arangodb 提供的 AQL 语句查询接口,我们用它来查询满足过滤条件的节点信息。这里只提供与本功能相关的说明,详细的 API 定义参见 arangodb 官方文档: AQL查询接口。
POST /_api/cursor
请求体参数:
query
(string类型,必选):包含要执行的 AQL 查询语句的字符串形式。这里它的内容为 "FOR v IN @@collection FILTER v.type == @entity_type return v"
,其中,v.type == @entity_type
子串是一个过滤条件,它表示过滤出观测类型为变量 entity_type
的值的节点,其中变量 entity_type
的值在 bindVars
参数中指定,该子串可以按需替换成其他的过滤条件。当有多个过滤条件时,可通过 and
关键字连接多个过滤条件,并替换上述子串,例如替换成 v.type == @entity_type and v.level == @level
,同时在 bindVars
参数中指定对应的变量值即可。count
(boolean类型,可选):指示是否应在输出参数的 “count” 属性中返回结果集中的文档数量,这里设置为 true。batchSize
(integer类型,可选):一次往返中从服务器传输到客户端的最大返回记录数量。如果未设置此属性,将使用服务器控制的默认值。bindVars
(字典类型,必选):表示 query
中绑定查询参数的键/值对。这里它包含两个属性,
@collection
(string类型,必选):节点集合entity_type
(string类型,可选):根据过滤条件中涉及的变量按需设置HTTP 201:请求成功时的响应码,返回内容包括:
error
(boolean类型):发生错误时标记为 truecode
(integer类型):HTTP 状态码result
:当前批次的文档(节点或边)列表hasMore
:是否还有下一批查询结果,如果这是最后一批,则为 falsecount
:文档的总数量id
:光标标识符,用于查询剩余的结果集,用法参见本文档的第5个API:从光标处读取下一批查询内容
[root@k8s-node3 ~]# curl -X POST --header 'accept: application/json' --data-binary @- --dump - http://10.137.17.122:8529/_db/spider/_api/cursor <<EOF
{
"query": "FOR v IN @@collection FILTER v.type == @entity_type return v",
"batchSize": 2,
"count": true,
"bindVars": { "@collection": "ObserveEntities_1654930858", "entity_type": "system_proc"}
}
EOF
HTTP/1.1 201 Created
X-Arango-Queue-Time-Seconds: 0.000000
X-Content-Type-Options: nosniff
Server: ArangoDB
Connection: Keep-Alive
Content-Type: application/json; charset=utf-8
Content-Length: 2434
{
"result": [
{
"_key": "SYSTEM_PROC_4f2eb1a8378b428f85af0a6c0cde9288_2133032",
"_id": "ObserveEntities_1654930858/SYSTEM_PROC_4f2eb1a8378b428f85af0a6c0cde9288_2133032",
"_rev": "_eTEPW-----",
"name": "redis-server",
"type": "system_proc",
"level": "PROCESS",
"timestamp": 1654930858,
"machine_id": "4f2eb1a8378b428f85af0a6c0cde9288",
"pid": "2133032",
"cmdline": "/opt/redis/redis-server 10.137.16.172:3742 ",
"comm": "redis-server"
},
{
"_key": "SYSTEM_PROC_4c739ef759c142c18e8c3c29f115e873_1687219",
"_id": "ObserveEntities_1654930858/SYSTEM_PROC_4c739ef759c142c18e8c3c29f115e873_1687219",
"_rev": "_eTEPW----_",
"name": "redis-server",
"type": "system_proc",
"level": "PROCESS",
"timestamp": 1654930858,
"machine_id": "4c739ef759c142c18e8c3c29f115e873",
"pid": "1687219",
"cmdline": "/opt/redis/redis-server 10.137.16.176:3746 ",
"comm": "redis-server"
}
],
"hasMore": true,
"id": "2206881",
"count": 20,
"extra": {...},
"cached": false,
"error": false,
"code": 201
}
当 POST /_api/cursor
接口的 AQL 语句查询结果比较多的时候,需要分批返回给用户,本接口通过 POST /_api/cursor
接口返回的光标标识 cursor-identifier
来不断读取下一批查询结果。这里只提供与本功能相关的说明,详细的 API 定义参见 arangodb 官方文档: AQL查询接口。
POST /_api/cursor/{cursor-identifier}
路径参数
cursor-identifier
(string类型,必选):光标标识符HTTP 200:请求成功时的响应码,返回内容包括:
id
:光标标识符result
:当前批次的文档(节点或边)列表hasMore
:是否还有下一批查询结果,如果这是最后一批,则为 falsecount
:文档的总数量error
(boolean类型):发生错误时标记为 truecode
(integer类型):HTTP 状态码[root@k8s-node3 ~]# curl -X POST --header 'accept: application/json' --data-binary @- --dump - http://10.137.17.122:8529/_db/spider/_api/cursor/2204099 <<EOF
> {
> "query": "FOR v IN @@collection FILTER v.type == @entity_type return v",
> "batchSize": 2,
> "count": true,
> "bindVars": { "@collection": "ObserveEntities_1654930858", "entity_type": "system_proc"}
> }
> EOF
HTTP/1.1 201 Created
X-Arango-Queue-Time-Seconds: 0.000000
X-Content-Type-Options: nosniff
Server: ArangoDB
Connection: Keep-Alive
Content-Type: application/json; charset=utf-8
Content-Length: 2434
{
"result": [
{
"_key": "SYSTEM_PROC_4c739ef759c142c18e8c3c29f115e873_1673685",
"_id": "ObserveEntities_1654930858/SYSTEM_PROC_4c739ef759c142c18e8c3c29f115e873_1673685",
"_rev": "_eTEPW----A",
"name": "redis-server",
"type": "system_proc",
"level": "PROCESS",
"timestamp": 1654930858,
"machine_id": "4c739ef759c142c18e8c3c29f115e873",
"pid": "1673685",
"cmdline": "/opt/redis/redis-server 10.137.16.176:3747 ",
"comm": "redis-server"
},
{
"_key": "SYSTEM_PROC_4c739ef759c142c18e8c3c29f115e873_1676378",
"_id": "ObserveEntities_1654930858/SYSTEM_PROC_4c739ef759c142c18e8c3c29f115e873_1676378",
"_rev": "_eTEPW----B",
"name": "redis-server",
"type": "system_proc",
"level": "PROCESS",
"timestamp": 1654930858,
"machine_id": "4c739ef759c142c18e8c3c29f115e873",
"pid": "1676378",
"cmdline": "/opt/redis/redis-server 10.137.16.176:3747 ",
"comm": "redis-server"
}
],
"hasMore": true,
"id": "2204099",
"count": 88,
"extra": {...},
"cached": false,
"error": false,
"code": 200
}
Arangodb 提供的 AQL 语句查询接口,我们用它来查询拓扑子图。这里只提供与本功能相关的说明,详细的 API 定义参见 arangodb 官方文档: AQL查询接口。
POST /_api/cursor
请求体参数:
query
(string类型,必选):包含要执行的查询字符串,这里它的内容为 "WITH @@collection FOR v, e IN 1..@depth ANY @start_id @edge_coll return {\"vertex\": v, \"edge\": e}"
,其中,@edge_coll
子串是一个过滤条件,它表示只遍历变量 edge_coll
的值指定的边集合,其中变量 edge_coll
的值在 bindVars
参数中指定。当需要遍历多个边集合时,可通过 ,
分隔多个边集合变量,并替换上述子串,例如替换成 @edge_coll1, @edge_coll2
,同时在 bindVars
参数中指定对应的变量值即可。count
(boolean类型,可选):指示是否应在输出参数的 “count” 属性中返回结果集中的文档数量,这里设置为 true。batchSize
(integer类型,可选):一次往返中从服务器传输到客户端的最大返回记录数量。如果未设置此属性,将使用服务器控制的默认值。bindVars
(字典类型,必选):表示 query
中绑定查询参数的键/值对。这里它包含4个属性,
@collection
(string类型,必选):节点集合depth
(string类型,必选):子图遍历的深度start_id
(string类型,必选):子图遍历的起始节点IDedge_coll
(string类型,必选):需要遍历的边集名称HTTP 201:请求成功时的响应码,返回内容包括:
error
(boolean类型):发生错误时标记为 truecode
(integer类型):HTTP 状态码result
:当前批次的查询结果列表。其中每个元素的内容为一个字典结构,它可在 query
参数中指定,这里它的属性如下,
edge
:当前节点的邻边信息vertex
:edge
边另一侧的节点信息hasMore
:是否还有下一批查询结果,如果这是最后一批,则为 falsecount
:文档的总数量id
:光标标识符,用于查询剩余的结果集,用法参见本文档的第5个API:从光标处读取下一批查询内容
[root@k8s-node3 ~]# curl -X POST --header 'accept: application/json' --data-binary @- --dump - http://10.137.17.122:8529/_db/spider/_api/cursor <<EOF
> {
> "query": "WITH @@collection FOR v, e IN 1..10 ANY @start_id @@edge_coll1, @@edge_coll2 return {\"vertex\": v, \"edge\": e}",
> "batchSize": 2,
> "count": true,
> "bindVars": { "@collection": "ObserveEntities_1654930858", "start_id": "ObserveEntities_1654930858/SYSTEM_PROC_4c739ef759c142c18e8c3c29f115e873_1676378", "@edge_coll1": "belongs_to", "@edge_coll2": "runs_on"}
> }
> EOF
HTTP/1.1 201 Created
X-Arango-Queue-Time-Seconds: 0.000000
X-Content-Type-Options: nosniff
Server: ArangoDB
Connection: Keep-Alive
Content-Type: application/json; charset=utf-8
Content-Length: 1053
{
"result": [
{
"vertex": {
"_key": "THREAD_4c739ef759c142c18e8c3c29f115e873_1676378",
"_id": "ObserveEntities_1654930858/THREAD_4c739ef759c142c18e8c3c29f115e873_1676378",
"_rev": "_eTEPW-C--E",
"name": "redis-server",
"type": "thread",
"level": "PROCESS",
"timestamp": 1654930858,
"machine_id": "4c739ef759c142c18e8c3c29f115e873",
"pid": "1676378",
"comm": "redis-server",
"major": "8",
"minor": "0",
"tgid": "1676378"
},
"edge": {
"_key": "1300207",
"_id": "belongs_to/1300207",
"_from": "ObserveEntities_1654930858/THREAD_4c739ef759c142c18e8c3c29f115e873_1676378",
"_to": "ObserveEntities_1654930858/SYSTEM_PROC_4c739ef759c142c18e8c3c29f115e873_1676378",
"_rev": "_eTEPW-m--B",
"type": "belongs_to",
"layer": "direct"
}
}
],
"hasMore": false,
"count": 1,
"cached": false,
"extra": {...},
"error": false,
"code": 201
}
基于上节的拓扑图查询 API,我们可以组合使用来实现特定功能场景的查询。
例如,查询观测范围内的所有主机以及主机之间的连接关系图,可以分两步进行查询:
首先,根据 API [4. 查询满足过滤条件的节点信息](#4. 查询满足过滤条件的节点信息) 过滤出所有的主机节点;
[root@master ~]# curl -X POST --header 'accept: application/json' --data-binary @- --dump - http://10.137.17.122:8529/_db/spider/_api/cursor <<EOF
{
"query": "FOR v IN @@collection FILTER v.type == @entity_type return v",
"batchSize": 10,
"count": true,
"bindVars": { "@collection": "ObserveEntities_1657616617", "entity_type": "host"}
}
EOF
...
{
"result": [
{
"_key": "4c739ef759c142c18e8c3c29f115e873_host",
"_id": "ObserveEntities_1657616617/4c739ef759c142c18e8c3c29f115e873_host",
"_rev": "_edEklBy--W",
"type": "host",
"level": "HOST",
"timestamp": 1657616617,
"machine_id": "4c739ef759c142c18e8c3c29f115e873",
"hostname": "localhost.localdomain",
"metrics": { }
},
{
"_key": "4f2eb1a8378b428f85af0a6c0cde9288_host",
"_id": "ObserveEntities_1657616617/4f2eb1a8378b428f85af0a6c0cde9288_host",
"_rev": "_edEklBy--X",
"type": "host",
"level": "HOST",
"timestamp": 1657616617,
"machine_id": "4f2eb1a8378b428f85af0a6c0cde9288",
"hostname": "localhost.localdomain",
"metrics": { }
},
{
"_key": "01b42f035bb14174b0bd39df3d9c16c7_host",
"_id": "ObserveEntities_1657616617/01b42f035bb14174b0bd39df3d9c16c7_host",
"_rev": "_edEklBy--Y",
"type": "host",
"level": "HOST",
"timestamp": 1657616617,
"machine_id": "01b42f035bb14174b0bd39df3d9c16c7",
"hostname": "master",
"metrics": { }
}
],
"hasMore": false,
"count": 3,
"cached": false,
"extra": {...},
"error": false,
"code": 201
}
其次,对每个主机节点调用 API [3. 查询指定节点的邻边信息](#3. 查询指定节点的邻边信息) 查询连接关系。
[root@master ~]# curl --header 'accept: application/json' --dump - http://10.137.17.122:8529/_db/spider/_api/edges/connect?vertex=ObserveEntities_1657616617/4c739ef759c142c18e8c3c29f115e873_host
HTTP/1.1 200 OK
...
{
"edges": [
{
"_key": "2855357",
"_id": "connect/2855357",
"_from": "ObserveEntities_1657616617/4c739ef759c142c18e8c3c29f115e873_host",
"_to": "ObserveEntities_1657616617/4f2eb1a8378b428f85af0a6c0cde9288_host",
"_rev": "_edXhYT----"
},
{
"_key": "2855385",
"_id": "connect/2855385",
"_from": "ObserveEntities_1657616617/4c739ef759c142c18e8c3c29f115e873_host",
"_to": "ObserveEntities_1657616617/01b42f035bb14174b0bd39df3d9c16c7_host",
"_rev": "_edXhx62---"
}
],
"error": false,
"code": 200,
"stats": {
"writesExecuted": 0,
"writesIgnored": 0,
"scannedFull": 0,
"scannedIndex": 2,
"filtered": 0,
"httpRequests": 0,
"executionTime": 0.0007191281765699387,
"peakMemoryUsage": 32768
}
}
例如,在获取到主机层的连接关系图后,当点击某个主机节点时可以查看该主机节点的基本信息。此外,当我们想查看在主机层中与该主机节点相关的其它信息时,比如磁盘、cpu、内存等,可以进一步通过 API [3. 查询指定节点的邻边信息](#3. 查询指定节点的邻边信息) 获取所有从属于该主机节点的邻居节点的ID信息。示例如下,
[root@master ~]# curl --header 'accept: application/json' --dump - http://10.137.17.122:8529/_db/spider/_api/edges/belongs_to?vertex=ObserveEntities_1657616617/4c739ef759c142c18e8c3c29f115e873_host
HTTP/1.1 200 OK
...
{
"edges": [
{
"_key": "2859678",
"_id": "belongs_to/2859678",
"_from": "ObserveEntities_1657616617/4c739ef759c142c18e8c3c29f115e873_disk_sda",
"_to": "ObserveEntities_1657616617/4c739ef759c142c18e8c3c29f115e873_host",
"_rev": "_edZoSy----",
"type": "belongs_to",
"timestamp": 1657616617
},
{
"_key": "2859718",
"_id": "belongs_to/2859718",
"_from": "ObserveEntities_1657616617/4c739ef759c142c18e8c3c29f115e873_cpu_0",
"_to": "ObserveEntities_1657616617/4c739ef759c142c18e8c3c29f115e873_host",
"_rev": "_edZpruS---",
"type": "belongs_to",
"timestamp": 1657616617
}
],
"error": false,
"code": 200,
"stats": {...}
}
获取到邻居节点的ID后,调用 API [2. 查询指定节点/边信息](#2. 查询指定节点/边信息) 获取邻居节点的详细信息,并根据返回的 level
属性过滤出主机层中的邻居节点信息。
[root@master ~]# curl --header 'accept: application/json' --dump - http://10.137.17.122:8529/_db/spider/_api/document/ObserveEntities_1657616617/4c739ef759c142c18e8c3c29f115e873_disk_sda
HTTP/1.1 200 OK
...
{
"_key": "4c739ef759c142c18e8c3c29f115e873_disk_sda",
"_id": "ObserveEntities_1657616617/4c739ef759c142c18e8c3c29f115e873_disk_sda",
"_rev": "_edZs5oO---",
"type": "disk",
"level": "HOST",
"disk_name": "sda",
"machine_id": "4c739ef759c142c18e8c3c29f115e873"
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。