I want to use a functions shell class in all of my EXE/XML scripts. Some functions are returning and maybe it is useful to handle events or „pushes“ to another shell file, beside PRTG.

Maybe a reset of an value in a database or something like that. Otherwise it is possible to declare the PRTG XML structure.

With a function you can access this structure via function name.


Article Comments

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:

  1. test.sh
  2. includes/
    1. 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


Dec, 2014 - Permalink