$navbar[$side][$option]['url'] = generate_url($vars, array('page' => "graphs", $option => NULL)); } else { $navbar[$side][$option]['url'] = generate_url($vars, array('page' => "graphs", $option => 'no')); $navbar[$side][$option]['class'] .= " active"; } } else { if ($vars[$option] == "yes") { $navbar[$side][$option]['url'] = generate_url($vars, array('page' => "graphs", $option => NULL)); $navbar[$side][$option]['class'] .= " active"; } else { $navbar[$side][$option]['url'] = generate_url($vars, array('page' => "graphs", $option => 'yes')); } } } } $navbar['options_right']['graph_link'] = array('text' => 'Link to Graph', 'url' => generate_graph_url($graph_array), 'link_opts' => 'target="_blank"'); print_navbar($navbar); unset($navbar); /* ?> <script type="text/javascript" src="js/jsrrdgraph/sprintf.js"></script> <script type="text/javascript" src="js/jsrrdgraph/strftime.js"></script> <script type="text/javascript" src="js/jsrrdgraph/RrdRpn.js"></script> <script type="text/javascript" src="js/jsrrdgraph/RrdTime.js"></script> <script type="text/javascript" src="js/jsrrdgraph/RrdGraph.js"></script> <script type="text/javascript" src="js/jsrrdgraph/RrdGfxCanvas.js"></script> <script type="text/javascript" src="js/jsrrdgraph/RrdGfxSvg.js"></script> <script type="text/javascript" src="js/jsrrdgraph/base64.js"></script>
function generate_graph_tag($args) { if (empty($args)) { return ''; } // Quick return if passed empty array $style = 'max-width: 100%; width: auto; vertical-align: top;'; if (isset($args['style'])) { if (is_array($args['style'])) { $style .= implode("; ", $args['style']) . ';'; } else { $style .= $args['style'] . ';'; } unset($args['style']); } // Detect allowed screen ratio for current browser $ua_info = detect_browser(); $zoom = $ua_info['screen_ratio']; if ($zoom >= 2) { // Add img srcset for HiDPI screens $args_x = $args; $args_x['zoom'] = $zoom; $srcset = ' srcset="' . generate_graph_url($args_x) . ' ' . $args_x['zoom'] . 'x"'; } else { $srcset = ''; } return '<img src="' . generate_graph_url($args) . '"' . $srcset . ' style="' . $style . '" alt="" />'; }
/** * 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'])); } } } }