Codebase list ganglia-web / 4de00969-2b19-4d94-8ad3-78e2e4c5fad7/main views_view.php
4de00969-2b19-4d94-8ad3-78e2e4c5fad7/main

Tree @4de00969-2b19-4d94-8ad3-78e2e4c5fad7/main (Download .tar.gz)

views_view.php @4de00969-2b19-4d94-8ad3-78e2e4c5fad7/mainraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
include_once("./eval_conf.php");
include_once("./functions.php");
include_once("./global.php");

if (! checkAccess(GangliaAcl::ALL_VIEWS, GangliaAcl::VIEW, $conf))
  die("You do not have access to view views.");

$view_name = NULL;

if (isset($_GET['vn']) && !is_proper_view_name($_GET['vn'])) {
?>
<div class="ui-widget">
  <div class="ui-state-default ui-corner-all" styledefault="padding: 0 .7em;">
    <p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span>
    View names valid characters are 0-9, a-z, A-Z, -, _ and space. View has not been created.</p>
  </div>
</div>
<?php
  exit(0);
} else {
  $view_name = sanitize($_GET['vn']);
}

function viewFileName($view_name) {
  global $conf;

  $view_suffix = str_replace(" ", "_", $view_name);
  return $conf['views_dir'] .
    "/view_" .
    preg_replace('/[^a-zA-Z0-9_\-]/', '', $view_suffix) . ".json";
}

$viewList = new ViewList();

///////////////////////////////////////////////////////////////////////////////
// Create new view
///////////////////////////////////////////////////////////////////////////////
if (isset($_GET['create_view'])) {
  if(! checkAccess(GangliaAcl::ALL_VIEWS, GangliaAcl::EDIT, $conf)) {
    $output = "You do not have access to edit views.";
  } else {
    if ($viewList->viewExists($view_name)) {
      $output = "<strong>Alert:</strong> View with the name " .
                $view_name .
                " already exists.";
    } else {
      $empty_view = array ("view_name" => $view_name,
                           "items" => array());
      $view_filename = viewFileName($view_name);
      if (pathinfo($view_filename, PATHINFO_DIRNAME) != $conf['views_dir']) {
        die('Invalid path detected');
      }

      $json = json_encode($empty_view);
      if (file_put_contents($view_filename,
                            json_prettyprint($json)) === FALSE) {
        $output = "<strong>Alert:</strong>" .
                  " Can't write to file " . htmlspecialchars($view_filename) .
                  " Perhaps permissions are wrong.";
      } else {
        $output = "View has been created successfully.";
      }
    }
  }
  $json = '{"output": "<div class=\"ui-widget\"><div class=\"ui-state-default ui-corner-all\" style=\"padding: 0 .7em;\"><p><span class=\"ui-icon ui-icon-alert\" style=\"float: left; margin-right: .3em;\"></span>' . $output . '</div></div>"';
  if ($conf['display_views_using_tree']) {
    $json .= ',"tree_node": {"text":"' . $view_name . '","id":"' . viewId($view_name) . '"';
    $json .= ',"view_name":"' . $view_name . '"';
    $json .= '}';
  }
  $json .= '}';
  echo $json;
  exit(0);
}

///////////////////////////////////////////////////////////////////////////////
// Delete view
///////////////////////////////////////////////////////////////////////////////
if (isset($_GET['delete_view'])) {
  if (! checkAccess(GangliaAcl::ALL_VIEWS, GangliaAcl::EDIT, $conf)) {
    $output = "You do not have access to edit views.";
  } else {
    if (!$viewList->viewExists($view_name)) {
      $output = "<strong>Alert:</strong> View with the name " .
      $view_name .
      " does not exist.";
    } else {
      $view_filename = viewFileName($view_name);
      if (pathinfo($view_filename, PATHINFO_DIRNAME) != $conf['views_dir']) {
        die('Invalid path detected');
      }

      if (unlink($view_filename) === FALSE) {
        $output = "<strong>Alert:</strong>" .
                  " Can't remove file $view_filename." .
                  " Perhaps permissions are wrong.";
      } else {
        $output = "View has been successfully removed.";
	$viewList->removeView($view_name);
      }
    }
  }
} // delete_view

///////////////////////////////////////////////////////////////////////////////
// Add to view
///////////////////////////////////////////////////////////////////////////////
if (isset($_GET['add_to_view'])) {
  if (! checkAccess(GangliaAcl::ALL_VIEWS, GangliaAcl::EDIT, $conf)) {
    $output = "You do not have access to edit views.";
  } else {
    if (!$viewList->viewExists($view_name)) {
      $output = "<strong>Alert:</strong> View " .
      $view_name .
      " does not exist. This should not happen.";
    } else {
      $view = $viewList->getView($view_name);

      # Check if we are adding an aggregate graph
      if (isset($_GET['aggregate'])) {
	foreach ($_GET['mreg'] as $key => $value)
	  $metric_regex_array[] = array("regex" => $value);

	foreach ($_GET['hreg'] as $key => $value)
	  $host_regex_array[] = array("regex" => $value);

	$item_array = array("aggregate_graph" => "true",
			    "metric_regex" => $metric_regex_array,
			    "host_regex" => $host_regex_array,
			    "graph_type" => stripslashes($_GET['gtype']),
			    "vertical_label" => stripslashes($_GET['vl']),
			    "title" => $_GET['title'],
			    "glegend" => $_GET['glegend']);

	if (isset($_GET['x']) && is_numeric($_GET['x'])) {
	  $item_array["upper_limit"] = $_GET['x'];
	}

	if (isset($_GET['n']) && is_numeric($_GET['n'])) {
	  $item_array["lower_limit"] = $_GET['n'];
	}
	if (isset($_GET['c'])) {
	  $item_array["cluster"] = $_GET['c'];
	}

	if (isset($_GET['h'])) {
	  $item_array['host'] = $_GET['h'];
	  unset($item_array['host_regex']);
	}
	if (isset($_GET['m'])) {
	  $item_array['metric'] = $_GET['m'];
	  unset($item_array['metric_regex']);
	}
	if (isset($_GET['g'])) {
	  $item_array['graph'] = $_GET['g'];
	}
	if ($item_array['host_regex'] == null)
	  $item_array['host_regex'] = '.*';

	$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"] = stripslashes($_GET['vertical_label']);
	  if (isset($_GET['title']))
            $items["title"] = stripslashes($_GET['title']);
	  if (isset($_GET['c']))
            $items["cluster"] = $_GET['c'];
          if (isset($_GET['warning']) && is_numeric($_GET['warning']))
            $items["warning"] = $_GET['warning'];
          if (isset($_GET['critical']) && is_numeric($_GET['critical']))
            $items["critical"] = $_GET['critical'];

	  $view['items'][] = $items;
	} else
	  $view['items'][] = array("hostname" => $_GET['host_name'],
                                   "graph" => $_GET['metric_name']);
      }

      $view_filename = $view['file_name'];
      // Remove the file_name attribute, it is not stored in the view defn.
      unset($view['file_name']);
      $json = json_encode($view);
      if (file_put_contents($view_filename,
                            json_prettyprint($json)) === FALSE ) {
        $output = "<strong>Alert:</strong>" .
	  " Can't write to file: \"$view_filename\"." .
	  " Perhaps permissions are wrong.";
      } else {
        $output = "View has been updated successfully.";
      }
    }
  }
?>
<div class="ui-widget">
  <div class="ui-state-default ui-corner-all" style="padding: 0 .7em;">
    <p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span>
    <?php echo $output ?></p>
  </div>
</div>
<?php
  exit(0);
}

class ViewTreeNode {
  private $name = NULL;
  private $data = NULL;
  private $children = NULL;
  private $parent = NULL;

  public function __construct($name) {
    $this->name = $name;
  }

  public function hasChild($name) {
    if ($this->children == NULL)
      return false;
    return array_key_exists($name, $this->children);
  }

  public function getChild($name) {
    return $this->children[$name];
  }

  public function getChildren() {
    return $this->children;
  }

  public function addChild($node) {
    if ($this->children == NULL)
      $this->children = array();
    $this->children[$node->getName()] = $node;
    $node->setParent($this);
    return $node;
  }

  public function getNode($path) {
    $parent = $this;
    foreach (explode("/", $path) as $node_name) {
      if ($parent->hasChild($node_name)) {
	$parent = $parent->getChild($node_name);
      } else {
	$parent = $parent->addChild(new ViewTreeNode($node_name));
      }
    }
    return $parent;
  }

  public function getData() {
    return $this->data;
  }

  public function setData($data) {
    $this->data = $data;
  }

  public function getName() {
    return $this->name;
  }

  public function getParent() {
    return $this->parent;
  }

  public function setParent($parent) {
    $this->parent = $parent;
  }

  public function toJson($initially_open) {
    $pathName = $this->getPathName();
    $id = viewId($pathName);
    $json = '{"text":"' . $this->name . '","id":"' . $id . '"';
    if ($this->data != NULL)
      $json .= ',"view_name":"' . $pathName . '"';

    if ($initially_open && in_array($pathName, $initially_open))
      $json .= ',"state":{"opened": true}';

    if ($this->children != NULL) {
      $json .= ',"children":[';
      $i = 0;
      foreach ($this->children as $child_node) {
	if ($i++ > 0) $json .= ',';
	$json .= $child_node->toJson($initially_open);
      }
      $json .= ']';
    }
    $json .= '}';
    return $json;
  }

  function getPathName() {
    global $VIEW_NAME_SEP;

    if ($this->parent == NULL)
      return $this->name;

    $pathName = $this->name;
    $parent = $this->parent;
    while ($parent->getParent() != NULL) {
      $pathName = $parent->getName() . $VIEW_NAME_SEP . $pathName;
      $parent = $parent->getParent();
    }
    return $pathName;
  }
}

function build_view_tree($views) {
  $view_tree = new ViewTreeNode('root');
  foreach ($views as $view) {
    $node = new ViewTreeNode($view['view_name']);
    $node->setData($view);
    if ($view['parent'] != NULL) {
      $parent = $view_tree->getNode($view['parent']);
      $parent->addChild($node);
    } else {
      $view_tree->addChild($node);
    }
  }
  return $view_tree;
}

function getViewSelectors($viewList,
                          $display_views_using_tree,
                          $view_tree_nodes_initially_open,
                          $selected_view) {
  $existing_views = '';
  if ($display_views_using_tree) {
    $initially_open = NULL;
    if (!isset($_SESSION['view_tree_built']) &&
        isset($view_tree_nodes_initially_open))
      $initially_open = $view_tree_nodes_initially_open;

    $view_tree = build_view_tree($viewList->getViews());
    $existing_views = '[';
    $i = 0;
    foreach ($view_tree->getChildren() as $view_node) {
      if ($i++ > 0) $existing_views .= ',';
      $existing_views .= $view_node->toJson($initially_open);
      $i++;
    }
    $existing_views .= ']';
    $_SESSION['view_tree_built'] = TRUE;
  } else {
    foreach ($viewList->getViews() as $view) {
      if ($view['parent'] == NULL) {
        $v = $view['view_name'];
        $vid = viewId($v);
        $checked = ($selected_view == $v);
        $existing_views .= '<input type="radio" id="' . $vid . '" name="views_menu_button_group" onchange="selectView(\'' . $v . '\'); return false;"' . ($checked ? " checked" : "") . '><label style="text-align:left;" class="nobr" for="' . $vid . '">' . $v . '</label>';
      }
    }
  }
  return $existing_views;
}

$existing_views = getViewSelectors($viewList,
                                   $conf['display_views_using_tree'],
                                   $conf['view_tree_nodes_initially_open'],
                                   $_GET['vn']);
if (isset($_GET['views_menu'])) {
  if (!$conf['display_views_using_tree']) {
    echo $existing_views;
  }
  exit(0);
}

$tpl = new Dwoo_Template_File( template("views_view.tpl") );
$data = new Dwoo_Data();
$data->assign("range", $range);

if (isset($conf['ad-hoc-views']) &&
    $conf['ad-hoc-views'] === true &&
    isset($_GET['ad-hoc-view'])) {
  $is_ad_hoc = true;
}

// Pop up a warning message if there are no available views
// (Disable temporarily, otherwise we can't create views)
if (count($viewList->getViews()) == -1 && !$is_ad_hoc) {
  $error_msg = '
    <div class="ui-widget">
      <div class="ui-state-error ui-corner-all" style="padding: 0 .7em;">
        <p><span class="ui-icon ui-icon-alert"
                 style="float: left; margin-right: .3em;"></span>
	   <strong>Alert:</strong> There are no views defined.</p>
      </div>
    </div>';
}

$size = isset($clustergraphsize) ? $clustergraphsize : 'default';

// set to 'default' to preserve old behavior
if ($size == 'medium')
  $size = 'default';

$additional_host_img_css_classes = "";
if (isset($conf['zoom_support']) && $conf['zoom_support'] === true)
  $additional_host_img_css_classes = "host_${size}_zoomable";

$data->assign("additional_host_img_css_classes",
              $additional_host_img_css_classes);

$data->assign("existing_views", $existing_views);
$data->assign("view_name", $view_name);
$data->assign("display_views_using_tree", $conf["display_views_using_tree"]);

$view_items = NULL;
if ($is_ad_hoc) {
  $data->assign("ad_hoc_view", true);
  $data->assign("ad_hoc_view_json", rawurlencode($_GET['ad-hoc-view']));
  $ad_hoc_view_json = json_decode(heuristic_urldecode($_GET['ad-hoc-view']),
				  true);
  $view_items = getViewItems($ad_hoc_view_json, $range, $cs, $ce);
} else {
  $view = $viewList->getView($view_name);
  if ($view != NULL) {
    $view_items = getViewItems($view, $range, $cs, $ce);
    if ($view['common_y_axis'] != 0)
      $data->assign("common_y_axis", 1);
  }
}

if (isset($view_items)) {
  $data->assign("view_items", $view_items);
  $data->assign("number_of_view_items", count($view_items));
}

$data->assign('GRAPH_BASE_ID', $GRAPH_BASE_ID);
$data->assign('SHOW_EVENTS_BASE_ID', $SHOW_EVENTS_BASE_ID);
$data->assign('graph_engine', $conf['graph_engine']);
$data->assign('flot_graph', isset($conf['flot_graph']) ? true : null);
$dwoo->output($tpl, $data);

?>