#!/usr/bin/env perl # Copyright (c) 2025, R. W. Rodolico # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # linux_system # Linux system and operatingsystem sections: uptime, OS name, version and kernel. # # Revision History: # 2026-10-18 Claude v1.0.0 new sensor, split from unix_system (bug 210) package linux_system; use strict; use warnings; use version; our $VERSION = version->declare('v1.0.0'); use FindBin; use File::Spec; use lib File::Spec->catdir($FindBin::Bin), File::Spec->catdir("$FindBin::Bin/.."); use library qw(:all); my $CATEGORY_SYSTEM = 'system'; my $CATEGORY_OS = 'operatingsystem'; ####################################################### # # parseLinuxUptime( $uptimeText ) # # Converts /proc/uptime content into old system uptime and # last_boot lines. # # Parameters: # $uptimeText - text from /proc/uptime # # Returns: # array reference of output lines # # Side Effects: # none # ####################################################### sub parseLinuxUptime { my $uptimeText = shift; my @out; if (defined($uptimeText) && $uptimeText =~ /^([0-9]+(?:\.[0-9]+)?)/) { my $uptime = int($1); push @out, "$CATEGORY_SYSTEM\tlast_boot\t" . (time() - $uptime); push @out, "$CATEGORY_SYSTEM\tuptime\t$uptime"; } return \@out; } ####################################################### # # parseLinuxOsValues( $lsbText, $unameText, $procVersion ) # # Parses linux_os_values-compatible keys from command text. # # Parameters: # $lsbText - text from lsb_release -a # $unameText - text from uname -s -r # $procVersion - text from /proc/version # # Returns: # array reference of output lines # # Side Effects: # none # ####################################################### sub parseLinuxOsValues { my ($lsbText, $unameText, $procVersion) = @_; my @out; my %map = ( 'Distributor ID' => 'distribution', 'Description' => 'description', 'Release' => 'release', 'Codename' => 'codename', ); if (defined($lsbText)) { foreach my $line (split(/\n/, $lsbText)) { foreach my $left (keys %map) { if ($line =~ /^\Q$left\E\s*:\s*(.*)$/) { my $v = trim($1); push @out, "$CATEGORY_OS\t$map{$left}\t$v" if $v ne ''; } } } } if (defined($unameText) && $unameText =~ /^(\S+)\s+(\S+)/) { push @out, "$CATEGORY_OS\tos_name\t$1"; push @out, "$CATEGORY_OS\tkernel\t$2"; } if (defined($procVersion)) { my $v = trim($procVersion); push @out, "$CATEGORY_OS\tos_version\t$v" if $v ne ''; } return \@out; } ####################################################### # # main() # # Emits system and operatingsystem keys. # # Parameters: # none # # Returns: # integer exit status (1 wrong OS, 2 not applicable) # # Side Effects: # runs cat, lsb_release and uname, writes STDOUT/STDERR # ####################################################### sub main { if (!checkOS({ 'linux' => undef })) { print STDERR "unsupported os\n"; return 1; } unless (validCommandOnSystem({ 'uname' => undef })) { print STDERR "uname not found\n"; return 2; } my @out; push @out, @{parseLinuxUptime(runCommand('cat', '/proc/uptime'))}; push @out, @{parseLinuxOsValues( runCommand('lsb_release', '-a'), runCommand('uname', '-s', '-r'), runCommand('cat', '/proc/version'), )}; print join("\n", @out), "\n" if @out; return 0; } exit main() unless caller; 1;