collectd 的数据如何映射到influxdb

collectd 的数据如何映射到influxdb

本文将使用collectdctl 向influxdb 发送数据, 同时介绍collectd 的数据是如何映射到influxdb 中(创建measurements,series)

  1. 配置influxdb

    [[collectd]]
      enabled = true
      bind-address = "127.0.0.1:25826"
      database = "collectd"
      retention-policy = ""
      # typesdb 
      typesdb = "/usr/local/Cellar/collectd/5.8.0/share/collectd/types.db"
  2. 配置collected

    LoadPlugin cpu
      LoadPlugin memory
      LoadPlugin network
      LoadPlugin unixsock
      <Plugin network>
              <Server "127.0.0.1" "25826">
              </Server>
      </Plugin>
  3. 查看influxdb 确认collectd push 数据
  4. collectdctl listval

    localhost/load/load
      localhost/memory/memory-active
      localhost/memory/memory-free
      localhost/memory/memory-inactive
      localhost/memory/memory-wired
      localhost/interface-en0/if_errors
      localhost/interface-en0/if_octets
  5. influx -database 'collectd' -execute 'show series'

    load_longterm,host=localhost,type=load
     load_midterm,host=localhost,type=load
     load_shortterm,host=localhost,type=load
     memory_value,host=localhost,type=memory,type_instance=active
     memory_value,host=localhost,type=memory,type_instance=free
     memory_value,host=localhost,type=memory,type_instance=inactive
     memory_value,host=localhost,type=memory,type_instance=wired
     interface_rx,host=localhost,instance=en0,type=if_errors
     interface_rx,host=localhost,instance=en0,type=if_octets
     interface_tx,host=localhost,instance=en0,type=if_errors
     interface_tx,host=localhost,instance=en0,type=if_octets
  6. cat /usr/local/Cellar/collectd/5.8.0/share/collectd/types.db

    load                    shortterm:GAUGE:0:5000, midterm:GAUGE:0:5000, longterm:GAUGE:0:5000
      memory                  value:GAUGE:0:281474976710656
      if_errors               rx:DERIVE:0:U, tx:DERIVE:0:U
      if_octets               rx:DERIVE:0:U, tx:DERIVE:0:U
  7. collectdb 用identifier 来描述一条数据,identifier相当与KV数据库的Key。
    identifier 格式如下
    [<hostname>/]<plugin>[-<plugin_instance>]/<type>[-<type_instance>]

    collectdb用types.db的映射关系来描述Value 的格式
    identifier 的type 一定要在types.db中 找到否则报错

    1. localhost/load/load 
       type:load  
       types.db 定义
       load shortterm:GAUGE:0:5000, midterm:GAUGE:0:5000, longterm:GAUGE:0:5000
       
     2. localhost/interface-en0/if_octets
       type: if_octets
       types.db  定义
       if_octets               rx:DERIVE:0:U, tx:DERIVE:0:U 
     3. localhost/memory/memory-free
        type: memory (free 并不是type)
        memory                  value:GAUGE:0:281474976710656
8.  其实通过4,5,6 的数据我们很容易看数collectd 到influxdb的映射关系
1. localhost/load/load
   plugin: load
   type: load
   typeValue: shortterm, midterm, longterm
   映射到influxdb
   measurements: load_shortterm load_midterm load_longterm
   tags:  host=localhost,type=load
 2. localhost/memory/memory-active
    plugin: memory
    type: memory
    typeValue: Value
    映射到influxdb
    measurements: memory_value
    tags: host=localhost,type=memory,type_instance=active
 3. localhost/interface-en0/if_octets
    plugin: interface
    type: if_octets
    typeValue: rx, tx
    measurements: interface_rx, interface_tx
    tags: host=localhost,instance=en0,type=if_octets
9.  collectd 的 identifier
 [<hostname>/]<plugin>[-<plugin_instance>]/<type>[-<type_instance>]
 
 的`plugin_typeValue`构成了influxdb的measurements
 tags:hostname=<hostname>, instance=<plugin_instance>, type=<type>, type_instance=<type_instance>
 
 由于 plugin_instance , type_instace 是可选的
 所以collectd 倒入influxdb的数据tags 至少由hostname, type
 

 
10. 练习
1. collectdctl putval localhost/xxx/load N:111
     influxdb 会新建xxx_longterm, xxx_shortterm, xxx_midterm measurement
     tag hostname=localhost,type=load
  2. collectdctl putval localhost/xxx/if_octets N:1:100
    influxdb 会新建xxx_rx, xxx_tx measurement
  3.echo "test    running:GAUGE:0:U" >> /usr/local/Cellar/collectd/5.8.0/share/collectd/types.db
    collectdct putval localhost xxx/test N:1000
    influxdb 会新建xxx_test measurement
```