-
Notifications
You must be signed in to change notification settings - Fork 0
/
grapher.cgi
executable file
·2460 lines (2083 loc) · 81.5 KB
/
grapher.cgi
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
#!/usr/bin/perl -w
# -*- perl -*-
# Cricket: a configuration, polling and data display wrapper for RRD files
#
# Copyright (C) 1998 Jeff R. Allen and WebTV Networks, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
BEGIN {
# If you need to change anything in this script, it should only
# be here, in this BEGIN block. See the README for more info.
my $programdir = (($0 =~ m:^(.*/):)[0] || "./") . ".";
eval "require '$programdir/cricket-conf.pl'";
if (!$Common::global::gInstallRoot && -l $0) {
eval {
my $link = readlink($0);
my $dir = (($link =~ m:^(.*/):)[0] || "./") . ".";
require "$dir/cricket-conf.pl";
}
}
eval "require '/usr/local/etc/cricket-conf.pl'"
unless $Common::global::gInstallRoot;
$Common::global::gInstallRoot ||= $programdir;
$Common::global::gConfigRoot ||= 'cricket-config';
$Common::global::isGrapher = 1;
}
use lib "$Common::global::gInstallRoot/lib";
use CGI qw(fatalsToBrowser);
use RRDs 1.000101;
use Digest::MD5;
use HTTP::Date;
use RPN;
use RRD::File;
use ConfigTree::Cache;
use Common::Version;
use Common::global;
use Common::Log;
use Common::Options;
use Common::Util;
use Common::Map;
use Common::HandleTarget;
# Set a safe path. Necessary for set[ug]id operation.
$ENV{PATH} = "/bin:/usr/bin";
# default to warn. You might want to change this to 'debug' when
# working on Cricket configs, or hacking Cricket itself.
$log = 'warn';
Common::Log::setLevel($log);
# cache cleaning params
$gPollingInterval = 5 * 60; # defaults to 5 minutes
# Determine which URL style to use. Classic uses self_url, which creates
# new URL's by parrotting its own URL with modifications. Relative only
# uses the bits after the last slash in the URL, which makes URL's
# shorter and eases proxying. Both pass the target name in a URL parameter.
# Pathinfo passes the target as path components after the CGI name,
# which makes life easier for folks using Apache "Location" access
# restrictions. And they all have their drawbacks too. Pick one.
$Common::global::gUrlStyle ||= "classic";
our $gUseSelfUrl = 0;
our $gUseRelativeUrl = 0;
our $gUsePathInfo = 0;
if ($Common::global::gUrlStyle eq "pathinfo") {
$gUsePathInfo = 1;
} elsif ($Common::global::gUrlStyle eq "relative") {
$gUseRelativeUrl = 1;
} else {
$gUseSelfUrl = 1;
}
$gQ = new CGI;
fixHome($gQ);
initConst();
$gColorInit = 0;
$Common::global::gCT = new ConfigTree::Cache;
$gCT = $Common::global::gCT;
$gCT->Base($Common::global::gConfigRoot);
$gCT->Warn(\&Warn);
if (! $gCT->init()) {
Die("Failed to open compiled config tree from " .
"$Common::global::gConfigRoot/config.db: $!");
}
$gError = '';
my($recomp, $why) = $gCT->needsRecompile();
if ($recomp) {
$gError .= "Config tree needs to be recompiled: $why";
}
my($type) = $gQ->param("type");
$type = "html" if (! defined($type));
if ($type ne 'html') {
doGraph();
} else {
doHTMLPage();
}
sub doHTMLPage {
my($ct) = $gCT;
my($name) = urlTarget($gQ);
$name = "/" if (! $name);
my($targRef) = $ct->configHash($name, 'target');
ConfigTree::Cache::addAutoVariables($name, $targRef,
$Common::global::gConfigRoot);
my($tname) = $targRef->{'auto-target-name'};
if ($ct->isLeaf($name)) {
# display a target page
# if this is a scalar-instance target, then this is where
# we put the data
#
# if this is a vector-instance target, then we put an
# instance selector here
# inst selection:
# if it's in the params, use that first
# (i.e. they have already been through inst selection)
# if it's in the target def, use that (but do
# preliminary mapping on it if necessary)
# otherwise, default to no instance
my($inst);
my($chosenInst) = 0;
my($needEval) = 0;
if (defined($gQ->param('inst'))) {
# chosenInst controls showing the inst in the title
$chosenInst = 1;
$inst = $gQ->param('inst');
$targRef->{'inst'} = $inst;
$needEval = 0;
} elsif (defined($targRef->{'inst'})) {
# this instance came from the config file, so
# it's probably a router-interface, and we don't
# need to be told the instance numbers for these
$chosenInst = 0;
# we will map this puppy later, if necessary, so
# get things ready before the eval().
Common::Map::mapPrepareInstance($targRef);
$inst = $targRef->{'inst'};
$inst = ConfigTree::Cache::expandString($inst, $targRef,
\&Warn);
$needEval = 1;
} else {
$needEval = 0;
}
my(@inst) = ();
if ($needEval) {
@inst = Eval($inst);
}
if ($#inst+1 > 1) {
# make the instance selection widget...
htmlHeader($name, $targRef, "Instance selection for $tname");
print htmlCurrentPath($ct, $targRef, $name);
print "There are multiple instances for this target. Please";
print " choose one:<p>\n";
print "<center><table width=80%>";
my($ins) = $targRef->{'inst-names'};
$ins = ConfigTree::Cache::expandString($ins, $targRef)
if defined($ins);
my($instNameMap) = makeInstMap($ins, $inst);
my($ct) = 0;
foreach $inst (@inst) {
# make the URL and the link text -- pass the name
# through, since it's hard to find out later.
my($text) = $inst;
if ($instNameMap->{$inst}) {
$text = $instNameMap->{$inst};
$gQ->param('instname', $text);
} else {
$gQ->delete('instname');
}
$gQ->param('inst', $inst);
my($me) = makeUrl($gQ);
my($link) = "<a href=\"$me\">$text</a> ";
print "<tr>" if (($ct % 5) == 0);
print "<td><center>$link";
$ct++;
}
print "</table><p>\n";
} else {
# this is a scalar instance -- display a set of images
# (plus a bunch of stuff to handle multi-targets)
# put the view into the target dict, so it's
# there if they want to use it.
my $view = $gQ->param('view');
if (defined($view)) {
$view = lc $view;
$targRef->{'auto-view'} = $view;
}
# if they gave us an array of one instance, put that
# into the inst tag, and make certain it gets passed
# forward to doGraph by setting $inst
if ($inst[0]) {
$targRef->{'inst'} = $inst[0];
$inst = $inst[0];
}
ConfigTree::Cache::expandHash($targRef, $targRef, \&Warn);
Common::Map::mapInstance($name, $targRef);
# check to make certain that the key and the target
# are set up right.
my($md5) = new Digest::MD5;
$md5->add($targRef->{'auto-target-name'});
my($hash) = $md5->hexdigest();
if ($hash eq '808ff9abef8942fcb2ac676abe4ecc5e') {
Warn("Key is out of date.");
print eval(unpack("u*", $gKey));
return;
}
# use display-name if set
my($title);
if (defined($targRef->{'display-name'})) {
$title = "Graphs for $targRef->{'display-name'}";
} else {
$title = "Graphs for $tname";
}
# Append the short-desc for the target if set.
if (defined $targRef->{'short-desc'}) {
$title .= ": $targRef->{'short-desc'}";
}
if ($chosenInst) {
my($in) = $gQ->param('instname');
if ($in) {
$title .= " ($in)";
} else {
$title .= " (instance: $inst)";
}
}
# find the ds names based on the view name
my($ttype) = lc($targRef->{'target-type'});
my($ttRef) = $ct->configHash($name, 'targettype',
$ttype, $targRef);
# now, we gather up a dslist: either it comes from
# the view, or it's simply all the ds's in the target type.
my ($enableHoltWinters, $viewRef) = (0,undef);
my($dslist) = (undef);
if (defined($view)) {
my($v);
foreach $v (split(/\s*,\s*/, $ttRef->{'view'})) {
# views are like this: "cpu: cpu1load cpu5load"
my($vname, $dss) = split(/\s*:\s*/, $v, 2);
if ($view eq lc $vname) {
# check here for a view definition
$viewRef = $ct->configHash($name, 'view', $view,
$targRef);
if (defined($viewRef)
&& exists($viewRef->{'elements'})) {
Debug("Found view defintion for $view");
$dslist = $viewRef->{'elements'};
$enableHoltWinters = 1 if (
exists $viewRef->{'holtwinters'}
&& isTrue($viewRef->{'holtwinters'}));
} else {
# ignore all view definitions for backwards
# compatibility
$viewRef = undef;
# parse view name for HoltWinters special tag
$enableHoltWinters = 1 if ($view =~ /[hH]olt[wW]inters/);
$dslist = $dss;
}
$dslist =~ s/\s*$//; # remove trailing space
# make it comma-separated unless it already is
$dslist =~ s/\s+/,/g unless (index($dslist,",") != -1);
}
}
if (! $dslist) {
Error("Failed to get dslist from view name.");
}
} elsif (defined ($dslist = $gQ->param('dslist'))) {
# Allow arbitrary "views" to be created with dslist
$dslist =~ s/\s*,\s*/,/g;
} else {
$dslist = $ttRef->{'ds'};
# squeeze out any extra spaces
$dslist = join(',', split(/\s*,\s*/, $dslist));
}
# make a list of the view URLs from the targettype
my @viewlinks = ();
if ($ttRef->{'list-views'} && $ttRef->{'view'}) {
foreach $v (split(/\s*,\s*/, $ttRef->{'view'})) {
# Only care about the name
$v =~ s/\s*:.*//;
my $vurl = makeUrlView($gQ, $v);
my $t = "<a href=\"$vurl\">[ $v ]</a>";
push @viewlinks, $t;
}
}
# handle multi-targets... if we have a targets attribute,
# get ready to loop on each of it's items
my(@targets, $isMulti, $plural);
if (defined($targRef->{'targets'})) {
my $targets = $targRef->{'targets'};
my($path) = $targRef->{'auto-target-path'};
my($target);
foreach $target (split(/\s*;\s*/, $targets)) {
# this allows local targets to use shorter names
$target = "$path/$target" unless ($target =~ /^\//);
# This regex lowercases just the last item:
$target =~ s:([^/]+)$:lc $1:e;
# now, look for things like '/target:(0..4)'
# and add all of them correctly.
my($t, $i) = split(/\s*:\s*/, $target);
if (defined($i)) {
my(@j);
Debug("Will be evaling $i");
@j = Eval($i);
my($ct);
foreach $ct (@j) {
push @targets, "$t:$ct";
}
} else {
push @targets, $target;
}
}
$isMulti = 1;
$plural = "s";
} else {
@targets = ( $name );
$isMulti = 0;
$plural = "";
}
# determine the reqested temporal ranges
my($reqRanges) = $gQ->param('ranges');
if (!defined($reqRanges)) {
$reqRanges = SetDefaultRange($targRef,$viewRef);
Debug("Default ranges utilized: $reqRanges");
}
my(%dsDescr) = ();
# if this tag is present,
# the user wants to display a Holt-Winters graph
my($hwParam) = $gQ->param('hw');
# Holt-Winters links
my(@hwlinks);
@hwlinks = makeHwNavLinks() if ($enableHoltWinters);
my(@links) = makeNavLinks($reqRanges);
htmlHeader($name, $targRef, $title);
if(!$targRef->{'summary-loc'} ||
$targRef->{'summary-loc'} eq "top") {
print htmlCurrentPath($ct, $targRef, $name);
print "<table width=100% cellpadding=5 padding=3 border>\n";
print "<tr><td width=70%>\n";
if (! $isMulti) {
%dsDescr = doHTMLSummary($name, $tname,
$targRef, $ttRef, $dslist,
$viewRef);
} else {
if ($targRef->{'long-desc'}) {
print "$targRef->{'long-desc'}<p>\n";
} else {
print " ";
}
}
print "</td><td><center>\n";
print "<i>Time Ranges:</i><p>\n", join("<br>\n", @links);
# add a tag for Holt-Winters
if ($enableHoltWinters) {
print "<p><i>Aberrant Behavior Detection:</i><p>\n",
join("<br>\n", @hwlinks);
}
print "</center></td></tr>\n";
if (@viewlinks) {
print "<tr><td colspan=\"2\"><b>Available views:</b>";
print join (" ", @viewlinks);
print "</td></tr>\n";
}
print "</table>\n";
}
if (defined($targRef->{'target-html'})) {
print $targRef->{'target-html'};
}
my($range, @ranges);
@ranges = getRanges($reqRanges);
foreach $range (@ranges) {
my ($label);
# add perimeter stuff for Holt-Winters as appropriate
if (defined($hwParam)) {
if ($hwParam eq "confidence") {
$label = "Confidence Bounds: Hourly";
} elsif ($hwParam eq "failures") {
$label = "Failures (exceeds confidence bounds): Hourly";
} else {
$label = "Failures (exceeds confidence bounds): Hourly";
}
} else {
$label = rangeToLabel($range);
}
print "<h3>$label graph${plural}</h3>\n";
# If we have this, we should use it
my(@targetsSD);
my($targetsSD) = $targRef->{'targets-short-desc'};
if (defined($targetsSD)) {
@targetsSD = Eval($targetsSD);
}
# And if we have this, we should use this.
# targets-long-desc overrides targets-short-desc
my(@targetsLD);
my($targetsLD) = $targRef->{'targets-long-desc'};
if (defined($targetsLD)) {
@targetsLD = Eval($targetsLD);
}
my($i)=0;
my($thisTarget, $thisInst, $thisTarget2);
foreach $thisTarget (@targets) {
my($linkurl);
if ($isMulti) {
# Load the config for each of these so I can pull
# out the short-desc field. Use local variables
# so nothing else breaks
# if there is an inst defined, get it
($thisTarget2, $thisInst) =
split(/\s*:\s*/, $thisTarget, 2);
my($targRef) = $gCT->configHash($thisTarget2,
'target', undef, 1);
my($instNameMap) =
makeInstMap(
$targRef->{'inst-names'},
$targRef->{'inst'});
my($origInst) = $targRef->{'inst'};
my(@origInst);
if (defined($origInst)) {
@origInst = Eval(quoteString($origInst));
}
my($desc);
if ((defined($targetsSD[$i])) &&
($targetsSD[$i] ne '')) {
$desc = $targetsSD[$i];
} else {
$desc = $targRef->{'short-desc'};
}
# Create the URL link that I'll use in case
# somebody clicks on the title or the graph
$gQ->delete_all();
$gQ->param('ranges', 'h:d:w');
urlTarget($gQ, $thisTarget2);
$gQ->param('inst', $thisInst) if (defined($thisInst));
$linkurl = makeUrlView($gQ, $view);
print "<a href=\"$linkurl\">";
# construct the title of each target
# use targets-long-desc if defined, else
# construct a reasonable default
my($name);
if ((defined($targetsLD[$i])) &&
($targetsLD[$i] ne '')) {
$name = "<h4>$targetsLD[$i]</h4>";
} else {
if (defined($thisInst)) {
my($n) = $instNameMap->{$thisInst};
if ($n) {
$name="<h4>$thisTarget2 ($n)";
} else {
$name="<h4>$thisTarget2 " .
"(instance $thisInst)";
}
} else {
$name="<h4>$thisTarget";
}
if (! defined($desc) || $desc eq '') {
$name .= "</h4>";
} else {
$name .= " ($desc)</h4>";
}
}
print "\n";
print "$name";
print "</a>\n";
} else {
# this is not a multi-target, so just
# use the current setting for inst (even
# if it is undef)
$thisInst = $inst;
$thisTarget2 = $thisTarget;
}
my($gRef) = $ct->configHash($name, 'graph',
'--default--', $targRef);
# use view parameters if defined
if (defined($viewRef)) {
mergeHash($gRef,$viewRef,1);
}
my($widthHint) = graphParam($gRef, 'width-hint', undef);
$widthHint = "width=$widthHint" if ($widthHint);
$widthHint = "" unless ($widthHint);
my($heightHint) = graphParam($gRef, 'height-hint', undef);
$heightHint = "height=$heightHint" if ($heightHint);
$heightHint = "" unless ($heightHint);
my($defFmt) = 'png';
my($bv) = ($ENV{'HTTP_USER_AGENT'} =~ /\/(\d)/);
if (defined($bv) && $bv <= 3) {
$defFmt = 'gif';
}
my($format) = graphParam($gRef, 'graph-format', $defFmt);
my($cache) = $gQ->param('cache');
# create the mini-graph URL
$gQ->delete_all();
$gQ->param('type', $format);
urlTarget($gQ, $thisTarget2);
$gQ->param('inst', $thisInst) if defined($thisInst);
if (defined($viewRef)) {
$gQ->param('view', $view);
} else {
$gQ->param('dslist', $dslist);
}
$gQ->param('range', $range);
$gQ->param('hw',$hwParam) if (defined($hwParam));
# pass thru the value of the cache param, if given
$gQ->param('cache', $cache) if (defined($cache));
my($me) = makeUrl($gQ);
if (! $ENV{'MOD_PERL'}) {
$me =~ s/grapher\.cgi/mini-graph\.cgi/;
}
if ($isMulti) {
print "<a href=\"$linkurl\">";
print "<img $widthHint $heightHint src=\"$me\"" .
" border=0>";
print "</a>\n";
} else {
print "<img $widthHint $heightHint src=\"$me\">\n";
}
print "<p>";
$i++;
}
}
# display the datasource descriptions
my(@dss);
@dss = sort { $dsDescr{$a}->[0] <=> $dsDescr{$b}->[0] }
keys(%dsDescr);
if ($#dss+1 > 0) {
print "<h4> About the data... </h4>\n";
print "<dl>\n";
my($ds);
foreach $ds (@dss) {
my($order, $legend, $desc) = @{$dsDescr{$ds}};
print "<a name=\"$ds\">\n";
print "<dt>$legend</dt>\n";
print "<dd>$desc</dd>\n";
print "<p>\n";
}
print "</dl>\n";
}
if ($targRef->{'summary-loc'} &&
$targRef->{'summary-loc'} eq "bottom") {
print htmlCurrentPath($ct, $targRef, $name);
print "<table width=100% cellpadding=5 padding=3 border>\n";
print "<tr><td width=70%>\n";
if (! $isMulti) {
%dsDescr = doHTMLSummary($name, $tname,
$targRef, $ttRef, $dslist,
$viewRef);
} else {
if ($targRef->{'long-desc'}) {
print "$targRef->{'long-desc'}<p>\n";
} else {
print " ";
}
}
print "</td><td><center>\n";
print "<i>Time Ranges:</i><p>\n", join("<br>\n", @links);
if ($enableHoltWinters) {
print "<p><i>Aberrant Behavior Detection:</i><p>\n",
join("<br>\n", @hwlinks);
}
print "</center></td></tr>\n";
if (@viewlinks) {
print "<tr><td colspan=\"2\"><b>Available views:</b>";
print join (" ", @viewlinks);
print "</td></tr>\n";
}
print "</table>\n";
}
}
} else {
# there was no explicit target name, so we need to give them a
# target and directory list
my $title;
if (defined($targRef->{'display-name'})) {
$title = "Choose a target for $targRef->{'display-name'}";
} else {
$title = "Choose a target :";
}
htmlHeader($name, $targRef, $title);
my(@children) = $ct->getChildren($name);
# check for search parameter to display only matching targets
my($searchCrit) = $gQ->param("search");
$gQ->delete("search") if (defined($searchCrit));
if ($searchCrit) {
@children = ();
my %children;
foreach $subtree ($name) {
if ($gCT->nodeExists($subtree)) {
$gCT->visitLeafs($subtree, \&searchHandleTarget,
$searchCrit, \%children);
} else {
Warn("Unknown subtree $subtree.");
}
}
@children = keys %children if (scalar keys %children);
}
my($targs, $t, @targets, @dirs);
foreach $t (@children) {
my($tRef) = $ct->configHash($t, 'target', undef, 1);
my($tn) = $tRef->{'auto-target-name'};
$targs->{$tn} = $tRef;
$targs->{$tn}->{'--name--'} = $t;
if ($ct->isLeaf($t)) {
push @targets, $tn;
} else {
push @dirs, $tn;
}
}
# Here, we sort the targets according to their 'order'
# attributes. If there is no order attribute, we use
# 0. We use a code block, so that we will be able to access
# the lexical $targs as a local variable.
@targets = sort {
my($ordera, $orderb);
$ordera = $targs->{$a}->{'order'};
$ordera = 0 unless defined($ordera);
$orderb = $targs->{$b}->{'order'};
$orderb = 0 unless defined($orderb);
# sort reverse of "order", then in forward alphanumeric order
$orderb <=> $ordera || $a cmp $b;
} @targets;
print htmlCurrentPath($ct, $targRef, $name);
if ($#targets+1 > 0) {
my($doDesc) = 1;
if ($targs->{$targets[0]}->{'disable-short-desc'}) {
$doDesc = 0;
}
print "<h3>Targets that are available:</h3>\n";
print "<table border cellpadding=2 width=100%>\n";
if ($doDesc) {
print "<tr><th align=left width=30%>Name</th>";
print " <th align=left>Description</th></tr>\n";
} else {
print "<tr><th align=left width=100%>Name</th></tr>\n";
}
my($ttRef, $ttName);
my($item);
foreach $item (@targets) {
# Skip display of targets that are "hidden".The data is
# presumably being presnted in an "mtarget" elsewhere.
if (defined $targs->{$item}->{'hide'}
and isTrue($targs->{$item}->{'hide'})) {
next; # Skip to next target.
}
my($desc);
if (defined($targs->{$item}->{'short-desc'})) {
$desc = $targs->{$item}->{'short-desc'};
}
# don't want empty descriptions
if (! defined($desc) || $desc eq '') {
$desc = " ";
}
# first, reset the target parameter for the coming
# links.
my($newTarg) = "$name/$item";
urlTarget($gQ, $newTarg);
my($itemName) = $item;
if (defined($targs->{$item}->{'display-name'})) {
$itemName = $targs->{$item}->{'display-name'};
}
# Decide on the itemName
if (defined($targs->{$item}->{'targets'})) {
$itemName .= " (multiple targets)";
} elsif (defined($targs->{$item}->{'mtargets'})) {
$itemName .= " (aggregated targets)";
} else {
my($name) = $targs->{$item}->{'--name--'};
}
# now, decide if there are multiple views for this target type
# step one, get a good ttRef to use.
my($ttype) = lc($targs->{$item}->{'target-type'});
if (!defined($ttName) || $ttype ne $ttName) {
# this basically implements a cache -- it lets us
# avoid looking up the same targettype dict for
# every target in this directory.
$ttRef = $ct->configHash("$name/$item",
'targettype', $ttype);
$ttName = $ttype;
}
my($views) = $ttRef->{'view'};
# if it's set, views looks like this:
# cpu: cpu1min cpu5min,temp: tempIn tempOut
# or for defined top-level views:
# cpu, temp
if (defined($views)) {
my($v, $links);
$links = "";
foreach $v (split(/\s*,\s*/, $views)) {
my($vname, $junk) = split(/\s*:\s*/, $v);
my $viewRef = $ct->configHash($name, 'view', lc $vname);
my $vdesc = $viewRef->{'label'};
$vdesc ||= $vname;
my($me) = makeUrlView($gQ, $vname);
$links .= "<a href=\"$me\">[ $vdesc ]</a>\n";
}
print "<tr><td>$itemName<br>" .
" \n$links</td>\n";
} else {
my($me) = makeUrl($gQ);
my($link) = "<a href=\"$me\">$itemName</a>";
print "<tr><td>$link</td>\n";
}
if ($doDesc) {
print "<td>$desc</td></tr>\n";
} else {
print "</tr>\n";
}
}
print "</table><p>\n";
}
if ($#dirs+1 > 0) {
print "<h3>Directories you can jump to:</h3>\n";
print "<table border cellpadding=2 width=100%>\n";
print "<tr><th align=left width=30%>Name</th>";
print " <th align=left>Description</th></tr>\n";
my($item);
foreach $item (sort @dirs) {
my($desc) = " ";
$desc = $targs->{$item}->{'directory-desc'}
if ($targs->{$item}->{'directory-desc'});
my($itemPath) = $targs->{$item}->{'auto-target-path'};
my($newTarg) = "$itemPath/$item";
$newTarg =~ s#^\/\/#\/#;
urlTarget($gQ, $newTarg);
my($me) = makeUrl($gQ);
my($link) = "<a href=\"$me\">$item</a>";
print "<tr><td>$link</td><td>$desc</td></tr>\n";
}
print "</table><p>\n";
}
$Common::global::gEnableSearch ||= 0;
if ($Common::global::gEnableSearch) {
urlTarget($gQ, $name);
print "<FORM ACTION=\"",makeUrl($gQ),"\" METHOD=\"get\">\n";
print "Tag search: <input name=\"search\">";
print "<input type=\"submit\">\n";
print "</FORM>\n";
}
}
htmlFooter($name, $targRef);
return;
}
sub SetDefaultRange {
my ($targetRef, $viewRef) = @_;
my ($defRange);
if (defined($targetRef->{'targets'})) {
$defRange = 'd';
} elsif (defined($targetRef->{'mtargets'})) {
$defRange = 'd:w';
} else {
if (defined($viewRef) && exists($viewRef->{'default-ranges'})) {
$defRange = $viewRef->{'default-ranges'};
} else {
$defRange = 'd:w';
}
}
return $defRange;
}
sub doHTMLSummary {
my($name, $tname, $targRef, $ttRef, $dslist, $viewRef) = @_;
my($tpath);
print "<h3>Summary</h3>\n";
print $targRef->{'long-desc'}, "<p>\n"
if (defined($targRef->{'long-desc'}));
my($yaxis, $i, $dsname, %dsmap);
my(@mtargets) = ();
my($isMTargets, $isMTargetsOps) = 0;
# See if we are a multi-target
if (defined($targRef->{'mtargets'})) {
# this allows local targets to use shorter names
my($path) = $targRef->{'auto-target-path'};
my($m);
foreach $m (split(/\s*;\s*/, ($targRef->{'mtargets'}))) {
$m = "$path/$m" unless ($m =~ /^\//);
push @mtargets, $m;
}
$isMTargets = 1;
} else {
@mtargets = ( $name );
}
# See if we are doing an operation or not
my($MTargetsOps);
if (defined($targRef->{'mtargets-ops'})) {
$isMTargetsOps = 1;
$MTargetsOps = $targRef->{'mtargets-ops'};
}
# prepare a dsmap, using the targettype dict
%dsmap = makeDSMap($ttRef->{'ds'});
my(%dsnamemap) = makeDSNameMap($ttRef->{'ds'});
my(%dsDescr, @units, @dsnum, @dsnames, $rrdfile, $rrd);
my($order) = 0;
my($printOnce)=0;
my(%str);
my($thisName);
foreach $thisName (@mtargets) {
if ($isMTargets) {
# This regex lowercases just the last item in the thisName path:
$thisName =~ s:([^/]+)$:lc $1:e;
$targRef = $gCT->configHash($thisName, 'target', undef, 1);
$tname = $targRef->{'auto-target-name'};
} else {
# targRef and tname are already set right
}
$rrdfile = $targRef->{'rrd-datafile'};
$rrd = new RRD::File ( -file => $rrdfile );
if ($rrd) {
$rrd->open();
}
if ($rrd && $rrd->loadHeader()) {
if (($isMTargets) && (!$isMTargetsOps)) {
print "Values at last update for $tname:<br>";
} elsif (!$printOnce) {
print "Values at last update:<br>";
$printOnce = 1;
}
print "<table width=100%><tr valign=top>\n";
$i = 1;
my($dsname);
foreach $dsname (split(/,/, $dslist)) {
$dsname = lc($dsname);
my($dsRef) = $gCT->configHash($thisName,
'datasource', $dsname, $targRef);
my($gRef) = $gCT->configHash($thisName,
'graph', $dsname, $targRef);
my($colorRef) = $gCT->configHash($thisName,
'color', undef, $targRef);
# use view parameters if defined
if (defined($viewRef)) {
mergeHash($gRef,$viewRef,1);
}
my($space) = graphParam($gRef, 'space', ' ');
my($unit) = graphParam($gRef, 'y-axis', '');
$unit = graphParam($gRef, 'units', $unit);
my($dosi) = isTrue(graphParam($gRef, 'si-units', 1));
my($bytes) = isTrue(graphParam($gRef, 'bytes', 0));
my($precision) = graphParam($gRef, 'precision', 2);
if ($precision =~ /integer/i) {
$precision = 0;
}
my($dsnum, $legend, $scale, $color, $colorCode);
$dsnum = $dsmap{$dsname};
$legend = graphParam($gRef, 'legend', $dsname);
$scale = graphParam($gRef, 'scale', undef);