Guest User

Untitled

a guest
Nov 19th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use threads;
  5. use Thread::Queue::Any;
  6.  
  7. use Net::OpenSSH;
  8.  
  9. use Parse::DMIDecode::Constants qw(@TYPES);
  10. use Parse::DMIDecode qw();
  11.  
  12. use Data::Dumper;
  13.  
  14. $| = 1;
  15.  
  16. sub inventory() {
  17. my ($host, $tqueue) = @_;
  18. my $remote = 'root@' . $host;
  19.  
  20. $SIG{'KILL'} = sub { threads->exit(); };
  21.  
  22. my $ssh = Net::OpenSSH->new($remote, timeout => 6);
  23. if ($ssh->error) {
  24. print STDERR "ERROR : couldn't establish SSH connection to $host: ". $ssh->error;
  25. $tqueue->enqueue("| [[$host]] |cannot connect|||||\n");
  26. threads->exit();
  27. }
  28.  
  29. my $result = $ssh->capture("dmidecode");
  30. if (! $result) {
  31. print STDERR "ERROR : remote command failed on $host: " . $ssh->error;
  32. $tqueue->enqueue("| [[$host]] |no dmidecode|||||\n");
  33. threads->exit();
  34. }
  35.  
  36. my $dmi = Parse::DMIDecode->new( nowarnings => 1 );
  37. $dmi->parse($result);
  38.  
  39. my $physical_cpus = 0;
  40. my $cpu_slots = 0;
  41. my $total_ram = 0;
  42. my $rammap = "";
  43.  
  44. for my $handle ($dmi->get_handles(group => "processor")) {
  45. my $type = ($handle->keyword("processor-type") or "");
  46. next unless $type =~ /Central Processor/i;
  47.  
  48. # Check the status of the cpu
  49. my $status = ($handle->keyword("processor-status") or "");
  50. if ($status !~ /Unpopulated/i) {
  51. $physical_cpus++;
  52. $cpu_slots++;
  53. } else {
  54. $cpu_slots++;
  55. }
  56. }
  57.  
  58. for my $handle ($dmi->get_handles( group => "memory" )) {
  59. if ($TYPES[$handle->dmitype] eq "Memory Device") {
  60. for my $keyword ($handle->keywords) {
  61. if ($keyword eq "memory-size") {
  62. if ($handle->keyword($keyword) eq "No Module Installed") {
  63. $rammap .= "/0";
  64. } else {
  65. my $ramsize = $handle->keyword($keyword);
  66. $ramsize =~ s/ MB//g;
  67. $total_ram += $ramsize;
  68. $rammap .= "/" . $ramsize;
  69. }
  70. }
  71. }
  72. }
  73. }
  74.  
  75. $rammap = substr($rammap, 1, length($rammap) - 1);
  76. $rammap =~ s/ MB//g;
  77.  
  78. my $cpu_type = $dmi->keyword("processor-version");
  79. if ($cpu_type eq "Not Specified") {
  80. $cpu_type = $ssh->capture("cat /proc/cpuinfo | grep 'model name' | head -1 | cut -f2 -d':'") or
  81. die "remote command failed: " . $ssh->error;
  82. chomp($cpu_type);
  83. $cpu_type = substr($cpu_type, 1, length($cpu_type) - 1);
  84. }
  85.  
  86. my %descr;
  87.  
  88. $descr{"hostname"} = $host;
  89. $descr{"system-manufacturer"} = $dmi->keyword("system-manufacturer");
  90. $descr{"system-product-name"} = $dmi->keyword("system-product-name");
  91. $descr{"cpu-count"} = $physical_cpus;
  92. $descr{"cpu-slots"} = $cpu_slots;
  93. $descr{"cpu-type"} = $cpu_type;
  94. $descr{"ram-total"} = $total_ram;
  95. $descr{"ram-map"} = $rammap;
  96. $descr{"system-serial-number"} = $dmi->keyword("system-serial-number");
  97.  
  98. $tqueue->enqueue(%descr);
  99. threads->exit();
  100. }
  101.  
  102.  
  103. sub main() {
  104. my $tqueue = Thread::Queue::Any->new;
  105. my @joinable;
  106. my $thr;
  107. my %results;
  108. my $starttime = time();
  109.  
  110. my $TIMEOUT = 10; # secs
  111.  
  112. foreach my $server (@ARGV) {
  113. my $thr = threads->create(\&inventory, $server, $tqueue);
  114. }
  115.  
  116.  
  117. while (threads->list()) {
  118. last if ((time() - $starttime) > $TIMEOUT);
  119. @joinable = threads->list(threads::joinable);
  120. foreach my $thr (@joinable) { $thr->join(); }
  121. }
  122.  
  123. my @leftovers = threads->list();
  124. foreach my $thr (@leftovers) { $thr->kill('KILL')->detach(); }
  125.  
  126. while ($tqueue->pending()) {
  127. my %data = $tqueue->dequeue();
  128. $results{$data{'hostname'}} = { %data };
  129. }
  130.  
  131. print "\n^ Hostname ^ Model ^ CPU ^ RAM ^ Serial ^ iLO IP ^\n";
  132.  
  133. foreach my $host (sort keys %results) {
  134. my $line = "| [[" . $host . "]] | ";
  135. $line .= $results{$host}{'system-manufacturer'} . " " . $results{$host}{'system-product-name'} . " | ";
  136. $line .= $results{$host}{'cpu-count'} . "(/" . $results{$host}{'cpu-slots'} . ') x ' . $results{$host}{'cpu-type'} . ' | ';
  137. $line .= $results{$host}{'ram-total'} . " MB (" . $results{$host}{'ram-map'} . ') | ';
  138. $line .= $results{$host}{'system-serial-number'} . ' | | ';
  139. $line .= "\n";
  140. print $line;
  141.  
  142. }
  143. }
  144.  
  145.  
  146.  
  147. &main();
Add Comment
Please, Sign In to add comment