Telegraf can collect metrics from a wIDe array of inputs and write them to a wIDe array of outputs. It is plugin-driven for both collection and output of data so it is easily extendable. It is written in Go,which means that it is compiled and standalone binary that can be executed on any system with no need for external dependencIEs,or package management tools required.
Telegraf is an open-source tool. It contains over 200 plugins for gathering and writing different types of data written by people who work with that data.
We wrote the Postgresql output plugin which also has the ability to send data to a TimescaleDB hypertable. The pull request is open and currently under revIEw by the Telegraf developers,waiting to be merged. To give developers the opportunity to try this functionality,we built downloadable binarIEs of Telegraf with our plugin already included.
This tutorial will run through a couple of examples on how to use the Postgresql/TimescaleDB output plugin for Telegraf.
Installation Before we startBefore we start,you will need TimescaleDB installed and a means to connect to it.
Setting up TelegrafTelegraf is written in Go,and the current build process of the tool is configured to produce one standalone binary. Because of this all the code for the different plugins must be part of that binary. We have an unofficial build of Telegraf version 1.10.4 with our plugin added.
Once you download the binary and extract it to a suitable location (or install the packages) we can test out the build. You may have to make the file executable by running chmod +x telegraf
. We can check the version of the installed Telegraf with
$ telegraf --version
If the installation was successful it should print out Telegraf 1.10.4-with-pg
.
When Telegraf is started,a config file needs to be specifIEd. The config file contains the setup for the:
Telegraf agent Collection interval Jitter Buffer and batch size and so on Global Tags added to all collected metrics from all inputs Enabled outputs,processors,aggregators,inputs (and their respective configuration)A sample config file with Postgresql included as a plugin can be generated by executing
$ telegraf --input-filter=cpu --output-filter=postgresql config > telegraf.conf
The above command generates a config file that enables the cpu input plugin (which samples varIoUs metrics about cpu usage) and the Postgresql output plugin.
The config file also includes all available input,output,processor,and aggregator plugins, but commented out. So,it‘s easy to see how a plugin should be configured.
To test our configuration,we can output a single collection to STDOUT. By running
$ telegraf --config telegraf.conf --test
we select the generated config file that enables only the cpu input plugin. And the output should look something like:
> cpu,cpu=cpu0,host=local usage_guest=0,usage_IDle=78.431372,usage_iowait=0,usage_irq=0,usage_softirq=0,usage_steal=0,usage_system=11.764705,usage_user=9.803921 1558613882000000000> cpu,cpu=cpu1,usage_IDle=92.156862,usage_system=3.921568,usage_user=3.921568 1558613882000000000> cpu,cpu=cpu-total,usage_IDle=87.623762,usage_system=6.435643,usage_user=5.940594 1558613882000000000
A line is outputted for each core of the cpu and the total. Values are presented in key=value
pairs with the timestamp last in the row. When writing to STDOUT you can distinguish between Tags,which are indexed fIElds (cpu
, host
) and value fIElds (usage_quest
, usage_user
...) by a blank space (in our example the space after host=local
). The distinction exists because different configuration options are available for the different fIElds.
The telegraf.conf
file we generated has a section (around line 80) headed with
################################################# OUTPUT PLUGINS #################################################
And below this header,the default configuration for the Postgresql output plugin is laID out.
[[outputs.postgresql]] ## specify address via a url matching: ## postgres://[pqgotest[:password]]@localhost[/dbname] ## ?sslmode=[disable|verify-ca|verify-full] ## or a simple string: ## host=localhost user=pqotest password=... sslmode=... dbname=app_production ## ## All connection parameters are optional. ## ## Without the dbname parameter,the driver will default to a database ## with the same name as the user. This dbname is just for instantiating a ## connection with the server and doesn‘t restrict the databases we are trying ## to grab metrics for. ## address = "host=localhost user=postgres sslmode=verify-full" ## Store Tags as foreign keys in the metrics table. Default is false. # Tags_as_foreignkeys = false ## Template to use for generating tables ## Available Variables: ## {table} - tablename as IDentifIEr ## {tableliteraL} - tablename as string literal ## {ColUMNS} - column deFinitions ## {KEY_ColUMNS} - comma-separated List of key columns (time + Tags) ## Default template # table_template = "CREATE table IF NOT EXISTS {table}({ColUMNS})" ## Example for timescaledb # table_template = "CREATE table {table}({ColUMNS}); SELECT create_hypertable({tableliteraL},‘time‘);" ## Schema to create the tables into # schema = "public" ## Use Jsonb datatype for Tags # Tags_as_Jsonb = false ## Use Jsonb datatype for fIElds # fIElds_as_Jsonb = false
From the config we can notice several things:
The top line enables the plugin,the plugin specific config is indented after this line There is currently only one parameter configured,address
. The others are commented out Possible parameters are commented out with a single #
. (Tags_as_foreignkeys
, table_template
, schema
, Tags_as_Jsonb
, fIElds_as_Jsonb
) Explanations of the parameters are commented out with a single ##
The commented out parameters also show their default values.
For the first example we‘ll set the address parameter to a proper connection string to establish a connection to an instance of TimescaleDB or Postgresql. All the other parameters will have their default values.
Creating hypertablesThe plugin we developed allows the user to configure several parameters. The table_template
parameter defines the sql to be executed when a new measurement is recorded by Telegraf and the required table doesn‘t exist in the output database. By default the table_template
used is CREATE table IF NOT EXISTS {table}({ColUMNS})
where {table}
and {ColUMNS}
are placeholders for the name of the table and the column deFinitions.
Let‘s update table_template
in the config for TimescaleDB:
table_template=`CREATE table IF NOT EXISTS {table}({ColUMNS}); SELECT create_hypertable({tableliteraL},‘time‘,chunk_time_interval := ‘1 week‘::interval,if_not_exists := true);`
This way when a new table is created it is converted into a hypertable,with each chunk holding 1 week intervals. nothing else is needed to use the plugin with TimescaleDB.
Running TelegrafWhen we run Telegraf we only need to specify the config file to be used. If we execute
$ telegraf --config telegraf.conf 2019-05-23T13:48:09Z I! Starting Telegraf 1.10.4-with-pg2019-05-23T13:48:09Z I! Loaded inputs: cpu2019-05-23T13:48:09Z I! Loaded outputs: postgresql2019-05-23T13:48:09Z I! Tags enabled: host=local2019-05-23T13:48:09Z I! [agent] Config: Interval:10s,QuIEt:false,Hostname:"local",Flush Interval:10s
In the output you can notice the loaded inputs (cpu
) and outputs (postgresql
) along with the global Tags and the intervals with which the agent will collect the data from the inputs,and flush to the outputs. We can stop the execution of Telegraf after ~10-15 seconds.
Let us Now connect to our Postgresql instance and inspect the data
$ psql -U postgres -h localhost
The cpu input plugin has one measurement,called cpu,and it‘s stored in a table of the same name (by default in the public schema). So with the sql query SELECT * FROM cpu
,depending on how long you left Telegraf running you will see the table populated with some values. We can find the average usage per cpu core with SELECT cpu,avg(usage_user) FROM cpu GROUP BY cpu
. The output should look like
cpu | avg -----------+------------------ cpu-total | 8.46385703620795 cpu0 | 12.4343351351033 cpu1 | 4.88380203380203 cpu2 | 12.2718724052057 cpu3 | 4.26716970050303Adding new Tags or FIElds
Your Telegraf configuration can change at any moment. An input plugin can be reconfigured to produce different data,or you may decIDe to index your data with different Tags. Our sql plugin can dynamically update the created tables with new columns as they appear. The prevIoUs configuration we used had no global Tags specifIEd other than the host
tag. We will Now add a new global tag in the configuration. Open the file in any text editor and update the [global_Tags]
section (around line 18) with:
[global_Tags] location="New York"
This way all metrics collected with the instance of Telegraf running with this config will be tagged with location="New York"
. If we run Telegraf again,collecting the metrics in TimescaleDB
$ telegraf --config telegraf.conf
And after a while we check on the cpu
table in the database
psql> \dS cpu\dS cpu;table "public.cpu" Column | Type ------------------+-------------------------- time | timestamp with time zone cpu | text host | text usage_steal | double precision usage_iowait | double precision usage_guest | double precision usage_IDle | double precision usage_softirq | double precision usage_system | double precision usage_user | double precision usage_irq | double precision location | text
The location
column was added and it contains "New York" for all rows.
The plugin we developed allows the user to select to have the tag sets inserted in a separate table and then referenced via foreign keys in the measurement table. Having the Tags in a separate table saves space for high cardinality tag sets,and allows certain querIEs to be written more efficIEntly. To enable this change,you need to uncomment the Tags_as_foreignkeys
parameter in the plugin config (around line 103 in telegraf.conf
) and set it to true
## Store Tags as foreign keys in the metrics table. Default is false. Tags_as_foreignkeys = true
To better visualize the result we‘ll drop the existing cpu
table from our database.
psql> DROP table cpu;
Now we‘ll fire Telegraf up again,this time with the config changed to write the Tags in a separate table.
$ telegraf --config telegraf.conf
We can turn it off after 20-30 seconds. If we check on the cpu
table in the database:
psql> \dS cpu\dS cputable "public.cpu" Column | Type ------------------+-------------------------- time | timestamp with time zone tag_ID | integer usage_irq | double precision usage_softirq | double precision usage_system | double precision usage_iowait | double precision usage_guest | double precision usage_user | double precision usage_IDle | double precision usage_steal | double precision
Notice that the cpu
, host
and location
columns are not there,instead there‘s a tag_ID
column. The tag sets are stored in a separate table called cpu_tag
:
psql> SELECT * FROM cpu_tag; tag_ID | host | cpu | location--------+-------+-----------+---------- 1 | local | cpu-total | New York 2 | local | cpu0 | New York 3 | local | cpu1 | New YorkJsONB column for Tags and FIElds
Additionally the Tags and fIElds can be stored as JsONB columns in the database. All you need to do is uncomment the Tags_as_Jsonb
or fIElds_as_Jsonb
parameters in telegraf.conf
(around line 120) and set them to true. In this example we‘ll store the fIElds as separate columns,but the Tags as JsON.
## Use Jsonb datatype for Tags Tags_as_Jsonb = true ## Use Jsonb datatype for fIElds fIElds_as_Jsonb = false
To better visualize the result we‘ll drop the existing cpu_tag
table from our database.
psql> DROP table cpu_tag;
Fire up Telegraf again,and turn it off after 20-30 seconds. Then we check the cpu_tag
table:
$ telegraf --config telegraf.conf
psql> SELECT * FROM cpu_tag; tag_ID | Tags --------+----------------------------------------------------------------------------------- 1 | {"cpu": "cpu-total","host": "local","location": "New York"} 2 | {"cpu": "cpu0","location": "New York"} 3 | {"cpu": "cpu1","location": "New York"}
And instead of having three text columns,one JsONB column is created.
@H_798_301@ 总结以上是内存溢出为你收集整理的Collecting metrics with the PostgreSQL and TimescaleDB output plugin for Telegraf全部内容,希望文章能够帮你解决Collecting metrics with the PostgreSQL and TimescaleDB output plugin for Telegraf所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)