Hi Forum,

I have the below written script that I would like to return values on PRTG, I can successfully display some values, however some don't display because they are not integers I guess.

#!/bin/bash
app_name="ListenerService" #App Name

app_id=`ps aux | grep $app_name | grep -v grep | awk {'print $2'}`  #Process ID
app_cpu=`ps aux | grep $app_name | grep -v grep | awk {'print $3'}` #CPU% Used
app_mem=`ps aux | grep $app_name | grep -v grep | awk {'print $4'}` #MEM% Used
app_res=`ps aux | grep $app_name | grep -v grep | awk {'print $5'}` #RES is the amount of RAM currently used by the process
app_virt=`ps aux | grep $app_name | grep -v grep | awk {'print $6'}` #VIRT is the full size of all memory the process is using
app_shar=`ps aux | grep $app_name | grep -v grep | awk {'print $7'}` #SHR, Shared Mem size (kb) The amount of shared memory used by a task

app_stat=`ps -q $app_id -o state --no-headers` #Process status 
app_threads=`sudo cat /proc/$app_id/status | grep Threads | grep -v grep | awk {'print $2'}` # Process thread count

#Check if the process is still alive and running
if [ "$app_stat" != "D" ] && [ "$app_stat" != "Z" ] && [ "$app_stat" != "X" ]
then
  app_stat="UP"
else
  app_stat="DOWN"
fi

#PRTG Sensor Output
result="<?xml version='1.0' encoding='Windows-1252' ?>\n"
result+="<prtg>\n"
result+="   \t<result>\n"
result+="   	\t\t<channel>CPU Usage %</channel>\n"
result+="   	\t\t<value>${app_cpu}</value>\n"
result+="   	\t\t<unit>custom</unit>\n"
result+="   	\t\t<customunit>%</customunit>\n"
result+="	\t</result>\n"
result+="   \t<result>\n"
result+="   	\t\t<channel>Memory %</channel>\n"
result+="   	\t\t<value>${app_mem}</value>\n"
result+="   	\t\t<unit>custom</unit>\n"
result+="   	\t\t<customunit>%</customunit>\n"
result+="   \t</result>\n"
result+="   \t<result>\n"
result+="   	\t\t<channel>Threads</channel>\n"
result+="   	\t\t<value>${app_threads}</value>\n"
result+="   	\t\t<unit>custom</unit>\n"
result+="   	\t\t<customunit>#</customunit>\n"
result+="   \t</result>\n"
result+="   \t<result>\n"
result+="       \t\t<channel>RES(RAM currently in use)</channel>\n"
result+="       \t\t<value>${app_res}</value>\n"
result+="       \t\t<unit>custom</unit>\n"
result+="       \t\t<customunit>KiB</customunit>\n"
result+="   \t</result>\n"
result+="   \t<result>\n"
result+="       \t\t<channel>VIRT(Virtual Memory Size)</channel>\n"
result+="       \t\t<value>${app_virt}</value>\n"
result+="       \t\t<unit>custom</unit>\n"
result+="       \t\t<customunit>KiB</customunit>\n"
result+="   \t</result>\n"
result+="   \t<result>\n"
result+="   	\t\t<channel>Status</channel>\n"
result+="   	\t\t<value>${app_stat}</value>\n"
result+="   	\t\t<unit>custom</unit>\n"
result+="   	\t\t<customunit></customunit>\n"
result+="   \t</result>\n"
result+="</prtg>\n"

echo -e $result

The values not displaying are app_cpu and app_mem. Am I doing something wrong on the bash script?

Looking forward to any help on the same.


Article Comments

Hi Brian,

Make sure the <Float> setting matches the kind of value provided. With <Float> you can define if the value is a float. The default is 0 (no). If set to 1 (yes), use a dot as decimal separator in values. If you don't define this and your value is a float PRTG shows 0 as values.

This means the output should look like this:

<prtg>
           <result>
           <channel>channel</channel>
           <value>10.3</value>
           <float>1</float>
           </result>
</prtg>

Kind regards

Felix Wiesneth - Team Tech Support


May, 2021 - Permalink

Hi Felix,

Work perfectly now, thank you for the great help.


May, 2021 - Permalink