forked from ganglia/ganglia-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.php
1424 lines (1208 loc) · 51.5 KB
/
graph.php
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// vim: tabstop=2:softtabstop=2:shiftwidth=2:expandtab
include_once "./eval_conf.php";
include_once "./get_context.php";
include_once "./functions.php";
///////////////////////////////////////////////////////////////////////////////
// Populate $rrdtool_graph from $config (from JSON file).
///////////////////////////////////////////////////////////////////////////////
function build_rrdtool_args_from_json( &$rrdtool_graph, $graph_config ) {
global $context, $hostname, $range, $rrd_dir, $size, $conf;
if ($conf['strip_domainname']) {
$hostname = strip_domainname($hostname);
}
$title = sanitize( $graph_config[ 'title' ] );
$rrdtool_graph[ 'title' ] = $title;
// If vertical label is empty or non-existent set it to space otherwise
// rrdtool will fail
if ( ! isset($graph_config[ 'vertical_label' ]) ||
$graph_config[ 'vertical_label' ] == "" ) {
$rrdtool_graph[ 'vertical-label' ] = " ";
} else {
$rrdtool_graph[ 'vertical-label' ] =
sanitize( $graph_config[ 'vertical_label' ] );
}
$rrdtool_graph['lower-limit'] = '0';
if( isset($graph_config['height_adjustment']) ) {
$rrdtool_graph['height'] +=
($size == 'medium') ? $graph_config['height_adjustment'] : 0;
} else {
$rrdtool_graph['height'] += ($size == 'medium') ? 28 : 0;
}
// find longest label length, so we pad the others accordingly to get
// consistent column alignment
$max_label_length = 0;
foreach( $graph_config[ 'series' ] as $item ) {
$max_label_length = max( strlen( $item[ 'label' ] ), $max_label_length );
}
$series = '';
$cdef = '';
$graphdef = '';
$stack_counter = 0;
// Available line types
$line_widths = array("1","2","3");
$total_ids = array();
// Loop through all the graph items
foreach( $graph_config[ 'series' ] as $index => $item ) {
// ignore item if context is not defined in json template
if ( isSet($item[ 'contexts' ]) and
in_array($context, $item['contexts']) == false )
continue;
$rrd_dir = $conf['rrds'] . "/" . $item['clustername'] . "/" . $item['hostname'];
$metric = sanitize( $item[ 'metric' ] );
$metric_file = $rrd_dir . "/" . $metric . ".rrd";
// Make sure metric file exists. Otherwise we'll get a broken graph
if ( is_file($metric_file) ) {
// Need this when defining graphs that may use same metric names
$unique_id = "a" . $index;
$total_ids[] = $unique_id;
# Pad the label with spaces
$label = str_pad( sanitize( $item[ 'label' ] ), $max_label_length );
// use custom DS defined in json template if it's
// defined (default = 'sum')
$DS = "sum";
if ( isset($item[ 'ds' ]) )
$DS = sanitize( $item[ 'ds' ] );
$series .= " DEF:'$unique_id'='$metric_file':'$DS':AVERAGE ";
if (isset($graph_config['scale'])) {
$cdef .= " CDEF:'s${unique_id}'=${unique_id},${graph_config['scale']},* ";
}
if (isset($graph_config['percent']) && $graph_config['percent'] == '1') {
$cdef .= " CDEF:'p${unique_id}'=${unique_id},total,/,100,* ";
}
// By default graph is a line graph
isset( $item['type']) ?
$item_type = $item['type'] : $item_type = "line";
// TODO sanitize color
switch ( $item_type ) {
case "line":
// Make sure it's a recognized line type
isset($item['line_width']) &&
in_array( $item['line_width'], $line_widths) ?
$line_width = $item['line_width'] : $line_width = "1";
$graphdef .= "LINE" . $line_width;
if (isset($graph_config['percent']) && $graph_config['percent'] == '1') {
$graphdef .= ":'p${unique_id}'#${item['color']}:'${label}' ";
} else if (isset($graph_config['scale'])) {
$graphdef .= ":'s${unique_id}'#${item['color']}:'${label}' ";
} else {
$graphdef .= ":'$unique_id'#${item['color']}:'${label}' ";
}
break;
case "stack":
case "percent":
// First element in a stack has to be AREA
if ( $stack_counter == 0 ) {
$graphdef .= "AREA";
$stack_counter++;
} else {
$graphdef .= "STACK";
}
if (isset($graph_config['percent']) && $graph_config['percent'] == '1') {
$graphdef .= ":'p${unique_id}'#${item['color']}:'${label}' ";
} else if (isset($graph_config['scale'])) {
$graphdef .= ":'s${unique_id}'#${item['color']}:'${label}' ";
} else {
$graphdef .= ":'$unique_id'#${item['color']}:'${label}' ";
}
break;
// Percentile lines
case "percentile":
$percentile = isset($item['percentile']) ? floatval($item['percentile']): 95;
$graphdef .= "VDEF:t${unique_id}=${unique_id},${percentile},PERCENT ";
isset($item['line_width']) && in_array( $item['line_width'], $line_widths) ?
$line_width = $item['line_width'] : $line_width = "1";
$graphdef .= "LINE" . $line_width . ":'t$unique_id'#{$item['color']}:'{$label}':dashes ";
break;
case "area":
$graphdef .= "AREA";
$graphdef .= ":'$unique_id'#${item['color']}:'${label}' ";
break;
} // end of switch ( $item_type )
if ( $conf['graphreport_stats'] ) {
if (isset($graph_config['percent']) && $graph_config['percent'] == '1') {
$graphdef .= legendEntry('p' . $unique_id, $conf['graphreport_stat_items']);
} else if (isset($graph_config['scale'])) {
$graphdef .= legendEntry('s' . $unique_id, $conf['graphreport_stat_items']);
} else {
$graphdef .= legendEntry($unique_id, $conf['graphreport_stat_items']);
}
}
} // end of if ( is_file($metric_file) ) {
} // end of foreach( $graph_config[ 'series' ] as $index => $item )
// Percentage calculation for cdefs, if required
//if (isset($graph_config['percent']) && $graph_config['percent'] == '1') {
$total = " CDEF:'total'=";
if (count($total_ids) == 0) {
// Handle nothing gracefully, do nothing
} else if (count($total_ids) == 1) {
// Concat just that id, leave it at that (100%)
$total .= $total_ids[0];
if (isset($graph_config['scale'])) {
$total .= ",${graph_config['scale']},*";
}
$cdef = $total . ' ' . $cdef;
} else {
$total .= $total_ids[0];
for ($i=1; $i<count($total_ids); $i++) {
$total .= ',' . $total_ids[$i] . ',ADDNAN';
}
if (isset($graph_config['scale'])) {
$total .= ",${graph_config['scale']},*";
}
// Prepend total calculation
$cdef = $total . ', ' . $cdef;
}
//} // if graph_config['percent']
if ( isset($graph_config['show_total']) && $graph_config['show_total'] == 1 ) {
$cdef .= " LINE1:'total'#000000:'Total' " . legendEntry('total', $conf['graphreport_stat_items']);
}
// If we end up with the empty series it means that no RRD files matched.
// This can happen if we are trying to create a report and metrics for
// this host were not collected. If that happens we should create an
// empty graph
if ( $series == "" ) {
$rrdtool_graph[ 'series' ] =
'HRULE:1#FFCC33:"No matching metrics detected"';
} else {
$rrdtool_graph[ 'series' ] = $series . ' ' . $cdef . ' ' . $graphdef;
}
return $rrdtool_graph;
}
///////////////////////////////////////////////////////////////////////////////
// Graphite graphs
///////////////////////////////////////////////////////////////////////////////
function build_graphite_series( $config, $host_cluster = "" ) {
global $context;
$targets = array();
$colors = array();
// Keep track of stacked items
$stacked = 0;
foreach( $config[ 'series' ] as $item ) {
if ( isSet($item[ 'contexts' ]) and in_array($context, $item['contexts'])==false )
continue;
if ( $item['type'] == "stack" )
$stacked++;
if ( isset($item['hostname']) && isset($item['clustername']) ) {
$host_cluster = $item['clustername'] . "." . str_replace(".","_", $item['hostname']);
}
$targets[] = "target=". urlencode( "alias($host_cluster.${item['metric']}.sum,'${item['label']}')" );
$colors[] = $item['color'];
}
$output = implode( $targets, '&' );
$output .= "&colorList=" . implode( $colors, ',' );
$output .= "&vtitle=" . urlencode( isset($config[ 'vertical_label' ]) ? $config[ 'vertical_label' ] : "" );
// Do we have any stacked elements. We assume if there is only one element
// that is stacked that rest of it is line graphs
if ( $stacked > 0 ) {
if ( $stacked > 1 )
$output .= "&areaMode=stacked";
else
$output .= "&areaMode=first";
}
return $output;
}
function build_value_for_json( $value ) {
if ( is_numeric( $value ) )
$val = floatval($value);
else
$val = $value;
return $val;
}
$gweb_root = dirname(__FILE__);
# RFM - Added all the isset() tests to eliminate "undefined index"
# messages in ssl_error_log.
# Graph specific variables
# ATD - No need for escapeshellcmd or rawurldecode on $size or $graph. Not used directly in rrdtool calls.
$size = isset($_GET["z"]) &&
in_array($_GET[ 'z' ], $conf['graph_sizes_keys']) ? $_GET["z"] : NULL;
$metric_name = isset($_GET["m"]) ? sanitize ( $_GET["m"] ) : NULL;
# In clusterview we may supply report as metric name so let's make sure it
# doesn't treat it as a metric
if ( preg_match("/_report$/", $metric_name) && !isset($_GET["g"]) ) {
$graph = $metric_name;
} else {
# If graph arg is not specified default to metric
$graph = isset($_GET["g"]) ? sanitize ( $_GET["g"] ) : "metric";
}
$graph_arguments = array();
if ($conf['enable_pass_in_arguments_to_optional_graphs']) {
$pos = strpos($graph, ",");
if ($pos !== FALSE) {
$graph_report = substr($graph, 0, $pos);
$args = str_getcsv(substr($graph, $pos + 1), ",", "'");
/*
ob_start();
var_dump($args);
$result = ob_get_clean();
error_log("args = $result");
*/
foreach ($args as $arg) {
if (is_numeric($arg)) {
if (ctype_digit($arg))
$graph_arguments[] = intval($arg);
else
$graph_arguments[] = floatval($arg);
} else
$graph_arguments[] = $arg;
}
$graph = $graph_report;
}
}
$grid = isset($_GET["G"]) ? sanitize( $_GET["G"]) : NULL;
$self = isset($_GET["me"]) ? sanitize( $_GET["me"]) : NULL;
$vlabel = isset($_GET["vl"]) ? sanitize($_GET["vl"]) : NULL;
$graph_scale = isset($_GET["gs"]) ? sanitize($_GET["gs"]) : NULL;
$scale = isset($_GET["scale"]) ? sanitize($_GET["scale"]) : NULL;
$show_total = isset($_GET["show_total"]) ? sanitize($_GET["show_total"]) : NULL;
$value = isset($_GET["v"]) ? sanitize ($_GET["v"]) : NULL;
# Max, min, critical and warning values
$max = isset($_GET["x"]) && is_numeric($_GET["x"]) ? $_GET["x"] : NULL;
$min = isset($_GET["n"]) && is_numeric($_GET["n"]) ? $_GET["n"] : NULL;
$critical = isset($_GET["crit"]) && is_numeric($_GET["crit"]) ? $_GET["crit"] : NULL;
$warning = isset($_GET["warn"]) && is_numeric($_GET["warn"]) ? $_GET["warn"] : NULL;
$sourcetime = isset($_GET["st"]) ? clean_number(sanitize($_GET["st"])) : NULL;
$load_color = isset($_GET["l"]) &&
is_valid_hex_color(rawurldecode($_GET['l'])) ?
sanitize($_GET["l"]) : NULL;
$summary = isset($_GET["su"]) ? 1 : 0;
$debug = isset($_GET['debug']) ? clean_number(sanitize($_GET["debug"])) : 0;
$showEvents = isset($_GET["event"]) ? sanitize ($_GET["event"]) : "show";
$user['time_shift'] = isset($_GET["ts"]) ? clean_number(sanitize($_GET["ts"])) : 0;
#
$user['view_name'] = isset($_GET["vn"]) ? sanitize ($_GET["vn"]) : NULL;
$user['item_id'] = isset($_GET["item_id"]) ? sanitize ($_GET["item_id"]) : NULL;
$command = '';
$graphite_url = '';
$user['json_output'] = isset($_GET["json"]) ? 1 : NULL;
# Request for live dashboard
if ( isset($_REQUEST['live']) ) {
$user['live_dashboard'] = 1;
$user['json_output'] = 1;
} else {
$user['live_output'] = NULL;
}
$user['csv_output'] = isset($_GET["csv"]) ? 1 : NULL;
$user['graphlot_output'] = isset($_GET["graphlot"]) ? 1 : NULL;
$user['flot_output'] = isset($_GET["flot"]) ? 1 : NULL;
$user['trend_line'] = isset($_GET["trend"]) ? 1 : NULL;
# How many months ahead to extend the trend e.g. 6 months
$user['trend_range'] = isset($_GET["trendrange"]) && is_numeric($_GET["trendrange"]) ? $_GET["trendrange"] : 6;
#
$user['trend_history'] = isset($_GET["trendhistory"]) && is_numeric($_GET["trendhistory"]) ? $_GET["trendhistory"] : 6;
// Get hostname
$raw_host = isset($_GET["h"]) ? sanitize($_GET["h"]) : "__SummaryInfo__";
// For graphite purposes we need to replace all dots with underscore. dot is
// separates subtrees in graphite
$host = str_replace(".","_", $raw_host);
# Assumes we have a $start variable (set in get_context.php).
# $conf['graph_sizes'] and $conf['graph_sizes_keys'] defined in conf.php.
# Add custom sizes there.
$size = in_array($size, $conf['graph_sizes_keys']) ? $size : 'default';
if (isset($_GET['height']) && is_numeric($_GET['height']))
$height = $_GET['height'];
else
$height = $conf['graph_sizes'][$size]['height'];
if (isset($_GET['width']) && is_numeric($_GET['width']))
$width = $_GET['width'];
else
$width = $conf['graph_sizes'][$size]['width'];
#$height = $conf['graph_sizes'][$size]['height'];
#$width = $conf['graph_sizes'][$size]['width'];
$fudge_0 = $conf['graph_sizes'][$size]['fudge_0'];
$fudge_1 = $conf['graph_sizes'][$size]['fudge_1'];
$fudge_2 = $conf['graph_sizes'][$size]['fudge_2'];
# Aliases for $user['cs'] and $user['ce'] (which are set in get_context.php).
$cs = $user['cs'];
$ce = $user['ce'];
///////////////////////////////////////////////////////////////////////////
// Set some variables depending on the context. Context is set in
// get_context.php
///////////////////////////////////////////////////////////////////////////
switch ($context)
{
case "meta":
$rrd_dir = $conf['rrds'] . "/__SummaryInfo__";
$rrd_graphite_link = $conf['graphite_rrd_dir'] . "/__SummaryInfo__";
$title = "$self ${conf['meta_designator']}";
break;
case "grid":
$rrd_dir = $conf['rrds'] . "/$grid/__SummaryInfo__";
$rrd_graphite_link = $conf['graphite_rrd_dir'] . "/$grid/__SummaryInfo__";
if (preg_match('/grid/i', $gridname))
$title = $gridname;
else
$title = "$gridname ${conf['meta_designator']}";
break;
case "cluster":
$rrd_dir = $conf['rrds'] . "/$clustername/__SummaryInfo__";
$rrd_graphite_link = $conf['graphite_rrd_dir'] . "/$clustername/__SummaryInfo__";
if (preg_match('/cluster/i', $clustername))
$title = $clustername;
else
$title = "$clustername Cluster";
break;
case "host":
$rrd_dir = $conf['rrds'] . "/$clustername/$raw_host";
$rrd_graphite_link = $conf['graphite_rrd_dir'] . "/" . $clustername . "/" . $host;
// Add hostname to report graphs' title in host view
if ($graph != 'metric')
if ($conf['strip_domainname'])
$title = strip_domainname($raw_host);
else
$title = $raw_host;
break;
default:
break;
}
$resource = GangliaAcl::ALL_CLUSTERS;
if( $context == "grid" ) {
$resource = $grid;
} else if ( $context == "cluster" || $context == "host" ) {
$resource = $clustername;
}
if( ! checkAccess( $resource, GangliaAcl::VIEW, $conf ) ) {
header( "HTTP/1.1 403 Access Denied" );
header ("Content-type: image/jpg");
echo file_get_contents( $gweb_root.'/img/access-denied.jpg');
die();
}
if ($cs and (is_numeric($cs) or strtotime($cs)))
$start = $cs;
if ($ce and (is_numeric($ce) or strtotime($ce)))
$end = $ce;
# Set some standard defaults that don't need to change much
$rrdtool_graph = array(
'start' => $start,
'end' => $end,
'width' => $width,
'height' => $height,
);
# automatically strip domainname from small graphs where it won't fit
if ($size == "small") {
$conf['strip_domainname'] = true;
# Let load coloring work for little reports in the host list.
}
if (! isset($subtitle) and $load_color)
$rrdtool_graph['color'] = "BACK#'$load_color'";
if ($debug) {
error_log("Graph [$graph] in context [$context]");
}
/* If we have $graph, then a specific report was requested, such as "network_report" or
* "cpu_report. These graphs usually have some special logic and custom handling required,
* instead of simply plotting a single metric. If $graph is not set, then we are (hopefully),
* plotting a single metric, and will use the commands in the metric.php file.
*
* With modular graphs, we look for a "${graph}.php" file, and if it exists, we
* source it, and call a pre-defined function name. The current scheme for the function
* names is: 'graph_' + <name_of_report>. So a 'cpu_report' would call graph_cpu_report(),
* which would be found in the cpu_report.php file.
*
* These functions take the $rrdtool_graph array as an argument. This variable is
* PASSED BY REFERENCE, and will be modified by the various functions. Each key/value
* pair represents an option/argument, as passed to the rrdtool program. Thus,
* $rrdtool_graph['title'] will refer to the --title option for rrdtool, and pass the array
* value accordingly.
*
* There are two exceptions to: the 'extras' and 'series' keys in $rrdtool_graph. These are
* assigned to $extras and $series respectively, and are treated specially. $series will contain
* the various DEF, CDEF, RULE, LINE, AREA, etc statements that actually plot the charts. The
* rrdtool program requires that this come *last* in the argument string; we make sure that it
* is put in it's proper place. The $extras variable is used for other arguemnts that may not
* fit nicely for other reasons. Complicated requests for --color, or adding --ridgid, for example.
* It is simply a way for the graph writer to add an arbitrary options when calling rrdtool, and to
* forcibly override other settings, since rrdtool will use the last version of an option passed.
* (For example, if you call 'rrdtool' with two --title statements, the second one will be used.)
*
* See ${conf['graphdir']}/sample.php for more documentation, and details on the
* common variables passed and used.
*/
// Calculate time range.
if ($sourcetime)
{
$end = $sourcetime;
# Get_context makes start negative.
$start = $sourcetime + $start;
}
// Fix from Phil Radden, but step is not always 15 anymore.
if ($range == "month")
$rrdtool_graph['end'] = floor($rrdtool_graph['end'] / 672) * 672;
///////////////////////////////////////////////////////////////////////////////
// Are we generating aggregate graphs
///////////////////////////////////////////////////////////////////////////////
if ( isset( $_GET["aggregate"] ) && $_GET['aggregate'] == 1 ) {
// Set start time
$start = time() + $start;
// If graph type is not specified default to line graph
if ( isset($_GET["gtype"]) && in_array($_GET["gtype"], array("stack","line","percent") ) )
$graph_type = $_GET["gtype"];
else
$graph_type = "line";
// If line width not specified default to 2
if ( isset($_GET["lw"]) && in_array($_GET["lw"], array("1","2", "3") ) )
$line_width = $_GET["lw"];
else
$line_width = "2";
if ( isset($_GET["glegend"]) && in_array($_GET["glegend"], array("show", "hide") ) )
$graph_legend = $_GET["glegend"];
else
$graph_legend = "show";
/////////////////////////////////////////////////////////////////////////////
// In order to reduce the load on the machine when someone is doing host
// compare we look whether host list has been supplied via hl arg.
// That way we do not have get metric cache
/////////////////////////////////////////////////////////////////////////////
if ( isset($_GET['hl']) ) {
$counter = 0;
$color_count = sizeof($conf['graph_colors']);
$metric_name = str_replace("$", "", str_replace("^", "", $_GET['mreg'][0]));
$host_list = explode(",", $_GET['hl']);
foreach ( $host_list as $index => $host_cluster ) {
$color_index = $counter % $color_count;
$parts = explode("|", $host_cluster);
$hostname = $parts[0];
$clustername = $parts[1];
$series = array("hostname" => $hostname,
"clustername" => $clustername,
"fill" => "true",
"metric" => $metric_name,
"color" => $conf['graph_colors'][$color_index],
"label" => $hostname,
"type" => $graph_type);
if ($graph_type == "line" || $graph_type == "area") {
$series['line_width'] = $line_width;
} else if ($graph_type == "percent") {
$graph_config['percent'] = "1";
} else {
$series['stack'] = "1";
}
$graph_config['series'][] = $series;
$counter++;
} // end of foreach ( $host_list as
} else {
$exclude_host_from_legend_label =
(array_key_exists('lgnd_xh', $_GET) &&
$_GET['lgnd_xh'] == "true") ? TRUE : FALSE;
$graph_config = build_aggregate_graph_config ($graph_type,
$line_width,
$_GET['hreg'],
$_GET['mreg'],
$graph_legend,
$exclude_host_from_legend_label);
}
// Set up
$graph_config["report_type"] = "standard";
$graph_config["vertical_label"] = $vlabel;
$graph_config["graph_scale"] = $graph_scale;
$graph_config["scale"] = $scale;
$graph_config["show_total"] = $show_total;
// Reset graph title
if ( isset($_GET['title']) && $_GET['title'] != "") {
$title = "";
$graph_config["title"] = sanitize($_GET['title']);
} else {
$title = "Aggregate";
}
}
///////////////////////////////////////////////////////////////////////////
// Composite graphs/reports specified in a view
///////////////////////////////////////////////////////////////////////////
if ( $user['view_name'] and $user['item_id'] ) {
$available_views = get_available_views();
foreach ( $available_views as $id => $view ) {
# Find view settings
if ( $user['view_name'] == $view['view_name'] )
break;
}
unset($available_views);
foreach ( $view['items'] as $index => $graph_config ) {
if ( $user['item_id'] == $graph_config['item_id'] )
break;
}
unset($view);
$title = "";
build_rrdtool_args_from_json ( $rrdtool_graph, $graph_config );
}
//////////////////////////////////////////////////////////////////////////////
// Check what graph engine we are using
//////////////////////////////////////////////////////////////////////////////
switch ( $conf['graph_engine'] ) {
case "flot":
case "rrdtool":
if ( ! isset($graph_config) ) {
if ( ($graph == "metric") &&
isset($_GET['title']) &&
$_GET['title'] !== '')
$metrictitle = sanitize($_GET['title']);
$php_report_file = $conf['graphdir'] . "/" . $graph . ".php";
$json_report_file = $conf['graphdir'] . "/" . $graph . ".json";
if( is_file( $php_report_file ) ) {
# Check for path traversal issues by making sure real path is actually in graphdir
if ( dirname(realpath($php_report_file)) != $conf['graphdir'] ) {
$rrdtool_graph[ 'series' ] = 'HRULE:1#FFCC33:"Check \$conf[graphdir] should not be relative path"';
} else {
include_once $php_report_file;
$graph_function = "graph_${graph}";
if ($conf['enable_pass_in_arguments_to_optional_graphs'] &&
count($graph_arguments)) {
$rrdtool_graph['arguments'] = $graph_arguments;
// Pass by reference call, $rrdtool_graph modified inplace
$graph_function($rrdtool_graph);
unset($rrdtool_graph['arguments']);
} else {
$graph_function($rrdtool_graph);
}
}
} else if ( is_file( $json_report_file ) ) {
if ( dirname(realpath($json_report_file)) != $conf['graphdir'] ) {
$rrdtool_graph[ 'series' ] = 'HRULE:1#FFCC33:"Check \$conf[graphdir] should not be relative path"';
} else {
$graph_config = json_decode( file_get_contents( $json_report_file ), TRUE );
# We need to add hostname and clustername if it's not specified
foreach ( $graph_config['series'] as $index => $item ) {
if ( ! isset($graph_config['series'][$index]['hostname'])) {
$graph_config['series'][$index]['hostname'] = $raw_host;
if (isset($grid))
$graph_config['series'][$index]['clustername'] = $grid;
else
$graph_config['series'][$index]['clustername'] = $clustername;
}
}
build_rrdtool_args_from_json ( $rrdtool_graph, $graph_config );
}
}
} else {
build_rrdtool_args_from_json ( $rrdtool_graph, $graph_config );
}
// We must have a 'series' value, or this is all for naught
if (!array_key_exists('series', $rrdtool_graph) ||
!strlen($rrdtool_graph['series']) ) {
$rrdtool_graph[ 'series' ] =
'HRULE:1#FFCC33:"Empty RRDtool command. Likely bad graph config"';
}
# Make small graphs (host list) cleaner by removing the too-big
# legend: it is displayed above on larger cluster summary graphs.
if (($size == "small" and ! isset($subtitle)) || ($graph_config["glegend"] == "hide"))
$rrdtool_graph['extras'] = isset($rrdtool_graph['extras']) ? $rrdtool_graph['extras'] . " -g" : " -g" ;
# add slope-mode if rrdtool_slope_mode is set
if (isset($conf['rrdtool_slope_mode']) &&
$conf['rrdtool_slope_mode'] == True)
$rrdtool_graph['slope-mode'] = '';
if (isset($rrdtool_graph['title']) && isset($title)) {
if ($conf['decorated_graph_title'])
$rrdtool_graph['title'] = $title . " " .
$rrdtool_graph['title'] .
" last $range";
else
$rrdtool_graph['title'] = $rrdtool_graph['title'];
}
$command = $conf['rrdtool'] . " graph - $rrd_options ";
// Look ahead six months
if ( $user['trend_line'] ) {
// We may only want to use last x months of data since for example
// if we are trending disk we may have added a disk recently which will
// skew a trend line. By default we'll use 6 months however we'll let
// user define this if they want to.
$rrdtool_graph['start'] = "-" . $user['trend_history'] * 2592000 . "s";
// Project the trend line this many months ahead
$rrdtool_graph['end'] = "+" . $user["trend_range"] * 2592000 . "s";
}
if ( $max ) {
$rrdtool_graph['upper-limit'] = $max;
}
if ( $min )
$rrdtool_graph['lower-limit'] = $min;
if ( isset($graph_config['percent']) && $graph_config['percent'] == '1' ) {
$rrdtool_graph['upper-limit'] = 100;
$rrdtool_graph['lower-limit'] = 0;
}
if ( $max || $min || ( isset($graph_config['percent']) && $graph_config['percent'] == '1' ) )
$rrdtool_graph['extras'] = isset($rrdtool_graph['extras']) ? $rrdtool_graph['extras'] . " --rigid" : " --rigid" ;
if ( isset($graph_config['graph_scale']) ) {
// Log scale support
if ( $graph_config['graph_scale'] == 'log' ) {
$rrdtool_graph['extras'] = isset($rrdtool_graph['extras']) ? $rrdtool_graph['extras'] . " --logarithm --units=si" : " --logarithm --units=si" ;
if (!isset($rrdtool_graph['lower-limit']) || $rrdtool_graph['lower-limit'] < 1) {
// With log scale, the lower limit *has* to be 1 or greater.
$rrdtool_graph['lower-limit'] = 1;
}
}
}
if ( $conf['rrdtool_base_1024'] and in_array($vlabel, array('bytes', 'Bytes', 'bytes/s', 'Bytes/s', 'kB', 'MB', 'GB', 'bits', 'Bits', 'bits/s', 'Bits/s')) ) {
// Set graph base value to 1024
$rrdtool_graph['extras'] = isset($rrdtool_graph['extras']) ? $rrdtool_graph['extras'] . " --base=1024" : " --base=1024" ;
}
// The order of the other arguments isn't important, except for the
// 'extras' and 'series' values. These two require special handling.
// Otherwise, we just loop over them later, and tack $extras and
// $series onto the end of the command.
foreach (array_keys ($rrdtool_graph) as $key) {
if (preg_match('/extras|series/', $key))
continue;
$value = $rrdtool_graph[$key];
if (preg_match('/\W/', $value)) {
//more than alphanumerics in value, so quote it
$value = "'$value'";
}
$command .= " --$key $value";
}
// And finish up with the two variables that need special handling.
// See above for how these are created
$command .= array_key_exists('extras', $rrdtool_graph) ? ' '.$rrdtool_graph['extras'].' ' : '';
$command .= " $rrdtool_graph[series]";
break;
/////////////////////////////////////////////////////////////////////////////
// USING Graphite
/////////////////////////////////////////////////////////////////////////////
case "graphite":
// Check whether the link exists from Ganglia RRD tree to the graphite
// storage/rrd_dir area
if ( ! is_link($rrd_graphite_link) ) {
// Does the directory exist for the cluster. If not create it
if ( ! is_dir ($conf['graphite_rrd_dir'] . "/" . str_replace(" ", "_", $clustername)) )
mkdir ( $conf['graphite_rrd_dir'] . "/" . str_replace(" ", "_", $clustername ));
symlink($rrd_dir, str_replace(" ", "_", $rrd_graphite_link));
}
// Generate host cluster string
if ( isset($clustername) ) {
$host_cluster = str_replace(" ", "_", $clustername) . "." . $host;
} else {
$host_cluster = $host;
}
$height += 70;
if ($size == "small") {
$width += 20;
}
// $title = urlencode($rrdtool_graph["title"]);
// If graph_config is already set we can use it immediately
if ( isset($graph_config) ) {
$target = build_graphite_series( $graph_config, "" );
} else {
if ( isset($_GET['g'])) {
// if it's a report increase the height for additional 30 pixels
$height += 40;
$report_name = sanitize($_GET['g']);
$report_definition_file = $conf['gweb_root'] . "/graph.d/" . $report_name . ".json";
// Check whether report is defined in graph.d directory
if ( is_file($report_definition_file) ) {
$graph_config = json_decode(file_get_contents($report_definition_file), TRUE);
} else {
error_log("There is JSON config file specifying $report_name.");
exit(1);
}
if ( isset($graph_config) ) {
switch ( $graph_config["report_type"] ) {
case "template":
$target = str_replace("HOST_CLUSTER", $conf['graphite_prefix'] . $host_cluster, $graph_config["graphite"]);
break;
case "standard":
$target = build_graphite_series( $graph_config, $conf['graphite_prefix'] . $host_cluster );
break;
default:
error_log("No valid report_type specified in the " .
$report_name .
" definition.");
break;
}
$title = $graph_config['title'];
} else {
error_log("Configuration file to $report_name exists however it doesn't appear it's a valid JSON file");
exit(1);
}
} else {
// It's a simple metric graph
$target = "target=" . $conf['graphite_prefix'] . "$host_cluster.$metric_name.sum&hideLegend=true&vtitle=" . urlencode($vlabel) . "&areaMode=all&colorList=". $conf['default_metric_color'];
$title = " ";
}
} // end of if ( ! isset($graph_config) ) {
if ($cs) $start = date("H:i_Ymd",strtotime($cs));
if ($ce) $end = date("H:i_Ymd",strtotime($ce));
if ($max == 0) $max = "";
$graphite_url = $conf['graphite_url_base'] . "?width=$width&height=$height&" . $target . "&from=" . $start . "&until=" . $end . "&yMin=" . $min . "&yMax=" . $max . "&bgcolor=FFFFFF&fgcolor=000000&title=" . urlencode($title . " last " . $range);
break;
} // end of switch ( $conf['graph_engine'])
// Output to JSON
if ( $user['json_output'] ||
$user['csv_output'] ||
$user['flot_output'] ||
$user['graphlot_output'] ) {
if ($conf['graph_engine'] == "graphite") {
if ( $user['json_output'] == 1 ) { $output_format = "json"; }
elseif ( $user['csv_output'] == 1 ) { $output_format = "csv"; }
echo file_get_contents($graphite_url . "&format=" . $output_format);
} else {
$rrdtool_graph_args = "";
// First find RRDtool DEFs by parsing $rrdtool_graph['series']
preg_match_all("/([^V]DEF|CDEF):(.*)(:AVERAGE|\s)/",
" " . $rrdtool_graph['series'],
$matches);
foreach ( $matches[0] as $key => $value ) {
$rrdtool_graph_args .= $value . " ";
}
preg_match_all("/(LINE[0-9]*|AREA|STACK):\'[^']*\'[^']*\'[^']*\'[^ ]* /",
" " . $rrdtool_graph['series'],
$matches);
foreach ( $matches[0] as $key => $value ) {
if ( preg_match("/(LINE[0-9]*:\'|AREA:\'|STACK:\')([^']*)(\')([^']*)(\')([^']*)(')/", $value, $out ) ) {
$ds_name = $out[2];
$cluster_name = "";
$host_name = "";
$metric_type = "line";
if (preg_match("/(STACK:|AREA:)/", $value, $ignore)) {
$metric_type = "stack";
}
$metric_name = $out[6];
$ds_attr = array( "ds_name" => $ds_name,
"cluster_name" => $cluster_name,
"graph_type" => $metric_type,
"host_name" => $host_name,
"metric_name" => $metric_name );
// Add color if it exists
$pos_hash = strpos($out[4], '#');
if ($pos_hash !== FALSE) {
$pos_colon = strpos($out[4], ':');
if ($pos_colon !== FALSE)
$ds_attr['color'] = substr($out[4],
$pos_hash,
$pos_colon - $pos_hash);
else
$ds_attr['color'] = substr($out[4], $pos_hash);
}
$output_array[] = $ds_attr;
$rrdtool_graph_args .= " " . "XPORT:'" . $ds_name . "':'" . $metric_name . "' ";
}
}
// This command will export values for the specified format in XML
$command = $conf['rrdtool'] . " xport --start '" . $rrdtool_graph['start'] . "' --end '" . $rrdtool_graph['end'] . "' "
// Allow a custom step, if it was specified by the user. Also, we need to
// specify a --maxrows in case the number of rows with $user['step'] end up
// being higher than rrdxport's default (in which case the step is changed
// to fit inside the default --maxrows), but we also need to guard against
// "underflow" because rrdxport craps out when --maxrows is less than 10.
. ( $user['step'] ?
" --step '" . $user['step'] . "' --maxrows '"
. max( 10, round(($rrdtool_graph['end'] - $rrdtool_graph['start'])/$user['step']) ) . "' " :
"" )
. $rrd_options . " " . $rrdtool_graph_args;
// Read in the XML
$string = "";
if (strlen($command) < 100000) {
$fp = popen($command,"r");
while (!feof($fp)) {
$buffer = fgets($fp, 4096);
$string .= $buffer;
}
} else {
$tempfile = tempnam("/tmp", "ganglia-graph-json");
file_put_contents($tempfile, $command);
$tempstring = exec("/bin/bash $tempfile", $tempout);
foreach( $tempout as $line ) {
$string .= $line;
}
unlink($tempfile);
}
// Parse it
$xml = simplexml_load_string($string);
# If there are multiple metrics columns will be > 1
$num_of_metrics = $xml->meta->columns;
//
$metric_values = array();
// Build the metric_values array
foreach ( $xml->data->row as $key => $objects ) {
$values = get_object_vars($objects);
// If $values["v"] is an array we have multiple data sources/metrics and
// we need to iterate over those
if ( is_array($values["v"]) ) {
foreach ( $values["v"] as $key => $value ) {
$output_array[$key]["datapoints"][] =
array(build_value_for_json($value), intval($values['t']));
}
} else {
$output_array[0]["datapoints"][] =
array(build_value_for_json($values["v"]), intval($values['t']));
}
}
// If JSON output request simple encode the array as JSON
if ( $user['json_output'] ) {
// First let's check if JSON output is requested for Live Dashboard and
// we are outputting aggregate graph. If so we need to add up all the values
if ( $user['live_dashboard'] && sizeof($output_array) > 1 ) {
$summed_output = array();
foreach ( $output_array[0]['datapoints'] as $index => $datapoint ) {
// Data point is an array with value and UNIX time stamp. Initialize
// summed output as 0
$summed_output[$index] = array( 0, $datapoint[1] );
for ( $i = 0 ; $i < sizeof($output_array) ; $i++ ) {
$summed_output[$index][0] += $output_array[$i]['datapoints'][$index][0];
}
}
unset($output_array);
$output_array[0]['datapoints'] = $summed_output;
}
header("Content-type: application/json");
header("Content-Disposition: inline; filename=\"ganglia-metrics.json\"");
print json_encode($output_array);
}