Advertisement
Guest User

Untitled

a guest
Sep 4th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4. use warnings;
  5. use Fcntl ':flock';
  6. use Scalar::Util qw(looks_like_number);
  7. use feature qw(switch);
  8.  
  9. my $server = $ARGV[0];
  10. my $class = $ARGV[1];
  11. my $key = $ARGV[2];
  12. my $type="";
  13. my $reqtype=$ARGV[3];
  14.  
  15. exit(1) if not defined $server or not defined $key;
  16. exit(1) if not defined $reqtype and $class eq "sensor";
  17.  
  18. my $expires = 60;
  19.  
  20. my $user = 'monitoring';
  21. my $pass = 'P@$$w0rd';
  22.  
  23. my $ipmi_cmd = '';
  24. my $cache_file = '';
  25. my $number = int(rand(10000));
  26.  
  27. if($class eq 'sensor') {
  28. $cache_file = '/var/tmp/ipmi_sensors_'.$server.'-'.$number;
  29. $ipmi_cmd = '/usr/sbin/ipmi-sensors -D LAN2_0 -h '.$server.' -u '.$user.' -p '.$pass.' -l USER -W discretereading --no-header-output --quiet-cache --sdr-cache-recreate --comma-separated-output --entity-sensor-names 2>/dev/null';
  30. } elsif($class eq 'chassis') {
  31. $cache_file = '/var/tmp/ipmi_chassis_'.$server.'-'.$number;
  32. $ipmi_cmd = '/usr/sbin/ipmi-chassis -D LAN2_0 -h '.$server.' -u '.$user.' -p '.$pass.' -l USER -W discretereading --get-status 2>/dev/null';
  33. } elsif($class eq 'fru') {
  34. $cache_file = '/var/tmp/ipmi_fru_'.$server.'-'.$number;
  35. $ipmi_cmd = '/usr/sbin/ipmi-fru -D LAN2_0 -h '.$server.' -u '.$user.' -p '.$pass.' -l USER -W discretereading 2>/dev/null';
  36. } elsif($class eq 'bmc') {
  37. $cache_file = '/var/tmp/ipmi_bmc_'.$server.'-'.$number;
  38. $ipmi_cmd = '/usr/sbin/bmc-info -D LAN2_0 -h '.$server.' -u '.$user.' -p '.$pass.' -l USER -W discretereading 2>/dev/null';
  39. } else {
  40. exit(1);
  41. }
  42.  
  43. my @rows = ();
  44.  
  45. my $results = results();
  46.  
  47. open(CACHE, '>>', $cache_file);
  48. if(flock(CACHE, LOCK_EX | LOCK_NB)) {
  49. truncate(CACHE, 0);
  50. print CACHE $results;
  51. close(CACHE);
  52. }
  53.  
  54. open(CACHE, '<' . $cache_file);
  55. flock(CACHE, LOCK_EX);
  56. @rows = <CACHE>;
  57. close(CACHE);
  58.  
  59. print "{\n";
  60. print " \"data\":[\n";
  61. my $flag=0;
  62.  
  63. foreach my $row (@rows) {
  64. if($class eq 'sensor') {
  65. my @cols = split(',', $row);
  66. my $UCols=uc($cols[1]);
  67. my $Section=$cols[2];
  68. my $UKey=uc($key);
  69. my $contains = 0;
  70.  
  71. given($UKey) {
  72. when("TEMP") {
  73. if ($Section eq "Temperature") {$contains = 1;}
  74. }
  75. when("FAN") {
  76. if ($Section eq "Fan") {$contains = 1;}
  77. }
  78. when("DISK") {
  79. if ($Section eq "Drive Slot") {$contains = 1;}
  80. }
  81. when("POWER METER") {
  82. if ($Section eq "Current") {$contains = 1;}
  83. }
  84. when(index($_, "POWER SUPPL") != -1) {
  85. if ($Section eq "Power Supply") {$contains = 1;}
  86. }
  87. when("VRM") {
  88. if ($Section eq "Power Unit") {$contains = 1;}
  89. }
  90.  
  91. when("MEMORY") {
  92. if ($Section eq "Memory") {$contains = 1;}
  93. }
  94. }
  95.  
  96.  
  97. if($contains > 0) {
  98. if (looks_like_number($cols[3])) {
  99. $type="numeric";
  100. } else {
  101. $type="discrete";
  102. }
  103.  
  104. if (($Section eq "Fan") and (index($UCols, "FANS") != -1)) {$type="discrete";}
  105.  
  106. if (($reqtype eq "discrete") and ($Section eq "Power Supply")) {$type="discrete";}
  107.  
  108. if (($type eq $reqtype) or ($reqtype eq "all")) {
  109. if($flag eq 1) {
  110. print ",\n";
  111. }
  112. print " {\n";
  113. print " \"{#CLASS}\":\"${class}\",\n";
  114. print " \"{#KEY}\":\"${cols[1]}\",\n";
  115. print " \"{#SECTION}\":\"${cols[2]}\",\n";
  116. print " \"{#TYPE}\":\"${type}\",\n";
  117. print " \"{#MEASURE}\":\"${cols[4]}\"}";
  118. $flag=1;
  119. }
  120. }
  121. } elsif(($class eq 'fru') or ($class eq 'bmc') or ($class eq 'chassis')) {
  122. $type="discrete";
  123. my @cols = split(':', $row);
  124. my $name=$cols[0];
  125. $name=~ s/(\s+)/ /gi;
  126. if (($class eq 'bmc') or ($class eq 'chassis')) {$name=substr($name, 0, -1);}
  127. my $UKey=uc($key);
  128. my $UCols=uc($name);
  129. if(0<=index($UCols,$UKey) and ($name)) {
  130. if($flag eq 1) {
  131. print ",\n";
  132. }
  133. print " {\n";
  134. print " \"{#CLASS}\":\"${class}\",\n";
  135. print " \"{#TYPE}\":\"${type}\",\n";
  136. print " \"{#KEY}\":\"${name}\"}";
  137. $flag=1;
  138. }
  139. }
  140. }
  141.  
  142. print "]}\n";
  143.  
  144. unlink $cache_file;
  145.  
  146. sub results {
  147. my $results = `$ipmi_cmd`;
  148. if((defined $results) and (length $results > 0)) {
  149. return $results;
  150. } else {
  151. return undef;
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement