-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.pl
executable file
·1083 lines (954 loc) · 43.5 KB
/
index.pl
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
# VMchart - browser-based generator for nice charts of LVM and BTRFS usage across different file servers
# (c) 2011 Christian Herzog <daduke@phys.ethz.ch> and Patrick Schmid <schmid@phys.ethz.ch>
# incremental AJAX loading by Philip Ezhukattil <philipe@phys.ethz.ch> and Claude Becker
# <beckercl@phys.ethz.ch>
# multithreading by Philip Ezhukattil <philipe@phys.ethz.ch> and Claude Becker <beckercl@phys.ethz.ch>
# distributed under the terms of the GNU General Public License version 2 or any later version.
# project website: https://readme.phys.ethz.ch/services/lvmchart/
# 2011/08/04 v1.0 - initial working version
# 2012/04/22 v2.0 - added backend information
# 2012/04/25 v2.1 - added PV change log to detect defective backends
# 2012/07/10 v3.0 - added BTRFS support
# 2012/07/11 v3.1 - added navigation menu
# 2012/07/31 v3.2 - added threads
use strict;
use warnings;
use JSON;
use POSIX qw(ceil strftime);
use MLDBM qw(DB_File); #to store hashes of hashes
use Clone qw(clone); #to clone hashes of hashes
use Fcntl; #to set file permissions
use File::Copy;
use File::Basename;
use threads qw(yield);
use threads::shared;
use Thread::Queue;
use List::MoreUtils qw(uniq);
use Math::Round qw(nearest);
use Data::Dumper;
my $myself = basename($0);
my @servers;
my %info;
my %PVhistory = ();
my $warnings = '';
my (%VMdata, $UNIT);
my ($markup, $javascript);
my %backends :shared = ();
my %PVlayout :shared = ();
my %emptySlices :shared = ();
my $lastUpdate = strftime "%H:%M:%S %d.%m.%Y", localtime;
my $grandTotal :shared;
my $grandTotalUsed :shared;
my $grandFSLevel :shared;
my $grandInFS :shared;
my $grandInLV :shared;
my $grandInVG :shared;
my $grandInPV :shared;
my $grandUnalloc :shared;
my $grandPVSize :shared;
#define parameters
use constant NTHREADS => 6;
my $BARSPERCHART = 8; #number of LV in bar chart
my $ORGSPERCHART = 9; #number of LV in org chart
my $CHARTSPERLINE = 3; #number of LV charts in one line
my $MAXCHARTFACTOR = 10; #max factor in one LV chart
my $GLOBALUNIT = 'TiB'; #summary data is in TiB
my %labels = (
'fsfill' => 'FS filling level',
'infs' => 'available in FS',
'inlv' => 'available in LVM2 LV',
'invg' => 'available in LVM2 VG',
'inpv' => 'available in LVM2 PV',
'unalloc' => 'not allocated',
);
#read hostnames from file
open HOSTS, "hostlist";
while (<HOSTS>) {
my ($host, $info) = split / - /;
chomp $info;
push @servers, $host;
$info{$host} = $info;
}
close HOSTS;
my $numberOfServers = @servers;
#determine whether we are in HTML or AJAX mode
my $option = $ENV{QUERY_STRING} || '';
if ($option eq 'data'){
print "Content-type:text/plain\r\n\r\n";
$| = 1; # Flush output continuously
my $Q = Thread::Queue->new;
my @threads = map threads->create( \&getdata, $Q ), 1 .. NTHREADS; #create N threads
$Q->enqueue($_) for @servers; #queue all servers
$Q->enqueue( (undef) x NTHREADS ); #flush queue
$_->join for @threads; #start threading
while ($Q->pending()) { #wait till queue is done
select(undef, undef, undef, 0.1); #sleep for 0.1 s
}
&getBackends();
} else {
print "Content-type:text/html\r\n\r\n";
print html();
}
sub getdata {
my $Q = shift;
my $tid = threads->tid;
while (my $server = $Q->dequeue) {
my $frontendInfo;
my $json_text;
if (!($json_text = `ssh -o IdentitiesOnly=yes -i /var/www/.ssh/remotesshwrapper root\@$server /usr/local/bin/remotesshwrapper vmchart.pl`)) {
$warnings .= "could not fetch JSON from server $server! $!<br />\n";
print "ENDOFELEMENTENDOFELEMENTENDOFELEMENT${warnings}ENDOFSERVER";
next;
} else {
# no autovivification for shared hashrefs
$backends{global} //= &share({});
my $json = JSON->new->allow_nonref;
my $VMdata;
if (!($VMdata = $json->decode($json_text))) {
$warnings .= "could not get LVM data from server $server<br />\n";
print "ENDOFELEMENTENDOFELEMENTENDOFELEMENT${warnings}ENDOFSERVER";
next;
}
%VMdata = %$VMdata;
next unless (keys %{$VMdata{vgs}}); #skip host if no vgs present
my $numLVs = 0; #count LV
foreach my $vg (keys %{$VMdata{vgs}}) {
foreach my $lv (keys %{$VMdata{$vg}{lvs}}) {
$numLVs++;
}
}
my $serverID = $server;
$serverID =~ s/-/_/g; #Google chart API needs underscores
$UNIT = $VMdata{unit}; #TiB or GiB comes from the JSON
if ($VMdata{warning}) {
$warnings .= "Host $server: ".$VMdata{warning}."<br />\n";
}
#populate backend information hash
foreach my $backend (sort keys %{$VMdata{backends}}) {
$backends{$backend} //= &share({});
$backends{$backend}{$server} //= &share({});
my $j;
foreach my $slice (sort { $a cmp $b } keys %{$VMdata{backends}{$backend}{slices}}) {
$backends{global}{$server} //= &share({});
my $vg = $VMdata{backends}{$backend}{slices}{$slice}{vg};
$backends{global}{$server}{$vg} //= &share({}); #TODO should move outside of backend loop for local storage
$backends{global}{$server}{btrfs} //= &share({});
$backends{global}{$server}{zfs} //= &share({});
$emptySlices{$backend} //= &share({});
$emptySlices{$backend}{$slice} //= &share({});
if ($vg eq 'freespace') {
my $size = $VMdata{backends}{$backend}{slices}{$slice}{size};
my $vmtype = $VMdata{backends}{$backend}{slices}{$slice}{vmtype};
$backends{global}{$server}{$vmtype} //= &share({});
$emptySlices{$backend}{$slice}{$vmtype} //= &share({});
$emptySlices{$backend}{$slice}{$vmtype}{size} = $size;
$emptySlices{$backend}{$slice}{$vmtype}{unit} = $UNIT;
$backends{global}{$server}{$vmtype}{slices} .= "$backend-$slice, \\n";
}
my $size = units($VMdata{backends}{$backend}{slices}{$slice}{size}, $UNIT, $GLOBALUNIT);
$backends{$backend}{$server}{$vg} //= &share({});
$backends{$backend}{size} += $size;
$backends{$backend}{$server}{size} += $size;
$backends{$backend}{$server}{$vg}{size} += $size;
$backends{$backend}{slices} .= "$slice ($size $UNIT), \\n";
$backends{$backend}{$server}{slices} .= "$slice ($size $UNIT), \\n";
$backends{$backend}{$server}{$vg}{slices} .= "$slice ($size $UNIT), \\n";
$backends{global}{$server}{$vg}{slices} .= "$backend-$slice ($size $UNIT), \\n";
$backends{global}{$server}{$vg}{vmtype} = $VMdata{backends}{$backend}{slices}{$slice}{vmtype};
$backends{global}{$server}{$vg}{raidtype} = $VMdata{backends}{$backend}{slices}{$slice}{raidtype} || '';
if ($backends{global}{$server}{$vg}{vmtype} eq 'btrfs') {
$backends{global}{$server}{btrfs}{slices} .= "$backend-$slice, \\n";
}
if ($backends{global}{$server}{$vg}{vmtype} eq 'zfs') {
$backends{global}{$server}{zfs}{slices} .= "$backend-$slice, \\n";
}
$PVlayout{$server} //= &share({});
$PVlayout{$server}{$vg} //= &share({});
$PVlayout{$server}{$vg}{"$backend-$slice"} = 1;
}
}
my $PVFSLevel = $VMdata{FSLevel}; #get PV data
my $PVInFS = $VMdata{inFS} - $PVFSLevel;
my $PVInLV = $VMdata{inLV};
my $PVInVG = $VMdata{inVG};
my $PVInPV = $VMdata{inPV};
my $unalloc = $VMdata{unalloc};
my $PVSize = $VMdata{size};
$grandFSLevel += nearest(.01, units($PVFSLevel, $UNIT, $GLOBALUNIT));
$grandInFS += nearest(.01, units($PVInFS, $UNIT, $GLOBALUNIT));
$grandInLV += nearest(.01, units($PVInLV, $UNIT, $GLOBALUNIT));
$grandInVG += nearest(.01, units($PVInVG, $UNIT, $GLOBALUNIT));
$grandInPV += nearest(.01, units($PVInPV, $UNIT, $GLOBALUNIT));
$grandUnalloc += nearest(.01, units($unalloc, $UNIT, $GLOBALUNIT));
$grandPVSize += nearest(.01, units($PVSize, $UNIT, $GLOBALUNIT));
$grandTotal += nearest(.01, units($PVSize, $UNIT, $GLOBALUNIT));
$grandTotalUsed += nearest(.01, units($PVFSLevel, $UNIT, $GLOBALUNIT));
$javascript .= pvData($serverID, $PVFSLevel, $PVInFS, $PVInLV, $PVInVG, $PVInPV, $unalloc, $PVSize, $UNIT);
my (%vgs, %lvs, $vgRows, %lvRows, $orgRows, $haveLVM, $haveBTRFS, $haveZFS);
my $VGslices;
my $lvGrpOrg = 0;
my $orgcount = 0;
my $orgChart = "<div class=\"orgchart\" id=\"org_chart_${serverID}_$lvGrpOrg\"></div>";
foreach my $vg (reverse sort { $VMdata{pv}{$a}{size} <=> $VMdata{pv}{$b}{size} } keys %{$VMdata{vgs}}) { #iterate over VGs
my $VGSize = $VMdata{pv}{$vg}{size};
my $vmType;
if ($vg eq 'btrfs') {
$vmType = 'btrfs';
} elsif ($vg eq 'zfs') {
$vmType = 'zfs';
} else {
$vmType = 'lvm2';
}
if ($vmType eq 'lvm2') {
my $VGFSLevel = $VMdata{pv}{$vg}{FSLevel} || 0; #get VG data
my $VGInFS = $VMdata{pv}{$vg}{inFS} || 0;
$VGInFS -= $VGFSLevel; #subtract filling level
my $VGInLV = $VMdata{pv}{$vg}{inLV} || 0;
my $VGInVG = $VMdata{pv}{$vg}{inVG} || 0;
$vgs{$vg}{size} = $VGSize;
$vgs{$vg}{js} = " {c:[{v: '$vg'},{v: $VGFSLevel, f:'$VGFSLevel $UNIT'},{v: $VGInFS, f:'$VGInFS $UNIT'},{v: $VGInLV, f:'$VGInLV $UNIT'},{v: $VGInVG, f:'$VGInVG $UNIT'}]},";
$backends{global}{$server} //= &share({}); #TODO should be handled further up
$backends{global}{$server}{$vg} //= &share({});
if ($VGslices = $backends{global}{$server}{$vg}{slices}) {
$VGslices = "LUNs for this VG: \\n" . $VGslices;
$VGslices = substr $VGslices, 0, -4;
} else {
$VGslices = "Volume group";
}
if ($VGInVG > 0 && $vg ne 'freespace') {
my $lv = "free";
my $key = "${lv}_$vg";
$orgRows .= "[{v:'$lv<div class=\"child freespace\">$VGInVG $UNIT</div>'},'$vg','free'],\n";
}
} elsif ($vmType eq 'btrfs') {
$VGslices = $backends{global}{$server}{$vg}{slices};
if ($VGslices) {
$VGslices = "LUNs for this volume: \\n" . $VGslices;
$VGslices = substr $VGslices, 0, -4;
}
} elsif ($vmType eq 'zfs') {
$VGslices = $backends{global}{$server}{$vg}{slices};
if ($VGslices) {
$VGslices = "devices for this pool: \\n" . $VGslices;
$VGslices = substr $VGslices, 0, -4;
}
}
my $vgName = ($vg eq 'freespace')?'<span class="free">free space</span>':$vg;
$orgRows .= "[{v: '$vg',f: '$vgName<div class=\"parent\">$VGSize $UNIT</div>'}, '','$VGslices'],";
foreach my $lv (reverse sort { $VMdata{pv}{$vg}{$a}{size} <=> $VMdata{pv}{$vg}{$b}{size} } keys %{$VMdata{$vg}{lvs}}) { #iterate over LVs, sorted by size
my $LVFSLevel = nearest(0.01, $VMdata{pv}{$vg}{$lv}{FSLevel}); #get LV data
my $LVInFS = nearest(0.01, $VMdata{pv}{$vg}{$lv}{inFS} - $LVFSLevel);
my $LVInLV = nearest(0.01, $VMdata{pv}{$vg}{$lv}{inLV});
my $LVSize = nearest(0.01, $VMdata{pv}{$vg}{$lv}{size});
my $FSType = $VMdata{pv}{$vg}{$lv}{FSType};
my $key = "${lv}_$vg";
if ($orgcount && !($orgcount % $ORGSPERCHART)) { #if org chart is full, create a new one
$javascript .= orgChart("${serverID}_$lvGrpOrg", $orgRows);
$lvGrpOrg++;
$orgChart .= "<br /><br /><div class=\"orgchart\" id=\"org_chart_${serverID}_$lvGrpOrg\"></div>\n";
$orgRows = "[{v:'$vg',f:'$vg<div class=\"parent\">(cont\\'d)</div>'}, '','$VGslices'],\n";
}
if ($vg ne 'freespace') { #we don't want VG and LV graphs for free space
$lvs{$vmType}{$key}{size} = $LVSize;
my $FSinfo = ($vmType eq 'btrfs' || $vmType eq 'zfs')?'':"($vg): $FSType";
$lvs{$vmType}{$key}{js} = "{c:[{v: '$lv $FSinfo'},{v: $LVFSLevel, f: '$LVFSLevel $UNIT'},{v: $LVInFS, f: '$LVInFS $UNIT'},{v: $LVInLV, f: '$LVInLV $UNIT'}]},"; #fill LV and org chart data
}
if ($vg eq 'freespace') {
my $LVslices = $backends{global}{$server}{$lv}{slices};
if ($LVslices) {
$LVslices = "LUNs for this volume: \\n" . $LVslices;
$LVslices = substr $LVslices, 0, -4;
}
$orgRows .= "[{v:'$lv<div class=\"child freespace\">$LVSize $UNIT ($FSType)</div>'},'$vg','$LVslices'],\n";
} elsif ($vmType eq 'lvm2') {
$haveLVM = 1;
$orgRows .= "[{v:'$lv<div class=\"child\">$LVSize $UNIT ($FSType)</div>'},'$vg','Logical volume'],\n";
} elsif ($vmType eq 'btrfs') {
$haveBTRFS = 1;
my $LVslices = $backends{global}{$server}{$lv}{slices};
if ($LVslices) {
$LVslices = "LUNs for this volume: \\n" . $LVslices;
$LVslices = substr $LVslices, 0, -4;
}
if (my $raid = $backends{global}{$server}{$lv}{raidtype}) {
$raid = uc($raid);
$FSType .= ", $raid";
}
$orgRows .= "[{v:'$lv<div class=\"child btrfs\">$LVSize $UNIT ($FSType)</div>'},'$vg','$LVslices'],\n";
} elsif ($vmType eq 'zfs') {
$haveZFS = 1;
my $LVslices = $backends{global}{$server}{$lv}{slices};
if ($LVslices) {
$LVslices = "devices for this pool: \\n" . $LVslices;
$LVslices = substr $LVslices, 0, -4;
}
$orgRows .= "[{v:'$lv<div class=\"child zfs\">$LVSize $UNIT ($FSType)</div>'},'$vg','$LVslices'],\n";
}
$orgcount++;
}
}
#create VG and LV graphs
my $maxInVGGraph = 0;
my $vgcount = 0;
my $vgGrpChart = 0;
foreach my $vg (reverse sort { $vgs{$a}{size} <=> $vgs{$b}{size} } keys %vgs) {
next if ($vg eq 'freespace');
if ($vgcount == 0) {
$maxInVGGraph = $vgs{$vg}{size};
}
if ( ($vgcount && !($vgcount % $BARSPERCHART))
|| ($vgs{$vg}{size} && (($maxInVGGraph / $vgs{$vg}{size})) > $MAXCHARTFACTOR) ) {
#if VG chart is full or bars get too short, create a new one
$javascript .= vgData("${serverID}_$vgGrpChart", $vgRows);
$vgGrpChart++;
$vgRows = '';
$vgcount = 0;
$maxInVGGraph = $vgs{$vg}{size};
}
$vgRows .= $vgs{$vg}{js};
$vgcount++;
}
my %GrpChart;
foreach my $vmType (qw(lvm2 btrfs zfs)) {
my $maxInLVGraph = 0;
my $lvcount = 0;
$GrpChart{$vmType} = 0;
foreach my $lv (reverse sort { $lvs{$vmType}{$a}{size} <=> $lvs{$vmType}{$b}{size} } keys %{$lvs{$vmType}}) {
if ($lvcount == 0) {
$maxInLVGraph = $lvs{$vmType}{$lv}{size};
}
if ( ($lvcount && !($lvcount % $BARSPERCHART))
|| ($lvs{$vmType}{$lv}{size} != 0 && (($maxInLVGraph / $lvs{$vmType}{$lv}{size})) > $MAXCHARTFACTOR) ) {
#if LV chart is full or bars get too short, create a new one
$javascript .= lvData("${serverID}_$GrpChart{$vmType}", $lvRows{$vmType}, $vmType);
$GrpChart{$vmType}++;
$lvRows{$vmType} = '';
$lvcount = 0;
$maxInLVGraph = $lvs{$vmType}{$lv}{size};
}
$lvRows{$vmType} .= $lvs{$vmType}{$lv}{js};
$lvcount++;
}
}
chop $vgRows; #trim last comma
chop $lvRows{lvm2} if ($lvRows{lvm2});
chop $lvRows{btrfs} if ($lvRows{btrfs});
chop $lvRows{zfs} if ($lvRows{zfs});
chop $orgRows;
$javascript .= vgData("${serverID}_$vgGrpChart", $vgRows) if ($vgRows);
$javascript .= lvData("${serverID}_$GrpChart{lvm2}", $lvRows{lvm2}, 'lvm2') if ($haveLVM);
$javascript .= lvData("${serverID}_$GrpChart{btrfs}", $lvRows{btrfs}, 'btrfs') if ($haveBTRFS);
$javascript .= lvData("${serverID}_$GrpChart{zfs}", $lvRows{zfs}, 'zfs') if ($haveZFS);
$javascript .= orgChart("${serverID}_$lvGrpOrg", $orgRows);
$markup .= chartTable($server, $serverID, $orgChart, $vgGrpChart+1, $GrpChart{lvm2}+1, $GrpChart{btrfs}+1, $GrpChart{zfs}+1, $haveLVM, $haveBTRFS, $haveZFS);
#print information about this server
$frontendInfo .= $javascript;
$frontendInfo .= "ENDOFELEMENT";
$frontendInfo .= $markup;
$frontendInfo .= "ENDOFELEMENT";
$frontendInfo .= "$server";
$frontendInfo .= "ENDOFELEMENT";
$frontendInfo .= "$warnings";
$frontendInfo .= "ENDOFELEMENT";
$frontendInfo .= "$grandTotal";
$frontendInfo .= "ENDOFELEMENT";
$frontendInfo .= "$grandTotalUsed";
$frontendInfo .= "ENDOFELEMENT";
$frontendInfo .= "$GLOBALUNIT";
$frontendInfo .= "ENDOFSERVER";
print $frontendInfo;
#clear variables for next server
$javascript = $markup = $warnings = "";
}
}
}
#create backend information orgchart
sub getBackends {
my $i = 0;
my $beGrpOrg = 0;
my $orgcount = 0;
$markup = "<div class=\"orgchart\" id=\"org_chart_backends_0\"></div>\n";
$javascript = '';
my $emptyTable;
my ($orgRows, $BEslices, $FEslices, $VGslices);
foreach my $backend (sort keys %backends) {
my ($class, $j);
next if ($backend eq 'size' || $backend eq 'global');
my $backendSize = $backends{$backend}{size};
$BEslices = "LUNs on this backend: \\n" . $backends{$backend}{slices};
$BEslices = substr $BEslices, 0, -4;
$orgRows .= "[{v: '$backend',f: '$backend<div class=\"parent\">$backendSize $GLOBALUNIT</div>'}, '', '$BEslices'],\n";
foreach my $server (sort keys %{$backends{$backend}}) {
next if (grep /\b$server\b/, qw(size slices));
my $serverSize = $backends{$backend}{$server}{size};
$FEslices = "LUNs for this frontend: \\n" . $backends{$backend}{$server}{slices};
$FEslices = substr $FEslices, 0, -4;
$orgRows .= "[{v: '$server-$backend',f: '$server<div class=\"child\">$serverSize $GLOBALUNIT</div>'}, '$backend', '$FEslices'],\n";
foreach my $vg (sort keys %{$backends{$backend}{$server}}) {
next if (grep /\b$vg\b/, qw(size slices));
my $VGsize = $backends{$backend}{$server}{$vg}{size};
my $vmType = '';
if ( exists $backends{global}{$server}{$vg}{vmtype} ) {
$vmType = $backends{global}{$server}{$vg}{vmtype};
}
if ($vmType eq 'btrfs') {
$VGslices = "LUNs in BTRFS: \\n" . $backends{$backend}{$server}{$vg}{slices};
$VGslices = substr $VGslices, 0, -4;
} elsif ($vmType eq 'zfs') {
$VGslices = "LUNs in ZFS \\n" . $backends{$backend}{$server}{$vg}{slices};
$VGslices = substr $VGslices, 0, -4;
} else { #LVM
$VGslices = "LUNs for this VG: \\n" . $backends{$backend}{$server}{$vg}{slices};
$VGslices = substr $VGslices, 0, -4;
$vmType = 'lvm';
}
if ($orgcount && !($orgcount % $ORGSPERCHART)) { #if org chart is full, create a new one
$javascript .= orgChart("backends_$beGrpOrg", $orgRows);
$beGrpOrg++;
$markup .= "<br /><br /><div class=\"orgchart\" id=\"org_chart_backends_$beGrpOrg\"></div>\n";
$orgRows = "[{v: '$backend',f: '$backend<div class=\"parent\">(cont\\'d)</div>'}, '', '$BEslices'],\n";
$orgRows .= "[{v: '$server-$backend',f: '$server<div class=\"child\">$serverSize $GLOBALUNIT</div>'}, '$backend', '$FEslices'],\n";
}
my $vgName = ($vg eq 'freespace')?'<span class="free">free space</span>':"$vg ($vmType)";
$orgRows .= "[{v: '$vg-$backend-$server',f: '$vgName<div class=\"grandchild\">$VGsize $GLOBALUNIT</div>'}, '$server-$backend', '$VGslices'],\n";
$orgcount++;
}
}
$class = ($i % 2)?'class="bg2"':'class="bg1"';
foreach my $slice (sort keys %{$emptySlices{$backend}}) {
foreach my $vm (sort keys %{$emptySlices{$backend}{$slice}}) {
my $size = $emptySlices{$backend}{$slice}{$vm}{size};
my $unit = $emptySlices{$backend}{$slice}{$vm}{unit};
$emptyTable .= "<tr $class><td>$backend</td><td>$slice</td><td>$vm</td><td class=\"r\">$size $unit</td></tr>\n";
$j = 1;
}
}
$i++ if ($j);
}
$javascript .= orgChart("backends_$beGrpOrg", $orgRows) if ($orgcount);
my $totalChart = pvData('grandtotal', $grandFSLevel, $grandInFS, $grandInLV, $grandInVG, $grandInPV, $grandUnalloc, $grandPVSize, $GLOBALUNIT);
print "BACKENDS";
print "$markup" if ($orgcount);
print "ENDOFELEMENT";
print "<br /><br /><a name=\"slices\"></a><h3 id=\"avail\" align=\"center\">Available slices</h3><table id=\"avail\" align=\"center\"><tr><th>Backend</th><th>Slice</th><th>Volume Mgmt.</th><th>Size</th></tr>$emptyTable</table>" if ($emptyTable);
print "ENDOFELEMENT";
print "$javascript";
print "ENDOFELEMENT";
print "$grandTotal";
print "ENDOFELEMENT";
print "$grandTotalUsed";
print "ENDOFELEMENT";
print "$totalChart";
print "ENDOFELEMENT";
print "$GLOBALUNIT";
print "ENDOFELEMENT";
#create PV diff log
my $changes;
tie(%PVhistory, 'MLDBM', 'PVhistory.db', O_CREAT|O_RDWR, 0666);
my %PVtemp = %{ clone (\%PVhistory) }; #needed b/c direct modify of tied HoH doesn't work
#segfaults but still works?????
foreach my $server (sort keys %PVlayout) {
foreach my $vg (sort keys %{$PVlayout{$server}}) {
foreach my $slice (sort keys %{$PVlayout{$server}{$vg}}) {
if (exists($PVhistory{$server}{$vg}{$slice})) {
delete $PVtemp{$server}{$vg}{$slice};
} else {
$changes .= "<tr><td><span class=\"free\">ADD</span> LUN <i>$slice</i></td><td> to VG <i>$vg</i></td><td> on frontend <i>$server</i></td></tr>\n";
}
}
}
}
foreach my $server (sort keys %PVtemp) {
foreach my $vg (sort keys %{$PVtemp{$server}}) {
foreach my $slice (sort keys %{$PVtemp{$server}{$vg}}) {
if ($PVtemp{$server}{$vg}{$slice}) {
$changes .= "<tr><td><span class=\"remove\">REMOVE</span> LUN <i>$slice</i></td><td> from VG <i>$vg</i></td><td> on frontend <i>$server</i></td></tr>\n";
}
}
}
}
%PVhistory = %{ shared_clone(\%PVlayout) };
untie %PVhistory;
open OLDLOG, "< changelog";
my @oldchanges = <OLDLOG>;
close OLDLOG;
if ($changes) {
my $timestamp = POSIX::strftime("%Y/%m/%d %H:%M:%S", localtime);
$changes = "$timestamp:<br />$changes";
print "<table>$changes</table><br /><br />@oldchanges";
open LOG, "> newlog";
print LOG "<table>$changes</table><br /><br />\n\n\n@oldchanges";
close LOG;
move("newlog", "changelog");
} else {
print "@oldchanges";
}
}
#----------------
sub units {
my ($value, $unit, $globalunit) = @_;
return $value if ($unit eq $globalunit);
if ($globalunit eq 'GB') {
$value *= 1024;
} else {
$value /= 1024;
}
return nearest(.01, $value);
}
sub pvData {
my ($serverID, $PVFSLevel, $PVInFS, $PVInLV, $PVInVG, $PVInPV, $unalloc, $PVSize, $UNIT) = @_;
$PVFSLevel = nearest(.01, $PVFSLevel);
$PVInFS = nearest(.01, $PVInFS);
$PVInLV = nearest(.01, $PVInLV);
$PVInVG = nearest(.01, $PVInVG);
$PVInPV = nearest(.01, $PVInPV);
$unalloc = nearest(.01, $unalloc);
$PVSize = nearest(.01, $PVSize);
return <<EOF;
//pv data
var pv_data_$serverID = new google.visualization.DataTable(
{
cols: [{id:'PV',label:'PV', type:'string'},
{id:'FSFill',label:'$labels{"fsfill"}', type:'number'}, // FSLevel
{id:'UsedInFS',label:'$labels{"infs"}', type:'number'}, // inFS - FSLevel
{id:'inLV',label:'$labels{"inlv"}', type:'number'}, // inLV
{id:'inVG', label:'$labels{"invg"}',type:'number'}, // inVG
{id:'inPV',label:'$labels{"inpv"}',type:'number'}, // inPV
{id:'unalloc',label:'$labels{"unalloc"}',type:'number'} // unallocated space
],
rows: [{c:[{v:'Disk space'},{v: $PVFSLevel, f: '$PVFSLevel $UNIT'},{v: $PVInFS, f: '$PVInFS $UNIT'},{v: $PVInLV, f: '$PVInLV $UNIT'},{v: $PVInVG, f: '$PVInVG $UNIT'},{v: $PVInPV, f: '$PVInPV $UNIT'},{v: $unalloc, f: '$unalloc $UNIT'}]}
]
});
//pv chart
var pv_chart_$serverID = new google.visualization.ColumnChart(document.getElementById('pv_chart_$serverID'));
pv_chart_$serverID.draw(pv_data_$serverID, {'title':'Total disk space overview',
'backgroundColor':'#eee',
'legend': 'right',
'legendTextStyle': {fontSize:10},
'isStacked': true,
'colors':['#3366cc','#3399ff','magenta','#dc3912','#ff9900','#3a3'],
'vAxis': {'title': '$UNIT','gridlineColor':'#808080'},
'hAxis':{'title':'Total: $PVSize$UNIT'}});
EOF
}
sub vgData {
my ($serverID, $vgRows) = @_;
return <<EOF;
//vg data
var vg_data_$serverID = new google.visualization.DataTable(
{
cols: [{id:'VG',label:'VG', type:'string'},
{id:'FSFill', label:'$labels{"fsfill"}',type:'number'}, // FSLevel
{id:'UsedInFS', label:'$labels{"infs"}',type:'number'}, // inFS - FSLevel
{id:'inLV',label:'$labels{"inlv"}', type:'number'}, // inLV
{id:'inVG',label:'$labels{"invg"}', type:'number'} // inVG
],
rows: [$vgRows
]
});
//vg chart
var vg_chart_$serverID = new google.visualization.ColumnChart(document.getElementById('vg_chart_$serverID'));
vg_chart_$serverID.draw(vg_data_$serverID, {'title':'Usage of Volume Groups',
'backgroundColor':'#eee',
'legend': 'right',
'legendTextStyle': {fontSize:10},
'isStacked': true,
'colors':['#3366cc','#3399ff','magenta','#dc3912'],
'vAxis': {'title': '$UNIT','gridlineColor':'#808080'}});
EOF
}
sub lvData {
my ($serverID, $lvRows, $vmType) = @_;
my $output =<<EOF;
//lv data
var lv_data_${vmType}_${serverID} = new google.visualization.DataTable(
{
cols: [{id:'LV',label:'LV', type:'string'},
{id:'FSFill', label:'$labels{"fsfill"}',type:'number'}, // FSLevel
{id:'UsedInFS', label:'$labels{"infs"}',type:'number'} // inFS - FSLevel
EOF
if ($vmType eq 'lvm2') {
$output .=<<EOF
, {id:'inLV',label:'$labels{"inlv"}', type:'number'} // inLV
EOF
}
$output .=<<EOF;
],
rows: [$lvRows
]
});
//lv chart
var lv_chart_${vmType}_${serverID} = new google.visualization.ColumnChart(document.getElementById('lv_chart_${vmType}_${serverID}'));
lv_chart_${vmType}_${serverID}.draw(lv_data_${vmType}_${serverID}, {'title':'Usage of Logical Volumes',
'backgroundColor':'#eee',
'legend': 'right',
'legendTextStyle': {fontSize:10},
'isStacked': true,
'colors':['#3366cc','#3399ff','magenta','#dc3912'],
'vAxis': {'title': '$UNIT', 'gridlineColor':'#808080'}});
EOF
return $output;
}
sub orgChart {
my ($serverID, $orgRows) = @_;
return <<EOF;
//orgchart data
var org_data_$serverID = new google.visualization.arrayToDataTable([
['Name', 'Parent', 'Tooltip'],
['', '', ''],
$orgRows
]);
//orgchart chart
var org_chart_$serverID = new google.visualization.OrgChart(document.getElementById('org_chart_$serverID'));
org_chart_$serverID.draw(org_data_$serverID, {allowHtml: true});
EOF
}
sub chartTable {
my ($server, $serverID, $orgChart, $vgGrpChart, $lvGrpChart, $btrGrpChart, $zfsGrpChart, $haveLVM, $haveBTRFS, $haveZFS) = @_;
my ($chart, %chartRows, %IDs);
for my $id (0..$vgGrpChart-1) {
push @{$IDs{lvm2}}, "vg_chart_${serverID}_$id";
}
for my $id (0..$lvGrpChart-1) {
push @{$IDs{lvm2}}, "lv_chart_lvm2_${serverID}_$id";
}
for my $id (0..$btrGrpChart-1) {
push @{$IDs{btrfs}}, "lv_chart_btrfs_${serverID}_$id";
}
for my $id (0..$zfsGrpChart-1) {
push @{$IDs{zfs}}, "lv_chart_zfs_${serverID}_$id";
}
foreach my $vmType (qw(lvm2 btrfs zfs)) {
my $rows = ceil(scalar @{$IDs{$vmType}} / $CHARTSPERLINE) || 0;
my $num = 0;
for my $row (1..$rows) {
$chartRows{$vmType} .= "<tr>";
for my $chart (1..$CHARTSPERLINE) {
my $td = ($IDs{$vmType}[$num])?"<div class=\"chart\" id=\"$IDs{$vmType}[$num]\"></div>":" ";
$chartRows{$vmType} .= "<td>$td</td>";
$num++;
}
$chartRows{$vmType} .= "</tr>";
}
}
my $LVMchart = '';
my $BTRFSchart = '';
my $ZFSchart = '';
if ($haveLVM) {
$LVMchart =<<EOF
<tr><td><h2>LVM2 overview</h2></td></tr>
$chartRows{lvm2}
EOF
}
if ($haveBTRFS) {
$BTRFSchart =<<EOF
<tr><td><h2>BTRFS overview</h2></td></tr>
$chartRows{btrfs}
EOF
}
if ($haveZFS) {
$ZFSchart =<<EOF
<tr><td><h2>ZFS zpool overview</h2></td></tr>
$chartRows{zfs}
EOF
}
return <<EOF;
<tr><td colspan="3" class="host"><a name="$server"></a>Host $server ($info{$server})</td></tr>
<tr><td><h2>Disk space overview</h2></td></tr>
<tr>
<td>
<div class="chart" id="pv_chart_$serverID"></div>
</td>
</tr>
$LVMchart
$BTRFSchart
$ZFSchart
<tr><tr><td><h2>Volume overview</h2></td></tr>
<td colspan="3">
$orgChart
</td>
</tr>
<tr><td colspan="3"><hr /></td></tr>
EOF
}
sub html {
my $javascript = javascript();
my $css = css();
return <<EOF;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>
VMchart - LVM and BTRFS Monitoring
</title>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
$javascript
</script>
<style type="text/css">
$css
</style>
</head>
<div id="navigation">go to: <span id="linklist"><a href="#frontends">Frontends</a> [<span id="felist"> </span>]</span><span id="total"></span></div>
<div class="header"><a href="https://readme.phys.ethz.ch/services/lvmchart/">VMchart - LVM and BTRFS Monitoring</a></div>
<body onload="init();">
<div id="message"></div>
<div id="grandtotal" style="display: none;">
<a name="grandtotal"></a>
<h1 align="center">Grand total</h1>
<h3 align="center">Last update: $lastUpdate</h3>
<div id="totalchart" align="center"></div>
</div>
<div align="center" id="data"><a name="frontends"></a><h1>Frontend information</h1></div>
<div id="javascr"></div>
<div align="center" id="backends"></div>
<div id="emptySlices"></div>
<hr />
<a name="log"></a><div align="left" id="changes"></div>
<!--[if !IE]>-->
<a name="warnings"></a><div id="warnings"></div>
<!--<![endif]-->
<!--[if IE]>
<div id="warnings">Please make sure you have Internet Explorer's compatibility mode disabled.<br /></div>
<![endif]-->
</body>
</html>
EOF
}
sub css {
return <<EOF;
body {
background: #eee;
font-family: Arial;
border: 0;
padding: 0;
margin: 0;
font-size: 90%;
}
a {
text-decoration: none;
color: #555;
}
a[name] {
padding-top: 40px;
}
.header {
font-size: 240%;
font-weight: bold;
text-align: center;
padding-top: 40px;
}
#navigation {
padding: 4px;
margin: 0;
position: fixed;
background: #444;
color: #fff;
border-bottom: 1px solid #888;
width: 100%;
z-index: 1000;
}
#navigation a {
font-weight: bold;
color: #bbb;
}
#felist {
font-size: 80%;
}
#total {
float: right;
padding-right: 10px;
}
h2 {
border-bottom: 1px solid #aaa;
font-size: 80%;
}
hr {
width: 99%;
}
#warnings {
color: red;
}
td.host {
font-weight: bold;
font-size: 150%;
}
#message {
padding-top: 10px;
color: green;
font-weight: bold;
text-align: center;
}
#counter {
}
div.chart {
width: 450px;
height: 350px;
}
div.parent {
color:#dc3912;
font-style:italic;
}
div.child {
color:#880088;
font-style:italic;
}
div.btrfs {
color:#000088;
}
div.zfs {
color:#000088;
}
div.freespace {
color:#0c0;
}
div.grandchild {
color:#000088;
font-style:italic;
}
div#changes {
padding-left: 20px;
font-size: 80%;
}
span.free {
color:#0d0;
font-weight: bold;
}
span.remove {
color:#f00;
font-weight: bold;
}
#avail {
padding-left: 20px;
}
#avail th {
border-bottom: 2px solid #333;
border-right: 1px solid #aaa;
}
#avail tr.bg1 {
background-color: #ddd;
}
#avail tr.bg2 {
background-color: #bbb;
}
#avail td {
border-bottom: 1px solid #aaa;
border-right: 1px solid #aaa;
padding: 3px;
}
#avail .r {
text-align: right;
}
EOF
}
sub javascript {
return <<EOF;
google.charts.load('current', {'packages':['corechart', 'orgchart'], 'language': 'ch'});
var numberOfServersDisplayed = 0;
var req = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); // Create Ajax request object
function init(){
//Use ajax to load server data
ajaxLoad("$myself?data", ajaxOnResult);
}
function ajaxLoad(url, callback){
req.open("GET", url, true);
req.send(null);
req.onreadystatechange = callback; //Start ajaxOnResult function after the stream
window.setTimeout( function() {ajaxOnProgress(req);},500);
}
function ajaxOnProgress(req) {
if (req.readyState != 4) { //while the data are received
var msg = document.getElementById("message");
if(msg.innerHTML == ''){
msg.innerHTML = '<div><img src="spinner.gif" /><div id="counter">Loading Server 1 of $numberOfServers</div></div>';
}
ajaxDisplayServer();
window.setTimeout( function() {ajaxOnProgress(req);},500);
}
}
function ajaxDisplayServer(){
if(req.responseText.match(/ENDOFSERVER/g)){
var server = req.responseText.split(/ENDOFSERVER/g); //Split the stream into individual servers
var numberOfServersReceived = server.length -1; //the number of servers in the array
for (i=numberOfServersDisplayed; i< numberOfServersReceived; i++){ //iterate over received server data
var serverdata = server[i].split(/ENDOFELEMENT/g); //Split into elements(js/markup/name/wanings)
var js=serverdata[0];
var markup=serverdata[1];
var servername=serverdata[2];
var warnings=serverdata[3];
var total=serverdata[4];
var totalUsed=serverdata[5];
var unit=serverdata[6];
document.getElementById('warnings').innerHTML += warnings;