一、簡介
OpenTSDB提供基於HTTP的接口,以實現與外部系統的集成。 幾乎所有OpenTSDB功能都可通過API訪問,例如查詢時間序列數據,管理元數據和存儲數據點。 在使用各個接口之前,需要閱讀相關API文檔。
目前OpenTSDB已經與很多開源的監控系統集成,這種情況下,對OpenTSDB的接口就相對簡單些,只需要掌握一些相對固定的查詢接口就可以實現來巡檢、自定製可視化界面的功能。
但如果是自行開發可視化界面和監控報警功能,就需要深入掌握OpenTSDB接口的請求方法和返回內容了。
本文內容:
1)OpenTSDB接口概覽
2)OpenTSDB常用接口詳解
二、OpenTSDB接口概覽
OpenTSDB默認使用json序列號來顯示請求和響應。
OpenTSDB目前無身份驗證和訪問控制系統,所以只能通過防火牆等方式來限制訪問。
和常規的http標準響應一樣,OpenTSDB的接口返回也會有響應代碼200、204、301等,以及400等錯誤代碼。如果是錯誤,返回內容會包括code、message、details、trace等信息用以顯示錯誤描述。
通常情況下,OpenTSDB的接口是使用GET查詢、POST更新及創建、PUT替換、DELETE刪除,但爲了方便開發,OpenTSDB的部分API也支持了通過GET、POST來完成PUT、DELETE操作,具體在各個API中會介紹。一般查詢操作是通過GET字符串請求來查,但由於編碼的複雜性,OpenTSDB的接口還支持POST+body content的形式進行訪問。
OpenTSDB的接口說明參見下面內容(大部分是結合官方文檔和我自己的理解寫的)。
三、OpenTSDB接口
- /s GET、POST、PUT、DELETE 訪問本地系統上的靜態文件,響應將是所請求文件的內容,並配置了適當的HTTP標頭
http://localhost:4242/s/queryui.nocache.js
http://localhost:4242/api/aggregators返回:["min","sum","max","avg","dev"]
Name
|
Data Type
|
Required
|
Description
|
Default
|
QS
|
RW
|
Example
|
startTime
|
Integer
|
Required
|
Unix時間戳,以秒爲單位,標記應記錄註釋事件的時間
|
|
start_time
|
RW
|
1369141261
|
endTime
|
Integer
|
Optional
|
事件的可選結束時間(如果已完成或已解決)
|
0
|
end_time
|
RW
|
1369141262
|
tsuid
|
String
|
Optional
|
如果註釋與時間序列關聯,則爲TSUID。如果註釋是針對全局事件,則此值可以爲null或爲空
|
|
tsuid
|
RW
|
000001000001000001
|
description
|
String
|
Optional
|
該事件的簡要說明。由於這可能出現在GnuPlot圖表上,描述應該非常短,理想情況下應少於25個字符。
|
|
description
|
RW
|
Network Outage
|
notes
|
String
|
Optional
|
關於該事件的詳細說明
|
|
notes
|
RW
|
Switch #5 died and was replaced
|
custom
|
Map
|
Optional
|
用於存儲自定義字段和值的鍵/值映射
|
null
|
|
RW
|
See Below
|
{"startTime":"1369141261","tsuid":"000001000001000001","description": "Testing Annotations","notes": "These would be details about the event, the description is just a summary","custom": {"owner": "jdoe","dept": "ops"}}返回:{"tsuid": "000001000001000001","description": "Testing Annotations","notes": "These would be details about the event, the description is just a summary","custom": {"owner": "jdoe","dept": "ops"},"endTime": 0,"startTime": 1369141261}
http://localhost:4242/api/config{"tsd.search.elasticsearch.tsmeta_type": "tsmetadata","tsd.storage.flush_interval": "1000","tsd.network.tcp_no_delay": "true",...省略}
Note此接口不會清除存儲圖形和其他文件的磁盤上臨時緩存。http://localhost:4242/api/dropcaches{"message": "Caches dropped","status": "200"}
-
/api/put POST 通過HTTP在OpenTSDB中存儲數據,以替代Telnet接口。
Name
|
Data Type
|
Required
|
Description
|
Default
|
QS
|
RW
|
Example
|
summary
|
Present
|
Optional
|
是否返回摘要信息
|
false
|
summary
|
|
/api/put?summary
|
details
|
Present
|
Optional
|
是否返回詳細信息
|
false
|
details
|
|
/api/put?details
|
sync
|
Boolean
|
Optional
|
是否在返回結果之前等待數據刷新到存儲。
|
false
|
sync
|
|
/api/put?sync
|
sync_timeout
|
Integer
|
Optional
|
在返回錯誤之前等待數據刷新到存儲的超時(以毫秒爲單位)。發生超時時,使用該details標誌將告知有多少數據點失敗以及有多少數據點成功。sync還必須爲此生效。值爲0表示寫入不會超時。
|
0
|
sync_timeout
|
|
/api/put/?sync&sync_timeout=60000
|
Name
|
Data Type
|
Required
|
Description
|
Default
|
QS
|
RW
|
Example
|
metric
|
String
|
Required
|
存儲的指標的名稱
|
|
|
W
|
sys.cpu.nice
|
timestamp
|
Integer
|
Required
|
Unix時間戳,以秒或毫秒爲單位。時間戳不得包含非數字字符。
|
|
|
W
|
1365465600
|
value
|
Integer, Float, String
|
Required
|
要記錄此數據點的值。它可以引用或不引用,並且必須符合OpenTSDB值規則:../../ user_guide / writing
|
|
|
W
|
42.5
|
tags
|
Map
|
Required
|
tag名稱/tag值對的映射。必須至少提供一對。
|
|
|
W
|
{"host":"web01"}
|
單個數據點請求:{"metric": "sys.cpu.nice","timestamp": 1346846400,"value": 18,"tags": {"host": "web01","dc": "lga"}}多個數據點請求:[{...略},{...略}]響應:{"failed": 1,"success": 0}明細響應:{"errors": [{"datapoint": {"metric": "sys.cpu.nice","timestamp": 1365465600,"value": "NaN","tags": {"host": "web01"}},"error": "Unable to parse value to a number"}],"failed": 1,"success": 0}
Name
|
Data Type
|
Required
|
Description
|
Default
|
QS
|
RW
|
Example
|
metric
|
String
|
Required
|
存儲的指標的名稱
|
|
|
W
|
sys.cpu.nice
|
timestamp
|
Integer
|
Required
|
Unix時間戳,以秒或毫秒爲單位。時間戳不得包含非數字字符。
|
|
|
W
|
1365465600
|
value
|
Integer, Float, String
|
Required
|
要記錄此數據點的值。它可以引用或不引用,並且必須符合OpenTSDB值規則:../../ user_guide / writing
|
|
|
W
|
42.5
|
tags
|
Map
|
Required
|
標籤名稱/標籤值對的映射。必須至少提供一對。
|
|
|
W
|
{"host":"web01"}
|
interval
|
String
|
Optional*
|
反映彙總值代表的時間跨度的時間間隔。間隔包括<amount><unit>類似於下采樣器或相對查詢時間戳。例如,5h對於5小時的數據,30m對於30分鐘的數據。
|
|
|
W
|
1h
|
aggregator
|
String
|
Optional*
|
用於生成彙總值的聚合函數。必須與提供的TSDB聚合器匹配。
|
|
|
W
|
SUM
|
groupByAggregator
|
String
|
Optional*
|
用於生成預聚合值的聚合函數。必須與提供的TSDB聚合器匹配。
|
|
W
|
COUNT
|
|
單個數據點{"metric": "sys.cpu.nice","timestamp": 1346846400,"value": 18,"tags": {"host": "web01","dc": "lga"},"interval": "1h","aggregator": "SUM","groupByAggregator": "SUM"}多個數據點[{...略},{...略}]響應與/api/put相同
Name
|
Data Type
|
Required
|
Description
|
Default
|
QS
|
RW
|
Example
|
metric
|
String
|
Required
|
指標的名稱
|
|
|
W
|
sys.cpu.nice
|
timestamp
|
Integer
|
Required
|
Unix時間戳,以秒或毫秒爲單位。時間戳不得包含非數字字符。
|
|
|
W
|
1365465600
|
id
|
Integer
|
Optional
|
在編寫默認簡單分塊直方圖以外的直方圖或草圖時,必須將此值設置爲tsd.core.histograms.config配置設置中定義的正確直方圖編解碼器的ID 。該值必須介於0到255之間。給定時,value必須設置。
|
|
|
w
|
1
|
value
|
String
|
Optional
|
基礎64編碼要存儲的直方圖或草圖的二進制數據。注意在寫入二進制數據時,還必須提供ID以匹配正確的編解碼器。
|
|
|
W
|
AgMIGo=
|
buckets
|
Map
|
Optional
|
存儲桶下限和上限(以逗號分隔)的映射作爲具有整數計數器存儲桶值的鍵。詳情如下。
|
|
|
W
|
{"0,1.75":12,"1.75,3.5":16}
|
underflow
|
Integer
|
Optional
|
測量計數低於最低桶下限。默認值爲零。
|
|
|
W
|
0
|
overflow
|
Integer
|
Optional
|
測量計數高於最高桶上限。默認值爲零。
|
|
|
W
|
0
|
tags
|
Map
|
Required
|
標籤名稱/標籤值對的映射。必須至少提供一對。
|
|
|
W
|
{"host":"web01"}
|
單個數據點{"metric": "sys.cpu.nice","timestamp": 1356998400,"overflow": 1,"underflow": 0,"buckets": {"0,1.75": 12,"1.75,3.5": 16},"tags": {"host": "web01","dc": "lga"}}多個數據點[{...略},{...略}]響應與/api/put相同
Name
|
Data Type
|
Required
|
Description
|
Default
|
QS
|
RW
|
Example
|
start
|
String, Integer
|
Required
|
|
start
|
|
1h-ago
|
|
end
|
String, Integer
|
Optional
|
current time
|
end
|
|
1s-ago
|
|
queries
|
Array
|
Required
|
用於選擇要返回的時間序列的一個或多個子查詢。這些可以是度量m或TSUID tsuids查詢
|
|
m or tsuids
|
|
See below
|
noAnnotations
|
Boolean
|
Optional
|
是否返回帶有查詢的註釋。默認設置是返回請求的時間跨度的註釋,但此標誌可以禁用返回。這會影響本地和全局註釋和覆蓋globalAnnotations
|
false
|
no_annotations
|
|
false
|
globalAnnotations
|
Boolean
|
Optional
|
查詢是否應檢索所請求的時間跨度的全局註釋
|
false
|
global_annotations
|
|
true
|
msResolution (or ms)
|
Boolean
|
Optional
|
是否以毫秒或秒爲單位輸出數據點時間戳。建議使用msResolution標誌。如果未提供此標誌且一秒內有多個數據點,則將使用查詢的聚合函數對這些數據點進行下采樣。
|
false
|
ms
|
|
true
|
showTSUIDs
|
Boolean
|
Optional
|
是否在結果中輸出與時間序列關聯的TSUID。如果將多個時間序列聚合到一個集合中,則將以排序的方式返回多個TSUID
|
false
|
show_tsuids
|
|
true
|
showSummary (2.2)
|
Boolean
|
Optional
|
false
|
show_summary
|
|
true
|
|
showStats (2.2)
|
Boolean
|
Optional
|
false
|
show_stats
|
|
true
|
|
showQuery (2.2)
|
Boolean
|
Optional
|
是否使用查詢結果返回原始子查詢。如果請求包含許多子查詢,那麼這是確定哪些結果屬於哪個子查詢的好方法。請注意,在*通配符或通配符查詢的情況下,這會產生大量重複輸出。
|
false
|
show_query
|
|
true
|
delete
|
Boolean
|
Optional
|
可以使用POST傳遞給JSON以刪除與給定查詢匹配的任何數據點。
|
false
|
|
W
|
true
|
timezone (2.3)
|
String
|
Optional
|
UTC
|
timezone
|
|
Asia/Kabul
|
|
useCalendar (2.3)
|
Boolean
|
Optional
|
是否使用基於給定時區的日曆來進行下采樣間隔
|
false
|
|
|
true
|
-
Metric Query - 提供metric的全名以及可選的tag列表。這被優化用於將多個時間序列聚合成一個結果。
-
TSUID Query - 共享公共metric的一個或多個TSUID的列表。這針對獲取不需要聚合的單個時間序列進行了優化。
Name
|
Data Type
|
Required
|
Description
|
Default
|
Example
|
aggregator
|
String
|
Required
|
|
sum
|
|
metric
|
String
|
Required
|
存儲在系統中的metric的名稱
|
|
sys.cpu.0
|
rate
|
Boolean
|
Optional
|
在返回之前是否應將數據轉換爲增量。如果度量標準是一個連續遞增的計數器,並且您想要查看數據點之間的變化率,這將非常有用。
|
false
|
true
|
rateOptions
|
Map
|
Optional
|
單調增加處理選項
|
See below
|
See below
|
downsample
|
String
|
Optional
|
可選的下采樣功能,用於減少返回的數據量。
|
See below
|
5m-avg
|
tags
|
Map
|
Optional
|
要深入查看特定時間序列或按標記對結果進行分組,請提供與查詢字符串格式相同的一個或多個映射值。標籤在2.2中轉換爲過濾器。請參閱以下有關轉化的說明。請注意,如果未指定任何標記,系統中的所有指標都將彙總到結果中。在2.2中棄用
|
|
See Below
|
filters (2.2)
|
List
|
Optional
|
發出結果中過濾時間序列。請注意,如果未指定過濾器,則給定度量標準的所有時間序列都將聚合到結果中。
|
|
See Below
|
explicitTags (2.3)
|
Boolean
|
Optional
|
僅返回包含過濾器中提供的tag的序列
|
false
|
true
|
percentiles (2.4)
|
List
|
Optional
|
獲取度量的直方圖數據,並計算數據上給定的百分位數列表。百分位數是從0到100的浮點值。更多詳細信息如下。
|
|
[99.9, 95.0, 75.0]
|
Name
|
Data Type
|
Required
|
Description
|
Default
|
Example
|
counter
|
Boolean
|
Optional
|
基礎數據是否是可以翻轉的單調遞增計數器
|
false
|
true
|
counterMax
|
Integer
|
Optional
|
一個正整數,表示計數器的最大值。
|
Java Long.MaxValue
|
65535
|
resetValue
|
Integer
|
Optional
|
一個可選值,當超出該值時,將導致聚合器返回0而不是計算的速率。經常重置數據源以避免虛假尖峯時很有用。
|
0
|
65000
|
dropResets
|
Boolean
|
Optional
|
是否簡單地丟棄翻轉或重置數據點。
|
false
|
true
|
<interval><units>-<aggregator>[c][-<fill policy>]1h-sum30m-avg-nan24h-max-zero1dc-sum0all-sumfileters過濾器使用
Name
|
Data Type
|
Required
|
Description
|
Default
|
Example
|
type
|
String
|
Required
|
|
regexp
|
|
tagk
|
String
|
Required
|
過濾的tag
|
|
host
|
filter
|
String
|
Required
|
過濾器表達式,取決於所使用的過濾器
|
|
web.*.mysite.com
|
groupBy
|
Boolean
|
Optional
|
是否按過濾器匹配的每個值對結果進行分組。默認情況下,與篩選器匹配的所有值都將聚合到一個系列中。
|
false
|
true
|
Example
|
Description
|
<tagk>=*
|
Wildcard filter, effectively makes sure the tag key is present in the series 通配符過濾器,有效地確保標記鍵存在於系列中
|
<tagk>=value
|
區分大小寫的字符OR過濾器
|
<tagk>=value1|value2|valueN
|
區分大小寫的字符OR過濾器
|
<tagk>=va*
|
不區分大小寫的通配符過濾器。帶有任何其他字符串的星號(星號)現在變爲通配符過濾器快捷方式
|
query with filters{"start": 1356998400,"end": 1356998460,"queries": [{"aggregator": "sum","metric": "sys.cpu.0","rate": "true","filters": [{"type":"wildcard","tagk":"host","filter":"*","groupBy":true},{"type":"literal_or","tagk":"dc","filter":"lga|lga1|lga2","groupBy":false}]},{"aggregator": "sum","tsuids": ["000001000002000042","000001000002000043"]}]}
Name
|
Description
|
metric
|
查詢的metric名稱
|
tags
|
僅當結果針對單個時間系列時,纔會返回標記列表。如果聚合了結果,則此值可能爲null或空映射
|
aggregatedTags
|
如果結果集中包含多個時間序列,即它們已聚合,則會顯示在所有時間序列中共同找到的標記名稱列表。
|
dps
|
聚合器處理後檢索的數據點。每個數據點由時間戳和值組成,格式由序列化程序確定。
|
annotations
|
如果查詢在請求的時間跨度內檢索了時間序列的註釋,則它們將在此組中返回。每個時間序列的註釋將合併爲一個集合並按其排序start_time。聚合器函數不會影響註釋,將爲跨度返回所有註釋。
|
globalAnnotations
|
如果用戶請求,查詢將在時間跨度內掃描全局註釋,並在此組中返回結果
|
-
/api/search/tsmeta - TSMETA Response
-
/api/search/tsmeta_summary - TSMETA_SUMMARY Response
-
/api/search/tsuids - TSUIDS Response
-
/api/search/uidmeta - UIDMETA Response
-
/api/search/annotation - Annotation Response
Name
|
Data Type
|
Required
|
Description
|
Default
|
QS
|
RW
|
Example
|
query
|
String
|
Optional
|
|
query
|
|
name:sys.cpu.*
|
|
limit
|
Integer
|
Optional
|
25
|
limit
|
|
100
|
|
startIndex
|
Integer
|
Optional
|
0
|
start_index
|
|
42
|
|
metric
|
String
|
Optional
|
用於lookup查詢的度量標準或通配符的名稱
|
*
|
metric
|
|
tsd.hbase.rpcs
|
tags
|
Array
|
Optional
|
|
tags
|
|
Name
|
Data Type
|
Description
|
Example
|
type
|
String
|
提交的查詢類型,即調用的接口。將是上面列出的接口之一。
|
TSMETA
|
query
|
String
|
提交的查詢請求。可能會被插件更改
|
name:sys.cpu.*
|
limit
|
Integer
|
結果集中返回的最大項目數。請注意,返回的實際數字可能小於限制。
|
25
|
startIndex
|
Integer
|
查詢中提供的當前結果集的起始索引
|
0
|
metric
|
String
|
用於lookup的metric
|
|
tags
|
Array
|
用於lookup查詢的標記對列表。可能是一個空列表。
|
[ ]
|
time
|
Integer
|
完成查詢所花費的時間(以毫秒爲單位)
|
120
|
totalResults
|
Integer
|
查詢匹配的結果總數
|
1024
|
results
|
Array
|
結果集。格式取決於請求的接口。
|
See Below
|
請求示例:data形式:{"query": "name:*","limit": 4,"startIndex": 5}響應:{"type": "TSMETA","query": "name:*","metric": "*","tags": [],"limit": 2,"time": 675,"results": [{"tsuid": "0000150000070010D0","metric": {"uid": "000015","type": "METRIC","name": "app.apache.connections","description": "","notes": "","created": 1362655264,"custom": null,"displayName": ""},"tags": [{"uid": "000007","type": "TAGK","name": "fqdn","description": "","notes": "","created": 1362655264,"custom": null,"displayName": ""},{"uid": "0010D0","type": "TAGV","name": "web01.mysite.com","description": "","notes": "","created": 1362720007,"custom": null,"displayName": ""}],"description": "","notes": "","created": 1362740528,"units": "","retention": 0,"max": 0,"min": 0,"displayName": "","dataType": "","lastReceived": 0,"totalDatapoints": 0},{"tsuid": "0000150000070010D5","metric": {"uid": "000015","type": "METRIC","name": "app.apache.connections","description": "","notes": "","created": 1362655264,"custom": null,"displayName": ""},"tags": [{"uid": "000007","type": "TAGK","name": "fqdn","description": "","notes": "","created": 1362655264,"custom": null,"displayName": ""},{"uid": "0010D5","type": "TAGV","name": "web02.mysite.com","description": "","notes": "","created": 1362720007,"custom": null,"displayName": ""}],"description": "","notes": "","created": 1362882263,"units": "","retention": 0,"max": 0,"min": 0,"displayName": "","dataType": "","lastReceived": 0,"totalDatapoints": 0}],"startIndex": 0,"totalResults": 9688066}
Field Name
|
Data Type
|
Description
|
Example
|
serializer
|
String
|
序列化程序的名稱,適用於查詢字符串serializer=<serializer_name>參數
|
xml
|
formatters
|
Array<String>
|
序列化程序實現的一組方法或端點,用於轉換響應數據。這些通常映射到端點,例如/api/suggest映射到Suggest。如果序列化程序未實現某種方法,則默認格式化程序將響應。每個名稱也以支持的API版本結束,例如,V1將支持版本1 API調用。
|
"Error","Suggest"
|
parsers
|
Array<String>
|
序列化程序實現的一組方法或端點,用於解析HTTP請求正文中的用戶輸入。這些通常映射到端點,例如/api/suggest將映射到Suggest。如果序列化程序未實現解析器,則將使用默認序列化程序。每個名稱也以支持的API版本結束,例如,V1將支持版本1 API調用。
|
"Suggest","Put"
|
響應:[{"formatters": ["SuggestV1","SerializersV1","ErrorV1","ErrorV1","NotFoundV1"],"serializer": "json","parsers": ["SuggestV1"],"class": "net.opentsdb.tsd.HttpJsonSerializer","response_content_type": "application/json; charset=UTF-8","request_content_type": "application/json"}]
請求:http://localhost:4242/api/stats響應:[{"metric": "tsd.connectionmgr.connections","timestamp": 1369350222,"value": "1","tags": {"host": "wtdb-1-4"}},{"metric": "tsd.connectionmgr.exceptions","timestamp": 1369350222,"value": "0","tags": {"host": "wtdb-1-4"}},{"metric": "tsd.rpc.received","timestamp": 1369350222,"value": "0","tags": {"host": "wtdb-1-4","type": "telnet"}}]
Name
|
Data Type
|
Required
|
Description
|
Default
|
QS
|
RW
|
Example
|
type
|
String
|
Required
|
要自動完成的數據類型。必須是以下之一:metrics,tagk或tagv
|
|
type
|
|
metrics
|
q
|
String
|
Optional
|
要匹配給定類型的字符串
|
|
q
|
|
web
|
max
|
Integer
|
Optional
|
要返回的最大建議結果數。必須大於0
|
25
|
max
|
|
10
|
請求:{"type":"metrics","q":"sys","max":10}響應:["sys.cpu.0.nice","sys.cpu.0.system","sys.cpu.0.user","sys.cpu.1.nice","sys.cpu.1.system","sys.cpu.1.user"]
Name
|
Data Type
|
Required
|
Description
|
Default
|
QS
|
RW
|
Example
|
treeId
|
Integer
|
Required*
|
|
treeid
|
RO
|
1
|
|
name
|
String
|
Required*
|
|
name
|
RW
|
Network Infrastructure
|
|
description
|
String
|
Optional
|
對樹的更長的描述
|
|
description
|
RW
|
Tree containing all network gear
|
notes
|
String
|
Optional
|
關於樹的詳細說明
|
|
notes
|
RW
|
|
strictMatch
|
Boolean
|
Optional
|
如果時間序列不符合一個或多個規則級別,則是否應將其包括在樹中。
|
false
|
strict_match
|
RW
|
true
|
enabled
|
Boolean
|
Optional
|
是否應該通過樹處理TSMeta。默認設置爲,false以便您可以在構建分支之前設置規則並測試某些對象。
|
false
|
enabled
|
RW
|
true
|
storeFailures
|
Boolean
|
Optional
|
是否應記錄碰撞和“不匹配”的TSUID。這可以創建非常寬的行。
|
false
|
store_failures
|
RW
|
true
|
definition
|
Boolean
|
Optional
|
僅在DELETE使用樹時使用,如果此標誌設置爲true,則將刪除整個樹定義以及所有分支,衝突和未匹配的條目
|
false
|
definition
|
|
true
|
Name
|
Data Type
|
Description
|
Example
|
rules
|
Map
|
爲樹組織的規則的地圖或字典,由level和組織order。如果尚未定義規則,則值爲null
|
See Examples
|
created
|
Integer
|
最初創建樹時的Unix時間戳(以秒爲單位)。
|
1350425579
|
-
/api/uid 此接口提供用於管理UID及其關聯數據的實用程序,提供了3個子接口
請求:http://localhost:4242/api/version響應:{"timestamp": "1362712695","host": "localhost","repo": "/opt/opentsdb/build","full_revision": "11c5eefd79f0c800b703ebd29c10e7f924c01572","short_revision": "11c5eef","user": "localuser","repo_status": "MODIFIED","version": "2.0.0"}