#!/bin/bash -e

if [ -z "$1" ]; then
  DIR="/sys/block/[sv]da"
elif [ -d "/sys/block/sd$1" ]; then
  DIR="/sys/block/sd$1"
elif [ -d "/sys/block/vd$1" ]; then
  DIR="/sys/block/vd$1"
elif [ -L "$1" ]; then
  DIR="/sys/block/$(basename $(readlink $1))"
elif [ -d /sys/block/$1 ]; then
  DIR=/sys/block/$1
else
  echo "DISKIO CRITICAL - Missing disk '$1'!"
  exit 2
fi

if [ -f "$DIR/queue/hw_sector_size" ]; then
  SECTOR_SIZE=$(cat "$DIR/queue/hw_sector_size")
else
  # RHEL does not have hw_sector_size
  SECTOR_SIZE=512
fi

awk '
  {
    max=1048576*1024*100/8
    ss='$SECTOR_SIZE'
    gsub("  *", " ")
    printf "DISKIO OK - '$(basename "$DIR")':%s|", $0
    printf "read=%dc;;;0;%d ", $3*ss, max
    printf "write=%dc;;;0;%d ", $7*ss, max
    printf "ioread=%dc ", $1
    printf "iowrite=%dc ", $5
    printf "queue=%dc;;;0;%d\n", $11, max
  }
' $DIR/stat

exit 0
