foreach ($graphs as $key => $graph) { $cid = $graph['type'] . $key; // Unencode data uri tag to file content list($gmime, $base64) = explode(';', $graph['data'], 2); $gmime = substr($gmime, 5); $base64 = substr($base64, 7); //print_vars(substr($graph['data'], 0, 20)); //print_vars($gmime); //print_vars(substr($base64, 0, 20)); $mime->addHTMLImage(base64_decode($base64), $gmime, $cid . '.png', FALSE, $cid); $message_tags_html['ENTITY_GRAPHS'] .= '<h4>' . $graph['type'] . '</h4>'; $message_tags_html['ENTITY_GRAPHS'] .= '<a href="' . $graph['url'] . '"><img src="cid:' . $cid . '"></a><br />'; } } //print_vars($message_tags_html); $message['html'] = simple_template('email_html', $message_tags_html, array('is_file' => TRUE)); unset($message_tags_html); foreach ($message as $part => $part_body) { switch ($part) { case 'text': case 'txt': case 'plain': $part_body .= "\n\nE-mail sent to: {$rcpts}\n"; $part_body .= "E-mail sent at: {$time_sent}\n\n"; $part_body .= "-- \n" . OBSERVIUM_PRODUCT_LONG . ' ' . OBSERVIUM_VERSION . "\n" . OBSERVIUM_URL . "\n"; $mime->setTXTBody($part_body); break; case 'html': $part_footer = "\n<br /><p style=\"font-size: 11px;\">E-mail sent to: {$rcpts}<br />\n"; $part_footer .= "E-mail sent at: {$time_sent}</p>\n"; $part_footer .= '<div style="font-size: 11px; color: #999;">-- <br /><a href="' . OBSERVIUM_URL . '">' . OBSERVIUM_PRODUCT_LONG . ' ' . OBSERVIUM_VERSION . "</a></div>\n";
/** * @dataProvider providerSimpleTemplate * @group simple_template */ public function testSimpleTemplate($template, $keys, $result) { $this->assertSame($result, simple_template($template, $keys)); }
function process_alerts($device) { global $config, $alert_rules, $alert_assoc; echo "处理警报 " . $device['hostname'] . PHP_EOL; $alert_table = cache_device_alert_table($device['device_id']); $sql = "SELECT * FROM `alert_table`"; $sql .= " LEFT JOIN `alert_table-state` ON `alert_table`.`alert_table_id` = `alert_table-state`.`alert_table_id`"; $sql .= " WHERE `device_id` = ? AND `alert_status` IS NOT NULL;"; foreach (dbFetchRows($sql, array($device['device_id'])) as $entry) { echo 'Alert: ' . $entry['alert_table_id'] . ' Status: ' . $entry['alert_status'] . ' '; // If the alerter is now OK and has previously alerted, send an recovery notice. if ($entry['alert_status'] == '1' && $entry['has_alerted'] == '1') { $alert = $alert_rules[$entry['alert_test_id']]; if (!$alert['suppress_recovery']) { $state = json_decode($entry['state'], TRUE); $conditions = json_decode($alert['conditions'], TRUE); $entity = get_entity_by_id_cache($entry['entity_type'], $entry['entity_id']); $metric_array = array(); foreach ($state['metrics'] as $metric => $value) { $metric_array[] = $metric . ' = ' . $value; } $message_tags = array('ALERT_STATE' => 'RECOVERY', 'ALERT_URL' => generate_url(array('page' => 'device', 'device' => $device['device_id'], 'tab' => 'alert', 'alert_entry' => $entry['alert_table_id'])), 'ALERT_MESSAGE' => $alert['alert_message'], 'METRICS' => implode(PHP_EOL . ' ', $metric_array), 'DURATION' => formatUptime(time() - $entry['last_failed']), 'ENTITY_LINK' => generate_entity_link($entry['entity_type'], $entry['entity_id'], $entity['entity_name']), 'ENTITY_NAME' => $entity['entity_name'], 'ENTITY_DESCRIPTION' => $entity['entity_descr'], 'DEVICE_HOSTNAME' => $device['hostname'], 'DEVICE_LINK' => generate_device_link($device), 'DEVICE_HARDWARE' => $device['hardware'], 'DEVICE_OS' => $device['os_text'] . ' ' . $device['version'] . ' ' . $device['features'], 'DEVICE_LOCATION' => $device['location'], 'DEVICE_UPTIME' => deviceUptime($device)); $message['text'] = simple_template('alert/email_text.tpl', $message_tags, array('is_file' => TRUE)); //$message_tags['CONDITIONS'] = nl2br($message_tags['CONDITIONS']); $message_tags['METRICS'] = nl2br($message_tags['METRICS']); $message['html'] = simple_template('alert/email_html.tpl', $message_tags, array('is_file' => TRUE)); //logfile('debug.log', var_export($message, TRUE)); alert_notify($device, alert_generate_subject('RECOVER', $device, $alert, $entity), $message, $entry['alert_test_id']); log_alert('恢复发送的通知', $device, $entry, 'RECOVER_NOTIFY'); } else { echo '已限制恢复.'; log_alert('限制恢复通知', $device, $entry, 'RECOVER_SUPPRESSED'); } $update_array['last_recovered'] = time(); $update_array['has_alerted'] = 0; dbUpdate($update_array, 'alert_table-state', '`alert_table_id` = ?', array($entry['alert_table_id'])); } if ($entry['alert_status'] == '0') { echo '警报已停止. '; // Has this been alerted more frequently than the alert interval in the config? /// FIXME -- this should be configurable per-entity or per-checker if (time() - $entry['last_alerted'] < $config['alerts']['interval'] && !isset($GLOBALS['spam'])) { $entry['suppress_alert'] = TRUE; } // Check if alert has ignore_until set. if (is_numeric($entry['ignore_until']) && $entry['ignore_until'] > time()) { $entry['suppress_alert'] = TRUE; } // Check if alert has ignore_until_ok set. if (is_numeric($entry['ignore_until_ok']) && $entry['ignore_until_ok'] == '1') { $entry['suppress_alert'] = TRUE; } if ($entry['suppress_alert'] != TRUE) { echo 'Requires notification. '; $alert = $alert_rules[$entry['alert_test_id']]; $state = json_decode($entry['state'], TRUE); $conditions = json_decode($alert['conditions'], TRUE); $entity = get_entity_by_id_cache($entry['entity_type'], $entry['entity_id']); $condition_array = array(); foreach ($state['failed'] as $failed) { $condition_array[] = $failed['metric'] . " " . $failed['condition'] . " " . $failed['value'] . " (" . $state['metrics'][$failed['metric']] . ")"; } $graphs = ""; $metric_array = array(); foreach ($state['metrics'] as $metric => $value) { $metric_array[] = $metric . ' = ' . $value; } if (is_array($config['entities'][$entry['entity_type']]['graph'])) { // We can draw a graph for this type/metric pair! $graph_array = $config['entities'][$entry['entity_type']]['graph']; foreach ($graph_array as $key => $val) { // Check to see if we need to do any substitution if (substr($val, 0, 1) == "@") { $nval = substr($val, 1); echo " replaced " . $val . " with " . $entity[$nval] . " from entity. " . PHP_EOL . "<br />"; $graph_array[$key] = $entity[$nval]; } } //print_r($graph_array); //logfile('debug.log', var_export($graph_array, TRUE)); $image_data_uri = generate_alert_graph($graph_array); //print_r($image_data_uri); //logfile('debug.log', var_export($image_data_uri, TRUE)); $graphs .= '<img src="' . $image_data_uri . '"><br />'; unset($graph_array); } $message_tags = array('ALERT_STATE' => 'ALERT', 'ALERT_URL' => generate_url(array('page' => 'device', 'device' => $device['device_id'], 'tab' => 'alert', 'alert_entry' => $entry['alert_table_id'])), 'ALERT_MESSAGE' => $alert['alert_message'], 'CONDITIONS' => implode(PHP_EOL . ' ', $condition_array), 'METRICS' => implode(PHP_EOL . ' ', $metric_array), 'DURATION' => formatUptime(time() - $entry['last_failed']), 'ENTITY_LINK' => generate_entity_link($entry['entity_type'], $entry['entity_id'], $entity['entity_name']), 'ENTITY_NAME' => $entity['entity_name'], 'ENTITY_DESCRIPTION' => $entity['entity_descr'], 'ENTITY_GRAPHS' => $graphs, 'DEVICE_HOSTNAME' => $device['hostname'], 'DEVICE_LINK' => generate_device_link($device), 'DEVICE_HARDWARE' => $device['hardware'], 'DEVICE_OS' => $device['os_text'] . ' ' . $device['version'] . ' ' . $device['features'], 'DEVICE_LOCATION' => $device['location'], 'DEVICE_UPTIME' => deviceUptime($device)); $message['text'] = simple_template('alert/email_text.tpl', $message_tags, array('is_file' => TRUE)); $message_tags['CONDITIONS'] = nl2br($message_tags['CONDITIONS']); $message_tags['METRICS'] = nl2br($message_tags['METRICS']); $message['html'] = simple_template('alert/email_html.tpl', $message_tags, array('is_file' => TRUE)); //logfile('debug.log', var_export($message, TRUE)); alert_notify($device, alert_generate_subject('ALERT', $device, $alert, $entity), $message, $entry['alert_test_id']); log_alert('Alert notification sent', $device, $entry, 'ALERT_NOTIFY'); $update_array['last_alerted'] = time(); $update_array['has_alerted'] = 1; dbUpdate($update_array, 'alert_table-state', '`alert_table_id` = ?', array($entry['alert_table_id'])); } else { echo "没有通知要求. " . (time() - $entry['last_alerted']); } } else { if ($entry['alert_status'] == '1') { echo "状态: OK. "; } else { if ($entry['alert_status'] == '2') { echo "Status: Notification Delayed. "; } else { if ($entry['alert_status'] == '3') { echo "Status: Notification Suppressed. "; } else { echo "未知的状态."; } } } } echo PHP_EOL; } }
} $emails = parse_email($email); if ($emails) { $rcpts_full = array(); foreach ($emails as $to => $to_name) { $rcpts_full[] = empty($to_name) ? $to : '"' . trim($to_name) . '" <' . $to . '>'; } $rcpts_full = implode(', ', $rcpts_full); print_message("尝试发送测试通知 %W" . $device['hostname'] . "%n 到这些邮箱:\n %g{$rcpts_full}%n", 'color', FALSE); // Create multipart (plain+html) message $template_html = '<html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> </head> <body> <tt>{{MESSAGE}}</tt><br /> </body> </html>'; // Use template here just for testing $message_multipart = array('text' => simple_template('{{MESSAGE}}', array('MESSAGE' => $message)), 'html' => simple_template($template_html, array('MESSAGE' => $message))); $status = alert_notify($device, "TEST: [" . $device['hostname'] . "]", $message_multipart, $alert_id); } else { // no one recipient print_message("%r有一些收件人%n 为找到 %W" . $device['hostname'] . "%n.\n 所有设备上设置SNMP设备或默认收件人: \$config['email']['default']", 'color'); } print_message("通知 " . $device['hostname'] . ($status ? ' %gSENT' : ' %rNOT sent') . "%n.", 'color'); } } else { print_message("使用方法:\n{$scriptname} -h device [-d debug]\n\nSYNTAX:\n-h <设备id> | <设备名称的通配符> 发送测试电子邮件管理员所述设备.\n-d Enable debug mode.\n -dd More verbose debugging output.\n\n%r无效的参数!%n", 'color'); } // EOF
<?php /** * Observium * * This file is part of Observium. * * @package observium * @subpackage alerting * @copyright (C) 2006-2013 Adam Armstrong, (C) 2013-2016 Observium Limited * */ $message['text'] = simple_template($method . '_text', $message_tags, array('is_file' => TRUE)); $url = 'https://api.telegram.org/bot' . $endpoint['bot_hash'] . '/sendMessage'; // POST Data $postdata = http_build_query(array("chat_id" => $endpoint['recipient'], "disable_web_page_preview" => 'true', "text" => $message['text'])); $context_data = array('method' => 'POST', 'content' => $postdata); // Send out API call and parse response into an associative array $response = get_http_request($url, $context_data); $notify_status['success'] = FALSE; if ($response !== FALSE) { $response = json_decode($response, TRUE); //var_dump($response); if (isset($response['ok']) && $response['ok'] == TRUE) { $notify_status['success'] = TRUE; } } unset($url, $send, $message, $response, $postdata, $context_data); // EOF