Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Linux Server Monitoring with IPMI


Listing 1 ipmi init script for Linux

#!/bin/sh
#
# chkconfig: 2345 11 60
# description: start, stop, or query ipmi system monitoring tools
# config: /etc/sysconfig/ipmi

# Source function library.
. /etc/init.d/functions

# Exit silently if we don't have a sysconfig file,
# and read IPMI setting from it to determine whether or
# not to continue.
# The only current setting is ipmi={YES|NO}, whether or not
# to enable IPMI.
[ -f /etc/sysconfig/ipmi ] || exit 0
. /etc/sysconfig/ipmi
[ "${IPMI}" = "yes" ] || exit 0

RETVAL=0

start() {
    echo -n $"Starting ipmi: "

    # If ipmidev isn't listed in /proc/devices, try
    # loading the modules.
    if ! grep -q ipmidev /proc/devices
    then
        /sbin/modprobe ipmi_msghandler || RETVAL=1
        /sbin/modprobe ipmi_devintf || RETVAL=1
        # Try loading new driver module, fall back to old
        # module if that fails.
        if ! /sbin/modprobe ipmi_si >/dev/null 2>&1
        then
        /sbin/modprobe ipmi_kcs_drv || RETVAL=1
        fi
    fi


    # If ipmidev still isn't listed in /proc/devices after we load
    # modules, this just isn't going to work.  Set RETVAL to mark
    # this failure.
    grep -q ipmidev /proc/devices || RETVAL=1

    # Check if the device file exists and create if not.
    if [ ! -c /dev/ipmi0 ] && [ $RETVAL -eq 0 ]
    then
        major=$(awk '/ ipmidev$/{print $1}' /proc/devices)
        /bin/mknod -m 0600 /dev/ipmi0 c $major 0 || RETVAL=1
    fi

    if [ $RETVAL -eq 0 ] && touch /var/lock/subsys/ipmi ; then
        echo_success
        echo
    else
        echo_failure
        echo
    fi
}

stop() {
    echo -n $"Shutting down ipmi: "

    # Stop doesn't actually do anything because we currently don't
    # unload ipmi modules on stop.  That might change in the future
    # if we decide unloading the ipmi modules is safe.
    RETVAL=0

    if [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/ipmi ; then
        echo_success
        echo
    else
        echo_failure
        echo
    fi
}

dostatus() {
    # Extract cpu temperatures from ipmitool output.

    # Abort if we don't have the ipmitool program.
    if ! /usr/bin/ipmitool -V >/dev/null
    then
        echo "/usr/bin/ipmitool not found!" >&2
        exit 1
    fi

    # Abort if ipmi isn't loaded.
    if ! grep -q ipmidev /proc/devices
    then
        echo "ipmi not listed in /proc/devices!" >&2
        exit 1
    fi

    # Check if we are running on a v1.0 IPMI system, and
    # change our processor search string appropriately.
    if /usr/bin/ipmitool -I open bmc info | \
        grep -q "IPMI Version.*1.0"
    then
        IpmiVersion="1.0"
    fi

    # Determine # of running processors
    NumProcs=$(grep -c processor /proc/cpuinfo)
    if [ $NumProcs -eq 0 ]
    then
        echo "Can't determine number of processors!" >&2
        exit 1
    fi

    # Now build the query string.  Concatenate it into
    # one string because that's more efficient on 2.4 systems.
    Count=1
    TempString=""
    while [ $Count -le $NumProcs ]
    do
        if [ x$IpmiVersion  = x"1.0" ]
        then
        TempString="$TempString CPU\ $Count"
        else
        TempString="$TempString Processor$Count\ Temp"
        fi
        Count=$((Count + 1))
    done
    # building TempString like this and eval'ing it is ugly, but
    # it's the only way I could make the quoting work.  Sorry.
    TempString="/usr/bin/ipmitool -I open sensor get $TempString"
    eval $TempString | awk -v "c=$Count" '
BEGIN {
   n = 1
}
/Sensor Reading/ {
   printf "CPU%s Temp: %s\n",n,$4
   n++
}
END {
   if ( n != c) {
    printf "Error: found %s CPUs, but got temps for %s\n",--c,--n >"/dev/stderr"
    exit 1
   }
   exit 0
}'
    RETVAL=$((RETVAL + $?))
    return $RETVAL
}

restart() {
    stop
    start
    RETVAL=$?
}

condrestart() {
    [ -e /var/lock/subsys/ipmi ] && restart || :
}

remove () {
    # Actually remove the drivers.  Don't do during stop in case
    # this causes system to become unstable (a la lm_sensors)
    if /sbin/lsmod | awk '{print $1}' | grep -q ipmi_
    then
        # Try removing both 2.4 and 2.6 modules.
        /sbin/rmmod ipmi_si 2>/dev/null
        /sbin/rmmod ipmi_kcs_drv 2>/dev/null
        /sbin/rmmod ipmi_devintf
        /sbin/rmmod ipmi_msghandler
    else
        echo "No ipmi modules loaded!" >&2
        RETVAL=1
        return $RETVAL
    fi

    # Wait a sec to give modules time to unload.
    sleep 1

    # Check if we failed to remove any modules, and complain if so.
    if /sbin/lsmod | awk '{print $1}' | grep -q ipmi_
    then
        echo "ipmi modules still loaded!" >&2
        RETVAL=1
        return $RETVAL
    fi
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    dostatus
    ;;
  restart|reload)
    restart
    ;;
  condrestart)
    condrestart
    ;;
  remove)
    remove
    ;;
  *)
    echo "Usage: ipmi {start|stop|status|restart|condrestart|remove}"
    exit 1
esac

exit $RETVAL


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.