#!/bin/sh
#
# Helper function to diagnose common configuration errors when setting up
# the DGEE environment.
#


MINMSGMNI=128
MINMSGMAX=4096

MINQUEUED=20


# LINUX
# -----
# Find out how many message queues system wide we can have

# TODO Detect system type get these values the native way (ipcs -l perhaps)
#
MSGMNI=`cat /proc/sys/kernel/msgmni`
MSGMNB=`cat /proc/sys/kernel/msgmnb`
MSGMAX=`cat /proc/sys/kernel/msgmax`

MAXQUEUED=$(($MSGMNB/$MSGMAX))
MINMSGMNB=$(($MSGMAX*$MINQUEUED))

PROBLEMCOUNT=0
PASSCOUNT=0

echo
echo DGEE System Configuration Diagnostic Tool
echo
echo Checking IPC Resource Limits.......
echo

# Check IPC Resources
#
if [ $MSGMNI -lt $MINMSGMNI ]
then
  PROBLEMCOUNT=$(($PROBLEMCOUNT+1))
  cat << EOF
** Problem Detected **
There are not enough message queues system wide to support the DGEE.
You should have at least $MINMSGMNI.

Under linux this may be increased in two ways:
    1. echo $MINMSGMNI > /proc/sys/kernel/msgmni  (as root)
 or 2. edit /etc/sysctl.conf and add :

# Increase Max Message Queues to $MINMSGMNI
kernel.msgmni = $MINMSGMNI

1 - The change remains until the next reboot.
2 - The change takes effect on next reboot.

You will probably want to do both of these to perminently fix the problem.

To view your current IPC settings:
shell> ipcs -l
-------------------------------------------------------------------------------

EOF
else
  PASSCOUNT=$(($PASSCOUNT+1))
  echo "Number of available message queues  : $MSGMNI"
fi

if [ $MSGMAX -lt $MINMSGMAX ]
then
  PROBLEMCOUNT=$(($PROBLEMCOUNT+1))
  cat << EOF
** Problem Detected **
The maximum size of a single message in a queue is too low.
Goldwater will have to resort to disk bound queuing for every message that
exceeds $MSGMAX bytes in size, which is very inefficient.

You should allow messages of at least $MINMSGMAX bytes.

Under linux this may be increased in two ways:
    1. echo $MINMSGMAX > /proc/sys/kernel/msgmax  (as root)
 or 2. edit /etc/sysctl.conf and add :

# Increase Max Message Size to $MINMSGMAX
kernel.msgmax = $MINMSGMAX

1 - The change remains until the next reboot.
2 - The change takes effect on next reboot.

You will probably want to do both of these to perminently fix the problem.

** Be advised that making this value too large will reduce the number of
** messages that may be present in a single queue (current maximum of $MSGMNB).
** You may find that you need to increase this value as well (maxmnb).

To view your current IPC settings:
shell> ipcs -l
-------------------------------------------------------------------------------

EOF
else
  PASSCOUNT=$(($PASSCOUNT+1))
  echo "Maximum number of bytes per queue   : $MSGMNB"
fi

if [ $MAXQUEUED -lt $MINQUEUED ]
then
  PROBLEMCOUNT=$(($PROBLEMCOUNT+1))

  if [ $MAXQUEUED -ge 2 ]
  then
WARNING="\
If you feel that the current setting of $MAXQUEUED messages per queue is not too
low for your system (perhaps because the DGEE will be very lightly loaded in 
use) and you know what you're doing, then you may choose to ignore this warning.
"
fi

  cat << EOF
** Problem Detected **
The maximum number of bytes allowed in a single message queue is rather low
(currently at a maximum of $MSGMNB bytes).

Your current configuration therefore allows $MAXQUEUED messages of $MSGMAX bytes
to be queued at any one time for a particular Goldwater server.  Any more than 
this in the queue and you will see queue failures. Really you should be looking
to allow somewhere in the region of $MINQUEUED maximum sized messages in a
queue at once for a really busy server.

The smaller a single message is, the more of them may be queued at once.

$WARNING
To correct the queue limit, you may do the following:

  1. You can increase the maximum number of bytes allowed in a single msg queue
  2. You can decrease the maximum size of a message that may be queued so that 
     more may be queued at once.

2 has the dissadvantage that if a message that wants to be queued for a server
exceeds the max size of a message, then Goldwater will have to resort to disk
bound queuing, which is very much slower.

To increase the maximum number of bytes that may be stored in a single message
queue you may do the following:

    1. echo $MINMSGMNB > /proc/sys/kernel/msgmnb  (as root)
 or 2. edit /etc/sysctl.conf and add :

# Increase Max Message Queues Size to $MINMSGMNB
kernel.msgmnb = $MINMSGMNB

1 - The change remains until the next reboot.
2 - The change takes effect on next reboot.

You will probably want to do both of these to perminently fix the problem.

To view your current IPC settings:
shell> ipcs -l
-------------------------------------------------------------------------------
EOF
else
  PASSCOUNT=$(($PASSCOUNT+1))
  echo Maximum number of bytes per message : $MSGMAX
  echo Maximum number of messages per queue: $MAXQUEUED
fi

echo
echo Checking File Permissions.......
echo
echo -=+ Still to do +=-
echo

cat << EOF
-------------------------------------------------------------------------------

Tests Complete!!

  Summary
  -------

  Passes  : $PASSCOUNT
  Failures: $PROBLEMCOUNT

EOF

# Check Problem Count
#
if [ $PROBLEMCOUNT -gt 0 ]
then
  cat << EOF
  $PROBLEMCOUNT Problems were detected with your current system configuration.
  Please read the above advice and make any changes necessary.
  Remember to re-run this diagnostic tool afterwards to verify the changes have
  been successful.

EOF
fi
#end
