Skip to content
check_hw_ikus 6.79 KiB
Newer Older
#!/bin/bash
###
#
# Repport hardware info
#
# @AUTHOR: Patrik Dufresne (http://patrikdufresne.com)
# Copyright 2015 Patrik Dufresne
# Last modified 2015-10-30
# Please send all comments, suggestions, bugs and patches to (info AT patrikdufresne DOT com)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
###

VERSION=1.0
AUTHOR="Patrik Dufresne"
SERVICE="HW"

# Replacement for the exit function, will cleanup any tempfiles or such
# before exiting.
function cleanup {
    exit $1
}

declare -rx PROGNAME=${0##*/}
declare -rx PROGPATH=${0%/*}/

if [ -r "${PROGPATH}utils.sh" ] ; then
    source "${PROGPATH}utils.sh"
else
    echo "Can't find utils.sh. This plugin needs to be run from the same directory as utils.sh which is most likely something like /usr/lib/nagios/plugins or /usr/lib64/nagios/plugins"
    printf "Currently being run from %s\n" "$PROGPATH"
    # Since we couldn't define STATE_UNKNOWN since reading utils.sh
    # failed, we use 3 here but everywhere else after this use cleanup $STATE
    cleanup 3
fi

# Set STATE to UNKNOWN as soon as we can (right after reading in util.sh
# where the STATES are defined)
STATE=$STATE_UNKNOWN

# provide a quick one liner of how to use the program
function usage {
    printf " %s %s for Nagios - Usage %s \
[-t timeout] [-v [-v [-v]]]\n" "$PROGNAME" "$VERSION" "$PROGNAME"
    cleanup $STATE_UNKNOWN
}

# provide detailed explanations of the command line syntax
function longhelp {
    # put your long help here
    printf "%s plugin version %s for Nagios by %s
  -h, --help          Display this message.
  -t, --timeout=sec   Set script timeout in seconds.
  -v, --verbose       Up the verbosity level by one.
  --verbosity=val     Set the verbosity level to val.
  -V, --version       Print version information.
  --range_help        Explain threshold ranges.
" "$PROGNAME" "$VERSION" "$AUTHOR"
    cleanup $STATE_UNKNOWN
}

# use getopt, trust me on this one. It's the easiest way
getopt -T
if [ $? -ne 4 ] ; then
    printf "%s: getopt is in compatibility mode.\n" "$SCRIPT"
    cleanup $STATE_UNKNOWN
fi

# Tell it which switches and longswitches you'll take and place a trailing
# colon (:) on the ones take arguments. Nagios guidelines require you to
# use all the ones specified below with the exception of --verbosity which I've
# added to circumvent the awkward -v -v -v syntax. Getopt takes care of
# positional parameters and errors for missing expected arguments so we can
# shift later without checking
RESULT=`getopt --name "$SCRIPT" --options "-h,-V,-v,-c:,-w:,-t:" --longoptions "help,version,verbose,verbosity:,warning:,critical:,timeout:" -- "$@"`

# make the result of getopt your new argument list ($@)
eval set -- "$RESULT"

declare WARNING
declare CRITICAL
# all scripts should have a mechanism to terminate themselves if they are
# running for too long. Scripts you might think of as innocuous could end
# up waiting forever on I/O, especially if a disk is failing
declare -i TIMELIMIT=15
# Nagios defines behavior for VERBOSITY 0 (default) through 3
declare -i VERBOSITY=0

while [ $# -gt 0 ] ; do
    case "$1" in
        -h | --help)
            longhelp;;
        -V | --version)
            print_revision "$PROGNAME" "$VERSION"
            cleanup $STATE;;
        -v | --verbose)
            VERBOSITY=$(($VERBOSITY + 1));;
        --verbosity)
            shift
            VERBOSITY=$1;;
        -w | --warning)
            shift
            WARNING=$1;;
        -c | --critical)
            shift
            CRITICAL=$1;;
        -t | --timeout)
            shift
            TIMELIMIT=$1;;
        --)
            shift
            break;;
        *)
            echo "Option $1 not supported. Ignored." >&2;;
    esac
    shift
done

#Verbosity level    Type of output
#0            Single line, minimal output. Summary
#1            Single line, additional information (eg list processes that fail)
#2            Multi line, configuration debug output (eg ps command used)
#3            Lots of detail for plugin problem diagnosis
if [ $VERBOSITY -gt 3 ] ; then
    shopt -o -s xtrace
fi

# what needs to happen in the event of a timeout
function timeout {
    echo "UNKNOWN - script timed out after $TIMELIMIT seconds."
    cleanup $STATE_UNKNOWN
}

# since we've processed the options which potentially set the timeout limit,
# we can setup a timeout trap now
trap timeout USR1
    ( sleep $TIMELIMIT; if [ `pgrep -U $USER -f "$SCRIPT" | grep -c ^$$$` -gt 0 ] ; then kill -USR1 $$ ; fi; ) </dev/null &>/dev/null &
    
    # Operating System
    OS=""
    if [ -r /etc/*release ]; then
        source /etc/*release
        OS=$(echo $PRETTY_NAME)
    fi
    
    # Get Virtualization Type & Role
    VIRT_TYPE=$(systemd-detect-virt)

    if [ "$VIRT_TYPE" = "none" ]; then
        # Get motherboard info
        BOARDVENDOR=""
        if [ -r /sys/devices/virtual/dmi/id/board_vendor ]; then
            BOARDVENDOR=$(cat /sys/devices/virtual/dmi/id/board_vendor)
        fi
        BOARDNAME=""
        if [ -r /sys/devices/virtual/dmi/id/board_name ]; then
            BOARDNAME=$(cat /sys/devices/virtual/dmi/id/board_name)
        elif [ $(grep -c 'Hardware' /proc/cpuinfo) -gt 0 ]; then
            DATA=$(grep -m 1 'Hardware' /proc/cpuinfo)
            BOARDNAME=${DATA##*: }
        fi
        # Get Disk info
        DISK_DATA=$(ls /dev/sd? | xargs -I {} sh -c 'echo -n "Disk {}: "; sudo smartctl -a {} | egrep "(Device Model:|Serial Number:)" | sed "s/Device Model: *//g;s/Serial Number: */SN: /g" | tr "\n" " " ; echo ')
        # Memory modules
        MEM_MODULES="$(sudo dmidecode --type 17 | grep -e 'Memory Device' -e 'Type:' -e 'Size' -e 'Serial' -e 'Part' | tr '\n' ' ' |  tr '\t' ' ' | tr -s ' ' | sed 's/Memory Device /\n/g')"
    # Get CPU info
    DATA=$(grep -m 1 'model name' /proc/cpuinfo)
    CPU=${DATA##*: }

    # Cpu count
    CPU_COUNT="$(cat /proc/cpuinfo  | grep -c 'model name')"

    # Memory
    MEMORY="$(awk '/MemTotal/ {printf( "%d\n", $2 / 1000 )}' /proc/meminfo) MiB"
if [ "$VIRT_TYPE" = "none" ]; then
  echo "Type: ${VIRT_TYPE^^}, OS: $OS, Board: $BOARDVENDOR $BOARDNAME, CPU: $CPU_COUNT X $CPU, MEM: $MEMORY"
  echo "Type: ${VIRT_TYPE^^}, OS: $OS, CPU: $CPU_COUNT, MEM: $MEMORY"
Type: ${VIRT_TYPE^^}
Board: $BOARDVENDOR $BOARDNAME
CPU Count: $CPU_COUNT
CPU: $CPU
Memory: $MEMORY
Memory Modules: $MEM_MODULES