Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- use strict;
- use warnings;
- use Fcntl ':flock';
- my $sensor = $ARGV[0];
- my $type = $ARGV[1];
- my $server = $ARGV[2];
- $type = 'numeric' if not defined $type;
- exit(1) if not defined $server or not defined $sensor;
- $sensor =~ s/\'//g;
- my $expires = 60;
- my $user = 'zabbix';
- my $pass = 'zzzzzz';
- my $ipmi_cmd = '';
- my $cache_file = '/var/tmp/ipmi_sensors_'.$server;
- $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';
- my @rows = ();
- if(not -e $cache_file) {
- open(CACHE, '>>', $cache_file);
- if(flock(CACHE, LOCK_EX | LOCK_NB)) {
- my $results = results();
- if(defined $results) {
- truncate(CACHE, 0);
- print CACHE $results;
- close(CACHE);
- } else {
- close(CACHE);
- unlink($cache_file);
- exit(1);
- }
- }
- } else {
- open(CACHE, '>>', $cache_file);
- if(flock(CACHE, LOCK_EX | LOCK_NB)) {
- my @stat = stat($cache_file);
- my $delta = time() - $stat[9];
- if($delta > $expires or $delta < 0) {
- my $results = results();
- if(defined $results) {
- truncate(CACHE, 0);
- print CACHE $results;
- close(CACHE);
- } else {
- close(CACHE);
- unlink($cache_file);
- exit(1);
- }
- }
- }
- }
- open(CACHE, '<' . $cache_file);
- flock(CACHE, LOCK_EX);
- @rows = <CACHE>;
- close(CACHE);
- foreach my $row (@rows) {
- my @cols = split(',', $row);
- if($cols[1] eq $sensor) {
- if($type eq 'discrete') {
- my $r = $cols[5];
- $r =~ s/\'//g;
- chop($r);
- print $r;
- } elsif($type eq 'numeric') {
- if($cols[3] eq '' or $cols[3] eq 'N/A') {
- print "0";
- } else {
- print $cols[3];
- }
- }
- }
- }
- sub results {
- my $results = `$ipmi_cmd`;
- if($? == 0) {
- return $results;
- } else {
- return undef;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment