Created
July 22, 2020 00:51
-
-
Save afresh1/2644ede84a74c10350474a195cb0b9f7 to your computer and use it in GitHub Desktop.
A perl script to convert OpenBSD's `sysctl hw.sensors` output into something that might be useful for Zabbix.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -T | |
use v5.16; | |
use warnings; | |
use OpenBSD::Pledge qw< pledge >; | |
use JSON::PP qw<>; | |
# Copyright (c) 2020 Andrew Hewus Fresh <[email protected]> | |
# | |
# Permission to use, copy, modify, and distribute this software for any | |
# purpose with or without fee is hereby granted, provided that the above | |
# copyright notice and this permission notice appear in all copies. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
%ENV = (); | |
pledge(qw< exec proc >) || die "Unable to pledge: $!"; | |
open my $fh, '-|', '/sbin/sysctl', 'hw.sensors' | |
or die "Unable to open sysctl: $!"; | |
pledge() or die "Unable to pledge: $!"; | |
my %sensors; | |
while ( my $line = readline $fh ) { | |
chomp $line; | |
my ($k, $v) = split /=/, $line, 2; | |
$k =~ s/^hw\.sensors\.//; | |
my ($type) = $k =~ /\. ([^.]*?) \d+ $/x; | |
# next unless $supported{$type}; # TODO | |
# from print_sensor() in src/sbin/sysctl/sysctl.c | |
my %sensor = ( | |
type => $type, | |
#raw => $v, | |
); | |
if ( $v =~ s/,\s+(OK|WARNING|CRITICAL|UNKNOWN)$// ) { | |
$sensor{status} = $1; | |
} | |
if ( $v =~ s/\s+\((.*)\)$// ) { | |
$sensor{desc} = $1; | |
} | |
if ( $type ne 'drive' and $v =~ s/\s+(.*)$// ) { | |
$sensor{units} = $1; | |
} | |
$sensor{value} = $v; | |
$sensors{$k} = \%sensor; | |
} | |
close $fh; | |
say JSON::PP->new->utf8->encode(\%sensors); | |
#say JSON::PP->new->utf8->canonical->pretty->encode(\%sensors); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment