System Engineering

When Nagios send alert mails with attachement…

When Nagios send alert mails with attachement…
Perhaps this problem is that if a device name or warning message include non-Latin characters.

 

1. Add new command to nagios configuration

Original commands

# 'notify-host-by-email' command definition
define command{
    command_name    notify-host-by-email
    command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
}

# 'notify-service-by-email' command definition
define command{
    command_name    notify-service-by-email
    command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /usr/bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
}

New commands

define command {
    command_name    notify-host-by-email-encodingsafe
    command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/bin/env php /usr/local/bin/convert-encoding.php | /usr/bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
}

define command {
    command_name    notify-service-by-email-encodingsafe
    command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /usr/bin/env php /usr/local/bin/convert-encoding.php | /usr/bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
}

 

2. Message filter script (PHP)

<?php
/*
 +-------------------------------------------------------------------------+
 | Copyright (C) 2016 Aiden Kim                                           |
 +-------------------------------------------------------------------------+
*/

/* do NOT run this script through a web browser */
if (!isset($_SERVER['argv'][0]) || isset($_SERVER['REQUEST_METHOD'])  || isset($_SERVER['REMOTE_ADDR'])) {
	die('<br><strong>This script is only meant to run at the command line.</strong>');
}

/* We are not talking to the browser */
$no_http_headers = true;

/* Supress all internal error */
error_reporting(0);

/* process calling arguments */
$parms = $_SERVER['argv'];
array_shift($parms);

/* stdin */
$handle = fopen ("php://stdin","r");
$stdin = stream_get_contents($handle);

if (sizeof($parms) || !empty($stdin)) {

	$input_string = '';
	$output_string = '';
	$input_file = '';
	$output_file = '';
	$input_encoding = '';
	$output_encoding = 'UTF-8';
	$detection_order = 'SJIS-win,SJIS,eucJP-win,EUC-JP,ISO-2022-JP-MS,ISO-2022-JP,CP51932,CP932,EUC-KR,ISO-2022-KR,GB18030,BIG-5,EUC-CN,EUC-TW,CP950,UTF-8,UTF-16,UCS-2,UCS-4,8bit,ASCII,7bit';
	
	for ($i=0; $i<count($parms); $i++) {
		@list($arg, $value) = @explode('=', $parms[$i]);
		switch ($arg) {
			case '--input-file':
				$input_file = trim($value);
				break;
			case '--output-file':
				$output_file = trim($value);
				break;
			case '--input-encoding':
				$input_encoding = trim($value);
				break;
			case '--output-encoding':
				$output_encoding = trim($value);
				break;
			case '--detection-order':	
				$detection_order = trim($value);
				break;
			default:
				if ( $i < count($parms)-1 ) {
					echo 'ERROR: Invalid Argument: ('.$arg.')'.PHP_EOL.PHP_EOL;
					exit(1);
				}else{
					$input_string = trim($parms[$i]);
				}
		}
	}

	if (!empty($stdin)) {
		$input_string = $stdin;
	}

	if (empty($input_string)) {
		if (empty($input_file)) {
			echo 'ERROR: No Input String (Requiring at least one of [STRING] and [--input-file])'.PHP_EOL.PHP_EOL;
			display_help();
			exit(1);
		}else{
			$input_string = @file_get_contents($input_file);
			if ($input_string===false) {
				echo 'ERROR: Could Not Read Input File ('.$input_file.')'.PHP_EOL.PHP_EOL;
				exit(1);
			}
		}
	}
	
	if (empty($input_encoding)) {
		$input_encoding = @mb_detect_encoding($input_string, $detection_order); 
	}
	
	$output_string = @mb_convert_encoding($input_string, $output_encoding, $input_encoding);
	
	if (!empty($output_file)) {
		$ret = @file_put_contents($output_file, $output_string);
		if ($ret===false) {
			echo 'ERROR: Could Not Write Output File ('.$output_file.')'.PHP_EOL.PHP_EOL;
			exit(1);
		}
	}else{
		echo $output_string;
	}
	exit(0);

} else {
	echo 'ERROR: no parameters given'.PHP_EOL.PHP_EOL;
	display_help();
	exit(1);
}

function display_help() {
	echo 'Usage: convertcharset.php [OPTION]... [STRING]'.PHP_EOL;
	echo 'STRING is the source string will be encoded.'.PHP_EOL;
	echo 'OPTION:'.PHP_EOL;
	echo '  --input-file=FILE                         Path to the file where to read source data.'.PHP_EOL;
	echo '  --input-encoding=ENCODING                 The character code name of source data.'.PHP_EOL;
	echo '  --output-file=FILE                        Path to the file where to write converted data. '.PHP_EOL;
	echo '  --output-encoding=ENCODING                The character code name of converted data. Default UTF-8.'.PHP_EOL;
	echo '  --detection-order=ENCODING[,ENCODING]...  Encoding order list of character encoding. List may be comma separated list string. '.PHP_EOL;
}
?>

Actually I wanted to make a filter by Python. But it was less accurate than PHP to determine the encoding of the input string.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.