-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweathermap
executable file
·1469 lines (1301 loc) · 43.8 KB
/
weathermap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
# Network Weathermap for Devmon version 1.1.5 (20090610)
# Derived from:
# Network Wearthermap - version 1.1.1 (20040422)
# http://netmon.grnet.gr/weathermap/
# Panagiotis Christias, <christias@noc.ntua.gr>
# Data structures
# nodes = (
# omicron => (
# pos => [xxx,yyy]
# image => xxx.png,
# label => "Omicron",
# interfaces => (
# gi2/6 => )
# rates => [55, 65],
# desc => "foo",
# status => "green",
# ),
# gi5/7 => [23, 45, "bar"],
# ),
# eta => (),
# );
# links = (
# omicron-eta => (
# nodea => omicron,
# nodeb => eta,
# inta => Gi2/6,
# intb => Gi5/4,
# ),
#
=head1 NAME
weathermap.pl - A Network Weathermap script for Hobbit/Xymon and Devmon
=head1 SYNOPSIS
-c, --config=FILE configuration file or directory of configuration files
-o, --output=FILE output image file (or directory in the case of multiple maps)
to write to
-s, --scale=INT Scale the entire map by an integer value (for large displays)
-f, --font=FILE Default TTF font to use (for FONTS 1-5 when scaled)
-t --theme Theme color to use (white|black, default white)
-v, --version print version
-h, --help print this text
-d, --debug enable debug output
--htmloutput=FILE enable HTML output to FILE, or a directory
--image-uri=URI use URI for the IMG tag when generating HTML
--icondir=DIR Directory where node icons are to be found
=head1 DESCRIPTION
weathermap.pl is a perl script to generate a weathermap-like diagram of a network.
This version, derived from Network Wearthermap - version 1.1.1 (20040422), available at
L<http://netmon.grnet.gr/weathermap/>, has been modified as follows:
=over
=item *
All data required to generate values (e.g. link utilisation) is queried from
Hobbit/Xymon. By default, link values will be determined from the table entry
in the if_load test that matches the INTERFACES value provided in the map
configuration file for that end of the link. Additionally, the "overlib"
popups over links will include the link description (if available to devmon).
Thus, the default behaviour should work well with a Hobbit/Xymon and Devmon
setup monitoring network devices, and should require much less configuration
(and significantly easier, if you are happy to edit config files) than most
other weathermap implementations.
=item *
By default, an overlib graph of the CPU utilisation of the device is offered
for mouseover events on devices.
=item *
Support for the majority of the configuration file syntax used by PHP Weathermap
(from L<http://www.network-weathermap.com>) has been added.
=item *
If all the options that take a file name are directories, all configuration
files with name ending in .conf will be processed, and the image and output
file names will be the part of the file name preceding .conf. E.g., if
the options -c /etc/xymon/weathermap -o /var/lib/xymon/www/weathermap are used,
and /etc/xymon/weathermap contains lan.conf and wan.conf, maps will created
for each, as lan.html with lan.png, and wan.conf with wan.png respectively.
=item *
Defaults for a number of options are taken from Xymon/Hobbit environment
variables, for good default operation from the hobbitlaunch.
=back
=head1 CONFIGURATION FILE SYNTAX
The configuration file consists of global options, then NODE and LINK sections.
PHP Network Weathermap configuration directives (L<http://www.network-weathermap.com/manual/latest/pages/config-reference.html>)
and their support status in this script:
=head2 NODE options
NODE <nodename> Must be hostname used by Xymon/Devmon
POSITION x y y
LABEL labeltext y Name that will be displayed on map for node
TARGET <rrd> N/A
USESCALE scalename n
MAXVALUE threshold N/A Uses Hobbit status by default
ICON filename y Except 'none','box','rbox','round'
USEICONSCALE N/A Uses Hobbit status by default
LABELOFFSET x y or x DIR n
LABELANGLE (0|90|180|270) n
LABELFONT ([1-5]) y Built-in fonts, and fonts defined with FONTDEFINE
COLOR n, use Hobbit status to set label background color
INFOURL url N/A Use Hobbit by default
OVERLIBGRAPH url N/A Use Hobbit by default
OVERLIBWIDTH x n
OVERLIBHEIGHT y n
OVERLIBCAPTION text N/A Use Hobbit/Devmon by default
NOTES text n
SET n
=head2 Link Options
LINK <linkname> y Link name, must be unique, does not appear anywhere
NODES y (no offset support) nodenames of nodes to link
TARGET y, but format is hobbit:testname:instancename hobbit:testname:instancename
e.g.: hobbit:if_load:Gi2/5 hobbit:if_load:Gi2/4
but hobbit: is optional, and test defaults to
if_load, so in many caes you can use:
TARGET Gi2/5 Gi2/6
USESCALE n
WIDTH y
BANDWIDTH n (Uses Devmon's bandwidth calculation)
DUPLEX n (In future could support Devmon cdp test)
BWLABEL n
INBWFORMAT n
OUTBWFORMAT n
BWSTYLE ['classic'|'angled'] n
BWLABELPOS n
BWFONT n
INCOMMENT n Uses Interface name for Devmon/if_load
OUTCOMMENT n Uses Interface name for Devmon/if_load
COMMENTFONT n
COMMENTPOS n
SPLITPOS x ([50]) n
COLOR n (Determined by Hobbit status color)
INFOURL n
ININFOURL n
OUTINFOURL n
OVERLIBGRAPH n Uses Hobbit based on TARGET value
INOVERLIBGRAPH n Uses Hobbit based on TARGET value
OUTOVERLIBGRAPH n Uses Hobbit based on TARGET value
OVERLIBWIDTH n
OVERLIBHEIGHT n
OVERLIBCAPTION N/A Uses Interface name from Devmon/if_load
INOVERLIBCAPTION N/A Uses Interface name from Devmon/if_load
OUTOVERLIBCAPTION N/A Uses Interface name from Devmon/if_load
NOTES n
INNOTES n
OUTNOTES n
VIA y
LINKSTYLE n
ARROWSTYLE n
SET n
=head2 Global options
BACKGROUND y
WIDTH y
HEIGHT y
HTMLOUTPUTFILE n
IMAGEOUTPUTFILE n (should support png, jpeg and gif)
FONTDEFINE fontnumber ttffile size y
*FONT fontnumber - TITLEFONT,KEYFONT,TIMEFONT y
*COLOR n
TIMEPOS y
TITLE,TITLEFONT,TITLECOLOR y (except TITLEFONT)
TITLEPOS y
KILO n
HTMLSTYLE y
SCALE [scalename] min max r g b [r2 g2 b2] [tagtext] y except r2 g2 b2
KEYPOS [scalename] x y [headingstring] y (except scalename)
KEYFONT y
KEYTEXTCOLOR y
KEYBGCOLOR y
KEYOUTLINECOLOR y
KEYSTYLE [scalename] stylename=['classic','vertical','horizontal'] [size] n
SET n
=cut
my $VERSION = "1.1.5";
use Getopt::Long 2.32;
use GD;
use Data::Dumper;
use File::Find;
use Pod::Usage;
use strict;
################################################################
#
# Configuration parameters
#
my $WGET = "/usr/local/bin/wget -qO -";
#$HTMLSTYLE = "static";
my $HTMLSTYLE = "overlib";
my $DEBUG = 0;
if ($ENV{'DEBUG'}) { $DEBUG=1;}
my ($WIDTH,$HEIGHT);
my $BWLABELS = "percent";
my $REVERSEDIRECTION = 0;
my $BB = "/usr/lib64/xymon/client/bin/bb";
my $BBDISP = "localhost";
my $BBWEBHOST = "http://localhost";
my $BBWEBPAGE = "/usr/lib64/xymon/server/bin/bb-webpage";
my $CGIBINURL = "/xymon-cgi";
my $GRAPHCGI = "hobbitgraph.sh";
my $ICONDIR="/etc/xymon/weathermap/images";
my $SCALE=1;
#$TTFONT="/etc/hobbit/LiberationSans-Regular.ttf";
my $TTFONT="/usr/share/rrdtool/fonts/DejaVuSansMono-Roman.ttf";
#$TTFONT="/usr/share/fonts/TTF/dejavu/DejaVuSansCondensed.ttf";
my $IMGCOMPRESS="/usr/bin/optipng";
#$TTFONT="/usr/share/fonts/bitstream-vera/Vera.ttf";
my $name;
my $indexscale = '25';
my $ncols = '2';
my $makeindex = 0;
my $theme = 'black';
my @mapsasked;
#$BBWEBHOSTURL = "http://localhost/hobbit"
#
################################################################
my $CONFIG;
my $HTMLOUTPUT;
my $OVERLIBJSURL;
my $OUTPUT;
my $OUTPUTURL;
my $OUTPUTURI;
my $OUTPUTDIR;
my $HTMLOUTPUTDIR;
my @defaultmaps;
#variables that need to be investigated
my $allmap;
#variables populated by read_config:
my %nodes;
my %font;
my %label;
my %links;
my %linkwidth;
my %fontdef;
my %scale;
my $scales;
my $title;
my %text;
my %pos;
my $background;
my $map;
my $bg;
my %color;
my %colorspec;
my $white;
my $gray;
my $black;
my $darkgray;
my $red;
if ($ENV{'BBHOME'}) {
#$CONFIG = "$ENV{'BBHOME'}/etc/weathermap.conf";
$CONFIG = "$ENV{'BBHOME'}/etc/weathermap";
@defaultmaps = ("$ENV{'BBHOME'}/etc/weathermap");
$BBWEBPAGE = "$ENV{'BBHOME'}/bin/bb-webpage";
$TTFONT = "$ENV{'BBHOME'}/etc/LiberationMono-Regular.ttf";
} else {
$CONFIG = "weathermap.conf";
@defaultmaps = ("weathermap.conf");
}
if ($ENV{'XYMONHOME'}) {
#Xymon 4.3.x has xymonpage instead of bb-webpage
$BBWEBPAGE = "$ENV{'XYMONHOME'}/bin/xymonpage";
#Graph CGI was renamed:
$GRAPHCGI = "showgraph.sh";
}
if ($ENV{'BBWWW'}) {
$OUTPUT = "$ENV{'BBWWW'}/weathermap";
$HTMLOUTPUT = "$ENV{BBWWW}/weathermap";
$OUTPUTURL = "$ENV{'BBSERVERWWWURL'}/weathermap";
$OVERLIBJSURL = "$ENV{'BBSERVERWWWURL'}/weathermap/overlib_mini.js";
$ICONDIR = "$ENV{'BBWWW'}/weathermap/images";
} else {
$OUTPUT = "weathermap.png";
$HTMLOUTPUT = "";
$OVERLIBJSURL = "overlib_mini.js";
}
if ($ENV{'BB'}) {
$BB = $ENV{'BB'};
}
if ($ENV{'BBDISP'}) {
$BBDISP = $ENV{'BBDISP'};
}
if ($ENV{'BBWEBHOST'}) {
$BBWEBHOST = $ENV{'BBWEBHOST'};
}
if ($ENV{'CGIBINURL'}) {
$CGIBINURL = $ENV{'CGIBINURL'};
}
my %optctl=();
#GetOptions(\%optctl, "config:s", "output:s", "version", "help", "debug", "") || exit(1);
GetOptions(\%optctl,
"image-uri:s",
"config:s" => \@mapsasked,
"output:s",
"version",
"help",
"debug",
"htmloutput:s",
"scale:s",
"theme:s",
# "thumbnailscale:s",
"ncols:s",
"font:s",
"icondir:s",
"") || exit(1);
#if($optctl{"config"}) { $CONFIG = $optctl{"config"} };
if($optctl{"output"}) { $OUTPUT = $optctl{"output"} };
if($optctl{"htmloutput"}) { $HTMLOUTPUT = $optctl{"htmloutput"} };
if($optctl{"scale"}) { $SCALE = $optctl{"scale"} };
if($optctl{"version"}) { &version; exit; }
if($optctl{"help"}) { pod2usage; exit; }
if($optctl{"debug"}) { $DEBUG=1; }
#if($optctl{'thumbnailscale'}) {$indexscale=$optctl{'thumbnailscale'}};
if($optctl{'theme'}) {$theme=$optctl{'theme'}};
if($optctl{'ncols'}) {$ncols=$optctl{'ncols'}};
if($optctl{'font'}) {$TTFONT=$optctl{'font'}};
if($optctl{'icondir'}) {$ICONDIR=$optctl{'icondir'}};
$WIDTH = $WIDTH * $SCALE;
$HEIGHT= $HEIGHT * $SCALE;
my @maps;
my $worstcolor;
my $worstindexcolor = 'green';
my $htmlindexcontent;
if (@mapsasked eq 0) { @mapsasked = @defaultmaps;}
foreach (@mapsasked) {
if (-d $_) {
print "Adding all maps in directory $_\n" if $DEBUG;
find(sub {push @maps, $File::Find::name if /\.conf$/},$_);
} elsif (-f $_) {
print "Adding map $_\n" if $DEBUG;
push @maps,$_;
}
}
if (@maps gt 1 && (! -d $HTMLOUTPUT || ! -d $OUTPUT)) {
print "Multiple maps requires output and htmloutput to be directories\n";
print "I have the following maps: @maps\n";
exit 1;
}
if (-d $HTMLOUTPUT) {
$makeindex = 1;
$HTMLOUTPUTDIR=$HTMLOUTPUT;
$OUTPUTDIR=$OUTPUT;
$htmlindexcontent .= '<table border=0>' . "\n";
}
my $mapnum = 1;
foreach (@maps) {
my $colnum = $mapnum % $ncols;
my @components = split '/';
$name = $components[-1];
$name =~ s/.conf$//g;
if (@maps gt 1) {
$OUTPUT = "$OUTPUTDIR/${name}.png";
$OUTPUTURI = "${name}.png";
$HTMLOUTPUT = "$HTMLOUTPUTDIR/${name}.html";
}
print "Generating map for $_ in $OUTPUT with $HTMLOUTPUT\n" if $DEBUG;
generate($_);
print "Finished generating map for $_\n" if $DEBUG;
if($makeindex){
print "Adding link ${name}.html with image ${name}.png to index column $colnum\n" if $DEBUG;
if ($colnum eq 1) {$htmlindexcontent .= "<tr>\n";}
$htmlindexcontent .= "<td><a href=\"${name}.html\"><img src=\"${name}.png\" width=\"100%\" height=\"100%\" border=\"0\"></a></td>\n";
if ($colnum eq 0) {$htmlindexcontent .= "</tr>\n";}
}
$mapnum = $mapnum+1;
}
if ($makeindex eq 1) {
$htmlindexcontent .= '</table>' . "\n";
print "Writing map index with color $worstindexcolor to $HTMLOUTPUTDIR/index.html.$$\n" if $DEBUG;
open(HTMLINDEX,"|$BBWEBPAGE --color=$worstindexcolor > $HTMLOUTPUTDIR/index.html.$$") or die "$HTMLOUTPUT: $!\n";
print HTMLINDEX $htmlindexcontent;
print "Closing index\n" if ($DEBUG);
close HTMLINDEX;
rename("$HTMLOUTPUTDIR/index.html.$$","$HTMLOUTPUTDIR/index.html") or warn "Renaming $HTMLOUTPUTDIR/index.html.$$ to $HTMLOUTPUTDIR/index.html failed: $!";
}
exit;
sub generate
# Reads one map config file, and generates a weathermap
{
my ($CONFIG) = @_;
#Set defaults
$WIDTH = 750 * $SCALE;
$HEIGHT = 550 * $SCALE;
%pos = (
KEY => [10 * $SCALE,$HEIGHT/2],
TIME => [$WIDTH/2,$HEIGHT - 20 * $SCALE],
TITLE => [$WIDTH/2,20 * $SCALE],
);
%font = (
KEY => 4,
LABEL => 4,
TITLE => 5,
TIME => 1,
);
if ($theme eq 'white') {
%colorspec = (
# 'BG' => [255,255,255,127],
'BG' => [255,255,255],
'KEYBG' => [255,255,255],
'KEYOUTLINE' => [0,0,0],
'KEYTEXT' => [0,0,0],
'TIME' => [0,0,0],
'TITLE' => [0,0,0],
);
} else {
warn "Unknown theme $theme, using black" if ($theme ne 'black');
%colorspec = (
'BG' => [0,0,0,127],
# 'BG' => [0,0,0],
'KEYBG' => [0,0,0],
'KEYOUTLINE' => [255,255,255],
'KEYTEXT' => [255,255,255],
'TIME' => [255,255,255],
'TITLE' => [255,255,255],
);
}
%scale = (
'0-1' => [255,255,255],
'1-10' => [140,0,255],
'10-25' => [32,32,255],
'25-50' => [0,192,255],
'50-75' => [0,240,0],
'75-90' => [240,240,0],
'90-95' => [255,128,0],
'95-100' => [255,0,0],
);
$scales = scalar keys(%scale);
print "Reading config $CONFIG\n" if $DEBUG;
&read_config($CONFIG) or next;
$allmap = '';
print Dumper(%nodes) if ($DEBUG);
$worstcolor = 'green';
if($background){
open (PNG,"$background") or { warn "$background: $!\n" and return 0};
$map = newFromPng GD::Image(*{PNG}) or {warn "newFromPng failed." and return 0};
($WIDTH,$HEIGHT) = $map->getBounds();
$bg = $map->colorClosest(@{$colorspec{'BG'}}[0 .. 2]);
$map->transparent($bg) if ($colorspec{'BG'}[3] == 127);
close PNG;
} else {
print "Starting new image with dimensions $WIDTH x $HEIGHT\n" if $DEBUG;
$map=new GD::Image($WIDTH,$HEIGHT,1);
#print "Setting background with @{$colorspec{'BG'}} colors\n" if $DEBUG;
if (@{$colorspec{'BG'}} eq 3) {
print "Setting background with 3 colors in @{$colorspec{'BG'}}\n" if $DEBUG;
$bg = $map->colorAllocate(@{$colorspec{'BG'}});
$map->transparent($bg);
$map->filledRectangle(0,0,$WIDTH,$HEIGHT,$bg);
} elsif (@{$colorspec{'BG'}} eq 4) {
print "Setting background with 4 colors in @{$colorspec{'BG'}}\n" if $DEBUG;
$bg = $map->colorAllocateAlpha(@{$colorspec{'BG'}});
$map->transparent($bg);
$map->filledRectangle(0,0,$WIDTH,$HEIGHT,$bg);
$map->alphaBlending(1);
}
#$map->interlaced('true');
#$map->saveAlpha(1);
}
&alloc_colors;
&alloc_fonts;
print "Querying hobbit...\n\n" if($DEBUG);
foreach my $thisnode (sort keys %nodes) {
my $hostname = $nodes{$thisnode}{'hostname'} ? $nodes{$thisnode}{'hostname'} : $thisnode;
my $statustest = ($nodes{$thisnode}{'statustest'} ne '') ? $nodes{$thisnode}{'statustest'} : "conn";
print "Checking node status for $thisnode ($hostname) with test $statustest on $BBDISP\n" if ($DEBUG);
open(CONN, "$BB $BBDISP \"query $hostname.$statustest\" |") or print "data: $!\n";
while (<CONN>) {
if (/^(\w+) /) {
$nodes{$thisnode}{'status'} = $1;
$nodes{$thisnode}{'color'} = $1;
worst_color($1);
print "Node status: $thisnode: $nodes{$thisnode}{'status'}\n" if ($DEBUG);
}
}
close CONN;
print "Finding values for $thisnode\n" if ($DEBUG);
foreach my $logtest (sort keys %{$nodes{$thisnode}{'targets'}}) {
print "Finding values for $thisnode ($hostname) test $logtest\n" if ($DEBUG);
my @interfaces = keys %{$nodes{$thisnode}{'targets'}{$logtest}};
open(DEVMON, "$BB $BBDISP \"hobbitdlog $hostname.$logtest\" |") or print "data: $!\n";
while(<DEVMON>){
if (/^$hostname\|if_load\|(\w+)\|/) {
#$nodes{$thisnode}{'color'} = $1;
#if ($nodes{$thisnode}{'status'} eq "red") {
# $nodes{$thisnode}{'color'} = $nodes{$thisnode}{'status'};
#}
# Dont make the device red for if_load being red
# we'll reserve that for conn
#if ($1 eq "red") { $nodes{$thisnode}{'color'} = "orange";}
#next;
}
elsif (/<tr><td>(\S+) ?([^<]+)?<\/td><td>[^<]+<\/td><td>[^<]+\((\d+\.\d+)\%\)<\/td><td>[^<]+\((\d+\.\d+)\%\)(.+)$/) {
my $intname = $1;
my $intdesc = $2;
my $inrate = $3;
my $outrate = $4;
#print "$thisnode interface $intname: desc: $intdesc, in $inrate, out $outrate\n" if ($DEBUG);
#next if (grep {!/$intname/} @interfaces);
#print "Matched $intname, storing values\n" if ($DEBUG);
$nodes{$thisnode}{$logtest}{'targets'}{$intname}{'rates'} = [$inrate,$outrate];
$nodes{$thisnode}{$logtest}{'targets'}{$intname}{'desc'} = $intdesc
#$nodes{$thisnode}{'interfaces'}{'label'} = $intdesc;
}
}
}
close(DEVMON);
print "Finding link status for $thisnode ($hostname)\n" if ($DEBUG);
my @interfaces = keys %{$nodes{$thisnode}{'if_load'}{'targets'}};
open(DEVMON, "$BB $BBDISP \"hobbitdlog $hostname.if_stat\" |") or warn "data: $!\n";
while(<DEVMON>){
if (/<tr><td>(\S+) ?([^<]+)?<\/td><td>(\d+\.?\d*)[^<]*<\/td><td>&(\w+) [^<]*.*$/) {
#my $intname = $1;
#my $intdesc = $2;
#my $speed = $3;
#my $status = $4;
$nodes{$thisnode}{'if_load'}{'targets'}{$1}{'status'} = $4;
print "Link status: $thisnode - $1: $nodes{$thisnode}{'if_load'}{'targets'}{$1}{'status'}\n" if ($DEBUG);
#next if (grep {!/$intname/} @interfaces);
}
}
close(DEVMON);
}
print "\nCalculating rates...\n\n" if($DEBUG);
print Dumper(%links) if ($DEBUG);
#print "Just dumped %links\n";
#print Dumper(%nodes) if ($DEBUG);
LINK: foreach my $link (sort keys %links) {
next LINK if $link eq "DEFAULT";
print "Finding rates for $link:\n" if ($DEBUG);
my @middle;
my $node1 = $links{$link}{'nodea'};
my $node2 = $links{$link}{'nodeb'};
my $int1 = $links{$link}{'inta'};
my $int2 = $links{$link}{'intb'};
my $test1 = $links{$link}{'testa'};
my $test2 = $links{$link}{'testb'};
if ($int1 eq "" and $int2 eq "") {
print "Link $link has no interfaces, interface rate wont be available\n";
#next;
}
print "Link details: $node1 - $int1 to $node2 - $int2\n" if ($DEBUG);
#print Dumper($nodes{$node1}{$test1});
#@link1 = @{$nodes{$links{$link}{'nodea'}}{$links{$link}{'inta'}}};
#@link2 = @{$nodes{$links{$link}{'nodeb'}}{$links{$link}{'intb'}}};
my (@link1,@link2);
if ($nodes{$node1}{$test1}{'targets'}{$int1}{'rates'}) {
@link1 = @{$nodes{$node1}{$test1}{'targets'}{$int1}{'rates'}}
};
if ($nodes{$node2}{$test2}{'targets'}{$int2}{'rates'}) {
@link2 = @{$nodes{$node2}{$test2}{'targets'}{$int2}{'rates'}};
}
my @posa = @{$nodes{$node1}{'pos'}};
my @posb = @{$nodes{$node2}{'pos'}};
print "In values: $link1[0] == $link2[1], Out values: $link1[1] == $link2[0]\n" if ($DEBUG);
my $outrate = $link1[1] != "" ? $link1[1] : $link2[0];
my $inrate = $link2[1] != "" ? $link2[1] : $link1[0];
#if($output{$link} != 0 && $outrate == 0) { $outrate=1 }
#if($input{$link} != 0 && $inrate == 0) { $inrate=1 }
my $outlabel = '';
my $inlabel = '';
if($BWLABELS eq "percent")
{
$outlabel = $outrate."%" if ($outrate ne '');
$inlabel = $inrate."%" if ($inrate ne '');
}
#else
#{
# $outlabel = format_bytes($output{$link} * 8,1);
# $inlabel = format_bytes($input{$link} * 8,1);
#}
my $outcolor = &select_color($outrate);
if ($nodes{$node1}{$test1}{'targets'}{$int1}{'status'} eq "red") {
$outcolor = $red;
$outlabel = "Link down";
worst_color('red');
}
my $incolor = &select_color($inrate);
if ($nodes{$node2}{$test2}{'targets'}{$int2}{'status'} eq "red") {
$incolor = $red;
$inlabel = "Link down";
worst_color('red');
}
#print "$target{$link}: outrate=$outrate%, inrate=$inrate%\n" if($DEBUG);
print "$link from $posa[0],$posa[1] to $posb[0],$posb[1] outrate=$outrate%, inrate=$inrate%\n" if($DEBUG);
# draw lines...
my $width=7;
$width=$linkwidth{$link};
if (defined $links{$link}{'via'}) {
@middle = @{$links{$link}{'via'}}
} else {
@middle[0] = &middle($posa[0],$posb[0]);
@middle[1] = &middle($posa[1],$posb[1]);
}
&draw_arrow(
$posa[0],
$posa[1],
$middle[0],
$middle[1],
$width, 1, $outcolor);
&draw_arrow(
$posa[0],
$posa[1],
$middle[0],
$middle[1],
$width, 0, $black,$link,0);
&label(
&middle($posa[0],$middle[0] + 0.1 * ($posb[0]-$posa[0])),
&middle($posa[1],$middle[1] + 0.1 * ($posb[1]-$posa[1])),
$outlabel, 0,$fontdef{$font{'LABEL'}},"","");
&draw_arrow(
$posb[0],
$posb[1],
$middle[0],
$middle[1],
$width, 1, $incolor);
&draw_arrow(
$posb[0],
$posb[1],
$middle[0],
$middle[1],
$width, 0, $black,$link,1);
&label(
&middle($posb[0],$middle[0] + 0.1 * ($posa[0]-$posb[0])),
&middle($posb[1],$middle[1] + 0.1 * ($posa[1]-$posb[1])),
$inlabel, 0, $fontdef{$font{'LABEL'}},"","");
}
print "\n" if($DEBUG);
foreach (sort keys %nodes) {
if (defined $nodes{$_}{'icon'}) {
&icon($nodes{$_}{'icon'},@{$nodes{$_}{'pos'}})
}
if ($nodes{$_}{'label'} ne '') {
&label($nodes{$_}{'pos'}[0],$nodes{$_}{'pos'}[1],$nodes{$_}{'label'},2,$fontdef{$font{'LABEL'}},$_,$nodes{$_}{'color'});
}
#$map->alphaBlending(1);
#$map->alphaBlending(0);
}
&annotation;
#×tamp;
# print image...
print "Generating image file $OUTPUT...\n";
open(IMG,">${OUTPUT}.$$")or { warn "$OUTPUT: $!\n" and return 0};
binmode(IMG); # for Windows systems
if ($OUTPUT =~ /\.png$/) {
print IMG $map->png(0);
} elsif ($OUTPUT =~ /\.gif$/) {
print IMG $map->gif;
} elsif ($OUTPUT =~ /\.jpe?g$/) {
print IMG $map->jpg;
} elsif ($OUTPUT =~ /\.jpe?g$/) {
}
close IMG;
print "Image file $OUTPUT generated\n";
if($HTMLOUTPUT ne "")
{
print "Generating HTML file $HTMLOUTPUT... with color $worstcolor\n";
#open(HTML,">$HTMLOUTPUT") ||die("$HTMLOUTPUT: $!\n");
open(HTML,"|$BBWEBPAGE --color=$worstcolor > ${HTMLOUTPUT}.$$") or { warn "$HTMLOUTPUT: $!\n" and return 0};
#print HTML '<HTML><HEAD><META HTTP-EQUIV="REFRESH" CONTENT="300" /><TITLE>Network Weathermap</TITLE></HEAD><BODY>';
if($HTMLSTYLE eq "overlib")
{
print HTML "<DIV id=\"overDiv\" style=\"position:absolute; visibility:hidden; z-index:1000;\"></DIV>\n";
print HTML "<SCRIPT language=\"JavaScript\" src=\"$OVERLIBJSURL\"><!-- overLIB (c) Erik Bosrup --></SCRIPT> \n";
}
my ($width,$height) = $map->getBounds();
#$imageuri = $optctl{'image-uri'} || $OUTPUT;
#$imageuri = "$OUTPUTURL/${name}.png";
my $imageuri = $optctl{'image-uri'} || $OUTPUTURI;
print HTML sprintf('<CENTER><IMG SRC="%s" WIDTH="%s" HEIGHT="%s" ALIGN=center BORDER=0 USEMAP="#weathermap_imap" /></CENTER>', $imageuri, $width, $height);
print HTML '<MAP NAME="weathermap_imap">';
print HTML $allmap;
print HTML '</MAP>';
#print HTML '</BODY></HTML>';
close(HTML);
}
if ($IMGCOMPRESS ne '') {
print "Compressing $OUTPUT.$$ using command $IMGCOMPRESS\n" if ($DEBUG);
system("$IMGCOMPRESS ${OUTPUT}.$$ >/dev/null 2>/dev/null");
}
rename("${OUTPUT}.$$","${OUTPUT}");
rename("${HTMLOUTPUT}.$$","${HTMLOUTPUT}");
# hint, resizing the image could make it look better
} # generate
sub timestamp
{
my $string = localtime();
my ($width,$height) = $map->getBounds();
my $xpos = $width - (gdSmallFont->width * (length($string) +1 ));
$map->string(gdSmallFont,
$xpos, 10,
$string, $color{'TIME'})
}
# print labels
sub label{
my($xpos,$ypos,$label,$pad,$infont,$nodename,$color)=@_;
return if ($label eq '');
my %font = %{$infont} if defined ($infont);
my $fontfile;
my $fontsize;
my $fonttype;
my $gdfont;
my @coords;
my $strwidth;
my $strheight;
my $pad = $pad * $SCALE;
if (defined($font{'type'})) {
if (defined ($font{'gdfont'})) {
$fonttype = 'gd';
$gdfont = $font{'gdfont'};
} elsif(defined($font{'fontfile'})) {
$fonttype = 'ttf';
$fontfile = $font{'fontfile'};
$fontsize = $font{'fontsize'};
} else {
print "ERROR: font $font{'type'} defined but not gdfont or ttf\n" if $DEBUG;
}
} else {
$fonttype = 'gd';
$gdfont = gdLargeFont;
}
if ($fonttype eq 'gd') {
($strwidth)=$gdfont->width*length($label);
($strheight)=$gdfont->height;
@coords = ($xpos-$strwidth/2-$pad-2, $ypos-$strheight/2-$pad+1,
$xpos+$strwidth/2+$pad+1, $ypos+$strheight/2+$pad);
print "label background should be at COORDS @coords\n" if $DEBUG;
} elsif ($fonttype eq 'ttf') {
my @coords1 = GD::Image->stringFT($black,$fontfile,$fontsize,0,
0, 0,
$label,{ resolution=>'72,72'});
print "String has COORDS @coords1\n" if $DEBUG;
@coords = ($xpos-(($coords1[4]-$coords1[0])/2)-$pad,$ypos+(($coords1[5]-$coords1[1])/2)-$pad,$xpos+(($coords1[4]-$coords1[0])/2)+$pad,$ypos-(($coords1[5]-$coords1[1])/2)+$pad);
print "Drawing label background at COORDS @coords\n" if $DEBUG;
} else {
print "ERROR: $fonttype not ttf or gdfont\n" if $DEBUG;
}
my $tile = new GD::Image(1,1);
my $tilecolor;
#$tilewhite = $tile->colorAllocateAlpha(255,255,255,15);
if ($color eq "purple") {$tilecolor = $tile->colorAllocateAlpha(255,0,255,31);}
elsif ($color eq "yellow") {$tilecolor = $tile->colorAllocateAlpha(255,255,0,31);}
elsif ($color eq "blue") {$tilecolor = $tile->colorAllocateAlpha(0,0,255,31);}
elsif ($color eq "green") {$tilecolor = $tile->colorAllocateAlpha(0,255,0,31);}
elsif ($color eq "orange") {$tilecolor = $tile->colorAllocateAlpha(255,127,0,31);}
elsif ($color eq "red") {$tilecolor = $tile->colorAllocateAlpha(255,0,0,31);}
else {$tilecolor = $tile->colorAllocateAlpha(255,255,255,31);}
#$tile->transparent($tilewhite);
$tile->fill(1,1,$tilecolor);
$map->setTile($tile);
#$map->filledRectangle(@coords,$white);
print "Drawing label background at COORDS @coords\n" if $DEBUG;
$map->filledRectangle(@coords,gdTiled);
#$map->rectangle(@coords,$black);
if ($fonttype eq 'gd') {
$map->string($gdfont,
$xpos-$strwidth/2, $ypos-$strheight/2+1,
$label, $black);
#$map->string($gdfont,
#$xpos-$strwidth, $ypos-$strheight,
#$label, $red);
} elsif($fonttype eq 'ttf') {
$map->stringFT($black,$fontfile,$fontsize,0,
@coords[0]+$pad, @coords[3]-$pad,
$label, { resolution=>'72,72'});
}
# produce some imagemap data if that's required
if($nodename ne "")
{
my $hostname = $nodes{$nodename}{'hostname'} ? $nodes{$nodename}{'hostname'} : $nodename;
my $statustest = ($nodes{$nodename}{'statustest'} ne '') ? $nodes{$nodename}{'statustest'} : "conn";
print "Producing a Rectangle for $nodename ($hostname) with overlib graph for $statustest\n" if($DEBUG);
my $overlibgraphurl = "${CGIBINURL}/${GRAPHCGI}?host=$hostname&service=$statustest&graph_height=120&graph_width=576&graph=hourly";
#$infourl = $BBWEBHOST . $CGIBINURL . "/bb-hostsvc.sh?HOST=" . $nodename . "&SERVICE=trends";
my $infourl = "${CGIBINURL}/bb-hostsvc.sh?HOST=${hostname}&SERVICE=$statustest";
my $active_html = make_html($nodename, $overlibgraphurl, $infourl);
if($active_html ne "")
{
my $html = "<AREA SHAPE=\"RECT\" COORDS=\"";
$html .= join(",",@coords) . "\" ";
$html .= $active_html;
$html .= ">\n";
# we put rectangles at the front of the image map, otherwise they'll always lose out
# to the arrows already there from when the links were drawn
$allmap = $html.$allmap;
}
}
}
sub icon {
my ($icon,@pos) = @_;
my ($iconname,$iconsize,$iconfile,$iconimage);
$icon =~ s,.*/([^/]*)$,$1, if ($icon !~ /^\//);
$icon =~ s/-black//g;
if ($SCALE != 1) {
if ($icon =~ m/^(\D+)-(\d+)\.png$/) {
print "Trying to scale $icon dimension $2\n" if $DEBUG;
$iconname=$1;
$iconsize=$2*$SCALE;
$icon = "$iconname-$iconsize.png";
} else {
print "Scaling requested, but icon $icon doesnt match NAME-SIZE.png\n" if $DEBUG;
}
}
$iconfile = $ICONDIR . "/$theme/" . $icon if ($icon !~ /^\//);
if (-f $iconfile) {
print "Trying to merge in $iconfile\n" if ($DEBUG);
if ($iconimage = GD::Image->newFromPng($iconfile,1)) {
# Even though we have the background, the actual
# color values may differ between the background (read or created)
# and the icon, so get a new color from the icon.
# If we don't do this, we get translucent rectangles
my $mybg = $iconimage->colorAllocate(@{$colorspec{'BG'}}[0 .. 2]);
$iconimage->transparent($mybg);
my @bounds = $iconimage->getBounds;
my @location = ($pos[0]-($bounds[0]/2),$pos[1]-($bounds[1]/2));
print "ICON: placing $icon at @location\n" if $DEBUG;
#$map->alphaBlending(0);
$map->copyMerge($iconimage,@location,0,0,@bounds,75);
#$map->alphaBlending(1);
} else {
print STDERR "newFromPng for $iconfile failed\n";
}
} else {
print STDERR "Icon $iconfile not present\n";
}
}
# print annotation
sub annotation{
my @currentpos;
my @coords;
if (defined $pos{'TITLE'} && $title ne '') {
print "Writing TITLE $title with font $font{'TITLE'} of type $fontdef{$font{'TITLE'}}{'type'}\n" if $DEBUG;
if ($fontdef{$font{'TITLE'}}{'type'} eq 'gd') {
$map->string(gdLargeFont,@{${pos}{'TITLE'}},$title,$color{'TITLE'});
} elsif ($fontdef{$font{'TITLE'}}{'type'} eq 'ttf') {
@coords = text_bounds($fontdef{$font{'TITLE'}},"$_%");
print "TITLE POS OFFSET: @coords\n" if $DEBUG;
my @fixedpos = (${pos}{'TITLE'}[0] - $coords[0],${pos}{'TITLE'}[1] - $coords[1]);
print "TITLE POS: Original loc: @{${pos}{'TITLE'}} (" . ${pos}{'TITLE'}[0] . "," . ${pos}{'TITLE'}[1] . "), moving by $coords[0],$coords[1], new pos: @fixedpos\n" if $DEBUG;
$map->stringFT($color{'TITLE'},$fontdef{$font{'TITLE'}}{'fontfile'},$fontdef{$font{'TITLE'}}{'fontsize'},0,
@fixedpos,
$title, { resolution=>'72,72'});
}
}
my($title)= (defined $text{'KEY'}) ? $text{'KEY'} : "Traffic load";
my $t=gmtime(time);
$map->string(gdSmallFont, @{${pos}{'TIME'}}, "Last update on $t UTC", $color{'TIME'}) if (@{${pos}{'TIME'}});
my %keyfont = %{$fontdef{$font{'KEY'}}};
my $pad = 5*$SCALE;
my @coords1;
if ($keyfont{'type'} eq 'gd') {
my ($strwidth)=$keyfont{'gdfont'}->width;
my ($strheight)=$keyfont{'gdfont'}->height;
print "key text size $strwidth:$strheight\n" if $DEBUG;
@coords = ($pos{'KEY'}[0],$pos{'KEY'}[1],
$pos{'KEY'}[0]+$strwidth*length($title)+2*$pad, $pos{'KEY'}[1]+$strheight*($scales+2));
print "key background should be at COORDS @coords\n" if $DEBUG;
} elsif ($keyfont{'type'} eq 'ttf') {
@coords1 = GD::Image->stringFT($black,$keyfont{'fontfile'},$keyfont{'fontsize'},0,
0, 0,
$title,{ resolution=>'72,72'});
@coords = ($pos{'KEY'}[0],$pos{'KEY'}[1],$pos{'KEY'}[0]+$coords1[2]-$coords1[0]+2*$pad,$pos{'KEY'}[1]+(($coords1[3]-$coords1[5])*($scales+1))+2*$pad);
print "Drawing key background for $scales scales with $pad padding at COORDS @coords\n" if $DEBUG;
}
#$map->filledRectangle(@{$pos{'KEY'}},
# $pos{'KEY'}[0]+gdLargeFont->width*length($title)+10*$SCALE,
# $pos{'KEY'}[1]+gdLargeFont->height*($scales+1)+10*$SCALE,
# $color{'KEYBG'});
$map->filledRectangle(@coords,$color{'KEYBG'});
#$map->rectangle($pos{'KEY'}[0],$pos{'KEY'}[1],
#$map->rectangle(@{$pos{'KEY'}},
# $pos{'KEY'}[0]+gdLargeFont->width*length($title)+10*$SCALE,
# $pos{'KEY'}[1]+gdLargeFont->height*($scales+1)+10*$SCALE,
# $color{'KEYOUTLINE'});
$map->rectangle(@coords,$color{'KEYOUTLINE'});
if ($keyfont{'type'} eq 'gd') {
$map->string($keyfont{'gdfont'},
$pos{'KEY'}[0]+4*$SCALE,
$pos{'KEY'}[1]+4*$SCALE,
$title, $color{'KEYTEXT'});
} elsif ($keyfont{'type'} eq 'ttf') {
$map->stringFT($color{'KEYTEXT'},$keyfont{'fontfile'},$keyfont{'fontsize'},0,
$coords[0]+$pad, $coords[1]+2*$pad+($coords1[1]-$coords1[7])/2,
$title, { resolution=>'72,72'});
} else {