$response .= implode(PHP_EOL, $whois_parts); //print_vars($request['data']['records']); } } } if ($response) { $_SESSION['cache']['response_' . $vars['entity_type'] . '_' . $ip] = '<pre class="small">' . $response . '</pre>'; echo $_SESSION['cache']['response_' . $vars['entity_type'] . '_' . $ip]; } else { echo 'Not Found'; } } else { echo 'Not correct IP address'; } exit; break; default: if (is_array($config['entities'][$vars['entity_type']])) { if (is_numeric($vars['entity_id']) && is_entity_permitted($vars['entity_id'], $vars['entity_type'])) { $entity = get_entity_by_id_cache($vars['entity_type'], $vars['entity_id']); echo generate_entity_popup($entity, $vars); } else { print_warning("You are not permitted to view this entity."); } } else { print_error("Unknown entity type."); } exit; break; } // EOF
/** * Display alert_table entries. * * @param array $vars * @return none * */ function print_alert_table($vars) { global $alert_rules; global $config; // This should be set outside, but do it here if it isn't if (!is_array($alert_rules)) { $alert_rules = cache_alert_rules(); } /// WARN HERE if (isset($vars['device']) && !isset($vars['device_id'])) { $vars['device_id'] = $vars['device']; } if (isset($vars['entity']) && !isset($vars['entity_id'])) { $vars['entity_id'] = $vars['entity']; } // Short? (no pagination, small out) $short = isset($vars['short']) && $vars['short']; list($query, $param, $query_count) = build_alert_table_query($vars); // Fetch alerts $count = dbFetchCell($query_count, $param); $alerts = dbFetchRows($query, $param); // Set which columns we're going to show. // We hide the columns that have been given as search options via $vars $list = array('device_id' => FALSE, 'entity_id' => FALSE, 'entity_type' => FALSE, 'alert_test_id' => FALSE); foreach ($list as $argument => $nope) { if (!isset($vars[$argument]) || empty($vars[$argument]) || $vars[$argument] == "all") { $list[$argument] = TRUE; } } // Hide device if we know entity_id if (isset($vars['entity_id'])) { $list['device_id'] = FALSE; } // Hide entity_type if we know the alert_test_id if (isset($vars['alert_test_id']) || TRUE) { $list['entity_type'] = FALSE; } // Hide entity types in favour of icons to save space if ($vars['pagination'] && !$short) { $pagination_html = pagination($vars, $count); echo $pagination_html; } echo '<table class="table table-condensed table-bordered table-striped table-rounded table-hover"> <thead> <tr> <th class="state-marker"></th> <th style="width: 1px;"></th>'; // No table id //<th style="width: 5%;">Id</th>'); if ($list['device_id']) { echo ' <th style="width: 15%">设备</th>'; } if ($list['alert_test_id']) { echo ' <th style="min-width: 15%;">警报</th>'; } if ($list['entity_type']) { echo ' <th style="width: 10%">类型</th>'; } if ($list['entity_id']) { echo ' <th style="">实体</th>'; } echo ' <th style="width: 20px">状态</th> <th style="width: 100px;">信息</th> <th style="width: 90px;">已检测</th> <th style="width: 90px;">已更改</th> <th style="width: 90px;">警告</th> <th style="width: 20px;"></th> </tr> </thead> <tbody>' . PHP_EOL; foreach ($alerts as $alert) { // Process the alert entry, generating colours and classes from the data humanize_alert_entry($alert); // Get the entity array using the cache $entity = get_entity_by_id_cache($alert['entity_type'], $alert['entity_id']); // Get the device array using the cache $device = device_by_id_cache($alert['device_id']); // Get the entity_name. ### FIXME - This is probably duplicated effort from above. We should pass it $entity $entity_name = entity_name($alert['entity_type'], $entity); // Set the alert_rule from the prebuilt cache array $alert_rule = $alert_rules[$alert['alert_test_id']]; echo '<tr class="' . $alert['html_row_class'] . '" style="cursor: pointer;" onclick="location.href=\'' . generate_url(array('page' => 'device', 'device' => $device['device_id'], 'tab' => 'alert', 'alert_entry' => $alert['alert_table_id'])) . '\'">'; echo '<td class="state-marker"></td>'; echo '<td style="width: 1px;"></td>'; // If we know the device, don't show the device if ($list['device_id']) { echo '<td><span class="entity-title">' . generate_device_link($device) . '</span></td>'; } // Print link to the alert rule page if ($list['alert_test_id']) { echo '<td><a href="', generate_url(array('page' => 'alert_check', 'alert_test_id' => $alert_rule['alert_test_id'])), '">', $alert_rule['alert_name'], '</a></td>'; } // If we're showing all entity types, print the entity type here if ($list['entity_type']) { echo '<td>' . nicecase($alert['entity_type']) . '</td>'; } // Print a link to the entity if ($list['entity_id']) { echo '<td><span class="entity-title"><i class="' . $config['entities'][$alert['entity_type']]['icon'] . '"></i> ' . generate_entity_link($alert['entity_type'], $alert['entity_id']) . '</span></td>'; } echo '<td>'; ## FIXME -- generate a nice popup with parsed information from the state array echo overlib_link("", '<i class="icon-info-sign"></i>', "<pre>" . print_r(json_decode($alert['state'], TRUE), TRUE) . "</pre>", NULL); echo '</td>'; echo '<td class="' . $alert['class'] . '">' . $alert['last_message'] . '</td>'; echo '<td>' . overlib_link('', $alert['checked'], format_unixtime($alert['last_checked'], 'r'), NULL) . '</td>'; echo '<td>' . overlib_link('', $alert['changed'], format_unixtime($alert['last_changed'], 'r'), NULL) . '</td>'; echo '<td>' . overlib_link('', $alert['alerted'], format_unixtime($alert['last_alerted'], 'r'), NULL) . '</td>'; echo '<td><a href="' . generate_url(array('page' => 'device', 'device' => $device['device_id'], 'tab' => 'alert', 'alert_entry' => $alert['alert_table_id'])) . '"><i class="oicon-gear" /></a></td>'; echo '</tr>'; } echo ' </tbody>' . PHP_EOL; echo '</table>' . PHP_EOL; if ($vars['pagination'] && !$short) { echo $pagination_html; } }
function generate_entity_link($type, $entity, $text = NULL, $graph_type = NULL) { global $config, $entity_cache; if (is_numeric($entity)) { $entity = get_entity_by_id_cache($type, $entity); } switch ($type) { case "mempool": if (empty($text)) { $text = $entity['mempool_descr']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => 'temperature')); break; case "processor": if (empty($text)) { $text = $entity['processor_descr']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => 'temperature')); break; case "sensor": if (empty($text)) { $text = $entity['sensor_descr']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => 'temperature')); break; case "port": $link = generate_port_link($entity, $text, $graph_type); break; case "storage": if (empty($text)) { $text = $entity['storage_descr']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => 'storage')); break; default: $link = $entity[$type . '_id']; } return $link; }
/** * Display alert_table entries. * * @param array $vars * @return none * */ function print_alert_table($vars) { global $alert_rules; global $config; // This should be set outside, but do it here if it isn't if (!is_array($alert_rules)) { $alert_rules = cache_alert_rules(); } /// WARN HERE if (isset($vars['device']) && !isset($vars['device_id'])) { $vars['device_id'] = $vars['device']; } if (isset($vars['entity']) && !isset($vars['entity_id'])) { $vars['entity_id'] = $vars['entity']; } // Short? (no pagination, small out) $short = isset($vars['short']) && $vars['short']; list($query, $param, $query_count) = build_alert_table_query($vars); // Fetch alerts $count = dbFetchCell($query_count, $param); $alerts = dbFetchRows($query, $param); // Set which columns we're going to show. // We hide the columns that have been given as search options via $vars $list = array('device_id' => FALSE, 'entity_id' => FALSE, 'entity_type' => FALSE, 'alert_test_id' => FALSE); foreach ($list as $argument => $nope) { if (!isset($vars[$argument]) || empty($vars[$argument]) || $vars[$argument] == "all") { $list[$argument] = TRUE; } } if ($vars['format'] != "condensed") { $list['checked'] = TRUE; $list['changed'] = TRUE; $list['alerted'] = TRUE; } if ($vars['short'] == TRUE) { $list['checked'] = FALSE; $list['alerted'] = FALSE; } // Hide device if we know entity_id if (isset($vars['entity_id'])) { $list['device_id'] = FALSE; } // Hide entity_type if we know the alert_test_id if (isset($vars['alert_test_id']) || TRUE) { $list['entity_type'] = FALSE; } // Hide entity types in favour of icons to save space if ($vars['pagination'] && !$short) { $pagination_html = pagination($vars, $count); echo $pagination_html; } echo generate_box_open($vars['header']); echo '<table class="table table-condensed table-striped table-hover">'; if ($vars['no_header'] == FALSE) { echo ' <thead> <tr> <th class="state-marker"></th> <th style="width: 1px;"></th>'; if ($list['device_id']) { echo ' <th style="width: 15%">Device</th>'; } if ($list['entity_type']) { echo ' <th style="width: 10%">Type</th>'; } if ($list['entity_id']) { echo ' <th style="">Entity</th>'; } if ($list['alert_test_id']) { echo ' <th style="min-width: 15%;">Alert</th>'; } echo ' <th style="width: 100px;">Status</th>'; if ($list['checked']) { echo ' <th style="width: 95px;">Checked</th>'; } if ($list['changed']) { echo ' <th style="width: 95px;">Changed</th>'; } if ($list['alerted']) { echo ' <th style="width: 95px;">Alerted</th>'; } echo ' <th style="width: 45px;"></th> </tr> </thead>'; } echo '<tbody>' . PHP_EOL; foreach ($alerts as $alert) { // Process the alert entry, generating colours and classes from the data humanize_alert_entry($alert); // Get the entity array using the cache $entity = get_entity_by_id_cache($alert['entity_type'], $alert['entity_id']); // Get the device array using the cache $device = device_by_id_cache($alert['device_id']); // Get the entity_name. ### FIXME - This is probably duplicated effort from above. We should pass it $entity $entity_name = entity_name($alert['entity_type'], $entity); // Set the alert_rule from the prebuilt cache array $alert_rule = $alert_rules[$alert['alert_test_id']]; echo '<tr class="' . $alert['html_row_class'] . '" style="cursor: pointer;" onclick="openLink(\'' . generate_url(array('page' => 'device', 'device' => $device['device_id'], 'tab' => 'alert', 'alert_entry' => $alert['alert_table_id'])) . '\')">'; echo '<td class="state-marker"></td>'; echo '<td style="width: 1px;"></td>'; // If we know the device, don't show the device if ($list['device_id']) { echo '<td><span class="entity-title">' . generate_device_link($device) . '</span></td>'; } // If we're showing all entity types, print the entity type here if ($list['entity_type']) { echo '<td>' . nicecase($alert['entity_type']) . '</td>'; } // Print a link to the entity if ($list['entity_id']) { echo '<td><span class="entity-title"><i class="' . $config['entities'][$alert['entity_type']]['icon'] . '"></i> ' . generate_entity_link($alert['entity_type'], $alert['entity_id']) . '</span></td>'; } // Print link to the alert rule page if ($list['alert_test_id']) { echo '<td class="entity"><a href="', generate_url(array('page' => 'alert_check', 'alert_test_id' => $alert_rule['alert_test_id'])), '">', escape_html($alert_rule['alert_name']), '</a></td>'; } echo '<td>'; echo '<span class="label label-' . ($alert['html_row_class'] != 'up' ? $alert['html_row_class'] : 'success') . '">' . generate_tooltip_link('', $alert['status'], '<div class="small" style="max-width: 500px;"><strong>' . $alert['last_message'] . '</strong></div>', $alert['alert_class']) . '</span>'; echo '</td>'; // echo('<td class="'.$alert['class'].'">'.$alert['last_message'].'</td>'); if ($list['checked']) { echo '<td>' . generate_tooltip_link('', $alert['checked'], format_unixtime($alert['last_checked'], 'r')) . '</td>'; } if ($list['changed']) { echo '<td>' . generate_tooltip_link('', $alert['changed'], format_unixtime($alert['last_changed'], 'r')) . '</td>'; } if ($list['alerted']) { echo '<td>' . generate_tooltip_link('', $alert['alerted'], format_unixtime($alert['last_alerted'], 'r')) . '</td>'; } echo '<td>'; // This stuff should go in an external entity popup in the future. $state = json_decode($alert['state'], true); $alert['state_popup'] = ''; if (count($state['failed'])) { $alert['state_popup'] .= generate_box_open(array('title' => 'Failed Tests')); //'<h4>Failed Tests</h4>'; $alert['state_popup'] .= '<table style="min-width: 400px;" class="table table-striped table-condensed">'; $alert['state_popup'] .= '<thead><tr><th>Metric</th><th>Cond</th><th>Value</th><th>Measured</th></tr></thead>'; foreach ($state['failed'] as $test) { $alert['state_popup'] .= '<tr><td><strong>' . $test['metric'] . '</strong></td><td>' . $test['condition'] . '</td><td>' . $test['value'] . '</td><td><i class="red">' . $state['metrics'][$test['metric']] . '</i></td></tr>'; } $alert['state_popup'] .= '</table>'; $alert['state_popup'] .= generate_box_close(); } $alert['state_popup'] .= generate_entity_popup_graphs($alert, array('entity_type' => 'alert_entry')); // Print (i) icon with popup of state. echo overlib_link("", '<i class="icon-info-sign text-primary"></i>', $alert['state_popup'], NULL); echo ' <a href="' . generate_url(array('page' => 'device', 'device' => $device['device_id'], 'tab' => 'alert', 'alert_entry' => $alert['alert_table_id'])) . '"><i class="icon-cog text-muted"></i></a>'; echo '</td>'; echo '</tr>'; } echo ' </tbody>' . PHP_EOL; echo '</table>' . PHP_EOL; echo generate_box_close(); if ($vars['pagination'] && !$short) { echo $pagination_html; } }
function generate_entity_popup($entity, $vars) { if (is_numeric($entity)) { $entity = get_entity_by_id_cache($entity, $vars['entity_type']); } $device = device_by_id_cache($entity['device_id']); $content = generate_device_popup_header($device); $content .= generate_entity_popup_header($entity, $vars); $content .= generate_entity_popup_graphs($entity, $vars); return $content; }
/** * Check all alerts for a device to see if they should be notified or not * * @param array device * @return NULL */ function process_alerts($device) { global $config, $alert_rules, $alert_assoc; echo "Processing alerts for " . $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` = ?"; 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']]; $state = json_decode($entry['state'], TRUE); $conditions = json_decode($alert['conditions'], TRUE); $entity = get_entity_by_id_cache($entry['entity_type'], $entry['entity_id']); $graphs = ""; $metric_text = ""; foreach ($state['metrics'] as $metric => $value) { $metric_text .= $metric . " = " . $value . PHP_EOL . "<br />"; } // FIXME De-dup this shit soon. // - adama $message = ' <head> <title>Observium Alert</title> <style> .observium{ width:100%; max-width: 500px; -webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; border:1px solid #DDDDDD; background-color:#FAFAFA; font-size: 13px; color: #777777; } .header{ font-weight: bold; font-size: 16px; padding: 5px; color: #555555; } .red { color: #cc0000; } #deviceinfo tr:nth-child(odd) { background: #ffffff; } </style> <style type="text/css"></style></head> <body> <table class="observium"> <tbody> <tr> <td> <table class="observium" id="deviceinfo"> <tbody> <tr><td class="header">RECOVERY</td><td><a style="float: right;" href="' . generate_url(array('page' => 'device', 'device' => $device['device_id'], 'tab' => 'alert', 'alert_entry' => $entry['alert_table_id'])) . '">Modify</a></td></tr> <tr><td><b>Alert</b></font></td><td class="red">' . $alert['alert_message'] . '</font></td></tr> <tr><td><b>Entity</b></font></td><td>' . generate_entity_link($entry['entity_type'], $entry['entity_id'], $entity['entity_name']) . '</font></td></tr>'; if (strlen($entity['entity_descr']) > 0) { $message .= '<tr><td><b>Descr</b></font></td><td>' . $entity['entity_descr'] . '</font>'; } $message .= ' <tr><td><b>Metrics</b></font></td><td>' . $metric_text . '</font></td></tr> <tr><td><b>Duration</b></font></td><td>' . formatUptime(time() - $entry['last_failed']) . '</font></td></tr> <tr><td colspan="2" class="header">Device</td></tr> <tr><td><b>Device</b></font></td><td>' . generate_device_link($device) . '</font></td></tr> <tr><td><b>Hardware</b></font></td><td>' . $device['hardware'] . '</font></td></tr> <tr><td><b>Operating System</b></font></td><td>' . $device['os_text'] . ' ' . $device['version'] . ' ' . $device['features'] . '</font></td></tr> <tr><td><b>Location</b></font></td><td>' . htmlspecialchars($device['location']) . '</font></td></tr> <tr><td><b>Uptime</b></font></td><td>' . deviceUptime($device) . '</font></td></tr> </tbody></table> </td></tr> <tr><td> <center>' . $graphs . '</center></td></tr> </tbody></table> </body> </html>'; alert_notify($device, "RECOVER: [" . $device['hostname'] . "] [" . $alert['entity_type'] . "] [" . $entity['entity_name'] . "] " . $alert['alert_message'], $message); $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 'Alert tripped. '; // 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; } 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_text = ""; foreach ($state['failed'] as $failed) { $condition_text .= $failed['metric'] . " " . $failed['condition'] . " " . $failed['value'] . " (" . $state['metrics'][$failed['metric']] . ")<br />"; } $graphs = ""; $metric_text = ""; foreach ($state['metrics'] as $metric => $value) { $metric_text .= $metric . " = " . $value . PHP_EOL . "<br />"; } 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); $image_data_uri = generate_alert_graph($graph_array); print_r(strlen($image_data_uri)); $graphs .= '<img src="' . $image_data_uri . '">' . "<br />"; unset($graph_array); } #$css = data_uri($config['html_dir'].'/css/bootstrap-mini.css' ,'text/css'); $message = ' <head> <title>Observium Alert</title> <style> .observium{ width:100%; max-width: 500px; -webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; border:1px solid #DDDDDD; background-color:#FAFAFA; font-size: 13px; color: #777777; } .header{ font-weight: bold; font-size: 16px; padding: 5px; color: #555555; } .red { color: #cc0000; } #deviceinfo tr:nth-child(odd) { background: #ffffff; } </style> <style type="text/css"></style></head> <body> <table class="observium"> <tbody> <tr> <td> <table class="observium" id="deviceinfo"> <tbody> <tr><td class="header">ALERT</td><td><a style="float: right;" href="' . generate_url(array('page' => 'device', 'device' => $device['device_id'], 'tab' => 'alert', 'alert_entry' => $entry['alert_table_id'])) . '">Modify</a></td></tr> <tr><td><b>Alert</b></font></td><td class="red">' . $alert['alert_message'] . '</font></td></tr> <tr><td><b>Entity</b></font></td><td>' . generate_entity_link($entry['entity_type'], $entry['entity_id'], $entity['entity_name']) . '</font></td></tr>'; if (strlen($entity['entity_descr']) > 0) { $message .= '<tr><td><b>Descr</b></font></td><td>' . $entity['entity_descr'] . '</font>'; } $message .= ' <tr><td><b>Conditions</b></font></td><td>' . $condition_text . '</font></td></tr> <tr><td><b>Metrics</b></font></td><td>' . $metric_text . '</font></td></tr> <tr><td><b>Duration</b></font></td><td>' . formatUptime(time() - $entry['last_failed']) . '</font></td></tr> <tr><td colspan="2" class="header">Device</td></tr> <tr><td><b>Device</b></font></td><td>' . generate_device_link($device) . '</font></td></tr> <tr><td><b>Hardware</b></font></td><td>' . $device['hardware'] . '</font></td></tr> <tr><td><b>Operating System</b></font></td><td>' . $device['os_text'] . ' ' . $device['version'] . ' ' . $device['features'] . '</font></td></tr> <tr><td><b>Location</b></font></td><td>' . htmlspecialchars($device['location']) . '</font></td></tr> <tr><td><b>Uptime</b></font></td><td>' . deviceUptime($device) . '</font></td></tr> </tbody></table> </td></tr> <tr><td> <center>' . $graphs . '</center></td></tr> </tbody></table> </body> </html>'; alert_notify($device, "ALERT: [" . $device['hostname'] . "] [" . $alert['entity_type'] . "] [" . $entity['entity_name'] . "] " . $alert['alert_message'], $message); $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 "No notification required. " . (time() - $entry['last_alerted']); } } elseif ($entry['alert_status'] == '1') { echo "Status: OK. "; } else { echo "Unknown status."; } echo PHP_EOL; } }
/** * Generate notifications for an alert entry * * @param array entry * @return NULL */ function alert_notifier($entry, $type = "alert") { global $config, $alert_rules; $device = device_by_id_cache($entry['device_id']); $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']] . ")"; } $metric_array = array(); foreach ($state['metrics'] as $metric => $value) { $metric_array[] = $metric . ' = ' . $value; } $graphs = array(); $graph_done = array(); foreach ($state['metrics'] as $metric => $value) { if ($config['email']['graphs'] !== FALSE && is_array($config['entities'][$entry['entity_type']]['metric_graphs'][$metric]) && !in_array($config['entities'][$entry['entity_type']]['metric_graphs'][$metric]['type'], $graph_done)) { $graph_array = $config['entities'][$entry['entity_type']]['metric_graphs'][$metric]; 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]; } } $image_data_uri = generate_alert_graph($graph_array); $image_url = generate_graph_url($graph_array); $graphs[] = array('label' => $graph_array['type'], 'type' => $graph_array['type'], 'url' => $image_url, 'data' => $image_data_uri); $graph_done[] = $graph_array['type']; } unset($graph_array); } if ($config['email']['graphs'] !== FALSE && count($graph_done) == 0 && 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_vars($graph_array); $image_data_uri = generate_alert_graph($graph_array); $image_url = generate_graph_url($graph_array); $graphs[] = array('label' => $graph_array['type'], 'type' => $graph_array['type'], 'url' => $image_url, 'data' => $image_data_uri); unset($graph_array); } $graphs_html = ""; foreach ($graphs as $graph) { $graphs_html .= '<h4>' . $graph['type'] . '</h4>'; $graphs_html .= '<a href="' . $graph['url'] . '"><img src="' . $graph['data'] . '"></a><br />'; } //print_vars($graphs); //print_vars($graphs_html); //print_vars($entry); $message_tags = array('ALERT_STATE' => $entry['alert_status'] == '1' ? "RECOVER" : "ALERT", 'ALERT_URL' => generate_url(array('page' => 'device', 'device' => $device['device_id'], 'tab' => 'alert', 'alert_entry' => $entry['alert_table_id'])), 'ALERT_ID' => $entry['alert_table_id'], 'ALERT_MESSAGE' => $alert['alert_message'], 'CONDITIONS' => implode(PHP_EOL . ' ', $condition_array), 'METRICS' => implode(PHP_EOL . ' ', $metric_array), 'DURATION' => $entry['alert_status'] == '1' ? $entry['last_ok'] > 0 ? formatUptime(time() - $entry['last_ok']) . " (" . format_unixtime($entry['last_ok']) . ")" : "Unknown" : ($entry['last_ok'] > 0 ? formatUptime(time() - $entry['last_ok']) . " (" . format_unixtime($entry['last_ok']) . ")" : "Unknown"), 'ENTITY_LINK' => generate_entity_link($entry['entity_type'], $entry['entity_id'], $entity['entity_name']), 'ENTITY_NAME' => $entity['entity_name'], 'ENTITY_TYPE' => $alert['entity_type'], 'ENTITY_DESCRIPTION' => $entity['entity_descr'], 'ENTITY_GRAPHS_ARRAY' => json_encode($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)); //logfile('debug.log', var_export($message, TRUE)); $title = alert_generate_subject($device, $message_tags['ALERT_STATE'], $message_tags); $message_tags['TITLE'] = $title; $alert_id = $entry['alert_test_id']; $notify_status = FALSE; // Set alert notify status to FALSE by default $notification_type = 'alert'; $transports = get_alert_contacts($device, $alert_id, $notification_type); if (!empty($transports)) { // WARNING, alerts queue currently experimental if (isset($config['alerts']['queue']) && isset($config['alerts']['queue'])) { // Add notification to queue $notification = array('device_id' => $device['device_id'], 'log_id' => $log_id, 'aca_type' => $notification_type, 'endpoints' => json_encode($transports), 'message_graphs' => $message_tags['ENTITY_GRAPHS_ARRAY'], 'notification_added' => time(), 'notification_lifetime' => 300, 'notification_entry' => json_encode($entry)); $notification_message_tags = $message_tags; unset($notification_message_tags['ENTITY_GRAPHS_ARRAY']); $notification['message_tags'] = json_encode($notification_message_tags); $notification_id = dbInsert($notification, 'notifications_queue'); } else { // Use classic instant notifications send $notification_count = 0; foreach ($transports as $method => $endpoints) { if (isset($config['alerts']['disable'][$method]) && $config['alerts']['disable'][$method]) { continue; } // Skip if method disabled globally foreach ($endpoints as $endpoint) { $method_include = $config['install_dir'] . "/includes/alerting/" . $method . ".inc.php"; if (is_file($method_include)) { print_cli_data("Notifying", "[" . $method . "] " . $endpoint['contact_descr'] . ": " . $endpoint['contact_endpoint']); // Split out endpoint data as stored JSON in the database into array for use in transport // The original string also remains available as the contact_endpoint key foreach (json_decode($endpoint['contact_endpoint']) as $field => $value) { $endpoint[$field] = $value; } include $method_include; // FIXME check success // FIXME log notification + success/failure! if ($notify_status['success']) { $notification_count++; } } else { print_cli_data("Missing include", $method_include); } } } if ($notification_count) { dbUpdate(array('notified' => 1), 'alert_log', '`event_id` = ?', array($notification['log_id'])); } } } }
} else { $update_state['ignore_until'] = array('NULL'); } if (is_array($update_state)) { $up_s = dbUpdate($update_state, 'alert_table', '`alert_table_id` = ?', array($vars['alert_entry'])); } // Refresh array because we've changed the database. $entry = get_alert_entry_by_id($vars['alert_entry']); } // End actions humanize_alert_entry($entry); $alert_rules = cache_alert_rules(); $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']); // r($entry); // r($alert); ?> <div class="row"> <div class="col-md-3"> <div class="box box-solid"> <div class="box-header with-border"> <!-- <i class="oicon-bell"></i> --><h3 class="box-title">Alert Details</h3> </div> <div class="box-body no-padding"> <table class="table table-condensed table-striped "> <tbody> <tr><th>Type</th><td><?php echo '<i class="' . $config['entities'][$alert['entity_type']]['icon'] . '"></i> ' . nicecase($entry['entity_type']);
<?php /** * Observium * * This file is part of Observium. * * @package observium * @subpackage graphs * @copyright (C) 2006-2013 Adam Armstrong, (C) 2013-2016 Observium Limited * */ if (is_numeric($vars['id']) && ($alert = get_alert_entry_by_id($vars['id']))) { $entity = get_entity_by_id_cache($alert['entity_type'], $alert['entity_id']); $device = device_by_id_cache($alert['device_id']); if (device_permitted($device['device_id']) || $auth) { $title = generate_device_link($device); $title_array = array(); $title_array[] = array('text' => $device['hostname'], 'url' => generate_url(array('page' => 'device', 'device' => $device['device_id']))); $auth = TRUE; $rrd_filename = get_rrd_path($device, "alert-" . $alert['alert_table_id']); } } else { // error? }
// array index -> ['measured']['port']['345'][] = sensor array $sensors_db['measured'][$entry['measured_class']][$entry['measured_entity']][] = $entry; } else { $sensors_db[$sensor_type][$entry['sensor_id']] = $entry; } } } //r($sensors_db['measured']); // Now print founded bundle (measured_class+sensor) if (isset($sensors_db['measured'])) { foreach ($sensors_db['measured'] as $measured_class => $measured_entity) { $box_args = array('title' => nicecase($measured_class) . ' sensors', 'icon' => 'oicon-node'); echo generate_box_open($box_args); echo ' <table class="table table-condensed table-striped">'; foreach ($measured_entity as $entity_id => $entry) { $entity = get_entity_by_id_cache($measured_class, $entity_id); $entity_name = entity_name($measured_class, $entity); $entity_link = generate_entity_link($measured_class, $entity); $entity_type = entity_type_translate_array($measured_class); //echo(' <tr class="'.$port['row_class'].'"> // <td class="state-marker"></td> echo ' <tr> <td colspan="6" class="entity"><i class="' . $entity_type['icon'] . '"></i> ' . $entity_link . '</td></tr>'; foreach ($entry as $sensor) { // Remove port name from sensor description $sensor['sensor_descr'] = trim(str_ireplace($entity_name, '', $sensor['sensor_descr'])); if (empty($sensor['sensor_descr'])) { // Some time sensor descriptions equals to entity name $sensor['sensor_descr'] = nicecase($sensor['sensor_class']); } print_sensor_row($sensor, $vars);
function generate_entity_link($entity_type, $entity, $text = NULL, $graph_type = NULL, $escape = TRUE, $short = FALSE) { if (is_numeric($entity)) { $entity = get_entity_by_id_cache($entity_type, $entity); } entity_rewrite($entity_type, $entity); switch ($entity_type) { case "device": $link = generate_device_link($entity); break; case "mempool": $url = generate_url(array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => 'mempool')); break; case "processor": $url = generate_url(array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => 'processor')); break; case "status": $url = generate_url(array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => 'status', 'id' => $entity['status_id'])); break; case "sensor": $url = generate_url(array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => $entity['sensor_class'], 'id' => $entity['sensor_id'])); break; case "printersupply": $url = generate_url(array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'printing', 'supply' => $entity['supply_type'])); break; case "port": $link = generate_port_link($entity, NULL, $graph_type, $escape, $short); break; case "storage": $url = generate_url(array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => 'storage')); break; case "bgp_peer": $url = generate_url(array('page' => 'device', 'device' => $entity['peer_device_id'] ? $entity['peer_device_id'] : $entity['device_id'], 'tab' => 'routing', 'proto' => 'bgp')); break; case "netscalervsvr": $url = generate_url(array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'loadbalancer', 'type' => 'netscaler_vsvr', 'vsvr' => $entity['vsvr_id'])); break; case "netscalersvc": $url = generate_url(array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'loadbalancer', 'type' => 'netscaler_services', 'svc' => $entity['svc_id'])); break; case "netscalersvcgrpmem": $url = generate_url(array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'loadbalancer', 'type' => 'netscaler_servicegroupmembers', 'svc' => $entity['svc_id'])); break; case "sla": $url = generate_url(array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'slas', 'id' => $entity['sla_id'])); break; case "pseudowire": $url = generate_url(array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'pseudowires', 'id' => $entity['pseudowire_id'])); break; case "maintenance": $url = generate_url(array('page' => 'alert_maintenance', 'maintenance' => $entity['maint_id'])); break; case "group": $url = generate_url(array('page' => 'group', 'group_id' => $entity['group_id'])); break; case "virtualmachine": // If we know this device by its vm name in our system, create a link to it, else just print the name. if (get_device_id_by_hostname($entity['vm_name'])) { $link = generate_device_link(device_by_name($entity['vm_name'])); } else { // Hardcode $link to just show the name, no actual link $link = $entity['vm_name']; } break; default: $url = NULL; } if (!isset($link)) { if (!isset($text)) { if ($short && $entity['entity_shortname']) { $text = $entity['entity_shortname']; } else { $text = $entity['entity_name']; } } if ($escape) { $text = escape_html($text); } $link = '<a href="' . $url . '" class="entity-popup ' . $entity['html_class'] . '" data-eid="' . $entity['entity_id'] . '" data-etype="' . $entity_type . '">' . $text . '</a>'; } return $link; }
function generate_entity_link($type, $entity, $text = NULL, $graph_type = NULL) { global $config, $entity_cache; if (is_numeric($entity)) { $entity = get_entity_by_id_cache($type, $entity); } switch ($type) { case "mempool": if (empty($text)) { $text = $entity['mempool_descr']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => 'mempool')); break; case "processor": if (empty($text)) { $text = $entity['processor_descr']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => 'processor')); break; case "sensor": if (empty($text)) { $text = $entity['sensor_descr']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => $entity['sensor_class'])); break; case "toner": if (empty($text)) { $text = $entity['toner_descr']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'printing')); break; case "port": $link = generate_port_link($entity, $text, $graph_type); break; case "storage": if (empty($text)) { $text = $entity['storage_descr']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => 'storage')); break; case "bgp_peer": if (Net_IPv6::checkIPv6($entity['bgpPeerRemoteAddr'])) { $addr = Net_IPv6::compress($entity['bgpPeerRemoteAddr']); } else { $addr = $entity['bgpPeerRemoteAddr']; } if (empty($text)) { $text = $addr . " (AS" . $entity['bgpPeerRemoteAs'] . ")"; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'routing', 'proto' => 'bgp')); break; case "netscaler_vsvr": if (empty($text)) { $text = $entity['vsvr_label']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'loadbalancer', 'type' => 'netscaler_vsvr', 'vsvr' => $entity['vsvr_id'])); break; case "netscaler_svc": if (empty($text)) { $text = $entity['svc_label']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'loadbalancer', 'type' => 'netscaler_services', 'svc' => $entity['svc_id'])); break; default: $link = $entity[$type . '_id']; } return $link; }
function generate_entity_link($entity_type, $entity, $text = NULL, $graph_type = NULL, $escape = TRUE) { global $config, $entity_cache; if (is_numeric($entity)) { $entity = get_entity_by_id_cache($entity_type, $entity); } entity_rewrite($entity_type, $entity); // Rewrite sensor subtypes to 'sensor' // $translate = entity_type_translate_array($type); // if (isset($translate['parent_type'])) { $type = $translate['parent_type']; } switch ($entity_type) { case "device": $link = generate_device_link($entity); break; case "mempool": $url = generate_url(array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => 'mempool')); break; case "processor": $url = generate_url(array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => 'processor')); break; case "status": $url = generate_url(array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => 'status')); break; case "sensor": $url = generate_url(array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => $entity['sensor_class'])); break; case "toner": $url = generate_url(array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'printing')); break; case "port": $link = generate_port_link($entity, NULL, $graph_type, $escape); break; case "storage": $url = generate_url(array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => 'storage')); break; case "bgp_peer": $url = generate_url(array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'routing', 'proto' => 'bgp')); break; case "netscaler_vsvr": $url = generate_url(array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'loadbalancer', 'type' => 'netscaler_vsvr', 'vsvr' => $entity['vsvr_id'])); break; case "netscaler_svc": $url = generate_url(array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'loadbalancer', 'type' => 'netscaler_services', 'svc' => $entity['svc_id'])); break; case "sla": $url = generate_url(array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'slas')); break; default: $url = NULL; } if (!isset($link)) { if (!isset($text)) { $text = $entity['entity_name']; } if ($escape) { $text = escape_html($text); } $link = '<a href="' . $url . '" class="entity-popup ' . $entity['html_class'] . '" data-eid="' . $entity['entity_id'] . '" data-etype="' . $entity_type . '">' . $text . '</a>'; } return $link; }
if ($list['group_id']) { echo ' <th style="min-width: 15%;">分组</th>'; } if ($list['entity_type']) { echo ' <th style="width: 10%">类型</th>'; } if ($list['entity_id']) { echo ' <th style="">实体</th>'; } echo ' </tr> </thead> <tbody>' . PHP_EOL; foreach ($members as $member) { // Get the entity array using the cache $entity = get_entity_by_id_cache($member['entity_type'], $member['entity_id']); // Get the device array using the cache $device = device_by_id_cache($member['device_id']); // Get the entity_name. ### FIXME - This is probably duplicated effort from above. We should pass it $entity $entity_name = entity_name($member['entity_type'], $entity); echo '<tr class="' . $entity['html_row_class'] . '" style="cursor: pointer;">'; echo '<td style="width: 1px; background-color: ' . $entity['table_tab_colour'] . '; margin: 0px; padding: 0px"></td>'; echo '<td style="width: 1px;"></td>'; // If we know the device, don't show the device if ($list['device_id']) { echo '<td><span class="entity-title">' . generate_device_link($device) . '</span></td>'; } // If we're showing all entity types, print the entity type here if ($list['entity_type']) { echo '<td>' . nicecase($member['entity_type']) . '</td>';
function get_port_by_id_cache($port_id) { return get_entity_by_id_cache('port', $port_id); }
<?php /** * Observium * * This file is part of Observium. * * @package observium * @subpackage graphs * @copyright (C) 2006-2013 Adam Armstrong, (C) 2013-2016 Observium Limited * */ if (is_numeric($vars['id'])) { $entity = get_entity_by_id_cache($type, $vars['id']); if (is_numeric($entity['device_id']) && ($auth || device_permitted($entity['device_id']))) { $device = device_by_id_cache($entity['device_id']); $title = generate_device_link($device); $title .= " :: Pseudowire :: " . escape_html($entity['pwID']); $auth = TRUE; $index = strtolower($entity['mib']) . '-' . $entity['pwIndex']; if ($subtype == 'uptime') { $index = strtolower($entity['mib']) . '-uptime-' . $entity['pwIndex']; } $unit_text = 'PW ' . $entity['pwID']; if ($entity['pwDescr']) { $unit_text .= ' - ' . $entity['pwDescr']; } $graph_title = $device['hostname'] . ' :: ' . $unit_text; // hostname :: SLA XX $rrd_filename = get_rrd_path($device, "pseudowire-" . $index . ".rrd"); $auth = TRUE;
<small>' . $port['ifDescr'] . '</small></td> </tr>'; } echo '</table>' . PHP_EOL; } else { echo '<p class="text-center text-warning bg-warning" style="padding: 10px; margin: 0px;"><strong>This user currently has no permitted ports</strong></p>'; //print_warning('This user currently has no permitted ports'); } echo generate_box_close(); // End port permissions // Start sensor permissions echo generate_box_open(array('header-border' => TRUE, 'title' => 'Sensor Permissions')); if (count($user_permissions['sensor'])) { echo '<table class="' . OBS_CLASS_TABLE . '">' . PHP_EOL; foreach (array_keys($user_permissions['sensor']) as $entity_id) { $sensor = get_entity_by_id_cache('sensor', $entity_id); $device = device_by_id_cache($sensor['device_id']); echo '<tr><td style="width: 1px;"></td> <td style="width: 200px; overflow: hidden;"><i class="' . $config['entities']['device']['icon'] . '"></i> ' . generate_entity_link('device', $device) . '</td> <td style="overflow: hidden;"><i class="' . $config['entities']['sensor']['icon'] . '"></i> ' . generate_entity_link('sensor', $sensor) . ' <td width="25"> </tr>'; } echo '</table>' . PHP_EOL; } else { echo '<p class="text-center text-warning bg-warning" style="padding: 10px; margin: 0px;"><strong>This user currently has no permitted sensors</strong></p>'; //print_warning('This user currently has no permitted sensors'); } echo generate_box_close(); // End sensor permissions }
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; } }
function generate_entity_link($type, $entity, $text = null, $graph_type = null) { global $config, $entity_cache; if (is_numeric($entity)) { $entity = get_entity_by_id_cache($type, $entity); } switch ($type) { case 'port': $link = generate_port_link($entity, $text, $graph_type); break; case 'storage': if (empty($text)) { $text = $entity['storage_descr']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => 'storage')); break; default: $link = $entity[$type . '_id']; } return $link; }
function generate_entity_link($type, $entity, $text = NULL, $graph_type = NULL, $escape = TRUE) { global $config, $entity_cache; if (is_numeric($entity)) { $entity = get_entity_by_id_cache($type, $entity); } // Rewrite sensor subtypes to 'sensor' $translate = entity_type_translate_array($type); if (isset($translate['parent_type'])) { $type = $translate['parent_type']; } switch ($type) { case "device": if (empty($text)) { $text = $entity['hostname']; } // FIXME use name_field property for all of these? Like entity_rewrite above does. $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id']), array(), $escape); break; case "mempool": if (empty($text)) { $text = $entity['mempool_descr']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => 'mempool'), array(), $escape); break; case "processor": if (empty($text)) { $text = $entity['processor_descr']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => 'processor'), array(), $escape); break; case "sensor": if (empty($text)) { $text = $entity['sensor_descr']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => $entity['sensor_class']), array(), $escape); break; case "toner": if (empty($text)) { $text = $entity['toner_descr']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'printing'), array(), $escape); break; case "port": $link = generate_port_link($entity, $text, $graph_type, $escape); break; case "storage": if (empty($text)) { $text = $entity['storage_descr']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => 'storage'), array(), $escape); break; case "bgp_peer": if (Net_IPv6::checkIPv6($entity['bgpPeerRemoteAddr'])) { $addr = Net_IPv6::compress($entity['bgpPeerRemoteAddr']); } else { $addr = $entity['bgpPeerRemoteAddr']; } if (empty($text)) { $text = $addr . " (AS" . $entity['bgpPeerRemoteAs'] . ")"; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'routing', 'proto' => 'bgp'), array(), $escape); break; case "netscaler_vsvr": if (empty($text)) { $text = $entity['vsvr_label']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'loadbalancer', 'type' => 'netscaler_vsvr', 'vsvr' => $entity['vsvr_id']), array(), $escape); break; case "netscaler_svc": if (empty($text)) { $text = $entity['svc_label']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'loadbalancer', 'type' => 'netscaler_services', 'svc' => $entity['svc_id']), array(), $escape); break; default: $link = $entity[$type . '_id']; } return $link; }