node exporter text file collector

376 views
Skip to first unread message

nina guo

unread,
Aug 8, 2022, 4:14:10 AM8/8/22
to Prometheus Users
Hi,

I used the following way to output the metrics and values to a file, and let node exporter to scrape it.

I have a question here, how to let the next metrics value overide the previous'?

I checked .prom file, there are some metrics with same labels and same values of labels but different value of the metrics. It is not correct. The next value of the metrics should override the previous old value. But how to implement this?

print (" # HELP ldap_query_success LDAP query command", file=open("/var/log/node_exporter/filecollector/ldap_query.prom", "a+"))
 print (" # TYPE ldap_query_success gauge", file=open("/var/log/node_exporter/filecollector/ldap_query.prom", "a+"))
print ('ldap_query_success'+'{'+'ldap_uri'+'='+service+','+'ldap_search_base'+'='+ldap_search_base+','+'} '+str(query_check), file=open("/var/log/node_exporter/filecollector/ldap_query.prom", "a+"))




Stuart Clark

unread,
Aug 8, 2022, 4:43:00 AM8/8/22
to nina guo, Prometheus Users
You shouldn't be appending to an existing file, but instead replacing
the file contents each time you do an update.

Depending on the tooling used it is often better to create a new file
with a random non *.prom filename and then rename it into the correct
name - renames are generally atomic whereas file updates often aren't,
meaning if you modify the file directly you could end up reading part
finished data.

--
Stuart Clark

nina guo

unread,
Aug 8, 2022, 4:58:40 AM8/8/22
to Prometheus Users
But the following 3 lines should be appended to a file first, then next time override the old content. But how to make the old content be overried by previous ones?

print (" # HELP ldap_query_success LDAP query command", file=open("/var/log/node_exporter/filecollector/ldap_query.prom", "a+"))
 print (" # TYPE ldap_query_success gauge", file=open("/var/log/node_exporter/filecollector/ldap_query.prom", "a+"))
print ('ldap_query_success'+'{'+'ldap_uri'+'='+service+','+'ldap_search_base'+'='+ldap_search_base+','+'} '+str(query_check), file=open("/var/log/node_exporter/filecollector/ldap_query.prom", "a+"))

Stuart Clark

unread,
Aug 8, 2022, 5:31:29 AM8/8/22
to nina guo, Prometheus Users
On 08/08/2022 09:58, nina guo wrote:
> But the following 3 lines should be appended to a file first, then
> next time override the old content. But how to make the old content be
> overried by previous ones?
>
> print (" # HELP ldap_query_success LDAP query command",
> file=open("/var/log/node_exporter/filecollector/ldap_query.prom", "a+"))
>  print (" # TYPE ldap_query_success gauge",
> file=open("/var/log/node_exporter/filecollector/ldap_query.prom", "a+"))
> print
> ('ldap_query_success'+'{'+'ldap_uri'+'='+service+','+'ldap_search_base'+'='+ldap_search_base+','+'}
> '+str(query_check),
> file=open("/var/log/node_exporter/filecollector/ldap_query.prom", "a+"))
>
File mode "a" will open for appending (so preserve anything already in
the file). Instead to fully replace the file you'd need to use file mode
"w".

--
Stuart Clark

nina guo

unread,
Aug 8, 2022, 7:00:42 AM8/8/22
to Prometheus Users
Thank you I have resolved the issue.

I also tried to use the interfaces to create and record the metrics. I have tested with following codes and found that the value of the metrcis will be overrided by the last value of the metrics.

For example:
real situation is:
service1 -> ldap_query_success{...}  0
service2 -> ldap_query_success{...}  0
service3 -> ldap_query_success{...}  1

but with the following codes:
service1 -> ldap_query_success{...}  1
service2 -> ldap_query_success{...}  1
service3 -> ldap_query_success{...}  1



from prometheus_client import Gauge, write_to_textfile, CollectorRegistry

for service in services:
  g1 = Gauge('ldap_query_success', 'LDAP query command', ['ldap_uri', 'ldap_search_base'], registry=registry)
  g1.labels(service,ldap_search_base,ldap_default_bind_dn).set(query_check)
  write_to_textfile("/var/log node_exporter/filecollector/ldap_query.prom", registry)

b...@ritcey.com

unread,
Aug 8, 2022, 2:58:16 PM8/8/22
to Prometheus Users
Move 

  g1 = Gauge('ldap_query_success', 'LDAP query command', ['ldap_uri', 'ldap_search_base'], registry=registry)

before the loop - you don't want to initialize it each time.

nina guo

unread,
Aug 8, 2022, 8:05:40 PM8/8/22
to Prometheus Users
Thank you. I moved this line before the loop, but still received the same issue.

Brian Candler

unread,
Aug 9, 2022, 4:07:20 AM8/9/22
to Prometheus Users
I am stating the obvious here, but unless you update your variable "query_check" inside the loop, then all your metrics will be set to the same value.

Also, your write_to_textfile should move outside the end of the loop.  You only need to write the file once.

Reply all
Reply to author
Forward
0 new messages