Dear Community User! We have started the migration process.
This community is now in READ ONLY mode.
Read more: Important information on the platform change.

cancel
Showing results for 
Search instead for 
Did you mean: 
SOLVED

Telegraf - Provide timestamp as input to InfluxDB

Telegraf - Provide timestamp as input to InfluxDB

star_board
Established Member

I am using Telegraf to send a measurement ("Test1") to InfluxDB. Currently, I have two fields read from DataLayer nodes: "Value" & "Timestamp".

Configuration:

[[inputs.ctrlx_datalayer.subscription]]
measurement="Test1"

[inputs.ctrlx_datalayer.subscription.tags]
SensorNum="Sensor1"

[[inputs.ctrlx_datalayer.subscription.nodes]]
name ="Value"
address="plc/app/Application/sym/GVL/Sensor1/Value"

[[inputs.ctrlx_datalayer.subscription.nodes]]
name ="Timestamp"
address="plc/app/Application/sym/GVL/Sensor1/PLCTimeStamp"

Example of the DataLayer nodes:

"plc/app/Application/sym/GVL/Sensor1/Value" : 20
"plc/app/Application/sym/GVL/Sensor1/PLCTimeStamp" : 1720520212398

The PLC variable "PLCTimeStamp" is a timestamp in milliseconds. Instead of providing it as a field, I wanted to provide it as an InfluxDB timestamp. Is this possible using Telegraf? 

Thanks in advance 🙂 

7 REPLIES 7

nickH
Community Moderator
Community Moderator

Hi @star_board 

in Telegraf you can either use aggregators an processors to transform your data. See the Telegraf manual for more information: Transform data with aggregator and processor plugins

I have not tested for your specific usecase but I found three plugins which could maybe help you: 

Best regards, 

Nick

star_board
Established Member

Is there anyway where I can save/see the ouput metrics of telegraf (besides influxdb) in order to test this different plugins?

Would something using an ouput plugin work on ctrlX, like File Ouput Plugin ?
[[outputs.file]]
   files = ["stdout", "/tmp/metrics.out"]
   data_format = "influx"

Sgilk
Frequent Contributor

Hi @star_board ,

Something like this should work for your use case, utilizing the Starlark processor @nickH mentioned.

 

[[outputs.influxdb_v2]]
  urls = ["https://127.0.0.1/influxdb"]
  token = "eaN73XZ4kWNzTwqkzWrhJpcH2ZJsZjkzuRwOPl-fe0cNMrMrg-2yRoJQvMYpnMVzBxLa8GN6EcXx42JEnEU2w="
  organization = "boschrexroth"
  bucket = "boschrexroth"
  insecure_skip_verify = true

## Configure the input plugin with the username and password to ctrlX Core  
[[inputs.ctrlx_datalayer]]
   server = "localhost"

   ## Authentication credentials
   username = "boschrexroth"
   password = "boschrexroth"

   ## Use TLS but skip chain & host verification
   insecure_skip_verify = true

   [[inputs.ctrlx_datalayer.subscription]]
      measurement = "metrics"
      nodes=[
      		{name="cpu_usage_percent", address="framework/metrics/system/cpu-utilisation-percent"},
        	{name ="timestamp",address="system/info/time"}
      ]

      ## The switch "output_json_string" enables output of the measurement as json. 
      ## That way it can be used in in a subsequent processor plugin, e.g. "Starlark Processor Plugin".
	output_json_string = false
      
[[processors.starlark]]
	source = '''
load("logging.star","log")
def apply(metric):
	log.error("----------------------")
	log.error(metric.tags['node'])
	if "timestamp" in metric.fields:
		log.error("Timestamp old: " + str(metric.time))
		metric.time = metric.fields['timestamp']
		log.error("Timestamp new: " + str(metric.time))
	return metric
'''

 

In the starlark processor, you would look for your PLCTimeStamp field and set the metric.time to that value. Here I used the time coming from the ctrlX system.

Sgilk
Frequent Contributor

@star_board ,

Yes, the File Output Plugin is supported. You can view a full list of supported plugins on the Github.

star_board
Established Member

If I use the File Output Plugin where can I save the output file? And how can I access it?

Sgilk
Frequent Contributor

You can write to the active configuration. USB access is currently not supported, but I requested this for a future release.

Example:

 

# Send telegraf metrics to file(s)
[[outputs.file]]
  ## Files to write to, "stdout" is a specially handled file.
  files = ["/var/snap/rexroth-solutions/common/solutions/activeConfiguration/telegraf/test.out"]

  data_format = "influx"

 

You can then access the file in the active configuration Web UI or via WebDAV.

Sgilk_1-1720712628293.png

 

star_board
Established Member

Thanks for the help! I did manage to do it using your example with starlark processor! 😁

I had to slightly modify my approach and store the variables "Value" and "PLCTimeStamp" in a DataLayer array, rather than in separate nodes, to retrieve them in the same metric instance:

 

 

[[processors.starlark]]  
  source = '''  
def apply(metric):     
    metric.time = int(metric.fields["PLCTimestamp"])   
    return metric 
        '''

 

 

Additionally, I experimented with using the Converter processor. I will leave the code here since it might be usefull to another person:

 

 

[[processors.converter]]
  [processors.converter.fields]
    timestamp = ["PLCTimestamp"]
    timestamp_format = "unix_ms"

 

 

 

 

 

Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist