To write an EXE/XML sensor shell script with a functions class in the same folder or another is easy, but also a bit tricky. PRTG started the scripts not in default „root“ in shell. PRTG launches the shell scripts in "/var/prtg/scripts" & "/var/prtg/scriptsxml".
So we have to tell the script, that it is in a shell.
########Functions##########
my_dir="$(dirname "$0")"
source "$my_dir/includes/functions.sh"
###########End#############
$(dirname „$0“) is a global function for the Folder where the shell file is executed. You can call it "absolute path", similar to (`pwd` /$0).
Now we can link a new file via „source“ to the first one and this will work correctly.
We are able to activate a „functions“ class in an external shell file and include it into the main part of the sensor file.
Example script:
test.sh
#!/bin/bash
my_dir="$(dirname "$0")"
source "$my_dir/includes/functions.sh"
VAL1="TEST1"
VAL2="TEST2"
OUTPUT=$(print_xml ${VAL1} ${VAL2})
echo "<prtg>"
echo $OUTPUT
echo "</prtg>"
functions.sh (in "includes" folder)
function print_xml() {
xmlresult=`cat << EOF
<result>
<channel>$1</channel>
<value>$2</value>
</result>
EOF
`
echo $xmlresult
Structur:
- test.sh
- includes/
- functions.sh
This is an example to write an template for creating the xml code inside PRTG XML sensor list.
This will make your script a bit „slender“ ;).
Best
Sascha
Add comment