Support Questions

Find answers, ask questions, and share your expertise

How to get a Nifi counter value using ExecuteScript Processor and Groovy ?

avatar
Contributor

I have a flow where I would like to read the value of a counter, is there a way to do that using ExecuteScript Processor and Groovy or any other scripting langage available ?

Thank you for your answers

1 REPLY 1

avatar

@Mohammed El Moumni

You can use the rest api to get the value of counter. You could create a script with the needed curl commands the get the value of the counter and then use the ExecuteScript processor to run the script.

You can access the rest api document via your NiFi instance, the url will be similar to this http://nifi-host:port/nifi-docs/rest-api.

For example, curl command to get counters in my flow, with output, is:

curl 'http://nifi-host:port/nifi-api/counters'

{"counters":{"aggregateSnapshot":{"generated":"15:12:55 BST","counters":[{"id":"46f421a7-d348-3e65-8a21-290cf1fc8fa1","context":"ExecuteScript (abde5770-015b-1000-0000-0000722fe055)","name":"my-counter","valueCount":0,"value":"0"},{"id":"bd62b47c-5124-35b1-926b-2149eb519f79","context":"ExecuteScript (abfdad0a-015b-1000-ffff-fffff0fcbeb9)","name":"my-counter","valueCount":56400,"value":"56,400"},{"id":"c6d58dcc-8171-3d63-adb2-8cc8e239cc9a","context":"Groovy script of counter (abfdad0a-015b-1000-ffff-fffff0fcbeb9)","name":"my-counter","valueCount":6930,"value":"6,930"},{"id":"776c5c43-40ee-372e-957c-f40b465d3326","context":"All ExecuteScript's","name":"my-counter"