Nagios NRPE wrapper to encode meta-characters

April 23, 2017 Leave a comment

The Nagios Remote Plugin Executor allows remote execution of Nagios check commands, which is a powerful tool for monitoring the health of the machines and services on your network.

If your remote commands take command line parameters, you might run into trouble regarding special characters, typically required in regular expressions and other values you might want to send across. To actually see the error on the target machine, you need to Create a Log File for NRPE.

If your command fails due to this kind of error, then read on:

Apr 6 18:06:58 somehost nrpe[somepid]: Error: Request contained illegal metachars!
Apr 6 18:06:58 somehost nrpe[somepid]: Client request was invalid, bailing out...

NRPE inspects the arguments for characters that have special meaning for typical Unix shells to prevent shell command injection. If it encounters any charcaters deemed unsafe the command execution is rejected and you will see the error message above.

Unfortunately NRPE does not provide a way to safely encode and decode the parameter values.

To work around this, I created a wrapper script for the check_nrpe command, let’s call it check_nrpe_urlencoded.sh. I chose url-encoding for its simplicity and familiarity. The goal is to be able to create Nagios command definitions like this:

$USER1$/custom_scripts/check_nrpe_urlencoded.sh -H $HOSTADDRESS$ -c my_remote_command -a '$ARG1$' '$ARG2$'

And the remote command would bedefined in /etc/nagios/nrpe.cfg or under in a custom file under /etc/nrpe.d/, depending on your Linux distribtion:

command[my_remote_command]=/path_to_my_custom_nrpe_plugins/my_remote_command.sh '$ARG1$' '$ARG2$'

Here is a possible implementation of check_nrpe_urlencoded.sh:

#! /bin/bash

command='/usr/lib/nagios/plugins/check_nrpe'
args='no'

urlencode() {
    old_lc_collate=$LC_COLLATE
    LC_COLLATE=C

    local length="${#1}"
    for (( i = 0; i < length; i++ )); do
        local c="${1:i:1}"
        case ${c} in
            [a-zA-Z0-9.~_-]) printf "$c" ;;
            *) printf '%%%02X' "'$c" ;;
        esac
    done

    LC_COLLATE=${old_lc_collate}
}

for x in "$@"; do
  if [ ${x} = '-a' ]; then
    args='yes';
  else
    if [ ${args} = 'yes' ]; then
       x=$(urlencode ${x})
    fi
  fi
  command="${command} ${x}"
done

# execute the check_nrpe with the encoded args:
${command}

In the remote shell script like my_remote_command.sh, you would then decode the arguments like this:

function urldecode() {
  local url_encoded="${1//+/ }"
  printf '%b' "${url_encoded//%/\\x}"
}
arg1="$(urldecode $1)"
arg2="$(urldecode $2)"

And then use the decoded arguments as before.

Categories: bash, coding Tags: ,

Summing up numeric values using bash

March 22, 2017 Leave a comment

For example to add up the disk usage at several disjoint locations:

me@box$ numfmt --to=iec $[$(du -s /home/*/docs | cut -f 1 | paste -sd+)]
101K

The $[..] is for arithmetic evaluation in bash.
Alternatively pipe to the bc command.

Categories: bash, coding

Simplest Dependency Injection

January 17, 2017 Leave a comment

diy

Note to self: If you create a Java application and think you need DI but not AOP, then do not automatically reach for containers like Spring, CDI, Guice. Just do all construction and wiring in the standard main method.

Example: https://github.com/odoepner/bagh-chal/blob/master/src/main/java/net/doepner/baghchal/Main.java

Redshift to reduce eye strain from nightly computer use

January 11, 2017 3 comments

Note: This tutorial is mainly for Linux users. For other operating systems you could consult the article “Best Automatic Display Adjustment Software for Mac, Windows, iOS and Android“.

Redshift is a little Free and Open Source tool that can reduce the blue component in the light emitted by your computer screen. By default, it does so between sunset and sunrise based on your latitude / longitude coordinates, but you can also use a permanent fixed light temperature.

The underlying idea is that too much blue light can strain your eyes, especially at night.

Permanent candle light

On Debian and derivatives like Ubuntu, the redshift command line version can be installed like this:

sudo apt-get install redshift

I personally like a “permanent candlelight” setting at all times. This simple example sets a relatively low fixed light temperature of 2200K and a slightly dimmed brightness (see man redshift for more details):

redshift -r -O 2200 -b 0.8

If you like this approach, you can run this command at X session start, similar to what is shown under “Autostart after Login” below.

Emulating Day and Night

If you want redshift to distinguish between day and night, it is convenient to use the GUI version with a config file that specifies your latitude and longitude as shown below.

On Debian and derivatives like Ubuntu, redshift with the GTK UI can be installed like this:

sudo apt-get install gtk-redshift

You can determine your coordinates by googling for the name of your town or city, combined with the words “longitude” and “latitude”, for example for the German town of “Rodgau” this would be: https://google.com/search?q=rodgau+longitude+latitude

Note that latitudes south of equator and longitudes west of Greenwich must be specified as negative values. The following shows an example ~/.config/redshift.conf for Halifax (44.65° North, 63.58° West):

[redshift]
location-provider=manual

[manual]
lat=44.65
lon=-63.58

You can visit the Redshift website for more details about installation and configuration, etc.

Run the tool for the first time either via Start Menu – Accessories – Redshift on Debian systems, or as redshift-gtk on the Linux command line. You should then be able to see a reddish light-bulb icon in the system tray (aka “notification area”) of your desktop system. Clicking on it gives you options to temporarily disable the tool or view info about your configured geo-location and whether redshift thinks it is currently night-time. If so, you should notice a reddish screen color temperature.

Autostart after Login

To have redshift-gtk start up on every X session, add an entry to the Autostart mechanism of your desktop environment or window manager. For XFCE on Debian, open Start Menu – Settings – Session and Startup – Application Autostart tab and add an entry like this:

add-redshift-to-xfce-autostart

Categories: debian, linux, xfce Tags: ,

Schedule wireless availability using ddwrt

January 7, 2017 Leave a comment

Sometimes I have problems ending my internet use in time to get enough quality sleep.

So I decided to set up a schedule that automatically disables our wireless home network during certain night hours for “nights before work/school day” and slightly longer hours for “night before weekend day”. Luckily this is quite easily done with the ddwrt firmware that I run on my router.

I couldn’t use the “Radio Time Restrictions” feature under Wireless – Advanced Settings, because it only supports one uniform schedule for all days of the week. Instead I used ddwrt’s cron support and the wl command.

I configured the following entries under Administration – Management – Cron. This turns on the wireless every morning at 6am, shuts it down at 10:30pm on Sunday to Thursday and shuts it down at 11:45pm on Friday and Saturday:

00 06 * * *   root wl radio on
30 22 * * 0-4 root wl radio off
45 23 * * 5-6 root wl radio off
Categories: coding Tags: , , ,

Convert mpc to mp3 on Linux

January 1, 2017 Leave a comment

You need the lame and mpcdec commands. On Debian, mpcdec is in the musepack-tools package:

sudo apt-get install lame musepack-tools

Then to convert all mpc files in the current directory to matchingly named mp3 files:

for x in *.mpc; do mpcdec "${x}" - | lame -r - "${x%.mpc}.mp3"; done
Categories: bash, coding, debian, linux, music

bash : Loop over lines in file with user prompt

December 8, 2016 Leave a comment

I used the following to loop over the lines in a file, while prompting the user for a key press on each iteration:

while read -u 3 line ; do 
  #clear the screen
  printf "\033c" 
  echo "$line"; echo

  # do something with the $line here

  read -n 1 -s -p "[Press any key to continue]"
done 3< "some-file.txt"

The reading of the lines is done via file descriptor 3 to avoid interference with the reading of the user’s key presses.

Categories: bash, coding Tags: ,