Telegraf: Dynamic Tags (global tags): Unterschied zwischen den Versionen
Admin (Diskussion | Beiträge) |
Admin (Diskussion | Beiträge) |
||
(24 dazwischenliegende Versionen des gleichen Benutzers werden nicht angezeigt) | |||
Zeile 1: | Zeile 1: | ||
+ | <span style="color:red">This is not really a smart solution. This means if you can collect this tag informations different, write this in another measurement and join this later, then I would recommend it this way. </span> <br> | ||
''Global tags can be specified in the [global_tags] table in key="value" format. All metrics that are gathered will be tagged with the tags specified.''<br> | ''Global tags can be specified in the [global_tags] table in key="value" format. All metrics that are gathered will be tagged with the tags specified.''<br> | ||
''With dynamic tags we want the use global_tags in a more flexible way. In short: The tags should be updated dynamically.'' | ''With dynamic tags we want the use global_tags in a more flexible way. In short: The tags should be updated dynamically.'' | ||
== Explanation about the workflow == | == Explanation about the workflow == | ||
− | We want to add the ClusterName (Hyper-V Cluster) as a global tag, because we want to filter/group by the ClusterName. | + | [[Datei:01-Telegraf-Dynamic Tags.png|800px|right]] |
+ | We want to add the ClusterName (Hyper-V Cluster) as a global tag, because we want to have the opportunity to filter/group by the ClusterName. <br> | ||
+ | ''Because Telegraf have no built-in feature to run commands to define global_tags, we write the the tag "ClusterName" to the Windows Telegraf service environment variables (Registry-Key).'' <br> | ||
+ | ''It would also be possible to write the tag to the Windows system environment variables. But it is not recommended.'' | ||
− | So we have three important files: | + | |
+ | '''So we have three important files:''' | ||
*'''tags.global.conf:''' all global tags are defined in this Telegraf configuration file. | *'''tags.global.conf:''' all global tags are defined in this Telegraf configuration file. | ||
*'''inputs.exec.set-globaltags.conf:''' this runs only the PowerShell script without any output. | *'''inputs.exec.set-globaltags.conf:''' this runs only the PowerShell script without any output. | ||
Zeile 11: | Zeile 16: | ||
'''Workflow''' | '''Workflow''' | ||
− | #The Telegraf Agent | + | #The Telegraf Agent reads '''inputs.exec.set-globaltags.conf''' |
− | #The PowerShell script '''tag.set-globaltags.ps1''' runs a command to get the ClusterName | + | #and runs every 4h the PowerShell script '''tag.set-globaltags.ps1''' because of the defined "interval". |
− | #Because of the restart of the Telegraf service, Telegraf reads the global_tags defined in the file | + | #The PowerShell script '''tag.set-globaltags.ps1''' runs a command to get the ClusterName and create/update the RegKey '''"Environment"''' with the '''ClusterName'''. |
− | #In the '''tags.global.conf''' | + | #The PowerShell script '''restart the service "Telegraf".''' |
− | + | #Because of the restart of the Telegraf service, '''Telegraf reads the global_tags defined in the file "tags.global.conf"'''. | |
− | #Telegraf find the the environment variable "ClusterName" and add a tag named '''Cluster''' with the ''' | + | #In the '''tags.global.conf''' there is a tag named "Cluster" defined. This tag has the value '''"$ClusterName"''' (this is an Windows service environment variable). '''Telegraf searches the environment variable "ClusterName"''' in the environment variable of the service. |
+ | #Telegraf find the the environment variable "ClusterName" and add a tag named '''Cluster''' with the value of the '''ClusterName''' to all metrics that are send to the InfluxDB. | ||
− | tags.global.conf | + | == Telegraf global_tags configuration file "tags.global.conf" == |
<pre> | <pre> | ||
# Global tags can be specified here in key="value" format. | # Global tags can be specified here in key="value" format. | ||
Zeile 32: | Zeile 38: | ||
− | + | == Telegraf exec plugin file "inputs.exec.set-globaltags.conf" == | |
− | + | ||
− | inputs.exec.set-globaltags.conf | + | |
<pre> | <pre> | ||
[[inputs.exec]] | [[inputs.exec]] | ||
commands = ['powershell -NoProfile -File "C:\Program Files\Telegraf\scripts\tag.set-globaltags.ps1"'] | commands = ['powershell -NoProfile -File "C:\Program Files\Telegraf\scripts\tag.set-globaltags.ps1"'] | ||
− | interval = " | + | interval = "4h" |
timeout = "1m" </pre> | timeout = "1m" </pre> | ||
− | + | == PowerShell "tag.set-globaltags.ps1" script that gets the ClusterName == | |
− | + | ||
− | tag.set-globaltags.ps1 | + | |
<source lang="powershell"> | <source lang="powershell"> | ||
− | |||
#Variable: ClusteName | #Variable: ClusteName | ||
$ClusterName = (Get-Cluster -ErrorAction SilentlyContinue).Name | $ClusterName = (Get-Cluster -ErrorAction SilentlyContinue).Name | ||
− | + | if ($null -eq $ClusterName) { | |
− | + | $ClusterName = "NotInCluster" | |
+ | } | ||
# Generate an array of the variables for the RegKey | # Generate an array of the variables for the RegKey | ||
Zeile 65: | Zeile 67: | ||
+ | ''Draw.io file: [[Datei:Drawio - Telegraf-Dynamic Tags.zip]]'' <br> | ||
+ | ''More information's: https://www.influxdata.com/blog/using-telegraf-on-windows/'' | ||
Zeile 72: | Zeile 76: | ||
− | [[Kategorie:TIG- | + | [[Kategorie:TIG-Stack]] |
Aktuelle Version vom 20. Januar 2022, 10:45 Uhr
This is not really a smart solution. This means if you can collect this tag informations different, write this in another measurement and join this later, then I would recommend it this way.
Global tags can be specified in the [global_tags] table in key="value" format. All metrics that are gathered will be tagged with the tags specified.
With dynamic tags we want the use global_tags in a more flexible way. In short: The tags should be updated dynamically.
Inhaltsverzeichnis
Explanation about the workflow
We want to add the ClusterName (Hyper-V Cluster) as a global tag, because we want to have the opportunity to filter/group by the ClusterName.
Because Telegraf have no built-in feature to run commands to define global_tags, we write the the tag "ClusterName" to the Windows Telegraf service environment variables (Registry-Key).
It would also be possible to write the tag to the Windows system environment variables. But it is not recommended.
So we have three important files:
- tags.global.conf: all global tags are defined in this Telegraf configuration file.
- inputs.exec.set-globaltags.conf: this runs only the PowerShell script without any output.
- tag.set-globaltags.ps1: PowerShell script that gets information about the ClusterName and put it into a RegKey as environment variables for the service "Telegraf".
Workflow
- The Telegraf Agent reads inputs.exec.set-globaltags.conf
- and runs every 4h the PowerShell script tag.set-globaltags.ps1 because of the defined "interval".
- The PowerShell script tag.set-globaltags.ps1 runs a command to get the ClusterName and create/update the RegKey "Environment" with the ClusterName.
- The PowerShell script restart the service "Telegraf".
- Because of the restart of the Telegraf service, Telegraf reads the global_tags defined in the file "tags.global.conf".
- In the tags.global.conf there is a tag named "Cluster" defined. This tag has the value "$ClusterName" (this is an Windows service environment variable). Telegraf searches the environment variable "ClusterName" in the environment variable of the service.
- Telegraf find the the environment variable "ClusterName" and add a tag named Cluster with the value of the ClusterName to all metrics that are send to the InfluxDB.
Telegraf global_tags configuration file "tags.global.conf"
# Global tags can be specified here in key="value" format. [global_tags] # dc = "us-east-1" # will tag all metrics with dc=us-east-1 # rack = "1a" ## Environment variables can be used as tags, and throughout the config file # user = "$USER" Cluster = "$ClusterName"
Telegraf exec plugin file "inputs.exec.set-globaltags.conf"
[[inputs.exec]] commands = ['powershell -NoProfile -File "C:\Program Files\Telegraf\scripts\tag.set-globaltags.ps1"'] interval = "4h" timeout = "1m"
PowerShell "tag.set-globaltags.ps1" script that gets the ClusterName
#Variable: ClusteName $ClusterName = (Get-Cluster -ErrorAction SilentlyContinue).Name if ($null -eq $ClusterName) { $ClusterName = "NotInCluster" } # Generate an array of the variables for the RegKey $RegKeyValue =@( "ClusterName=$ClusterName", ) # Create or update the RegKey "Environment" with the Variables New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\telegraf" -Name "Environment" -Value $RegKeyValue -PropertyType "MultiString" -Force | Out-Null # Restart the Telegrafg agent to apply the configuration changes Restart-Service -Name Telegraf
Draw.io file: Datei:Drawio - Telegraf-Dynamic Tags.zip
More information's: https://www.influxdata.com/blog/using-telegraf-on-windows/