Guest User

ipmi sensors

a guest
Jul 23rd, 2013
863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4. use warnings;
  5. use Fcntl ':flock';
  6.  
  7. my $sensor = $ARGV[0];
  8. my $type = $ARGV[1];
  9. my $server = $ARGV[2];
  10.  
  11. $type = 'numeric' if not defined $type;
  12. exit(1) if not defined $server or not defined $sensor;
  13.  
  14. $sensor =~ s/\'//g;
  15.  
  16. my $expires = 60;
  17.  
  18. my $user = 'zabbix';
  19. my $pass = 'zzzzzz';
  20.  
  21. my $ipmi_cmd = '';
  22. my $cache_file = '/var/tmp/ipmi_sensors_'.$server;
  23. $ipmi_cmd = '/opt/freeipmi/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 2>/dev/null';
  24.  
  25. my @rows = ();
  26. if(not -e $cache_file) {
  27. open(CACHE, '>>', $cache_file);
  28. if(flock(CACHE, LOCK_EX | LOCK_NB)) {
  29. my $results = results();
  30. if(defined $results) {
  31. truncate(CACHE, 0);
  32. print CACHE $results;
  33. close(CACHE);
  34. } else {
  35. close(CACHE);
  36. unlink($cache_file);
  37. exit(1);
  38. }
  39. }
  40. } else {
  41. open(CACHE, '>>', $cache_file);
  42. if(flock(CACHE, LOCK_EX | LOCK_NB)) {
  43. my @stat = stat($cache_file);
  44. my $delta = time() - $stat[9];
  45. if($delta > $expires or $delta < 0) {
  46. my $results = results();
  47. if(defined $results) {
  48. truncate(CACHE, 0);
  49. print CACHE $results;
  50. close(CACHE);
  51. } else {
  52. close(CACHE);
  53. unlink($cache_file);
  54. exit(1);
  55. }
  56. }
  57. }
  58. }
  59.  
  60. open(CACHE, '<' . $cache_file);
  61. flock(CACHE, LOCK_EX);
  62. @rows = <CACHE>;
  63. close(CACHE);
  64.  
  65. foreach my $row (@rows) {
  66. my @cols = split(',', $row);
  67.  
  68. if($cols[1] eq $sensor) {
  69. if($type eq 'discrete') {
  70. my $r = $cols[5];
  71. $r =~ s/\'//g;
  72. chop($r);
  73. print $r;
  74. } elsif($type eq 'numeric') {
  75. if($cols[3] eq '' or $cols[3] eq 'N/A') {
  76. print "0";
  77. } else {
  78. print $cols[3];
  79. }
  80. }
  81. }
  82. }
  83.  
  84. sub results {
  85. my $results = `$ipmi_cmd`;
  86. if($? == 0) {
  87. return $results;
  88. } else {
  89. return undef;
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment