In the TICK stack (Telegraf, InfluxDB, Chronograf, Kapacitor) from InfluxData, Telegraf serves as the crucial "T"—the agent responsible for collecting and sending data. Metrics stored in InfluxDB can then be transformed into actionable insights using visualization tools like Grafana.
Telegraf's power lies in its pluggable architecture. By modifying the /etc/telegraf/telegraf.conf file, you can enable a wide range of input and output plugins to suit your infrastructure.
For example, to export swap information from a host with a custom tag, you would add the following to your configuration:
[[inputs.swap]] [inputs.swap.tags] metrics_source="telegraf_demo"
To verify the data flow during development, you can use the file output plugin to pipe metrics directly to stdout or a local file in InfluxDB line protocol format:
[[outputs.file]] files = ["stdout", "/tmp/metrics.out"] data_format = "influx"
When monitoring containerized environments, it is common practice to run Telegraf itself within a container.
We can create a simple Dockerfile to bundle our custom configuration:
FROM telegraf COPY telegraf.conf /etc/telegraf/telegraf.conf CMD ["telegraf"]
Build the container: # docker build -t telegrafdemo .
When running the container, you will see the output plugin in action, displaying metrics formatted as InfluxDB Line Protocol:
swap,metrics_source=telegraf_demo,host=e9e7086036b8 total=8271163392i,used=0i,free=8271163392i 1514876555000000000
If the built-in plugins don't meet your specific needs, the Exec Plugin allows you to execute external scripts and collect their output.
The following script parses the output of the uptime command and formats it for InfluxDB:
#!/sh hostname=`hostname` uptime=`awk '{print $1}' /proc/uptime` # ... (logic to extract load averages) echo "uptime,host=$hostname uptime=$uptime,load1=$load1,load5=$load5,load15=$load15"
Running the script manually confirms the output format: # sh uptime.sh uptime,host=localhost.localdomain uptime=25524.65,load1=1.14,load5=1.06,load15=1.04
To see the end-to-end flow, we need to connect Telegraf to a running InfluxDB instance using a Docker network.
# Create a dedicated bridge network docker network create --driver=bridge mintonw # Start InfluxDB container docker run --net=mintonw --name influxdemo influxdb
Update your telegraf.conf to point to the InfluxDB container and include the custom script:
[[inputs.exec]] commands = ["/etc/telegraf/uptime.sh"] data_format = "influx" [[outputs.influxdb]] url = "http://influxdemo:8086" database = "telegraf"
Once the Telegraf container is running on the same network, you can query InfluxDB to verify the custom metrics:
> use telegraf > show measurements name: measurements ---- swap uptime > select * from uptime limit 3 time host load1 load15 load5 metrics_source uptime ---- ---- ----- ------ ----- -------------- ------ 1514874715000000000 3595f01eba01 0.26 0.66 0.54 telegraf_demo 13198.45
Custom metrics can be exported easily using Telegraf's exec plugin. While this example uses uptime for demonstration, remember that many standard system metrics can be collected using native Telegraf plugins if the host's /proc or /var/run directories are mounted into the container.
Are you ready to save up to 60% in operational costs?