[SOLVED]Count the number of running process with Telegraf


I’m using telegraf, influxdb and grafana to make a monitoring system for a distributed application. The first thing I want to do is to count the number of java process running on a machine.

But when I make my request, the result are egal to the correct process number or the correct process number + 1.

I’m running centos 7 and Telegraf v1.5.0 (git: release-1.5 a1668bbf)

All Java process I want to count :

[root@localhost ~]# pgrep -f java

My telegraf.conf :


Configuration for telegraf agent

interval = "5s"
round_interval = true
metric_batch_size = 1000
metric_buffer_limit = 10000
collection_jitter = "0s"
flush_interval = "10s"
flush_jitter = "0s"
precision = ""
debug = true
quiet = false
logfile = "/var/log/telegraf/telegraf.log"
hostname = "my_server"
omit_hostname = false

My input.conf :

Read metrics about disk usagee

fielddrop = [ “inodes*” ]
mount_points=["/", “/workspace”]


files = [“myfile.log”]

Commands array

commands = ["/etc/telegraf/telegraf.d/customScript.sh"]
name_override = "java_process"
data_format = “value”

My /etc/telegraf/telegraf.d/customScript.sh :

ps -aux | grep java | wc -l

I think the problem comes to grep but I don’t know how resolve this. I tried to use pgrep -f -c java but the result are same.

When using grep with the output of ps, one of the matches will be the grep process itself.

For example, I am running two python processes on my machine. Here’s what happens when I pipe ps to grep:

$ ps -aux | grep python
vagrant   1480  0.0  0.6  31348  6408 pts/1    S+   14:39   0:00 python
vagrant   1512  0.0  0.6  31348  6416 pts/3    S+   14:40   0:00 python
vagrant   1515  0.0  0.0  14224   932 pts/2    S+   14:40   0:00 grep --color=auto python

There are three lines: the two python processes, and the grep process and its arguments. Because of this, you can’t count the number of lines to determine the number of processes.

Using pgrep with the -c argument should work. If I test with my machine running two python processes this is the result:

$ pgrep -c python

Yes, I see what did you explain. But I have tried to use pgrep -f -c java and the result are the same (some time 8, some time 9). Whether I use ps -aux | grep java | wc -l or pgrep -f -c java, the result are the same. However, in the first case, the result should always be 9 and in the second case always 8. Except it’s not the case and i don’t understand why.

The -f argument in pgrep -f -c matches the full command line. It’s possible that you are matching something other than a java process.

Can you try using pgrep -c instead?

1 Like

Sorry, I didn’t see you didn’t put the -f argument. Looks like it works! Thank you very much !

1 Like