#!/bin/bash
# $Id: snmpdiskio,v 1.3 2006/04/04 13:11:47 mikaelf Exp $
# snmpdiskio v0.9.4 (c) 2006 Mikael Fridh <mikael@meanstreak.se>

# Set default procfile for kernel 2.4
PROCFILE="/proc/partitions"
MODE="linux24"

# Probably kernel 2.6:
if [ -f /proc/diskstats ]; then
    PROCFILE=/proc/diskstats
    MODE="linux26"
fi


function hdNum()
{
	awk ' BEGIN { num=0 } $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ { num++ } END { print num } ' $PROCFILE
}

function hdIndex()
{
	awk ' BEGIN { num=0 } $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ { num++; print num } ' $PROCFILE
}

function hdDescr()
{
	if [ "$MODE" = "linux26" ]; then
		awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ { printf "%s\n", $3 }' $PROCFILE
	else
		awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ { printf "%s\n", $4 }' $PROCFILE
	fi
}

function hdInBlocks()
{
	awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && $10 ~ /[0-9]+/ { printf "%.0f\n", $10 * 512 }
	      $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && $10 !~ /[0-9]+/ { printf "%.0f\n", $5 * 512 } ' $PROCFILE
}

function hdOutBlocks()
{
	awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && $8 ~ /[0-9]+/ { printf "%.0f\n", $8 * 512 }
	      $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && $8 !~ /[0-9]+/ { printf "%.0f\n", $7 * 512 } ' $PROCFILE
}

function usage()
{
	cat <<-EOUSAGE
Usage: $0 <hdNum|hdIndex|hdDescr|hdInBlocks|hdOutBlocks>
EOUSAGE
}

if [ 1 -ne $# ]; then
	usage
	exit 1
fi

case $1 in 
	hdNum|hdIndex|hdDescr|hdInBlocks|hdOutBlocks)
		$1
	;;
	'hdNum')
		hdNum
	;;
	'hdIndex')
		hdIndex
	;;
	'hdDescr')
		hdDescr
	;;
	'hdInBlocks')
		hdInBlocks
	;;
	'hdOutBlocks')
		hdOutBlocks
	;;
	*)
		usage
		exit 1
	;;
esac

exit 0
