#!/bin/bash
#
# Copyright (c) 2010,2012 Red Hat, Inc.
#
# 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, either version 2 of
# the License, or (at your option) any later version.
#
# 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/.

PATH=/sbin:/usr/sbin:$PATH

SLABLOG=$(mktemp -p /mnt/testarea -t Slab.XXXXXX)

function WatchFile ()
{
    FILE=$1

    TMPFILE=$(mktemp -p /mnt/testarea -t Watch.XXXXX)
    while true; do
	diff=$(diff -q $FILE $TMPFILE)
	rc=$?
	if [ $rc != 0 ]; then
	    echo "File is still growing.."
	    cat $FILE > $TMPFILE
	else
	    rm $TMPFILE
	    return 0
	fi
	sleep 2
    done
}

function SysRq ()
{
    ACTION=$1
    logger -p local2.info -t "List of $ACTION Tasks" Start
    sleep 1
    echo $ACTION > /proc/sysrq-trigger
    WatchFile /var/log/messages
    logger -p local2.info -t "List of $ACTION Tasks" Stop
}

# Verbose tree view of running processes:

ps -elfH | logger -p local2.info -t "LWD:ps-elfH"

# Get some system information from the system

PROCLIST='m t w'

for p in $PROCLIST ; do
    # Dump a list of blocked tasks and their information
    SysRq $p
    sleep 2
done

# Send slab info to a file for logging
timestamp=$(/bin/date '+%F %T')
echo "Dumping slabinfo - Start - $timestamp" >> $SLABLOG
cat /proc/slabinfo >> $SLABLOG
timestamp=$(/bin/date '+%F %T')
echo "Dumping slabinfo - Stop - $timestamp" >> $SLABLOG
logger -p local2.info -t "SlabCacheInfo" -f $SLABLOG

