manuales:nagios:capacitacion:tipos_de_notificaciones
                Diferencias
Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anteriorRevisión previaPróxima revisión | Revisión previa | ||
| manuales:nagios:capacitacion:tipos_de_notificaciones [2016/07/14 14:47] – [Definiciones previas] cayu | manuales:nagios:capacitacion:tipos_de_notificaciones [2019/03/26 20:25] (actual) – [Telegram] cayu | ||
|---|---|---|---|
| Línea 1: | Línea 1: | ||
| ====== Tipos de notificaciones ====== | ====== Tipos de notificaciones ====== | ||
| ==== Definiciones previas  | ==== Definiciones previas  | ||
| - | **Indicador**: | + |   * **Indicador**: | 
| - | + |    | |
| - | **Alerta**: //Señal o aviso con el objetivo de desviar la atención hacia el dato obtenido del indicador.// | + |    | 
| - | + |    | |
| - | **Notificación**: | + | |
| - | + | ||
| - | **Criterio**: | + | |
| Línea 125: | Línea 122: | ||
| {{: | {{: | ||
| ===== WhatsApp (Dependencia externa sin garantía) ===== | ===== WhatsApp (Dependencia externa sin garantía) ===== | ||
| + |   * https:// | ||
| ===== Telegram ===== | ===== Telegram ===== | ||
| + | <code python> | ||
| + | # | ||
| + | |||
| + | import argparse | ||
| + | from twx.botapi import TelegramBot | ||
| + | |||
| + | def parse_args(): | ||
| + |     parser = argparse.ArgumentParser(description=' | ||
| + |     parser.add_argument(' | ||
| + |     parser.add_argument(' | ||
| + |     parser.add_argument(' | ||
| + |     parser.add_argument(' | ||
| + |     parser.add_argument(' | ||
| + |     parser.add_argument(' | ||
| + |     parser.add_argument(' | ||
| + |     parser.add_argument(' | ||
| + |     parser.add_argument(' | ||
| + |     parser.add_argument(' | ||
| + | args = parser.parse_args() | ||
| + | return args | ||
| + | |||
| + | def send_notification(token, | ||
| + | bot = TelegramBot(token) | ||
| + |     bot.send_message(user_id, | ||
| + | |||
| + | def host_notification(args): | ||
| + |     state = '' | ||
| + |     if args.hoststate == ' | ||
| + |         state = u' | ||
| + |     elif args.hoststate == ' | ||
| + |         state = u' | ||
| + |     elif args.hoststate == ' | ||
| + |         state = u' | ||
| + | |||
| + | return "%s%s (%s): %s" % ( | ||
| + | state, | ||
| + |         args.hostname, | ||
| + |         args.hostaddress, | ||
| + |         args.output, | ||
| + | ) | ||
| + | |||
| + | def service_notification(args): | ||
| + |     state = '' | ||
| + |     if args.servicestate == ' | ||
| + |         state = u' | ||
| + |     elif args.servicestate == ' | ||
| + |         state = u' | ||
| + |     elif args.servicestate == ' | ||
| + |         state = u' | ||
| + |     elif args.servicestate == ' | ||
| + |         state = u' | ||
| + | |||
| + |     return " | ||
| + | state, | ||
| + |         args.hostname, | ||
| + |         args.servicedesc, | ||
| + |         args.output, | ||
| + | ) | ||
| + | |||
| + | def main(): | ||
| + | args = parse_args() | ||
| + | user_id = int(args.contact) | ||
| + |     if args.object_type == ' | ||
| + | message = host_notification(args) | ||
| + |     elif args.object_type == ' | ||
| + | message = service_notification(args) | ||
| + |     send_notification(args.token, | ||
| + | |||
| + | if __name__ == ' | ||
| + | main() | ||
| + | </ | ||
| + |   * https:// | ||
| + | <code php> | ||
| + | # | ||
| + | <?php | ||
| + | // Example: php / | ||
| + | // Using to send messages about new threads in moderated status. | ||
| + | // where 370022700 - chat_id. | ||
| + | // | ||
| + | /* | ||
| + | Bot: nagiosnotifybot | ||
| + | API: | ||
| + | ----- | ||
| + | How to get Chat_ID or Username of Telegram channel/ | ||
| + | 1. Add bot to chat/ | ||
| + | 2. Type command /my_id in chat. | ||
| + | 3. See output here: https:// | ||
| + | ---- | ||
| + | */ | ||
| + | // | ||
| + | ini_set(' | ||
| + | ini_set(' | ||
| + | error_reporting(E_ALL ^ E_NOTICE); | ||
| + | set_time_limit(30); | ||
| + | define(' | ||
| + | |||
| + | $chat_id = $argv[1]; | ||
| + | $botToken = $argv[2]; | ||
| + | $flag = $argv[3]; | ||
| + | $message = $argv[4]; | ||
| + | |||
| + | //############## | ||
| + | $log_file= __DIR__ . '/ | ||
| + | |||
| + | if(!file_exists ($log_file)) | ||
| + | { | ||
| + | $fh = fopen ($log_file, " | ||
| + |   fwrite ($fh, "" | ||
| + | fclose ($fh); | ||
| + | } | ||
| + | //##################################// | ||
| + | |||
| + | if (isset($flag)) | ||
| + | { | ||
| + | if ($flag != 1) | ||
| + | { | ||
| + | 	//$msg = 'Flag is NOT valid: ' .$flag.' | ||
| + | 	//echo $msg." | ||
| + | 	// | ||
| + | /**** Flag must be = 1. ****/ | ||
| + | die; | ||
| + | } | ||
| + | } else{ | ||
| + | 	//$msg = 'Not input flag: ' .$flag.' | ||
| + | 	//echo $msg." | ||
| + | 	// | ||
| + | die; | ||
| + | } | ||
| + | |||
| + | $msg = " | ||
| + | file_put_contents($log_file, | ||
| + | |||
| + | /* Debug | ||
| + | $msg = "----- DEBUG: | ||
| + | file_put_contents($log_file, | ||
| + | Debug */ | ||
| + | |||
| + | if (!$chat_id)  | ||
| + | { | ||
| + | 	$msg = 'Not input chat_id. Exit.';  | ||
| + | 	echo $msg." | ||
| + | 	file_put_contents($log_file, | ||
| + | die; | ||
| + | } | ||
| + | |||
| + | if (!$botToken)  | ||
| + | { | ||
| + | 	$msg = 'Not input botToken. Exit.';  | ||
| + | 	echo $msg." | ||
| + | 	file_put_contents($log_file, | ||
| + | die; | ||
| + | } | ||
| + | |||
| + | //Fix line break | ||
| + | // 0XzXZ0 - it's fake line break character defined in Nagios command definition. | ||
| + | function replace_linebreak($message) | ||
| + | { | ||
| + | global $message; | ||
| + | 	$linebreak = " | ||
| + | 	$message = preg_replace('/ | ||
| + | 	$message = urlencode($message); | ||
| + | return $message; | ||
| + | } | ||
| + | |||
| + | function Send_msg_to_Telegram ($botToken, $telegram_chat_id, | ||
| + | global $url, $log_file; | ||
| + | 	$bot_url  | ||
| + | //Fix new break | ||
| + | 	$message = replace_linebreak($message); | ||
| + | 	$url = $bot_url." | ||
| + | 	if ($content = json_decode(file_get_contents($url), | ||
| + | 		return $content[' | ||
| + | }else{ | ||
| + | 	$msg = "ERROR with Telegram chat: " | ||
| + | 	echo $msg." | ||
| + | 	file_put_contents($log_file, | ||
| + | } | ||
| + | } | ||
| + | |||
| + | $TelegramGROUPS=explode(',', | ||
| + | |||
| + | foreach ($TelegramGROUPS as $telegram_chat_id) | ||
| + | { | ||
| + | 	echo " | ||
| + | 	$msg = ' | ||
| + | 	echo $msg." | ||
| + | 	file_put_contents($log_file, | ||
| + | 	$telegram_data = Send_msg_to_Telegram($botToken, | ||
| + | if (isset($telegram_data) AND $telegram_data == 1) { | ||
| + | 		$msg = " | ||
| + | 		echo $msg." | ||
| + | 		file_put_contents($log_file, | ||
| + | } else { | ||
| + | 		$msg = " | ||
| + | 		echo $msg." | ||
| + | 		file_put_contents($log_file, | ||
| + | continue; | ||
| + | } | ||
| + | $msg = " | ||
| + | echo $msg." | ||
| + | file_put_contents($log_file, | ||
| + | } | ||
| + | |||
| + | //Clean OLD logs every 30 days | ||
| + | 	$files = glob(realpath(dirname(__FILE__)).'/ | ||
| + | if (empty($files)) { | ||
| + | exit; | ||
| + | } | ||
| + | foreach($files as $file){ | ||
| + | if(time() - filectime($file) > 2592000){ | ||
| + | 			unlink($file); | ||
| + | } | ||
| + | } | ||
| + | //Clean OLD logs every 30 days | ||
| + | ?> | ||
| + | </ | ||
| + | https:// | ||
| + | <code ini> | ||
| + | # Host notification via Telegram bot | ||
| + | define command{ | ||
| + |         command_name  | ||
| + |         command_line  | ||
| + | } | ||
| + | |||
| + | # Service notification via Telegram bot | ||
| + | define command{ | ||
| + |         command_name  | ||
| + |         command_line  | ||
| + | } | ||
| + | </ | ||
| + | https:// | ||
| ===== Visual general ===== | ===== Visual general ===== | ||
manuales/nagios/capacitacion/tipos_de_notificaciones.1468507643.txt.gz · Última modificación: 2016/07/14 14:47 por cayu
                
                