File: grapher.cgi

package info (click to toggle)
cricket 1.0.5-19
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,936 kB
  • sloc: perl: 8,276; ansic: 329; sh: 162; makefile: 63; sql: 16
file content (2399 lines) | stat: -rwxr-xr-x 81,133 bytes parent folder | download | duplicates (4)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
#!/usr/local/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.

    require '/etc/cricket/cricket-conf.pl';
    $Common::global::isGrapher = 1;
}

use lib "$Common::global::gInstallRoot/lib";

use CGI qw(fatalsToBrowser);
use RRDs 1.000101;
use Digest::MD5;

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";

my $gLongDSName = $Common::global::gLongDSName;

# 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";
my $gUseSelfUrl = 0;
my $gUseRelativeUrl = 0;
my $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";
            }

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

            } else {
                $dslist = $ttRef->{'ds'};
                # squeeze out any extra spaces
                $dslist = join(',', split(/\s*,\s*/, $dslist));
            }

            # 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 "&nbsp;";
                    }
                }
                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>\n";
                print "</tr></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));
                        if (defined($view))  {
                            $gQ->param('view', $view);
                        }
                        $linkurl = makeUrl($gQ);

                        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';
                    } elsif ($ENV{'HTTP_USER_AGENT'}=~/Blazer 1.0/i ) {
                        $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));

                    # this parameter is to trick Netscape into
                    # always asking the CGI for the image, instead
                    # of trying to cache it inappropriately
                    $gQ->param('rand', int(rand(1000)));

                    # 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 "&nbsp;";
                    }
                }
                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>\n";
                print "</tr></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 = "&nbsp;";
                }

                # 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;
                        # put it in just long enough to get a URL out
                        $gQ->param('view', $vname);
                        my($me) = makeUrl($gQ);
                        $gQ->delete('view');

                        $links .= "<a href=\"$me\">[&nbsp;$vdesc&nbsp;]</a>\n";
                    }

                    print "<tr><td>$itemName<br>" .
                        "&nbsp;&nbsp;&nbsp;\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) = "&nbsp;";
                $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";
        }
        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);

                $color = graphParam($gRef, 'color', nextColor($colorRef));
                if ((defined($color)) && (!$isMTargetsOps))  {
                    $colorCode = colorToCode($colorRef, $color);
                    usedColor($color);
                }

                if (defined($dsRef->{'desc'})) {
                    $dsDescr{$dsname} = [ $order, $legend,
                                          $dsRef->{'desc'} ];
                    $order++;
                }

                # get and scale the value (if necessary)
                my($value) = $rrd->getDSCurrentValue($dsnum);

                if (defined($value) && !isNaN($value) && defined($scale)) {
                    my($rpn) = new RPN;
                    my($res) = $rpn->run("$value,$scale");

                    if (! defined($res)) {
                        Warn("Problem scaling value. " .
                             "Reverting to unscaled value.");
                    } else {
                        $value = $res;
                    }
                }

                # save the numbers for later, when we'll add them
                if ($isMTargetsOps)  {
                    # we set NaN's to 0 since it's better
                    # to get slightly wrong sums than no sum at all.
                    # (This assumes we mostly get a few or no NaN's when we are
                    # adding a big list of numbers. YMMV.)
                    $value = 0 if (isNaN($value));
                    push @{$str{$dsname}}, $value;
                }

                if ((defined($value)) && (!$isMTargetsOps)) {
                    $value = prepareValue($value, $dosi, $bytes,
                                          $precision, $space, $unit);

                    # only allow three columns... if more, add a new row.
                    if ($i > 3) {
                        print "<tr>";
                        $i = 1;
                    }
                    $i++;

                    my($show) = isTrue(graphParam($gRef, 'show-avg-max', 1));
                    $show = 0 if (! defined($show));

                    my($mmax);
                    if ($show) {
                        if (! defined($scale)) {
                            $scale = "1,*";
                        }

                        $dsidx = $dsnamemap{$dsname};
                        my(@args) = (
                                     "/dev/null",
                                     "DEF:ds0=$rrdfile:$dsidx:AVERAGE",
                                     "DEF:ds1=$rrdfile:$dsidx:MAX",
                                     "CDEF:sds0=ds0,$scale",
                                     "CDEF:sds1=ds1,$scale",
                                     "PRINT:sds0:AVERAGE:\%lf",
                                     "PRINT:sds1:MAX:\%lf" );

                        ($mmax, undef, undef) = RRDs::graph @args;
                    }

                    print "<td>";
                    if (defined($color)) {
                        print "<font color=\"#$colorCode\">$legend</font>";
                    } else {
                        print $legend;
                    }

                    if (! $show) {
                        print ": $value";
                    } else {
                        print " (for the day): <br><b>Cur</b>: $value<br>";

                        $value = prepareValue($mmax->[0], $dosi, $bytes,
                                              $precision, $space, $unit);
                        print " <b>Avg</b>: $value<br>";

                        $value = prepareValue($mmax->[1], $dosi, $bytes,
                                              $precision, $space, $unit);
                        print " <b>Max</b>: $value<br>";
                    }

                    if (exists($dsDescr{$dsname})) {
                        print " <a href=\"#$dsname\">[ ? ]</a>";
                    } else {
                        # nothing
                    }
                    print "</td>\n";
                }
            }
            if (!$isMTargetsOps)  {
                print "</tr></table>\n";
                print "Last updated at ", scalar(localtime($rrd->last_up())),
                "\n";
            }
        } else {
            print "Current values not available: ";
            print "$RRD::File::gErr<br>\n" if (defined($RRD::File::gErr));
            # FIXME: This should be dealt with in a cleaner way, but that's
            # left for a larger mtargets cleanup. driehuis 20020314
            if ($isMTargetsOps)  {
                foreach my $dsname (split(/,/, $dslist)) {
                    push @{$str{lc $dsname}}, 0;
                }
            }
        }
        if (!$isMTargetsOps)  {
            print "<p>";
        }

        $rrd->close();
    }

    my($colorRef) = $gCT->configHash($name, 'color', undef, $targRef);

    if ($isMTargetsOps)  {
        my($i) = 1;
        foreach $dsname (split(/,/, $dslist))  {
            $dsname = lc($dsname);
            my($gRef) = $gCT->configHash($name, 'graph',
                                         $dsname, $targRef);
            # use view parameters if defined
            if (defined($viewRef)) {
               mergeHash($gRef,$viewRef,1);
            }
            my($color) = graphParam($gRef, 'color', nextColor($colorRef));
            my($legend) = graphParam($gRef, 'legend', $dsname);
            my($colorCode);
            if (defined($color))  {
                $colorCode = colorToCode($colorRef, $color);
                usedColor($color);
            }

            my(@values) = @{$str{$dsname}};
            $MTargetsOps = convertOps($MTargetsOps, $#values+1);
            my($calc) = join(',', @values, $MTargetsOps);

            # Now do the operation on this to end up with a value
            my($rpn) = new RPN;
            my($res) = $rpn->run($calc);
            my($value);
            if (! defined($res))  {
                Warn("Problem performing operation.");
                $value = "?";
            }  else  {
                $value = $res;
            }

            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;
            }

            $value = prepareValue($value, $dosi, $bytes, $precision,
                                  $space, $unit);

            # only allow three columns... if more, add a new row.
            if ($i > 3) {
                print "</tr><tr valign=top>";
                $i = 1;
            }
            $i++;

            print "<td>";
            if (defined($color)) {
                print "<font color=\"#$colorCode\">$legend</font>";
            }  else  {
                print $legend;
            }
            print ": $value";
            if (exists($dsDescr{$dsname})) {
                print " <a href=\"#$dsname\">[ ? ]</a>";
            }  else  {
                # nothing
            }
            print "</td>\n";
        }
        print "</tr></table>\n";
        print "<p>";
    }

    return %dsDescr;
}

sub makeDSNameMap {
    my($dslist) = @_;
    my($i) = 0;
    my($dsname, %dsnamemap);

    if ($Common::global::gLongDSName) {
        foreach $dsname (split(/\s*,\s*/, $dslist)) {
            $dsnamemap{lc($dsname)} = Common::Util::mungeDsName($dsname);
            $i++;
        }
    } else {
        foreach $dsname (split(/\s*,\s*/, $dslist)) {
            $dsnamemap{lc($dsname)} = "ds$i";
            $i++;
        }
    }

    return %dsnamemap;
}

sub makeDSMap {
    my($dslist) = @_;
    my($i) = 0;
    my($dsname, %dsmap);

    foreach $dsname (split(/\s*,\s*/, $dslist)) {
        $dsmap{lc($dsname)} = $i;
        $i++;
    }

    return %dsmap;
}

sub initConst {
    $kMinute = 60;           #  60 seconds/min
    $kHour   = 60 * $kMinute;#  60 minutes/hr
    $kDay    = 24 * $kHour;  #  24 hrs/day
    $kWeek   = 7  * $kDay;   #   7 days/week
    $kMonth  = 30 * $kDay;   #  30 days/month
    $kYear   = 365 * $kDay;  # 365 days/year

    $kTypeUnknown = 0;
    $kTypeUnknown = 0;    # shut up, -w.
    $kTypeHourly  = 1;
    $kTypeDaily   = 2;
    $kTypeWeekly  = 3;
    $kTypeMonthly = 4;
    $kTypeYearly  = 5;

    @gRangeNameMap = ( undef, 'Hourly', 'Daily', 'Weekly', 'Monthly', 'Yearly' );

    $gKey = "M)&=1+3YH96%D97(H)W1E>'0O<&QA:6XG*2P\@*&]P96XH5" .
            "\"P\@(CPD0V]M;6]N\nM.CIG;&]B86PZ.F=);G-T86QL4F]" .
            "O=\"]42\$%.2U,B*2 F)B!J;VEN*\"<G+\" " .
            "\\\n%5#XI*0IB\n";
}

sub si_unit {
    my($value, $bytes) = @_;
    return ($value, '') if ($value eq "?" || isNaN($value) || $value == 0);

    my(@symbol) = ('a', 'f', 'p', 'n', '&#181;', 'milli',
                   '',
                   'k', 'M', 'G', 'T', 'P', 'E');
    my($symbcenter) = 6;

    my($digits) = int(log(abs($value))/log(10) / 3);

    my($magfact);
    if ($bytes) {
        $magfact = 2 ** ($digits * 10);
    } else {
        $magfact = 10 ** ($digits * 3.0);
    }

    if ((($digits + $symbcenter) > 0) &&
        (($digits + $symbcenter) <= $#symbol)) {
        return ($value/$magfact, $symbol[$digits + $symbcenter]);
    } else {
        return ($value, '');
    }
}

sub getRanges {
    my($scales) = @_;
    $scales = "h:d:w:m:y" unless defined $scales;

    # these definitions mirror how MRTG 2.5 sets up its graphs
    my(%scaleMap) = (
        'h' => $kHour  * 10,
        'd' => $kHour  * 42,
        'w' => $kDay   * 10,
        'm' => $kWeek  *  6,
        'y' => $kMonth * 16,
    );

    my($scale, @res);
    foreach $scale (split(/\s*:\s*/, $scales)) {
        # later, we might do more sophisticated scale specification
        $scale = $scaleMap{$scale};
        push @res, $scale;
    }
    return @res;
}

sub rangeToLabel {
    my($range) = @_;
    return $gRangeNameMap[rangeType($range)];
}

sub rangeType {
    my($range) = @_;
    my($rangeHours) = $range / 3600;

    # question: when is kTypeUnknown appropriate?

    if ($range < $kDay) {
        return $kTypeHourly;
    } elsif ($range < $kWeek) {
        return $kTypeDaily;
    } elsif ($range < $kMonth) {
        return $kTypeWeekly;
    } elsif ($range < $kYear) {
        return $kTypeMonthly;
    } else {
        return $kTypeYearly;
    }
}

sub doGraph {
    my($type) = $gQ->param('type');
    my($imageName) = generateImageName($gQ, $type);

    # check the image's existance (i.e. no error from stat()) and age
    my($mtime);
    my($needUnlink);

    if (defined($imageName)) {
        $mtime = (stat($imageName))[9];
    } else {
        $imageName = "$Common::global::gCacheDir/cricket.$$.$type";
        $needUnlink++;
    }

    # Untaint $imageName.
    if ($imageName =~ m,^($Common::global::gCacheDir/[^/]+)$,) {
        $imageName = $1;
    }

    if (!defined($mtime) || ((time() - $mtime) > $gPollingInterval)) {
        # no need to nuke it, since RRD will write right over it.
    } else {
        Debug("Cached image exists. Using that.");
        sprayPic($imageName);
        return;
    }

    my($name) = urlTarget($gQ);

    if (! defined($name)) {
        Die("No target given.");
    }

    my($targRef) = $gCT->configHash($name, 'target', undef, 1);
    my($tname) = $targRef->{'auto-target-name'};

    my(@mtargets);
    my($isMTarget) = 0;
    my($isMTargetsOps) = 0;
    my($MTargetsOps);
    my($unkIsZero) = 0;

    if (defined($targRef->{'mtargets'}))  {
        $isMTarget = 1;
        @mtargets = split(/\s*;\s*/, ($targRef->{'mtargets'}));
    }  else  {
        @mtargets = ( $tname );
    }

    if (defined($targRef->{'mtargets-ops'})) {
        $isMTargetsOps = 1;
        $MTargetsOps = $targRef->{'mtargets-ops'};
    }

    if (defined($targRef->{'unknown-is-zero'})) {
        $unkIsZero = 1;
    }

    # things we will need from the params
    my($view) = $gQ->param('view');
    $view = lc $view if defined $view;
    # a comma-separated list of data sources
    my($dslist);
    my $viewRef = undef;
    if (defined($view)) {
        Debug("Found view tag $view in doGraph");
        $viewRef = $gCT->configHash($name, 'view', $view, $targRef);
        # skip error checking because doGraph args are generated by doHTMLPage
        $dslist = $viewRef->{'elements'};
        $dslist =~ s/\s*$//; # remove trailing space
        # make it comma-separated unless it already is
        $dslist =~ s/\s+/,/g unless (index($dslist,",") != -1);
    } else {
        $dslist = $gQ->param('dslist');
    }
    my($range) = $gQ->param('range');

    # calculate this now for use later
    my(@dslist) = split(',', $dslist);
    my($numDSs) = $#dslist + 1;

    my($gRefDef) = $gCT->configHash($name, 'graph',
                                    '--default--', $targRef);
    my($colorRef) = $gCT->configHash($name, 'color', undef, $targRef);
    # use view parameters if defined
    if (defined($viewRef)) {
        mergeHash($gRefDef,$viewRef,1);
    }
    my($width) = graphParam($gRefDef, 'width', 500);
    my($height) = graphParam($gRefDef, 'height', 200);
    my($useGprint) = graphParam($gRefDef, 'use-gprint', 0);

    my($interlaced) = graphParam($gRefDef, 'interlaced', undef);
    my(@interlaced) = ();
    if (defined($interlaced) && isTrue($interlaced)) {
        Debug('Graph will be interlaced.');
        @interlaced = ( '-i' );
    }

    my($ymax) = graphParam($gRefDef, 'y-max', undef);
    my($ymin) = graphParam($gRefDef, 'y-min', undef);

    my ($ymaxlck) = 0;
    my ($yminlck) = 0;

    if (isNonNull($ymax)) {
        $ymaxlck = 1;
    } else {
        $ymaxlck = 0;
    }

    if (isNonNull($ymin)) {
        $yminlck = 1;
    } else {
        $yminlck = 0;
    }

    # show Holt-Winters graphs
    my ($hwParam) = $gQ->param('hw');
    if (defined($hwParam)) {
        Debug("Holt Winters tag: $hwParam");
        # verify single target
        if ($isMTarget) {
            Warn("Holt-Winters forecasting not supported for multiple targets");
            $hwParam = undef;
        }
        # verify a single data source
        if ($numDSs != 1) {
            Warn("Holt-Winters forecasting not supported for multiple data " .
                "sources");
            $hwParam = undef;
        }
    }

    # ok, lets attempt to handle mtargets.  We need to loop through
    # each of the individual targets and construct the graph command
    # on each of those.  The other initializations should be outside
    # the loop, and the actual graph creation should be after the loop.

    my(@defs) = ();
    my(@cdefs) = ();
    my($yaxis) = "";
    my($bytes) = 0;
    my(@lines) = ();
    my($ct) = 0;
    my($usedArea) = 0;
    my($usedStack) = 0;
    my(@linePushed);
    my(%scaled);
    my(@target_pass_args);
    my(@gprints) = ();
    my($lasttime) = "unknown";

    # prepare a dsmap, using the target and targettype dicts
    # we do this outside the loop to keep the DS map from expanding

    my($ttype) = lc($targRef->{'target-type'});
    my($ttRef) = $gCT->configHash($name, 'targettype', $ttype, $targRef);
    my(%dsnamemap) = makeDSNameMap($ttRef->{'ds'});

    my($path) = $targRef->{'auto-target-path'};
    my($thisName, $mx);
    foreach $thisName (@mtargets) {
        # this allows local targets to use shorter name
        $thisName = "$path/$thisName" unless ($thisName =~ /^\//);
        # This regex lowercases just the last item in the thisName path:
        $thisName =~ s:([^/]+)$:lc $1:e;

        my($targRef) = $gCT->configHash($thisName, 'target', undef);
        ConfigTree::Cache::addAutoVariables($thisName, $targRef,
                                          $Common::global::gConfigRoot);
        my($thisTname) = $targRef->{'auto-target-name'};

        # check for paint-nan background option
        my ($paintNaN) = (defined($viewRef) && exists($viewRef->{'paint-nan'})
            && isTrue($viewRef->{'paint-nan'}));

        # take the inst from the url if it's there
        my($inst) = $gQ->param('inst');
        if (defined($inst)) {
            $targRef->{'inst'} = $inst;
        }

        # now that inst is set right, expand it.
        ConfigTree::Cache::expandHash($targRef, $targRef, \&Warn);

        # Then pick up the values
        # things we pick up form the target dict
        my($rrd) = $targRef->{'rrd-datafile'};
        $lasttime = scalar(localtime(RRDs::last($rrd)));
        $lasttime =~ s/:/\\:/g;

        # use the dslist to create a set of defs/cdefs

        my($ds);
        foreach $ds (split(/,/, $dslist)) {
            $ds = lc($ds);

            my($legend, $color, $colorCode, $drawAs, $scale,
               $colormax, $clmxCode, $drmxAs);

            my($gRef) = $gCT->configHash($name, 'graph', $ds, $targRef);
            # use view parameters if defined
            if (defined($viewRef)) {
                mergeHash($gRef,$viewRef,1);
            }

            $legend = graphParam($gRef, 'legend', $ds);

            if (($isMTarget) && (!$isMTargetsOps)) {
                $legend .= " ($thisTname)";
            }

            $color = graphParam($gRef, 'color', nextColor($colorRef));
            usedColor($color);

            $drawAs = graphParam($gRef, 'draw-as', 'LINE2');
            $drawAs = uc($drawAs);

            $drmxAs = graphParam($gRef, 'draw-max-as', 'LINE2');
            $drmxAs = uc($drmxAs);

            # if stack first must be area
            if ($drawAs eq "STACK") {
                if (!$usedStack && !$usedArea)  {
                    $drawAs = 'AREA';
                    $usedStack = 1;
                    $usedArea = 1;
                }
            }

            # only allow 1 area graph per gif
            if ($drawAs eq "AREA")  {
                if ($usedArea)  {
                    $drawAs = 'LINE2';
                }  else  {
                    $usedArea = 1;
                }
            }
            if ($drmxAs eq "AREA")  {
                if ($usedArea)  {
                    $drmxAs = 'LINE2';
                }  else  {
                    $usedArea = 1;
                }
            }

            # Note: the values in the hash %scaled are inserted as
            # lowercase.

            $scale = graphParam($gRef, 'scale', undef);
            if (defined($scale))  {
                $scaled{$ds} = 1;
            } else {
                $scaled{$ds} = 0;
            }

            # this way, we only take the _first_ yaxis that
            # was offered to us. (If they are trying to graph
            # different things on one graph, they get what they deserve:
            # a mis-labeled graph. So there.)
            if (! $yaxis) {
                $yaxis = graphParam($gRef, 'y-axis', '');
            }

            my($ym);

            $ym = graphParam($gRef, 'y-max');
            if (isNonNull($ym) && ! $ymaxlck) {
                if (! defined($ymax) || $ym > $ymax) {
                    $ymax = $ym;
                }
            }

            $ym = graphParam($gRef, 'y-min');
            if (isNonNull($ym) && ! $yminlck) {
                if (! defined($ymin) || $ym < $ymin) {
                    $ymin = $ym;
                }
            }

            # pick up the value for bytes, if we have not already
            # found it.
            if (! $bytes) {
                $bytes = isTrue(graphParam($gRef, 'bytes', 0));
            }

            $colorCode = colorToCode($colorRef, $color);

            # default to not doing max stuff, since it's still a bit
            # messy -- due to bad default RRA setup, etc.
            $mx = isTrue(graphParam($gRef, 'show-max', 0));
            # if hwParam, disable max, no matter what the config says
            $mx = 0 if (defined($hwParam));
            if ($mx) {
                $colormax = graphParam($gRef, 'max-color',
                                       nextColor($colorRef));
                usedColor($colormax);
                $clmxCode = colorToCode($colorRef, $colormax);
            }

            # push NaN bars on first in background
            $paintNaN && push @cdefs, "CDEF:unavail$ct=ds$ct,UN,INF,0,IF", "CDEF:unavailneg$ct=ds$ct,UN,NEGINF,0,IF";
            $paintNaN && push @lines, "AREA:unavail$ct#FFCCCC", "AREA:unavailneg$ct#FFCCCC";

            my($dsidx) = $dsnamemap{$ds};
            if (defined($dsidx)) {
                push @defs, "DEF:mx$ct=$rrd:$dsidx:MAX" if ($mx);
                push @defs, "DEF:ds$ct=$rrd:$dsidx:AVERAGE";
                if (defined($hwParam)) {
                    if ($hwParam eq "failures" || $hwParam eq "all") {
                        # push failures onto the line stack first now, so that
                        # they will appear in the background of the graph
                        push @defs, "DEF:fail$ct=$rrd:$dsidx:FAILURES";
                        # hard code colors for now
                        push @lines, "TICK:fail$ct#ffffa0:1.0:" .
                            "Failures $legend";
                    }
                    if ($hwParam eq "confidence" || $hwParam eq "all") {
                        push @defs, "DEF:hw$ct=$rrd:$dsidx:HWPREDICT";
                        push @defs, "DEF:dev$ct=$rrd:$dsidx:DEVPREDICT";
                        my $cbscale = graphParam($gRef,'confidence-band-scale',
                                                 2);
                        push @cdefs, "CDEF:upper$ct=hw$ct,dev$ct,$cbscale,*,+";
                        push @cdefs, "CDEF:lower$ct=hw$ct,dev$ct,$cbscale,*,-";
                        # Confidence bands need to be scaled along with the
                        # observed data
                        if (defined($scale)) {
                            push @cdefs, "CDEF:supper$ct=upper$ct,$scale";
                            push @cdefs, "CDEF:slower$ct=lower$ct,$scale";
                            push @lines, "LINE1:supper$ct#ff0000:" .
                                "Upper Bound $legend";
                            push @lines, "LINE1:slower$ct#ff0000:" .
                                "Lower Bound $legend";
                        } else {
                            push @lines, "LINE1:upper$ct#ff0000:" .
                                "Upper Bound $legend";
                            push @lines, "LINE1:lower$ct#ff0000:" .
                                "Lower Bound $legend";
                        }
                        # convert $drawAs
                        $drawAs = 'LINE2' if ($drawAs eq 'AREA');
                    }
                }

                my($mod) = $ct % $numDSs;
                if (defined($scale)) {
                    push @cdefs, "CDEF:smx$ct=mx$ct,$scale" if ($mx);
                    push @cdefs, "CDEF:sds$ct=ds$ct,$scale";
                    if ($isMTargetsOps) {
                        if (!$linePushed[$mod])  {
                            push @lines, "$drmxAs:totmx$mod#$clmxCode:" .
                                "Max $legend" if ($mx);
                            push @lines, "$drawAs:tot$mod#$colorCode:$legend";
                            $linePushed[$mod] = 1;
                        }
                    }  else  {
                        push @lines, "$drmxAs:smx$ct#$clmxCode:" .
                                     "Max $legend" if ($mx);
                        push @lines, "$drawAs:sds$ct#$colorCode:$legend";
                    }

                    if ($mx) {
                        push (@gprints,
                              "GPRINT:smx$ct:LAST:$legend  Last\\: %8.1lf%S",
                              "GPRINT:smx$ct:AVERAGE:Avg\\: %8.1lf%S",
                              "GPRINT:smx$ct:MIN:Min\\: %8.1lf%s",
                              "GPRINT:smx$ct:MAX:Max\\: %8.1lf%s\\l");
                    } else {
                        push (@gprints,
                              "GPRINT:sds$ct:LAST:$legend  Last\\: %8.1lf%S",
                              "GPRINT:sds$ct:AVERAGE:Avg\\: %8.1lf%S",
                              "GPRINT:sds$ct:MIN:Min\\: %8.1lf%s",
                              "GPRINT:sds$ct:MAX:Max\\: %8.1lf%s\\l");
                    }

                } else {
                    if ($isMTargetsOps)  {
                        if (!$linePushed[$mod])  {
                            push @lines, "$drmxAs:totmx$mod#$clmxCode:" .
                                         "Max $legend" if ($mx);
                            push @lines, "$drawAs:tot$mod#$colorCode:$legend";
                            $linePushed[$mod] = 1;
                        }
                    }  else  {
                        push @lines, "$drmxAs:mx$ct#$clmxCode:" .
                                     "Max $legend" if ($mx);
                        push @lines, "$drawAs:ds$ct#$colorCode:$legend";
                    }

                    if ($mx) {
                        push (@gprints,
                              "GPRINT:mx$ct:LAST:$legend  Last\\: %8.1lf%S",
                              "GPRINT:mx$ct:AVERAGE:Avg\\: %8.1lf%S",
                              "GPRINT:mx$ct:MIN:Min\\: %8.1lf%s",
                              "GPRINT:mx$ct:MAX:Max\\: %8.1lf%s\\l");
                    } else {
                        push (@gprints,
                              "GPRINT:ds$ct:LAST:$legend  Last\\: %8.1lf%S",
                              "GPRINT:ds$ct:AVERAGE:Avg\\: %8.1lf%S",
                              "GPRINT:ds$ct:MIN:Min\\: %8.1lf%s",
                              "GPRINT:ds$ct:MAX:Max\\: %8.1lf%s\\l");
                    }

                }
                $ct++;
            } else {
                # ERR: Unknown ds-name in dslist.
            }
        }

        # This is the end of the loop we do for each target
    }

    # This is where we will deal with arithmetic operations

    if ($isMTargetsOps)  {
        # first build the cdefs
        my($i) = -1;
        my(@dsnames, @mxnames);
        while ($i < ($ct-1))  {
            $i++;
            my $scalepre = "";
            if ($scaled{lc $dslist[$i % $numDSs]}) {
                $scalepre = "s";
            }
            push @{$dsnames[$i % $numDSs]}, "${scalepre}ds$i";
            push @{$mxnames[$i % $numDSs]}, "${scalepre}mx$i";
        }

        my($j) = 0;
        while ($j < $numDSs)  {
            my(@d) = @{$dsnames[$j]};
            my(@f) = @{$mxnames[$j]};

            # Deal with unknown values
            my($x, @e, @g, $sum, @l, @n);
            if ($unkIsZero)  {
                $sum = "sum";
                foreach $x (@d)  {
                    push @l, $x, "UN";
                    push @e, $x, "UN", 0, $x, "IF";
                }
                foreach $x (@f)  {
                    push @n, $x, "UN";
                    push @g, $x, "UN", 0, $x, "IF";
                }
            } else {
                $sum = "";
                @l = @n = ();
                @e = @d;
                @g = @f;
            }

            my($str2) = "CDEF:${sum}tot$j=" .
                join(',', @e, convertOps($MTargetsOps, $#d+1));
            push @cdefs, $str2;
            push @cdefs, "CDEF:tot$j=" .
                join(',', @l, "UNKN", ("${sum}tot$j", "IF") x @d) if ($sum);
            if ($mx) {
                my($str2) = "CDEF:${sum}totmx$j=" .
                    join(',', @g, convertOps($MTargetsOps, $#d+1));
                push @cdefs, $str2;
                push @cdefs, "CDEF:totmx$j=" .
                    join(',', @n, "UNKN", ("${sum}totmx$j", "IF") x @f)
                                                                if ($sum);
            }

            $j++;
        }

        # we built the line commands earlier
    }

    # add a vrule for each "zero" time:
    #   for a daily graph, zero times are midnights
    #   for a weekly graph, zero times are Monday Midnights
    #   for a monthly graph, zero times are 1st of the month midnight
    #   for a yearly graph, zero times are 1st of the year

    my($vruleColor) = graphParam($gRefDef, 'vrule-color', undef);
    my(@vrules);
    if (defined($vruleColor) and $vruleColor ne 'none') {
        $vruleColor = colorToCode($colorRef, $vruleColor);

        my($rangeType) = rangeType($range);

        # first, find the time of the most recent zero mark
        my($timeToZeroTime) = 0;
        my($deltaZeroTime) = 0;     # the number of seconds between zero times
        my($now) = time();
        my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
                                                           localtime($now);

        # find out how many seconds we are past the last zero time
        $timeToZeroTime += $sec;
        $timeToZeroTime += $min * 60;
        $timeToZeroTime += $hour * 60 * 60;
        $deltaZeroTime = 60 * 60 * 24;

        if ($rangeType == $kTypeWeekly) {
            my($numDaysToMonday) = ($wday - 1);
            $timeToZeroTime += $numDaysToMonday * 60 * 60 * 24;
            $deltaZeroTime *= 7;
        }
        if ($rangeType == $kTypeMonthly) {
            $timeToZeroTime += ($mday - 1) * 60 * 60 * 24;
            $deltaZeroTime *= 30;
        }
        if ($rangeType == $kTypeYearly) {
            $timeToZeroTime += $yday * 60 * 60 * 24;
            # yikes... what about leap year? Ick.
            $deltaZeroTime *= 365;
        }
        my($zeroTime) = $now - $timeToZeroTime;

        # loop and add a vrule for every zero point from
        # now back to ($now - $range). This loop has
        # the nice property that it will skip even the first VRULE,
        # if that wouldn't fit on the graph.
        while ($zeroTime > ($now - $range)) {
            push @vrules, "VRULE:$zeroTime#$vruleColor:";
            $zeroTime -= $deltaZeroTime;
        }
    }

    if ($#defs+1 == 0 || $#lines+1 == 0) {
        Error("No graph to make?");
    }

    if (! -d $Common::global::gCacheDir) {
        mkdir($Common::global::gCacheDir, 0777);
        chmod(0777, $Common::global::gCacheDir);
    }

    # this sets -b based on the value of the bytes parameter
    my(@base) = ( '--base', '1000' );
    if ($bytes) {
        @base = ( '--base', '1024' );
    }

    # handle passthrough arguments
    my($pass) = graphParam($gRefDef, 'rrd-graph-args', undef);
    my(@pass) = ();
    if ($#target_pass_args >= 0) {
        push(@pass, @target_pass_args);
    }
    if (defined($pass)) {
        @pass = split(/\s+/, $pass);
    }

    my(@rules, $e);
    my($eventlist) = (undef);
    if (defined($viewRef)) {
        $eventlist = $viewRef->{'events'};
    } else {
        $eventlist = $targRef->{'events'};
    }
    if ($eventlist) {
        foreach $e (split(/\s*,\s*/, $eventlist)) {
            my($evRef) = $gCT->configHash($name, 'event', lc($e), $targRef);
            if ($evRef && $evRef->{'time'}) {
                push @rules, join('', 'VRULE', ':', $evRef->{'time'}, '#',
                                  colorToCode($colorRef, $evRef->{'color'}),
                                  ':', $evRef->{'name'});
            }
        }
        push @rules, 'COMMENT:\s';
    }

    my(@rigid);

    if (isNonNull($ymin) || isNonNull($ymax)) {
        push @rigid, '-r';
        push @rigid, '-u', $ymax if (isNonNull($ymax));
        push @rigid, '-l', $ymin if (isNonNull($ymin));
    }

    my(@fmt);
    if ($type eq 'gif') {
        @fmt = ('-a', 'GIF');
    } else {
        @fmt = ('-a', 'PNG');
    }

    if (isTrue($useGprint)) {
        my $title = $tname;
        if (defined($targRef->{'display-name'})) {
            $title = $targRef->{'display-name'};
        }
        unshift @gprints, "--title", $title;
        unshift @gprints, "COMMENT:\\s", "COMMENT:\\s";
        push @gprints, "COMMENT:\\s", "COMMENT:Last updated at $lasttime",
    } else {
        @gprints = ();
    }

    my(@args) = ($imageName, @fmt, @rigid, @interlaced,
                 @base, @pass, @rules,
                 '--start', "-$range",
                 '--vertical-label', $yaxis,
                 '--width',          $width,
                 '--height',         $height,
                 @defs, @cdefs, @lines, @vrules, @gprints);

    # we unlink the image so that if there's a failure, we
    # won't accidentally display an old image.

    Debug("RRDs::graph " . join(" ", @args));
    unlink($imageName);
    my($avg, $w, $h) = RRDs::graph(@args);

    if (my $error = RRDs::error) {
        Warn("Unable to create graph: $error\n");
        Warn("rrd graph: ".join(' ', @args)."\n");
    }

    my($wh) = graphParam($gRefDef, 'width-hint', undef);
    my($hh) = graphParam($gRefDef, 'height-hint', undef);

    Warn("Actual graph width ($w) differs from width-hint ($wh).")
        if ($w && $wh && ($wh != $w));
    Warn("Actual graph height ($h) differs from height-hint ($hh).")
        if ($h && $hh && ($hh != $h));


    sprayPic($imageName);
    unlink($imageName) if $needUnlink;
}

sub suckPic {
    my($pic) = @_;
    my($res) = '';

    if (! open(GIF, "<$pic")) {
        Warn("Could not open $pic: $!");
        return;
    } else {
        my($stuff, $len);
        binmode(GIF);
        while ($len = read(GIF, $stuff, 8192)) {
            $res .= $stuff;
        }
        close(GIF);
    }

    return $res;
}

sub sprayPic {
    my($pic) = @_;

    # we need to make certain there are no buffering problems here.
    local($|) = 1;

    my($picData) = suckPic($pic);

    if (! defined($picData)) {
        $pic = "/usr/share/cricket/images/failed.gif";
        $picData = suckPic($pic);
        if (! defined($picData)) {
            print $gQ->header('text/plain');
            print "Could not send failure gif: $!\n";

            Warn("Could not send failure gif: $!");
            return;
        }
    }

    if ($pic =~ /png$/i) {
        print $gQ->header('image/png');
    } else {
        print $gQ->header('image/gif');
    }
    print $picData;

    return 1;
}

sub getHTMLDict {
    my($name, $targRef) = @_;

    my($h) = $gCT->configHash($name, 'html');

    $h->{'auto-long-version'} = $Common::global::gVersion;
    my($sv) = ($Common::global::gVersion =~ /Cricket version (.*) \(/);
    $sv = "?" unless ($sv);
    $h->{'auto-short-version'} = $sv;

    $h->{'auto-error'} = $gError;
    $h->{'auto-title'} = '';

    # put the contents of the target dict into the HTML dict
    # so that it will be available for expansion
    my($tag);
    foreach $tag (keys(%{$targRef})) {
        $h->{$tag} = $targRef->{$tag};
    }

    return $h;
}

sub htmlHeader {
    my($name, $targRef, $title) = @_;
    my(@headerArgs);

    my($h) = getHTMLDict($name, $targRef);
    $h->{'auto-title'} = $title;
    ConfigTree::Cache::expandHash($h, $h, \&Warn);

    print $gQ->header('text/html');
    print "<html>\n";
    print "<head>\n";
    if ($h->{'head'}) {
        print $h->{'head'}, "\n";
    }
    print "<meta name=\"generator\" content=\"",
          $h->{'auto-long-version'}, "\">\n";
    print "</head>\n";

    my($body) = $h->{'body-options'};
    $body = "" unless (defined($body));

    print "<body $body>\n";

    if ($h->{'page-header'}) {
        print $h->{'page-header'}, "\n";
    }

    return;
}

sub htmlFooter {
    my($name, $targRef) = @_;

    my($h) = getHTMLDict($name, $targRef);
    ConfigTree::Cache::expandHash($h, $h, \&Warn);

    if ($h->{'page-footer'}) {
        print $h->{'page-footer'}, "\n";
    }

    print "</body>\n";
    print "</html>\n";
    return;
}

# routines to manage the colors

sub usedColor {
    my($c) = @_;
    my($i, @res);
    foreach $i (@gColors) {
        push @res, $i unless (lc($i) eq lc($c));
    }
    @gColors = @res;
}

sub nextColor {
    my($colorRef) = @_;

    # make the color list, when necessary
    if (! $gColorInit) {
        if (defined($colorRef)) {
            my($order) = $colorRef->{'--order--'};
            if (! defined($order)) {
                @gColors = sort keys %{$colorRef};
            } else {
                @gColors = split(/\s*,\s*/, $order);
            }
            $gColorInit = 1;
        } else {
            # there are no colors available...
            @gColors = ();
        }
    }

    my($color) = '00cc00';      # default to green if none left (or given)
    if ($#gColors+1 > 0) {
        $color = $gColors[0];
    }
    return $color;
}

sub colorToCode {
    my($colorRef, $color) = @_;
    my($code) = $colorRef->{$color};
    # if we didn't find one, then use the passed in color, assuming it's
    # a color code...
    $code = $color if (! defined($code));
    return $code;
}

# This routine chooses the right value for a graph parameter;
# If uses the default passed in, then the value from the --default--
# dict, then the value from the dict named after the ds (if given).

sub graphParam {
    my($gRef, $param, $default) = @_;

    $param = lc($param);
    my($res) = $default;

    if (defined($gRef->{$param})) {
        $res = $gRef->{$param};
    }
    return $res;
}

# make the range-size navigation links
sub makeNavLinks {
    my $reqRanges = shift;
    my($r, @links);
    my @r     = ('h', 'd', 'w', 'm', ,'y', 'h:d:w', 'm:y', 'h:d:w:m:y');
    my @rName = ('Hourly', 'Daily', 'Weekly', 'Monthly', 'Yearly', 'Short-Term',
                  'Long-Term', 'All');
    my($i) = 0;
    foreach $r (@r) {
        $gQ->param('ranges', $r[$i]);
        my($me) = makeUrl($gQ);
        if (defined($reqRanges) && $reqRanges eq $r[$i]) {
            push @links, "[ $rName[$i] ]&nbsp;&nbsp;&nbsp;";
        } else {
            push @links, "<a href=\"$me\">$rName[$i]</a>" .
                "&nbsp;&nbsp;&nbsp;";
        }
        $i++;
    }
    return @links;
}

sub htmlCurrentPath {
    my($ct, $targRef, $target) = @_;
    my($html);

    return "" if (!defined($targRef->{'show-path'}) ||
                  $targRef->{'show-path'} ne "yes");

    if ($target !~ /^\s*\/\s*$/) {
        $html = "Current path:\n";
        $html .= htmlCurrentPathLinks($ct, $target);
        $html .= "<br>\n";
    }

    return $html;
}

sub htmlCurrentPathLinks {
    my($ct, $target) = @_;
    my($html);

    my($path) = "/";
    foreach my $p (split(/\//, $target)) {
        my($lQ) = new CGI;
        $path .= "$p";
        $lQ->delete_all() unless ($ct->isLeaf($path));
        urlTarget($lQ, $path);
        $p .= "/" unless ($p =~ /\/$/) || ($ct->isLeaf($path));
        $path .= "/" unless $path =~ /\/$/;
        $html .= " <a href=\"" . makeUrl($lQ) . "\">" . $p . "</a>\n";
    }

    return $html;
}

# make the Holt-Winters navigation links
sub makeHwNavLinks {
    my (@links) = ();
    my ($localurl);
    $gQ->param('ranges','d');
    $gQ->param('hw','confidence');
    $localurl = makeUrl($gQ);
    push @links, "<a href=\"$localurl\">Confidence Bounds</a>" .
        "&nbsp;&nbsp;&nbsp;";
    $gQ->param('hw','failures');
    $localurl = makeUrl($gQ);
    push @links, "<a href=\"$localurl\">Failures</a>" .
        "&nbsp;&nbsp;&nbsp;";
    $gQ->param('hw','all');
    $localurl = makeUrl($gQ);
    push @links, "<a href=\"$localurl\">Confidence Bounds and Failures</a>" .
        "&nbsp;&nbsp;&nbsp;";
    $gQ->delete('hw');
    return @links;
}

sub generateImageName {
    my($q, $type) = @_;
    my($param, $md5);

    $md5 = new Digest::MD5;

    # make sure to munge $target correctly if $gUrlStyle = pathinfo
    $md5->add(urlTarget($q));

    foreach $param ($q->param()) {
        next if ($param eq 'rand');
        next if ($param eq 'target');
        if ($param eq 'cache') {
            if (lc($q->param($param)) eq 'no') {
                return;
            }
        }
        $md5->add($param, $q->param($param));
    }
    my($hash) = unpack("H8", $md5->digest());

    return "$Common::global::gCacheDir/cricket-$hash.$type";
}

sub byFirstVal {
    $a->[0] <=> $b->[0];
}

# fixHome:
# This subroutine is a bit of a hack to properly set $HOME based
# on the assumption that this script will be called via a URL that
# looks like: http://www/~cricket/grapher.cgi. If this doesn't apply
# to your installation, then you might want to simply uncomment the
# brute force method, below.
# Note that effective with the 1.0.3 release of Cricket, the preferred
# method is to add gCricketHome to cricket-conf.pl and put that in the
# same directory as the CGI script (or in /usr/local/etc/cricket-conf.pl).

sub fixHome {

    # brute force:
    # $Common::global::gCricketHome = '/path/to/cricket/home';
    # return;

    return if (defined($Common::global::gCricketHome) &&
               $Common::global::gCricketHome =~ /\//);

    my($sname) = $gQ->script_name();
    if ($sname =~ /\/~([^\/]*)\//) {
        my($username) = $1;
        my($home) = (getpwnam($username))[7];
        if ($home) {
            $Common::global::gCricketHome = $home;
            return;
        } else {
            Info("Could not find a home directory for user $username." .
                 "gCricketHome is probably not set right.");
        }
    } else {
        Info("Could not find a username in SCRIPT_NAME. " .
             "gCricketHome is probably not set right.");
    }
    # Last ditch effort... If all else fails, assume Cricket's home
    # is one directory up from grapher.cgi.
    $Common::global::gCricketHome ||= $Common::global::gInstallRoot . "/..";
}

sub convertOps {
    my($mto, $num) = @_;

    if (lc($mto) eq 'sum()') {
        my($i, @plusses);
        for ($i = 0; $i < $num-1; $i++) {
            push @plusses, '+';
        }
        return join(',', @plusses);
    }

    if (lc($mto) eq 'avg()') {
        my($i, @plusses);
        for ($i = 0; $i < $num-1; $i++) {
            push @plusses, '+';
        }
        push @plusses, $num;
        push @plusses, '/';
        return join(',', @plusses);
    }

    return $mto;
}

sub makeInstMap {
    my($ins, $inst) = @_;

    return unless $ins;

    my(@ins) = Eval($ins);

    if (! $inst) {
        $inst = '()';
    }

    my($hash) = {};
    my($ct) = 0;
    my($i);

    my(@inst) = Eval($inst);

    if ($#inst+1 > 0) {
        # they gave us an inst array, so match them up one to
        # one.
        foreach $i (@inst) {
            $hash->{$i} = $ins[$ct++];
        }
        Debug("inst array is: ", join(", ", @inst));
    } else {
        # there's 0 or 1 inst's, so make a simple table
        foreach $i (@ins) {
            $hash->{$ct++} = $i;
        }
    }

    return $hash;
}

sub prepareValue {
    my($value, $dosi, $bytes, $precision, $space, $unit) = @_;

    if (isNaN($value)) {
        return "$value$space$unit";
    }

    my($prefix) = "";
    if ($dosi) {
        ($value, $prefix) = si_unit($value, $bytes);
    }

    if ($value ne "?" && !isNaN($value)) {
        $value = sprintf("%0.${precision}f", $value);
    }
    return "$value$space$prefix$unit";
}

# Return a URL as a text string, based on the current state of the
# $cgi object.
sub makeUrl {
    my $cgi = shift;
    return $cgi->self_url() if $gUseSelfUrl;
    return $cgi->url(-relative=>$gUseRelativeUrl,
                     -query=>1,
                     -path_info=>$gUsePathInfo);
}

# Get or set the target from the $cgi object.
sub urlTarget {
    my $cgi = shift;
    my $target = shift;
    return $cgi->param('target', $target) if !$gUsePathInfo;
    if (!defined($target)) {
        $target = $cgi->path_info();
        $target =~ s/\/+$//;  # Zonk any trailing slashes
        $target ||= "/";      # but we name the root explicitly
        return $target;
    }
    $cgi->path_info($target);
}

# Find matching targets for arbitrary tag/value searching.
# Or default value search on snmp-host and display-name tags.
sub searchHandleTarget {
    my($name) = shift;
    my($search) = shift;
    my($searchHash) = shift;
    my($match) = 0;
    my($path) = "";
    my($target) = $gCT->configHash($name, 'target', undef, 1);

    $path = $target->{'auto-target-path'};
    return if (exists($searchHash->{$path}));

    # Return if it is not the chassis target 
    # or contains at least one tag/value pair
    return unless (defined($target->{'auto-target-name'}) && 
           ($search =~ /\w+[=|!=]\w+/ || $target->{'auto-target-name'} eq 'chassis'));

    foreach my $val (split ",", $search) {
        if ($val =~ /\w+=\w+/) {
            my ($attr, $tval) = split (/=/, $val);
            if (defined($target->{$attr}) && ($target->{$attr} =~ /$tval/i)) {
                $match = 1;
            }
        } elsif ($val =~ /\w+!=\w+/) {
            my ($attr, $tval) = split (/!=/, $val);
            if (defined($target->{$attr}) && ($target->{$attr} !~ /$tval/i)) {
                $match = 1;
            }
        } elsif (defined($target->{'display-name'})
             && $target->{'display-name'} =~ /$val/i) {
            $match = 1;
        } elsif (defined($target->{'snmp-host'}) 
             && $target->{'snmp-host'} =~ /$val/i) {
            $match = 1;
        }
    }
    # No duplicate targets should be displayed after a search
    $searchHash->{$path} = $path if ($match);
}

# Local Variables:
# mode: perl
# indent-tabs-mode: nil
# tab-width: 4
# perl-indent-level: 4
# End: