
########################################################
# Please file all bug reports, patches, and feature
# requests under:
#      https://sourceforge.net/p/logwatch/_list/tickets
# Help requests and discusion can be filed under:
#      https://sourceforge.net/p/logwatch/discussion/
########################################################

##########################################################################
# Copyright (c) 2013 gulikoza
# Covered under the included MIT/X-Consortium License:
#    http://www.opensource.org/licenses/mit-license.php
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
###########################################################################
#Main

use strict;
#use warnings;

#Exit early if undesired
exit 0 unless $ENV{LOGWATCH_DETAIL_LEVEL} || $ENV{PRINTING} && $ENV{PRINTING} eq 'y';

#Exit early if the report is not for the current host.
if( $ENV{LOGWATCH_ONLY_HOSTNAME} ) {
    my $hostname = $ENV{HOSTNAME};   # see scripts/logwatch.pl, line 702!
    $hostname ||= do { require POSIX; ( POSIX::uname() )[1]; }; # fallback ;-)
    # "ONLY_HOSTNAME" is a misnomer, as it is actually defined as "hostlimit"
    # @ scripts/logwatch.pl, line 702
    # which is a list of hosts, per scripts/logwatch.pl, line 1131
    # so, actually bailout if "current host" is NOT included in this list.
    my $found = 0;
    for my $wanted ( split /[,]/, $ENV{LOGWATCH_ONLY_HOSTNAME} ) {
        my $match_to = $hostname;
        # shorten only if that's also short
        if( -1 == index( $wanted, '.' ) ) {
            $match_to =~ s/[.].*//;
        }
        if( $wanted eq $match_to ) {
            $found = 1;
            last;
        }
    }
    exit 0 if !$found;
}

#Exit early if running in some virtual environment
my $kvm = 0;
if( $ENV{get_kvm_status} ) {
    $kvm = qx($ENV{get_kvm_status});
}
else {
    if( open my $fh, '<', '/proc/cpuinfo' ) {
        while( my $line = <$fh> ) {
            next if -1 == index( $line, 'QEMU Virtual CPU' );
            $kvm = 1;
            last;
        }
        close $fh;
    }
}
exit 0 if $kvm;

#Output sensors stats
for my $exe ( $ENV{pathto_sensors}, '/usr/bin/sensors' ) {
    if( defined $exe && -x $exe ) {
        my $sensors = qx($exe);
   if ($sensors) {
      print $sensors;
   }
}
}

# vi: shiftwidth=4 tabstop=4 syntax=perl expandtab
