#!/bin/bash

set -eu

function send_ncsa {
    # handle the case of not getting as far as running the seeder
    if [ -z $CODE ]; then
        CODE=3
        OUTPUT="UNKNOWN: seeder-wrapper-script exited before seeder returned an exit code"
    fi
    if [ -z $OUTPUT ]; then
        CODE=3
        OUTPUT="UNKNOWN: seeder-wrapper exited before setting OUTPUT - this should never happen"
    fi
    printf "<%= @fqdn %>\t<%= @seed_service_desc %>\t$CODE\t$OUTPUT\n" | /usr/sbin/send_nsca -H alert.cluster >/dev/null
    exit $?
}
trap send_ncsa EXIT

export GOVUK_CRAWLER_AMQP_PASS='<%= @amqp_pass %>'

OUTPUT=`<%= @seeder_script_path %> <%= @seeder_script_args %>`
CODE=$?
if [ "$OUTPUT" == "" ]; then
  OUTPUT="Seeder script exited with no output"
fi
# Force everything that isn't OK to be a WARNING
if [ $CODE -gt 0 ]; then
  CODE=1
fi

#!/bin/bash

set -eu

function send_ncsa {
    # unusual ${var+x} construct explained: http://stackoverflow.com/a/13864829
    if [ -z ${CODE+x} ]; then
        CODE=3
        OUTPUT="UNKNOWN: seeder-wrapper-script exited before seeder returned an exit code"
    fi
    # unusual ${var+x} construct explained: http://stackoverflow.com/a/13864829
    if [ -z ${OUTPUT+x} ]; then
        CODE=3
        OUTPUT="UNKNOWN: seeder-wrapper exited before setting OUTPUT - this should never happen"
    fi
    printf "<%= @fqdn %>\t<%= @seed_service_desc %>\t$CODE\t$OUTPUT\n" | /usr/sbin/send_nsca -H alert.cluster >/dev/null
    exit $?
}
trap send_ncsa EXIT

export GOVUK_CRAWLER_AMQP_PASS='<%= @amqp_pass %>'

OUTPUT=`<%= @seeder_script_path %> <%= @seeder_script_args %>`
CODE=$?
if [ "$OUTPUT" == "" ]; then
  OUTPUT="Seeder script exited with no output"
fi
# Force everything that isn't OK to be a WARNING
if [ $CODE -gt 0 ]; then
  CODE=1
fi

Test whether a variable is set when using set -u
Test whether a variable is set when using set -u

You can't just do `[ -z $VAR ]` when bash is running with the -u
(nounset) option, because `$VAR` is unset and will cause an error, even
though it's an error we want to handle.

Instead, we use the `[ -x ${VAR+x} ]` construct where `${VAR+x}` is a
parameter expansion which evaluates to null if var is unset and
substitutes the string "x" otherwise.
