
# OpenTSDB CLI Tools介绍
OpenTSDB提供了客户端工具，可以直接调用相关命令对OpenTSDB进行操作。客户端工具同开源社区版本保持一致，请参见<https://opentsdb.net/docs/build/html/user_guide/cli/index.html>。
客户端工具使用方法：
1. 登录任意一个Master节点。
2. 初始化环境变量。 
   **source /opt/client/bigdata_env**
   
   
3. 如果当前集群已启用Kerberos认证，执行以下命令认证当前用户。如果当前集群未启用Kerberos认证，则无需执行此命令。 
   **kinit** ***MRS*** ***集群用户***
   例如：**kinit opentsdbuser**
   
   
4. 执行**tsdb** 命令。例如执行**tsdb** 可以打印出当前opentsdb所支持的所有命令，如，fsck, import, mkmetric, query, tsd, scan, search, uid, version。
   
   ```
   tsdb: error: unknown command ''
   usage: tsdb <command> [args]
   Valid commands: fsck, import, mkmetric, query, tsd, scan, search, uid, version
   ```
   
   
#### 创建OpenTSDB指标
创建存入到OpenTSDB中的指标名称，可执行**tsdb mkmetric sys.cpu.user** 命令创建sys.cpu.user**。**
```
Start run net.opentsdb.tools.UidManager, args: assign metrics sys.cpu.user
metrics sys.cpu.user: [0, 0, 6]
```
#### 查询OpenTSDB指标
tsdb命令可以获取到当前opentsdb存入的指标，可执行**tsdb uid metrics sys.cpu.user**命令。
```
Start run net.opentsdb.tools.UidManager, args: metrics sys.cpu.user
metrics sys.cpu.user: [0, 0, 6]
```
若想获取更多信息，可通过执行命令**tsdb uid**。
```
Start run net.opentsdb.tools.UidManager, args:
Not enough arguments
Usage: uid <subcommand> args
Sub commands:
  grep [kind] <RE>: Finds matching IDs.
  assign <kind> <name> [names]: Assign an ID for the given name(s).
  rename <kind> <name> <newname>: Renames this UID.
  delete <kind> <name>: Deletes this UID.
  fsck: [fix] [delete_unknown] Checks the consistency of UIDs.
        fix            - Fix errors. By default errors are logged.
        delete_unknown - Remove columns with unknown qualifiers.
                         The "fix" flag must be supplied as well.
  [kind] <name>: Lookup the ID of this name.
  [kind] <ID>: Lookup the name of this ID.
  metasync: Generates missing TSUID and UID meta entries, updates
            created timestamps
  metapurge: Removes meta data entries from the UID table
  treesync: Process all timeseries meta objects through tree rules
  treepurge <id> [definition]: Purge a tree and/or the branches
            from storage. Provide an integer Tree ID and optionally
            add "true" to delete the tree definition
Example values for [kind]: metrics, tagk (tag name), tagv (tag value).
  --config=PATH    Path to a configuration file (default: Searches for file see docs).
  --idwidth=N      Number of bytes on which the UniqueId is encoded.
  --ignore-case    Ignore case distinctions when matching a regexp.
  --table=TABLE    Name of the HBase table where to store the time series (default: tsdb).
  --uidtable=TABLE Name of the HBase table to use for Unique IDs (default: tsdb-uid).
  --verbose        Print more logging messages and not just errors.
  --zkbasedir=PATH Path under which is the znode for the -ROOT- region (default: /hbase).
  --zkquorum=SPEC  Specification of the ZooKeeper quorum to use (default: localhost).
  -i               Short for --ignore-case.
  -v               Short for --verbose.
```
#### 向OpenTSDB指标中导入数据
tsdb命令可以使用"tsdb import"命令批量导入指标数据，可执行如下命令：
- 准备指标数据，如包含如下内容的importData.txt文件。
  ```
  sys.cpu.user 1356998400 41 host=web01 cpu=0
  sys.cpu.user 1356998401 42 host=web01 cpu=0
  sys.cpu.user 1356998402 44 host=web01 cpu=0
  sys.cpu.user 1356998403 47 host=web01 cpu=0
  sys.cpu.user 1356998404 42 host=web01 cpu=0
  sys.cpu.user 1356998405 42 host=web01 cpu=0
  ```
  
- 导入指标数据，可执行如下命令**tsdb import importData.txt** 。
  ```
  Start run net.opentsdb.tools.TextImporter, args: importData.txt
  2019-06-26 15:45:22,091 INFO  [main] TextImporter: reading from file:importData.txt
  2019-06-26 15:45:22,102 INFO  [main] TextImporter: Processed importData.txt in 11 ms, 6 data points (545.5 points/s)
  2019-06-26 15:45:22,102 INFO  [main] TextImporter: Total: imported 6 data points in 0.012s (504.0 points/s)
  ```
  
#### 扫描OpenTSDB的指标数据
tsdb命令可以使用"tsdb query"命令批量查询导入的指标数据，例如执行**tsdb query 0 1h-ago sum sys.cpu.user host=web01**命令。
```
Start run net.opentsdb.tools.CliQuery, args: 0 1h-ago sum sys.cpu.user host=web01
sys.cpu.user 1356998400000 41 {host=web01, cpu=0}
sys.cpu.user 1356998401000 42 {host=web01, cpu=0}
sys.cpu.user 1356998402000 44 {host=web01, cpu=0}
sys.cpu.user 1356998403000 47 {host=web01, cpu=0}
sys.cpu.user 1356998404000 42 {host=web01, cpu=0}
sys.cpu.user 1356998405000 42 {host=web01, cpu=0}
```
#### 删除录入的OpenTSDB指标
tsdb命令可以使用"tsdb uid delete"命令删除录入的指标及值，例如执行**tsdb uid delete metrics sys.cpu.user**命令。
```
Start run net.opentsdb.tools.UidManager, args: delete metrics sys.cpu.user
```
