Skip to content

Commit

Permalink
Merge pull request vvuksan#100 from jbuchbinder/master
Browse files Browse the repository at this point in the history
Implement driver segmentation for events
  • Loading branch information
vvuksan committed Dec 18, 2011
2 parents cd6d455 + ca9eaaf commit 8ceb716
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 100 deletions.
73 changes: 5 additions & 68 deletions ganglia-web/api/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
$conf['ganglia_dir'] = dirname(dirname(__FILE__));

include_once $conf['ganglia_dir'] . "/eval_conf.php";
include_once $conf['ganglia_dir'] . "/lib/function.php";
include_once $conf['ganglia_dir'] . "/lib/common_api.php";

if ( ! $conf['overlay_events'] ) {
Expand All @@ -31,14 +32,9 @@
api_return_error( "Error: You need to specify an action at a minimum" );
}

$events_json = file_get_contents($conf['overlay_events_file']);

$events_array = json_decode($events_json, TRUE);

switch ( $_GET['action'] ) {

case "add":

if ( ! isset($_GET['start_time']) || ! isset($_GET['summary']) || ! isset($_GET['host_regex']) ) {
api_return_error( "Error: You need to supply start_time, summary, host_regex at a minimum" );
}
Expand All @@ -64,78 +60,19 @@

if ( isset($_GET['end_time']) )
$event['end_time'] = $_GET['end_time'] == "now" ? time() : strtotime($_GET['end_time']);

$events_array[] = $event;

$json = json_encode($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", "event_id" => $event_id);
}

$message = ganglia_events_add( $event );
break;

case "edit":

$event_found = 0;
if ( isset($_GET['event_id']) ) {
foreach ( $events_array as $key => $event ) {
if ( $event['event_id'] == $_GET['event_id'] ) {
$event_found = 1;

// Modify the event here

if (isset( $_GET['start_time'] )) {
if ( $_GET['start_time'] == "now" )
$event['start_time'] = time();
else if ( is_numeric($_GET['start_time']) )
$event['start_time'] = $_GET['start_time'];
else
$event['start_time'] = strtotime($_GET['start_time']);
}
foreach(array('cluster', 'description', 'summary', 'grid', 'host_regex') AS $k) {
if (isset( $_GET[$k] )) {
$event[$k] = $_GET[$k];
}
}
if ( isset($_GET['end_time']) ) {
$event['end_time'] = $_GET['end_time'] == "now" ? time() : strtotime($_GET['end_time']);
}

} // end found event

// Add either original or modified event back in
$new_events_array[] = $event;

} // end of foreach ( $events_array as $key => $event

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 . " removed successfully" );
}

} else {
api_return_error( "Event ID ". $event_id . " not found" );
}

unset($new_events_array);

} else {
api_return_error( "No event_id has been supplied." );
}

$message = ganglia_event_modify( $_GET );
break;

case "remove":
case "delete":

$events_array = ganglia_events_get();

$event_found = 0;
if ( isset($_GET['event_id']) ) {
foreach ( $events_array as $key => $event ) {
Expand Down
4 changes: 1 addition & 3 deletions ganglia-web/api/views.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
api_return_error( "Error: You need to specify an action at a minimum" );
}

$events_json = file_get_contents($conf['overlay_events_file']);

$events_array = json_decode($events_json, TRUE);
$events_array = ganglia_events_get();

if( ! checkAccess(GangliaAcl::ALL_VIEWS, GangliaAcl::VIEW, $conf) ) {
api_return_error("You do not have access to view views.");
Expand Down
8 changes: 8 additions & 0 deletions ganglia-web/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@

include_once ( dirname(__FILE__) . "/lib/json.php" );

#
# Load event API driver.
#
$driver = ucfirst(strtolower( !isset($conf['overlay_events_provider']) ? "Json" : $conf['overlay_events_provider'] ));
if (file_exists( dirname(__FILE__) . "/lib/Events/Driver_${driver}.php")) {
include_once( dirname(__FILE__) . "/lib/Events/Driver_${driver}.php" );
}

#------------------------------------------------------------------------------
# Allows a form of inheritance for template files.
# If a file does not exist in the chosen template, the
Expand Down
3 changes: 1 addition & 2 deletions ganglia-web/graph.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -639,8 +639,7 @@
//////////////////////////////////////////////////////////////////////////////
if ( $conf['overlay_events'] && $conf['graph_engine'] == "rrdtool" && ! in_array($range, $conf['overlay_events_exclude_ranges']) ) {

$events_json = file_get_contents($conf['overlay_events_file']);
$events_array = json_decode($events_json, TRUE);
$events_array = ganglia_events_get();

if (!empty($events_array)) {

Expand Down
27 changes: 0 additions & 27 deletions ganglia-web/lib/Events/Driver_JSON.php

This file was deleted.

77 changes: 77 additions & 0 deletions ganglia-web/lib/Events/Driver_Json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

$conf['ganglia_dir'] = dirname(dirname(dirname(__FILE__)));

include_once $conf['ganglia_dir'] . "/eval_conf.php";
include_once $conf['ganglia_dir'] . "/lib/common_api.php";

function ganglia_events_add( $event ) {
$events_array = ganglia_events_get();
$events_array[] = $event;
$json = json_encode($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", "event_id" => $event_id);
}
return $message;
} // end method ganglia_events_add

function ganglia_events_get() {
global $conf;
$events_json = file_get_contents($conf['overlay_events_file']);
$events_array = json_decode($events_json, TRUE);
return $events_array;
} // end method ganglia_events_get

function ganglia_event_modify( $event ) {
global $conf;
$event_found = 0;
$events_array = ganglia_events_get();
$new_events_array = array();

if (isset($event['id'])) {
api_return_error( "Event ID not found" );
} // isset event_id

foreach ( $events_array AS $k => $e ) {
if ( $e['event_id'] == $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 . " removed successfully" );
}
} // end if event_found

return $message;
} // end method ganglia_event_modify

?>

0 comments on commit 8ceb716

Please sign in to comment.