3.Telegraf自定义脚本模块
官网:https://github.com/influxdata/telegraf/tree/master/plugins/inputs/exec
通过exec自定义模块可以很方便的将自定义脚本输出的内容同步到influxdb。
telegraf.conf 开启自定义插件:
[[inputs.exec]]
## Commands array
commands = [
"/tmp/test.sh",
"/usr/bin/mycollector --foo=bar",
"/tmp/collect_*.sh"
]
## Timeout for each command to complete.
timeout = "5s"
## measurement name suffix (for separating different commands)
name_suffix = "_mycollector"
## Data format to consume.
## Each data format has its own unique set of configuration options, read
## more about them here:
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
data_format = "influx"
脚本输出格式如下:
#!/bin/sh
echo 'example,tag1=a,tag2=b i=42i,j=43i,k=44i'
示例:
[root@szpbs-nonerp-uat-mysql02 telegraf]# cat telegraf.d/collect_ip.conf
[[inputs.exec]]
##Commands array
commands = ["bash /etc/telegraf/exec/collect_ip.sh",]
timeout='5s'
##measurements的后缀
name_suffix="_exec"
data_format="influx"
#!/bin/bash
addr=$(/usr/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep 10|grep -v inet6|awk '{print $2}'|tr -d "addr:"|head -1)
host=$(hostname)
echo 'systeminfo,addr='$addr' hostname="'$host'",area="深圳鹏博士",item="AI测试2",env="moni"'(注意这里$addr和$host的区别)
echo 'measurement,tag1=a,tag2=b field1="str1",field2=1231'(经测试,tag字段可以不加双引号输出,filed字段,值非数字需要双引号)