-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathboxinfo.pl
executable file
·4026 lines (3263 loc) · 133 KB
/
boxinfo.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/env perl
# -*-mode:cperl; indent-tabs-mode: nil-*-
## Gather important information about a box
## Output in HTML or MediaWiki friendly format
##
## Greg Sabino Mullane <[email protected]>
## Copyright End Point Corporation 2008-2017
## BSD licensed
## See: http://bucardo.org/wiki/Boxinfo
use strict;
use 5.006001;
use warnings;
use Data::Dumper qw{ Dumper };
use Getopt::Long qw{ GetOptions };
use File::Basename qw{ basename };
our $VERSION = '1.5.0';
my $USAGE = "Usage: $0 <options>
Important options:
--verbose
--skippgport=#
--postgresonly
Set ENV{PG_CONFIG} if not in the path (or adjust your path!)
For complete help, please visit:
http://bucardo.org/wiki/Boxinfo
";
my %opt;
GetOptions(
\%opt,
'version',
'verbose',
'quiet',
'help',
'format=s',
'os=s',
'client=s',
'skippgport=s',
'postgresonly',
'nopostgres',
'postgresnohost',
'nohost',
'nomysql',
'nosendmail',
'useballoons',
'timeout=i',
) or die $USAGE;
$opt{help} and die $USAGE;
if ($opt{version}) {
print "$0 version $VERSION\n";
exit 0;
}
my $OS = $opt{os} || $^O;
my $quiet = $opt{quiet} || 0;
my $verbose = $opt{verbose} || 0;
my $use_balloons = exists $opt{useballoons} ? $opt{useballoons} : 1;
my $timeout = exists $opt{timeout} ? $opt{timeout} : 10;
my $format = $opt{format} || 'wiki';
$opt{use_su_postgres} = 0;
## Used to identify the version of programs and Perl modules
my $versionre = qr{\d+\.\d+(?:\S*)};
## Inline CSS
my $vtop = ' style="vertical-align: top"';
## For ease of writing the HTML later on
my $wrap = '<br />';
## What to print if we can't figure out what something is
my $UNKNOWN_VALUE = '?';
my $UNKNOWN_VERSION = '?';
## Dump the actual output of each command into a debug file
my $debugfile = 'boxinfo.debug';
open my $debugfh, '>', $debugfile or die qq{Could not write "$debugfile": $!\n};
printf {$debugfh} "PROGRAM: $0\nStart: %s\n", scalar localtime();
## When we leave for any reason, close the debug file and remove the temp file
END {
defined $debugfh and (close $debugfh or die qq{Could not close "$debugfile": $!\n});
!$quiet and defined $debugfile and print "Raw data is in $debugfile\n";
unlink '/tmp/boxinfo.tmp';
}
## Output messages per language
my %msg = (
'en' => {
'time-day' => q{day},
'time-days' => q{days},
'time-hour' => q{hour},
'time-hours' => q{hours},
'time-minute' => q{minute},
'time-minutes' => q{minutes},
'time-month' => q{month},
'time-months' => q{months},
'time-second' => q{second},
'time-seconds' => q{seconds},
'time-week' => q{week},
'time-weeks' => q{weeks},
'time-year' => q{year},
'time-years' => q{years},
},
'fr' => {
'time-day' => q{jour},
'time-days' => q{jours},
'time-hour' => q{heure},
'time-hours' => q{heures},
'time-minute' => q{minute},
'time-minutes' => q{minutes},
'time-month' => q{mois},
'time-months' => q{mois},
'time-second' => q{seconde},
'time-seconds' => q{secondes},
'time-week' => q{semaine},
'time-weeks' => q{semaines},
'time-year' => q{année},
'time-years' => q{années},
},
'de' => {
},
'es' => {
},
);
## Figure out which language to use for output
our $lang = $ENV{LC_ALL} || $ENV{LC_MESSAGES} || $ENV{LANG} || 'en';
$lang = substr($lang,0,2);
## Figure out our hostname and the short version
my $hostname = qx{hostname};
chomp $hostname;
if (length $hostname) {
print {$debugfh} "HOST: $hostname\n";
}
my $shorthost = $hostname;
if ($shorthost =~ m{^(\w+\d\.\w+)\.\w+\.\w+$}) {
$shorthost = $1;
}
else {
$shorthost =~ s/^(.+?)\..+/$1/;
}
## Pick a client name for use in the page title
my $clientname = 'ACME';
if ($opt{client}) {
$clientname = $opt{client};
}
elsif ($hostname =~ m{([^\.]+)\.\w+$}) {
$clientname = ucfirst lc $1;
}
## We put the current time at the top of the report
my $nowtime = qx{date};
chomp $nowtime;
my %data = (
program_version => $VERSION,
program_name => basename ($0),
program_start => $nowtime,
OS => $OS,
hostname => $hostname,
shorthost => $shorthost,
clientname => $clientname,
);
my %distlist = (
'redhat' => ['redhat-release', 'Red Hat', 'release' ],
'fedora' => ['fedora-release', 'Fedora', 'release' ],
'SuSE' => ['SuSE-release', 'SuSE', 'release' ],
'gentoo' => ['gentoo-release', 'Gentoo', 'release' ],
'debian' => ['debian_version', 'Debian', 'version' ],
'slackware' => ['slackware-release', 'Slackware', 'release' ],
'mandrake' => ['mandrake-release', 'Mandrake', 'release' ],
'yellowdog' => ['yellowdog-release', 'Yellowdod', 'release' ],
);
## Gather versions of all kinds of programs (should be run first)
gather_versions();
if ($opt{postgresonly}) {
## Postgres information (generic)
gather_postgresinfo();
goto GATHERDONE;
}
## yum and apt-get : what's installed?
gather_package_info();
## Postgres information
gather_postgresinfo();
## MySQL information
gather_mysqlinfo();
## uname
gather_uname();
## lsb-release and redhat-release
gather_dist() if $OS eq 'linux';
## memory is very OS specific
gather_memory();
## Is it a VM?
gather_vminfo();
## RightScale information
gather_rightscale();
## Grab all cron information we can find
gather_croninfo();
## All information on disks and filesystems
gather_mounts();
## Detailed info on each disk
#gather_disk_settings();
## Size of disks
gather_disksize();
## User limits
gather_ulimits();
## Environment variables
gather_envs();
## Network interface (ethernet cards)
gather_interfaces();
## Network routing information
gather_routes();
## What is set to run on boot?
gather_chkconfig();
## CPU information
gather_cpuinfo();
## Gather Perl module versions and detailed Perl information
gather_perlinfo();
## Information on the uptime
gather_uptime();
## Puppet info
gather_puppet();
## Loaded modules
gather_lsmod();
## LifeKeeper information
gather_lifekeeper();
## Heartbeat (Linux HA information)
gather_heartbeat();
## Ruby gems (local)
gather_gems();
GATHERDONE:
## remove any temporary constructs from the hash
for (keys %data) {
delete $data{$_} if $_ =~ /^tmp_/;
}
if ('wiki' eq $format or 'html' eq $format) {
create_html_output();
}
else {
die "Unknown format: must be 'html' or 'wiki'\n";
}
exit;
sub slurp_table_info {
my $arg = shift or die;
run_command($arg->{command},'tmp_slurp');
my $slinfo = $data{tmp_slurp};
if ($arg->{failregex} and $slinfo =~ /$arg->{failregex}/) {
warn "$arg->{failregexmsg}\n";
return;
}
return unless $slinfo =~ /\w/;
my ($n,$v);
my $currname = '';
for my $line (split /\n/ => $slinfo) {
next if $line !~ /^(\w+).*\| (.*)/o;
($n,$v) = (lc $1,$2||'');
if ($n eq $arg->{pk}) {
$currname = $v;
}
$arg->{var}{$currname}{$n} = $v;
}
return;
} ## end of slurp_table_info
sub gather_uname {
if ($OS eq 'linux') {
run_command('uname --kernel-name' => 'Kernel name');
run_command('uname --nodename' => 'Node name');
run_command('uname --kernel-release' => 'Kernel release');
run_command('uname --kernel-version' => 'Kernel version');
run_command('uname --machine' => 'Hardware name');
run_command('uname --processor' => 'Processor');
run_command('uname --hardware-platform' => 'Hardware platform');
}
elsif ($OS =~ /bsd/ or $OS =~ /darwin/) {
run_command('uname -s' => 'Kernel name');
run_command('uname -n' => 'Node name');
run_command('uname -r' => 'Kernel release');
run_command('uname -v' => 'Kernel version');
run_command('uname -p' => 'Processor');
run_command('uname -m' => 'Hardware platform');
}
else {
$quiet or warn qq{Cannot gather uname information on OS "$OS"\n};
}
return;
} ## end of gather_uname
sub gather_dist {
my $lsb1 = '/etc/lsb-release';
my $lsb2 = '/usr/bin/lsb_release';
if (-e $lsb1) {
open my $fh, '<', $lsb1 or die qq{Could not open "$lsb1": $!\n};
while (<$fh>) {
if (/^DISTRIB_(.+?)=(.+)/) {
my ($nam,$val) = ($1,$2);
$val =~ s/^"(.+)"$/$1/;
$data{lsb_release}{$nam} = $val;
}
elsif (/^LSB_VERSION="(\d[\.\d]*)"/) {
$data{lsb_version} = $1;
}
else {
$quiet or warn qq{Unknown line $. in $lsb1: $_\n};
}
}
close $fh or die qq{Could not close "$lsb1": $!\n};
}
## Check for lsb_release executable for the codename
if (-e $lsb2) {
run_command("$lsb2 -a", 'tmp_lsb');
if ($data{tmp_lsb} =~ /Codename:\s+(.+)/) {
$data{dist_codename} = $1;
}
}
for my $dist (keys %distlist) {
my $file = "/etc/$distlist{$dist}->[0]";
if (-e $file) {
open my $fh, '<', $file or die qq{Could not open "$file": $!\n};
while (<$fh>) {
chomp;
$data{dist}{$dist} = $_;
last;
}
close $fh or die qq{Could not close "$file": $!\n};
}
}
return;
} ## end of gather_dist
sub gather_vminfo {
$data{VM} = 'No';
## Check for a EC2 motd
my $file = '/etc/motd';
if (-e $file) {
if (open my $fh, '<', $file) {
my $slurp;
{ local $/; $slurp = <$fh>; }
close $fh or warn qq{Could not close "$file": $!\n};
if ($slurp =~ /eip:\s+(\S+)\ninstance id:\s+(\S+)\ntype:\s+(\S+)/) {
($data{EC2}{eip}, $data{EC2}{id}, $data{EC2}{type}) = ($1,$2,$3);
}
}
}
## Find the EC2 ami version
$file = '/usr/bin/ec2-ami-tools-version';
if (-x $file) {
run_command($file, 'tmp_ec2');
if ($data{tmp_ec2} =~ /\d/) {
$data{EC2}{ami} = $data{tmp_ec2};
}
## Grab the rest of the EC2 information on the fly
if (exists $data{version}{curl}) {
my %meta;
my $uri = 'http://169.254.169.254/latest/meta-data/';
parse_ec2_uri(\%meta, $uri);
sub parse_ec2_uri {
my ($tempname, $tempurl) = @_;
my $info = qx{curl -s $tempurl};
for my $word (split /\n/ => $info) {
if ($word =~ m{/$}) {
$tempname->{$word} = {};
parse_ec2_uri($tempname->{$word}, "${tempurl}$word");
}
elsif ($word =~ /\w$/) {
my $uri = "${tempurl}$word";
$tempname->{$word} = qx{curl -s $uri};
}
}
return;
}
$data{EC2}{meta} = \%meta;
}
}
if ($OS =~ /bsd/ or $OS =~ /darwin/) {
run_command('ps -o stat', 'tmp_ps');
if ($data{tmp_ps} =~ /J$/m) {
$data{VM} = 'BSD jail';
return;
}
}
run_command('dmidecode', 'tmp_dmi');
if ($data{tmp_dmi} =~ /VMware/) {
$data{VM} = 'VMware';
return;
}
if ($data{tmp_dmi} =~ /Manufacturer: Microsoft/) {
$data{VM} = 'VirtualPC';
return;
}
if (-e '/proc/xen/capabilities') {
$data{VM} = sprintf 'Xen %s', -e '/proc/xen/grant' ? '3.x' : '2.x';
return;
## Capture xm list?
}
return;
} ## end of gather_vminfo
sub gather_rightscale {
## If /etc/rightscale.d exists, we can be pretty sure this is a RightScale box
my $dir = '/etc/rightscale.d';
return if ! -d $dir;
$data{RightScale} = {};
## What type of cloud?
my $file = "$dir/cloud";
if (open my $fh, '<', $file) {
## Simply grab the first line
$data{RightScale}{cloud} = <$fh>;
chomp $data{RightScale}{cloud};
close $fh or warn qq{Could not close "$file": $!\n};
}
## What version?
$file = "$dir/rightscale-release";
if (open my $fh, '<', $file) {
## Simply grab the first line
$data{RightScale}{version} = <$fh>;
chomp $data{RightScale}{version};
close $fh or warn qq{Could not close "$file": $!\n};
}
return;
} ## end of gather_rightscale
sub gather_memory {
if ($OS eq 'linux') {
run_command('cat /proc/meminfo', 'tmp_meminfo');
## Extract ueful bits out and erase the main entry
## Seems to be all in kB
my $info = $data{'tmp_meminfo'};
my @memstuff = (
['Total', 'MemTotal'],
['Free', 'MemFree'],
['Cached', 'Cached'],
['Active', 'Active'],
['Inactive', 'Inactive|Inact_clean'],
['Swap Total', 'SwapTotal'],
['Swap Free', 'SwapFree'],
['HPsize', 'Hugepagesize'],
['HPtotal', 'HugePages_Total'],
['HPfree', 'HugePages_Free'],
['HPreserved', 'HugePages_Rsvd'],
);
for (@memstuff) {
my ($name,$olabel) = @$_;
my $found = 0;
for my $label (split /\s*\|\s*/ => $olabel) {
if ($info =~ /$label:\s+(\d+) kB/) {
my $val = $1 * 1024;
$data{memory}{$name} = $val;
$data{memory}{pretty}{$name} = pretty_size($val);
$found = 1;
last;
}
elsif ($info =~ /$label:\s+(\d+)/) {
$data{memory}{$name} = $1;
$found = 1;
last;
}
}
if (!$found and $name !~ /^HP/) {
$quiet or warn qq{Could not determine "$name" from meminfo output\n};
}
}
## Now shared memory settings
for my $name (qw/shmmax shmmni shmall/) {
run_command("cat /proc/sys/kernel/$name", 'tmp_shm');
$data{memory}{$name} = delete $data{'tmp_shm'};
if ($name !~ /i$/) {
$data{memory}{pretty}{$name} = pretty_size($data{memory}{$name});
}
}
run_command('ipcs -m', 'tmp_active_shared_memory');
$data{memory}{active_shared} = ($data{tmp_active_shared_memory} =~ y/\n/\n/) - 3;
$data{memory}{active_shared} = 0 if $data{tmp_active_shared_memory} !~ /\d/;
run_command('ipcs -s', 'tmp_active_semaphores');
$data{memory}{active_semaphores} = ($data{tmp_active_semaphores} =~ y/\n/\n/) - 3;
$data{memory}{active_semaphores} = 0 if $data{tmp_active_semaphores} !~ /\d/;
run_command('ipcs -q', 'tmp_active_message_queues');
$data{memory}{active_messages} = ($data{tmp_active_message_queues} =~ y/\n/\n/) - 3;
$data{memory}{active_messages} = 0 if $data{tmp_active_message_queues} !~ /\d/;
run_command('ipcs -u', 'tmp_ipcs_summary');
## Future: parse the above
## Major VM tunables
for my $vm (qw/swappiness dirty_ratio dirty_background_ratio nr_hugepages/) {
run_command("cat /proc/sys/vm/$vm", 'tmp_swap');
if ($data{'tmp_swap'} =~ /\d/) {
$data{vm}{$vm} = $data{'tmp_swap'};
}
}
## Huge pages
run_command('cat /sys/kernel/mm/transparent_hugepage/enabled', 'tmp_hugetemp');
if ($data{tmp_hugetemp} =~ /always/) {
$data{memory}{transparent_hugepages} = $data{tmp_hugetemp};
}
}
elsif ($OS =~ /bsd/ or $OS =~ /darwin/) {
exists $data{'tmp_sysctl'} or run_command('sysctl -a', 'tmp_sysctl');
my $info = $data{'tmp_sysctl'};
my @memstuff = (
['Total', 'hw.physmem'],
['User', 'hw.usermem'],
['Real', 'hw.realmem'],
['VM kmem', 'vm.kmem_size'],
);
for (@memstuff) {
my ($name,$label) = @$_;
if ($info =~ /$label\s*[=:]\s*(\d+)/) {
my $val = $1;
$data{memory}{$name} = $val;
$data{memory}{pretty}{$name} = pretty_size($val);
}
elsif (!$quiet) {
if ($OS !~ /darwin/ or $label !~ /realmem|kmem/) {
warn qq{Could not find "$label" in sysctl output\n};
}
}
}
run_command('ipcs -m', 'tmp_active_shared_memory');
$data{memory}{active_shared} = ($data{tmp_active_shared_memory} =~ y/\n/\n/) - 3;
$data{memory}{active_shared} = 0 if $data{tmp_active_shared_memory} !~ /\d/;
run_command('ipcs -s', 'tmp_active_semaphores');
$data{memory}{active_semaphores} = ($data{tmp_active_semaphores} =~ y/\n/\n/) - 3;
$data{memory}{active_semaphores} = 0 if $data{tmp_active_semaphores} !~ /\d/;
run_command('ipcs -q', 'tmp_active_message_queues');
$data{memory}{active_message} = ($data{tmp_active_message_queues} =~ y/\n/\n/) - 3;
$data{memory}{active_messages} = 0 if
(! defined $data{memory}{active_messages} or $data{tmp_active_message_queues} !~ /\d/);
run_command('ipcs -a', 'tmp_ipcs_all');
run_command('ipcs -T', 'tmp_ipcs_T');
}
else {
$quiet or warn qq{Do not know how to check OS "$OS" for memory\n};
}
return;
} ## end of gather_memory
sub gather_croninfo {
if ($OS eq 'linux') {
run_command('cat /etc/crontab', 'tmp_etc_crontab');
my $info = $data{tmp_etc_crontab};
for my $var (qw/d hourly daily weekly monthly/) {
my $crondir = "/etc/cron.$var";
if (opendir my $dirh, $crondir) {
for my $file (grep { /^\w/ } readdir $dirh) {
run_command("cat $crondir/$file", 'tmp_cron');
$info = $data{tmp_cron};
}
}
}
my $crondir = '/var/spool/cron';
if (opendir my $dirh, $crondir) {
for my $file (grep { /^\w/ } readdir $dirh) {
run_command("cat $crondir/$file", 'tmp_cron');
$info = $data{tmp_cron};
}
}
}
elsif ($OS =~ /bsd/ or $OS =~ /darwin/) {
run_command('cat /etc/crontab', 'tmp_crontab');
my $info = $data{tmp_crontab};
}
else {
$quiet or warn qq{Do not know how to find cron infomation on OS "$OS"\n};
}
return;
} ## end of gather_croninfo
sub gather_mounts {
if ($OS eq 'linux') {
run_command('mount -l -f', 'tmp_mount');
for my $line (split /\n/ => $data{tmp_mount}) {
if ($line =~ m{^(.+?) on (.+?) type (.+?) \((.+)\)\s*(.*)}) {
my ($dev,$dir,$type,$options,$label) = ($1,$2,$3,$4,$5);
$data{fs}{$dev} = {dir => $dir, type => $type, options => $options, readahead => '', scheduler => ''};
if ($dev =~ m{^\/dev\/([a-z]+)\d*$}) {
my $name = $1;
if (! exists $data{block}{$name}) {
run_command("cat /sys/block/$name/queue/read_ahead_kb", 'tmp_readahead');
if ($data{tmp_readahead} =~ /(\d+)/) {
$data{block}{$name}{readahead} = (($1 * 512) / 1024) . ' KB';
}
else {
$data{block}{$name}{readahead} = '?';
}
run_command("cat /sys/block/$name/queue/scheduler", 'tmp_scheduler');
my $sc = $data{tmp_scheduler} || '?';
$data{block}{$name}{scheduler} = $sc =~ /^cat/ ? '?' : $sc;
## shows all available
}
}
if ($label =~ s/\[(.+)\]/$1/) {
$data{fs}{$dev}{label} = $label;
}
}
else {
$quiet or warn qq{Do not know how to parse df line: $line\n};
}
}
if (-f '/proc/mdstat') {
run_command('cat /proc/mdstat', 'tmp_mdstat');
if ($data{tmp_mdstat} =~ /Personalities : \S/) {
$data{mdstat} = $data{tmp_mdstat};
}
}
}
elsif ($OS =~ /bsd/ or $OS =~ /darwin/) {
run_command('mount -d -v', 'tmp_mount');
for my $line (split /\n/ => $data{tmp_mount}) {
## Example: /dev/foo on / (ufs, local)
if ($line =~ m{^(.+?) on (.+?)(?: \((.+?)\))*}) {
my ($dev,$dir,$type,$options,$label) = ($1,$2,$3||'','','');
my @options;
($type, @options) = split /, /, $type;
$options = join ', ', @options;
$data{fs}{$dev} = {dir => $dir, type => $type, options => $options};
if ($label =~ s/\[(.+)\]/$1/) {
$data{fs}{$dev}{label} = $label;
}
}
else {
$quiet or warn qq{Do not know how to parse df line: $line\n};
}
}
}
else {
$quiet or die qq{Do not know how to get mount information on OS "$OS"\n};
}
return;
} ## end of gather_mounts
sub gather_disksize {
if ($OS eq 'linux' or $OS =~ /bsd/ or $OS =~ /darwin/) {
run_command('df -h -P', 'tmp_disk_space');
run_command('df -i -P', 'tmp_disk_space_inodes');
my $info = $data{tmp_disk_space};
for my $line (split /\n/ => $info) {
next if $line =~ /^Filesystem/;
next unless $line =~ s{^(.+?)\s+(\d)}{$2};
my $name = $1;
my @info = split /\s+/ => $line;
if ($OS eq 'linux') {
$data{fs}{$name}{size} = $info[0];
$data{fs}{$name}{used} = $info[1];
$data{fs}{$name}{available} = $info[2];
$data{fs}{$name}{capacity} = $info[3];
$data{fs}{$name}{mounted} = $info[4];
}
else { ## BSD ... is not dead
$data{fs}{$name}{size} = $info[1] + $info[2];
$data{fs}{$name}{used} = $info[1];
$data{fs}{$name}{available} = $info[2];
$data{fs}{$name}{capacity} = $info[3];
$data{fs}{$name}{mounted} = $info[4];
}
}
$info = $data{tmp_disk_space_inodes};
for my $line (split /\n/ => $info) {
next if $line =~ /^Filesystem/;
next unless $line =~ s{^(.+?)\s+(\d)}{$2};
my $name = $1;
my @info = split /\s+/ => $line;
if ($OS eq 'linux') {
$data{fs}{$name}{inodes} = $info[0];
$data{fs}{$name}{inodes_used} = $info[1];
$data{fs}{$name}{inodes_free} = $info[2];
$data{fs}{$name}{inodes_usage} = $info[3];
}
else { ##BSD
$data{fs}{$name}{inodes} = $info[4] + $info[5];
$data{fs}{$name}{inodes_used} = $info[4];
$data{fs}{$name}{inodes_free} = $info[5];
$data{fs}{$name}{inodes_usage} = $info[6];
}
}
}
else {
$quiet or die qq{Do not know how to get disk size information on OS "$OS"\n};
}
return;
} ## end of gather_disksize
sub gather_ulimits {
run_command(q{bash -c 'ulimit -a'}, 'tmp_ulimit');
for my $line (split /\n/ => $data{tmp_ulimit}) {
if ($line =~ /^(.+?)\s+\(.+\) (.+)/) {
$data{ulimit}{$1} = $2;
}
else {
$quiet or warn qq{Could not parse line of ulimit output: $line\n};
}
}
return;
} ## end of gather_ulimits
sub gather_envs {
for (keys %ENV) {
$data{ENV}{$_} = $ENV{$_};
}
return;
} ## end of gather_envs
sub gather_interfaces {
run_command('/sbin/ifconfig -v -a', 'tmp_ifconfig');
my $int = '?';
my $hwhex = qr{[a-fA-F0-9:\-]};
my $ip = qr{[0-9]+\.\d+\.\d+\.\d+};
for my $line (split /\n/ => $data{tmp_ifconfig}) {
if ($line =~ /^([\w:]+)/) {
$int = $1;
}
if ($line =~ /\s+HWaddr ($hwhex+)/) {
$data{interface}{$int}{hardware_address} = $1;
}
if ($line =~ /\s+ether ($hwhex+)/) {
$data{interface}{$int}{hardware_address} = $1;
}
if ($line =~ /\s+inet (?:addr:)?($ip)/) {
$data{interface}{$int}{address} = $1;
}
if ($line =~ /\s+Bcast:($ip)/i) {
$data{interface}{$int}{broadcast} = $1;
}
if ($line =~ /\s+Broadcast: ($ip)/i) {
$data{interface}{$int}{broadcast} = $1;
}
if ($line =~ /\s+Mask:($ip)/) {
$data{interface}{$int}{mask} = $1;
}
if ($line =~ /\s+netmask 0x(\S+)/) {
$data{interface}{$int}{mask} = $1;
}
if ($line =~ /\s+inet6 addr: (\S+)/) {
$data{interface}{$int}{inet6_address} = $1;
}
if ($line =~ /\bmtu[: ](\d+)/i) {
$data{interface}{$int}{mtu} = $1;
for my $opt (qw/UP BROADCAST RUNNING MULTICAST ALLMULTI PROMISC POINTOPOINT NOARP/) {
if ($line =~ /\b$opt\b/) {
$data{interface}{$int}{$opt} = 1;
}
}
}
for my $opt ('collisions','txnqueuelen','RX bytes','TX bytes') {
if ($line =~ /\s+$opt:(\d+)/) {
$data{interface}{$int}{$opt} = $1;
}
}
if ($line =~ /\s+([RT])X packets:(\d+)/) {
my $x = lc $1;
$data{interface}{$int}{"${x}x_packets"} = $2;
for my $opt (qw/errors dropped overruns frame carrier/) {
if ($line =~ /\b$opt:(\d+)/) {
$data{interface}{$int}{"${x}x_$opt"} = $1;
}
}
}
if ($line =~ /\boptions=/) {
while ($line =~ /([A-Z_]+)/g) {
$data{interface}{$int}{$1} = 1;
}
}
}
## Check all hostnames for active interfaces, check the speed
for my $int (keys %{$data{interface}}) {
next if ! exists $data{interface}{$int}{UP};
run_command("ethtool $int", 'tmp_ethtool');
if ($data{tmp_ethtool} =~ /Speed: (\d+)(\S+)/) {
$data{interface}{$int}{current_speed} = "$1$2";
$data{interface}{$int}{nowspeed} = $1;
}
if ($data{tmp_ethtool} =~ /Duplex: (\S+)/) {
$data{interface}{$int}{duplex} = $1;
}
if ($data{tmp_ethtool} =~ /Supported link modes: (.+?)Sup/ms) {
my $sup = $1;
my $maxspeed = 0;
while ($sup =~ /(\d+)baseT/g) {
$maxspeed = $1 if $1 > $maxspeed;
}
$data{interface}{$int}{maxspeed} = $maxspeed;
}
next if exists $data{interface}{$int}{NOARP};
my $ip = $data{interface}{$int}{address};
ip2hostname($ip);
}
return;
} ## end of gather_interfaces
sub gather_routes {
if ($OS eq 'linux') {
run_command('route -n', 'tmp_route');
for my $line (split /\n/ => $data{tmp_route}) {
next unless $line =~ /^\d/o;
if ($line =~ /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S+)/o) {
my ($d,$gw,$m,$f,$mt,$ref,$use,$int) = ($1,$2,$3,$4,$5,$6,$7,$8);
push @{$data{route}}, {dest=>$d,gateway=>$gw,mask=>$m,flags=>$f,metric=>$mt,ref=>$ref,use=>$use,int=>$int};
}
else {
$quiet or warn "Could not parse line of route output: $line\n";
}
}
}
elsif ($OS =~ /bsd/ or $OS =~ /darwin/) {
run_command('netstat -r', 'tmp_route');
}
else {
$quiet or warn qq{Do not know how to determine routing information on OS "$OS"\n};
}
return;
} ## end of gather_routes
sub gather_versions {
## --version
for my $prog (qw/
apt-get aptitude autoconf awk bash cc check_postgres.pl chkconfig convert curl cvs dpkg dovecot
elinks emacs emerge find gcc gdb geos-config git gnome-panel gpg gzip iconv initdb interchange
knock konquerer links make mii-tool nano ntpd pdns_server perl pg_config pg_dump
postgres psql puppet python rrdtool rsync ruby s3cmd
screen sed service svn syslog syslog-ng tar tail_n_mail.pl tail_n_mail tcbmgr vi vim wget yum /) {
my $maxtime = $timeout;
if ('yum' eq $prog and 30 > $maxtime) {
$maxtime = 30;
}
run_command("$prog --version", 'tmp_version', $maxtime);
$data{version}{$prog} = ($data{tmp_version} =~ /($versionre)/) ? $1 : $UNKNOWN_VERSION;
}
## version
for my $prog (qw/ openssl /) {
run_command("$prog version", 'tmp_version');
$data{version}{$prog} = ($data{tmp_version} =~ /($versionre)/) ? $1 : $UNKNOWN_VERSION;
}
## -version
for my $prog (qw/ sqlite/) {
run_command("$prog -version", 'tmp_version');
$data{version}{$prog} = ($data{tmp_version} =~ /($versionre)/) ? $1 : $UNKNOWN_VERSION;
}
## -v
for my $prog (qw/ lighttpd php rsyslogd slonik /) {
run_command("$prog -v", 'tmp_version');
$data{version}{$prog} = ($data{tmp_version} =~ /($versionre)/) ? $1 : $UNKNOWN_VERSION;
}
## -V
for my $prog (qw/ nginx pgbouncer rcs sar ssh /) {
run_command("$prog -V", 'tmp_version');
$data{version}{$prog} = ($data{tmp_version} =~ /($versionre)/) ? $1 : $UNKNOWN_VERSION;
}
## -help
for my $prog (qw/ zip /) {
run_command("$prog -help", 'tmp_version');
$data{version}{$prog} = ($data{tmp_version} =~ /($versionre)/) ? $1 : $UNKNOWN_VERSION;
}
## --help
for my $prog (qw/ bzip2 ethtool nrpe 7zr /) {
run_command("$prog --help", 'tmp_version');
$data{version}{$prog} = ($data{tmp_version} =~ /($versionre)/) ? $1 : $UNKNOWN_VERSION;
}
## -h
for my $prog (qw/ memcached /) {
run_command("$prog -h", 'tmp_version');
$data{version}{$prog} = ($data{tmp_version} =~ /($versionre)/) ? $1 : $UNKNOWN_VERSION;
}
## Special cases
run_command('postconf mail_version', 'tmp_version');
$data{version}{postfix} = ($data{tmp_version} =~ /($versionre)/) ? $1 : $UNKNOWN_VERSION;
if (! $opt{nosendmail}) {
run_command('echo \\\$Z | sendmail -bt -d0', 'tmp_version');
$data{version}{sendmail} = ($data{tmp_version} =~ /($versionre)/) ? $1 : $UNKNOWN_VERSION;
}
run_command('exim -bV', 'tmp_version');
$data{version}{exim} = ($data{tmp_version} =~ /($versionre)/) ? $1 : $UNKNOWN_VERSION;
run_command('mysql --version', 'tmp_version');