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 [2018/07/24 15:51] – [Telegram] cayu | manuales:nagios:capacitacion:tipos_de_notificaciones [2019/03/26 20:25] (actual) – [Telegram] cayu | ||
|---|---|---|---|
| Línea 122: | Línea 122: | ||
| {{: | {{: | ||
| ===== WhatsApp (Dependencia externa sin garantía) ===== | ===== WhatsApp (Dependencia externa sin garantía) ===== | ||
| + |   * https:// | ||
| ===== Telegram ===== | ===== Telegram ===== | ||
| <code python> | <code python> | ||
| Línea 194: | Línea 195: | ||
| main() | 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.1532447463.txt.gz · Última modificación: 2018/07/24 15:51 por cayu
                
                