function ganglia_events_api_error_handler ($no, $str, $file, $line, $context) { switch ($no) { case E_ERROR: case E_CORE_ERROR: case E_COMPILE_ERROR: case E_USER_ERROR: api_return_error( "$file [$line] : $str" ); break; } }
function ganglia_api_error_handler($no, $str, $file, $line, $context) { switch ($no) { case E_ERROR: case E_CORE_ERROR: case E_COMPILE_ERROR: case E_USER_ERROR: api_return_error("{$file} [{$line}] : {$str}"); break; } }
$end_time = time(); } else { if (is_numeric($_REQUEST['end_time'])) { $end_time = $_REQUEST['end_time']; } else { $end_time = strtotime($_REQUEST['end_time']); } } $event['end_time'] = $end_time; } $message = ganglia_events_add($event); break; case "edit": $message = ganglia_event_modify($_REQUEST); break; case "remove": case "delete": if (!isset($_REQUEST['event_id'])) { api_return_error("No event_id has been supplied."); } $message = ganglia_event_delete($_REQUEST['event_id']); break; case "list": $message = ganglia_events_get(); break; default: api_return_error("No valid action specified"); break; } // end of switch ( $_REQUEST['action'] ) { print json_encode($message);
<?php ///////////////////////////////////////////////////////////////////////////// // This API allows you to get cluster name for a hostname // // Currently only query supported is // // hostname=YOURHOSTNAME&search_type=exact // // will give you back the an array of cluster names where host is in ///////////////////////////////////////////////////////////////////////////// $conf['gweb_root'] = dirname(dirname(__FILE__)); include_once $conf['gweb_root'] . "/eval_conf.php"; include_once $conf['gweb_root'] . "/lib/common_api.php"; include_once $conf['gweb_root'] . "/functions.php"; ganglia_cache_metrics(); if (isset($_GET['hostname']) and isset($_GET['search_type']) and $_GET['search_type'] == "exact") { $hostname = $_GET['hostname']; if (isset($index_array['cluster'][$hostname])) { api_return_ok($index_array['cluster'][$hostname]); } else { api_return_error("Cluster not found"); } } else { api_return_error("No valid search provided"); }
function ganglia_event_modify($event) { global $conf; $event_found = 0; $events_array = ganglia_events_get(); $new_events_array = array(); if (!isset($event['event_id'])) { api_return_error("Event ID not found"); } // isset event_id foreach ($events_array as $k => $e) { if ($e['event_id'] == $event['event_id']) { $event_found = 1; if (isset($event['start_time'])) { if ($event['start_time'] == "now") { $e['start_time'] = time(); } else { if (is_numeric($event['start_time'])) { $e['start_time'] = $event['start_time']; } else { $e['start_time'] = strtotime($event['start_time']); } } } // end isset start_time foreach (array('cluster', 'description', 'summary', 'grid', 'host_regex') as $k) { if (isset($event[$k])) { $e[$k] = $event[$k]; } } // end foreach if (isset($event['end_time'])) { $e['end_time'] = $event['end_time'] == "now" ? time() : strtotime($event['end_time']); } // end isset end_time } // if event_id // Add either original or modified event back in $new_events_array[] = $e; } // foreach events array if ($event_found == 1) { $json = json_encode($new_events_array); if (file_put_contents($conf['overlay_events_file'], $json) === FALSE) { api_return_error("Can't write to file " . $conf['overlay_events_file'] . ". Perhaps permissions are wrong."); } else { $message = array("status" => "ok", "message" => "Event ID " . $event_id . " modified successfully"); } } // end if event_found return $message; }
} file_put_contents($conf['nagios_cache_file'], serialize($new_metrics)); unset($metrics); $metrics = $new_metrics; unset($new_metrics); } # Get a list of all hosts $ganglia_hosts_array = array_keys($metrics); $host_found = 0; # Find a FQDN of a supplied server name. for ($i = 0; $i < count($ganglia_hosts_array); $i++) { if (!strcasecmp($ganglia_hosts_array[$i], $host)) { $fqdn = $ganglia_hosts_array[$i]; $host_found = 1; break; } } # Host has been found in the Ganglia tree if ($host_found == 1) { # Check for the existence of a metric if (isset($metrics[$fqdn][$metric_name]['VAL'])) { $metric_value = $metrics[$fqdn][$metric_name]['VAL']; } else { api_return_error($metric_name . " - Invalid metric request for this host. Please check metric exists."); exit(3); } $ganglia_units = $metrics[$fqdn][$metric_name]['UNITS']; api_return_ok(array('metric_value' => $metric_value, 'units' => $ganglia_units)); } else { api_return_error($metric_name . " - Hostname info not available. Likely invalid hostname"); }
} $view['items'][] = $item_array; unset($item_array); } else { if ($_GET['type'] == "metric") { $items = array("hostname" => $_GET['host_name'], "metric" => $_GET['metric_name']); if (isset($_GET['vertical_label'])) { $items["vertical_label"] = $_GET['vertical_label']; } if (isset($_GET['title'])) { $items["title"] = $_GET['title']; } $view['items'][] = $items; } else { $view['items'][] = array("hostname" => $_GET['host_name'], "graph" => $_GET['metric_name']); } } $json = json_encode($view); if (file_put_contents($view_filename, json_prettyprint($json)) === FALSE) { api_return_error("Can't write to file {$view_filename}. Perhaps permissions are wrong."); } else { api_return_ok("View has been updated successfully."); } // end of if ( file_put_contents($view_filename, $json) === FALSE ) } // end of if ( $view_exists == 1 ) } break; // end add_to_view } // end case action
if (isset($v['TITLE'])) { $graph_arguments['ti'] = $metrics[$cluster_url]['TITLE']; } $graph['description'] = isset($metrics[$cluster_url]['DESC']) ? $metrics[$cluster_url]['DESC'] : ''; $graph['title'] = isset($metrics[$cluster_url]['TITLE']) ? $metrics[$cluster_url]['TITLE'] : $rrd; # Setup an array of groups that can be used for sorting in group view if (isset($metrics[$name]['GROUP'])) { $groups = $metrics[$name]['GROUP']; } else { $groups = array(""); } $graph['graph_image'] = array('script' => 'graph.php', 'params' => $graph_arguments); $graph['graph_url'] = form_image_url('graph.php', $graph_arguments); $r['graph'][] = $graph; } // end foreach metrics if ($debug) { print "<pre>"; print_r($r); die("</pre>"); } api_return_ok($r); break; // end get // end get default: api_return_error("Invalid action."); break; // bad action } // end case action
function ganglia_event_modify($event) { global $conf; if (!isset($event['event_id'])) { api_return_error("event_id not set"); } $db =& MDB2::factory($conf['overlay_events_dsn']); if (DB::isError($db)) { api_return_error($db->getMessage()); } $clauses = array(); if (isset($event['start_time'])) { if ($event['start_time'] == "now") { $start_time = time(); } else { if (is_numeric($event['start_time'])) { $start_time = $event['start_time']; } else { $start_time = strtotime($event['start_time']); } } $clauses[] = "start_time = " . $db->quote($start_time, 'integer'); } // end isset start_time foreach (array('cluster', 'description', 'summary', 'grid', 'host_regex') as $k) { if (isset($event[$k])) { $clauses[] = "{$k} = " . $db->quote($event[$k], 'text'); } } // end foreach if (isset($event['end_time'])) { $end_time = $event['end_time'] == "now" ? time() : strtotime($event['end_time']); $clauses[] = "end_time = " . $db->quote($end_time, 'integer'); } // end isset end_time $sql = "UPDATE overlay_events SET " . implode(",", $clauses) . " WHERE event_id = " . $db->quote($event['event_id'], 'integer'); $result =& $db->exec($sql); if (PEAR::isError($result)) { api_return_error($result->getMessage()); } $message = array("status" => "ok", "event_id" => $event['event_id']); return $message; }