#!/bin/bash PROGNAME=`basename $0` # ============================= Functions ============================ ############################## Debugging Functions ###################### function BUGPAUSE { echo " " echo "-----------------------------------------------------------" echo " MANUAL BUG CHECK!" echo " Is the program doing what you expect? Please " echo " review preceeding output for correctness. Thank you." echo "-----------------------------------------------------------" echo " " echo "Here are the arguments so far:" echo "ARG1: $ARG1" echo "ARG2: $ARG2" echo "ARG3: $ARG3" echo "ARG4: $ARG4" echo "ARG5: $ARG5" echo " " ask "Continue? [Y/n]" y if [ $? -ne 0 ]; then { exit 100 } fi } #### #### # # Error Handler. # #### #### function ERROR { clear echo "----------------" echo "| ABEND: Code $1 |" echo "-------------------------------------------------------------------------" echo " Bummer! An error occurred!" echo " Don't you just hate it when..." echo "${2}?" echo "==========================" echo " Maybe this will help...." echo "==========================" SYNTAX exit $1 } #### #### # # - SYNTAX -------- Bet you cannot guess what this does. # #### #### function SYNTAX { MORE "Do you wish to view the Syntax for `VERSION`" y clear echo " " echo "=======" echo "WARNING" echo "=======" echo "Serverturbo is a dangerous program, as it fondles sensitive" echo "system settings." echo "If you don't know what you are doing, please do not use" echo "serverturbo, as it may eat your system, melt your hard" echo "disk, spam your great aunt Esmerelda and molest your " echo "neighbor's cat." echo " " echo "SYNTAX:" echo " " echo "Serverturbo modifies the following in an effort to" echo "optimize the performance of a linux host or server." echo " Hard drive performance (hdparm)" echo " Hard drive cache/buffer performance" echo " Virtual memory Cache performance" echo " Network perfomrance" echo " " MORE "Do you wish to continue?" y echo " " echo "Serverturbo has five command-line switches." echo "They are all optional." echo "The command syntax is:" echo "serverturbo [hard-drive to tune][max-readahead] /" echo "[min-readahead][network-optimize][bdflush optimize]" echo " " echo "Example: serverturbo -a 512 128 0 0" echo "Optimizes the first IDE drive, set's max- and min-" echo "readahead to 512 and 128 respectively, and turns on" echo "optimizations for network protocol stacks and bdflush." echo " " echo "Example: serverturbo -u Disables all optimization" echo " " echo "Example: serverturbo -o Enables all optimization" echo " " echo "These options are discussed below." MORE "Do you wish to continue?" y echo " " echo "First command-line argument:" echo "============================" echo "OPTIONS: -h,-v,-a,-b,-c,-d,-o,-u" echo "This is really the only switch that matters. The other" echo "four are for fine-tuning the program settings." echo "This is a multipurpose argument:" echo " -v will cause serverturbo to report its version and " echo "newscript version and then exit." echo " -h will summon this on-line help." echo " -o will instruct serverturbo to optimize ALL parameters" echo "listed above." echo " -u will instruct serverturbo to set ALL parameters " echo "listed above to the standard defaults for most distros." echo "These defaults are taken from Debian Woody, and appear" echo "to be the same for RedHat 7.3." MORE "Do you wish to continue?" y echo " " echo "Omission of the first argument will cause serverturbo" echo "leave the hard-drive tuning parameters as they currently" echo "are and change ALL OTHER SETTINGS to the Standard" echo "defaults." echo "-a, -b, -c, or -d will cause serverturbo to attempt to" echo "do an hdparm tune-up of that IDE drive, e.g. -a will cause" echo "serverturbo to try to tune /dev/hda for optimum performance." echo "If the drive doesn't exist, an error-message is displayed" echo "on stderror by the hdparm program." echo "Any other argument will result in an error-message." echo " " MORE "Do you wish to continue?" y echo " " echo "Second and Third command-line arguments:" echo "========================================" echo "These are used to tune the min and max read-ahead" echo "parameters in /proc/sys/vm. They must be entered as" echo "two numbers, such that the first one, max-readahead, is" echo "at least 28 more than the second one." echo "Omitting these parameters will result in their being set" echo "to the standard defaults." echo "Currently, the -o option sets these parameters to 512 and" echo "128 for max-readahead and min-readahead respectively." echo " " MORE "Do you wish to continue?" y echo " " echo "Fourth command-line argument:" echo "=============================" echo "This is either a 0 or a 1. If omitted, the value will be" echo "defaulted and set to 1 to disable optimization of the network" echo "protocol stack." echo "In order to avoid defaulting this value, a 0 must be typed in" echo "or the -o switch used as the first argument to serverturbo." echo " " MORE "Do you wish to cntinue?" y echo " " echo "Fifth command-line argument:" echo "============================" echo "This is either a 0 or a 1. If omitted, the value will be" echo "defaulted and set to 1 in order to disable optimization of" echo "the cache and VM settings controlled by /proc/sys/vm/bdflush." echo "Currently, the defaults for this parameter are:" echo " 30 500 0 0 500 3000 60 20 0" echo "and the optimized settings are" echo " 100 5000 640 2560 150 30000 5000 1884 2." echo" " MORE "Do you wish to continue?" y echo " " echo "NOTES: " echo "======" echo " " echo "Avoid confusion. Use only the first command-line" echo "argument:" echo " -u to disable all optimizations." echo " -o to enable all optimizations." echo " No argument to disable everything except leave the " echo "hard drive optimizations at their current settings." echo " " echo "Do not attempt to use Serverturbo with Kernel 2.5.x or 2.6.x." echo "It was designed for use with newer 2.4.x kernels, specifically" echo "2.4.18 through 2.4.22. It has been tested with 2.4.21 and 2.4.22" echo "in particular." echo " " MORE "Do you wish to continue?" y echo " " echo "Serverturbo should work nicely in /etc/rc.d/rc.local or " echo "whatever boot-up script you want to use. Experiment " echo "carefully. Heed the warning above!" echo " " echo "Careful examination of the code, particularly in the " echo "GETARGS function will reveal that serverturbo is actually" echo "fairly intelligent about the sorts of arguments it will" echo "accept on the command-line, e.g. -o, --OPTIMIZE, --Turbo, etc." echo "all work, while "o" by itself with no leading hyphen will" echo "not." echo " " echo "Look at the code. Help me debug it! ;-)" echo " " echo "**** End of help. Don't you wish there was more? ****" echo "Strike a key to exit help." read DUMMY return } #### #### # # MORE # #### #### function MORE() { ask "$1 [y/n]" $2 if [ $? -eq 0 ]; then return else exit 0 fi } #### #### # # - CHECKCMD --------- Locates the system cammands used by this script.------- # This makes sure the commands we need exist. If a command is missing, # this function will cause the script to abort. # #### #### function CHECKCMD { # Check for required *NIX executable commands. echo "---->In CHECKCMD...." for i in id seq echo hostname touch cp mv rm rsync mount smbmount ask; do which $i; if [ $? -ne 0 ]; then ERROR 1 "the command $i is missing from the system" fi; done } #### #### # # CHECK_HOST ----Aborts the program if the hostname keyfile is missing. # #### #### function CHECK_HOST() { AUTHORIZED_BACKUP_HOST=`cat /root/bin/snapback_hostkey` if [ $AUTHORIZED_BACKUP_HOST = `hostname` ]; then { echo "Host $AUTHORIZED_BACKUP_HOST validated." } else { ERROR 20 "When you try to run $PROGNAME on an unauthorized backup host" } fi } #### #### # # - R_U_ROOT ----- Aborts the program if the person running it is not root. # #### #### function R_U_ROOT { ID=`id -u` if [ $ID -ne 0 ]; then { echo "`basename $0` is a potentially dangerous program." echo "Therefore it is only to be run by the root user." echo "You appear to be running it as `id -nu`." echo "Please try again as root or have the system administrator run" echo "`basename $0` for you." ERROR 10 "you forget to log in as root first" } fi } #### #### # # - GETARGS ----------- Converts command-line args to appropriate parameters. Otherwise, # it prompts for the necessary arguments. # #### #### function GETARGS { if [ -z $1 ]; then { echo "Leaving Settings for HDPARM as you had them." } fi echo "IN GETARGS...." case "$1" in -o|-O|--optimize|--OPTIMIZE|--Optimize|-optimize|-Optimize|-OPTIMIZE|optimize|Optimize|OPTIMIZE|turbo|TURBO|Turbo|-turbo|-TURBO|-Turbo|--turbo|--Turbo|--TURBO) echo "In TURBO MODE!" echo "Optimizing...." echo "HDPARM -c1 -d1 is enabled for ALL DRIVES" ARG1=ALL echo "MAX READAHEAD: 512" ARG2=512 echo "MIN READAHEAD: 128" ARG3=128 echo "Tune_TCP_STACK is ON" echo "Disable TCP_TIMESTAMPS is ON" ARG4=0 echo "Inserting optimized settings for BDFLUSH" ARG5=YES return ;; -u|-U|--default|--DEFAULT|--Default|-default|-DEFAULT|-Default|default|DEFAULT|Default) echo "Setting to Debian Woody Defaults." echo "in DE-TURBO (Standard) Mode." echo "HDPARM -c0 -d0 is enabled for All drives" ARG1=NONE echo "MAX READAHEAD: 31" ARG2=31 echo "MIN READAHEAD: 3" ARG3=3 echo "TUNE_TCP_STACK is OFF" echo "Disable TCP_TIMESTAMPS is OFF" ARG4=1 echo "Inserting default settings for BDFLUSH" ARG5=NO BUGPAUSE return ;; *) echo "Defaulting to tuning only the first IDE hard drive" ARG1=A ;; esac if [ -z $2 ]; then { echo "Defaulting MAX READAHEAD to 31!" ARG2=31 } else { echo "Optimizing MAX READAHEAD to $2" ARG2=$2 } fi if [ -z $3 ]; then { echo "Defaulting MIN READAHEAD to 3!" ARG3=3 } else { echo "Optimizing MIN READAHEAD to $3" ARG3=$3 } fi VALID=`expr $ARG2 - $ARG3` if [ $VALID -lt 28 ]; then { ERROR 30 "when you forget that MAXREADAHEAD must be at least 28 higher than MINREADAHEAD" } fi if [ $4 -eq 0 ]; then { echo "Optimizing /proc/sys/net/ipv4/tcp_sac" echo "Optimizing /proc/sys/net/ipv4/tcp_timestamps" ARG4=0 } else { echo "Defaulting /proc/sys/net/ipv4/tcp_sac" echo "Defaulting /proc/sys/net/ipv4/tcp_timestamps" ARG4=1 } fi if [ $5 -eq 0 ]; then { echo "Optimizing values for /proc/sys/vm/bdflush." ARG5=yes } else { echo "Defaulting values for /proc/sys/vm/bdflush." ARG5=no } fi } #### #### # # HD_TUNE - This identifies existing IDE partitions from /etc/fstab and then # attempts to tune them. Maybe it will work. # It will tune or de-tune any one drive or all of them. # #### #### function HD_TUNE { case "$ARG1" in -a|-A|a|A) hdparm -c1 -d1 -k1 /dev/hda ;; -b|-B|b|B) hdparm -c1 -d1 -k1 /dev/hdb ;; -c|-C|c|C) hdparm -c1 -d1 -k1 /dev/hdc ;; -d|-D|d|D) hdparm -c1 -d1 -k1 /dev/hdd ;; ALL|all|All|--All|--all|--ALL) { for i in a b c d e f; do { cat /etc/fstab | grep /dev/hd$i if [ $? -eq 0 ]; then { echo "Boosting /dev/hd$i!!!" hdparm -c1 -d1 -k1 /dev/hd$i } else { echo "/dev/hd$i does not exist." echo "No action was taken regarding /dev/hd$i" } fi } done } ;; none|NONE|None|n|N|-n|-N|-none|-NONE|-None|--none|--NONE|--None) { for i in a b c d e f; do { cat /etc/fstab | grep /dev/hd$i if [ $? -eq 0 ]; then { echo "Boosting /dev/hd$i!!!" hdparm -c0 -d0 -k0 /dev/hd$i } else { echo "/dev/hd$i does not exist." echo "No action was taken regarding /dev/hd$i" } fi } done } ;; *) echo "an invalid parameter was entered." echo "The parameter was $ARG1." echo "Nothing was done." ;; esac } #### #### # # TUNE_READAHEAD -- Sets values for Min and Max Readahead # #### #### function TUNE_READAHEAD { echo $ARG2 >/proc/sys/vm/max-readahead echo $ARG3>/proc/sys/vm/min-readahead echo "-------- NEW SETTINGS -----------------------------------------------" echo "MAX_READAHEAD: `cat /proc/sys/vm/max-readahead`" echo "MIN_READAHEAD: `cat /proc/sys/vm/min-readahead`" echo " " } #### #### # # TUNE_TCP_STACK # #### #### function TUNE_TCP_STACK { echo "Here are the original settings:" echo "proc/sys/net/ipv4/tcp-sack: `cat /proc/sys/net/ipv4/tcp_sack`" echo "proc/sys/net/ipv4/tcp_timestamps:`cat /proc/sys/net/ipv4/tcp_timestamps`" cat /proc/sys/net/ipv4/tcp_timestamps echo $ARG4 > /proc/sys/net/ipv4/tcp_sack echo $ARG4 > /proc/sys/net/ipv4/tcp_timestamps echo "===============================" echo "Here are the new values." echo "/proc/sys/net/ipv4/tcp_sack: `cat /proc/sys/net/ipv4/tcp_sack`" echo "/proc/sys/net/ipv4/tcp_timestamps:`cat /proc/sys/net/ipv4/tcp_timestamps`" } #### #### # # SET_BDFLUSH -- Sets some nice parameters in BDFLUSH to take better advantage of # virtual memory optimizations. # #### #### function SET_BDFLUSH { case "$ARG5" in y|Y|-y|-Y|--yes|--Yes|--YES|yes|YES|Yes|-yes|-YES|-Yes) echo "Setting new defaults for bdflush". echo "You may want to edit the following command-line" echo "to change the defaults. Those given here" echo "are simply a good starting point." echo "The command is :" echo " " echo "'echo 100 5000 640 2560 150 30000 5000 1884 2 > /proc/sys/vm/bdflush'" echo 100 5000 640 2560 150 30000 5000 1884 2 > /proc/sys/vm/bdflush ;; *) echo "Resetting bdflush to default." echo 30 500 0 0 500 3000 60 20 0 > /proc/sys/vm/bdflush ;; esac } #### #### # # Reports the version for this program. # #### #### function VERSION { echo "`basename $0` Version 11132k3-c, NEWSCRIPT Version 11032k3a" } #======================================= No more functions. ========== ############################################################################# ################################# #################################### ################################# MAIN #################################### ################################# #################################### ############################################################################# case "$1" in v|V|-v|-V|--version|--VERSION|--Version|-version|-Version|-VERSION--ver|--Ver|--VER|-ver|-Ver|-VER) VERSION exit 0 ;; h|H|-h|-H|--help|--HELP|--Help|HELP|help|Help|-help|-HELP|-Help) SYNTAX exit 0 ;; esac #CHECK_HOST R_U_ROOT VERSION CHECKCMD GETARGS $1 $2 $3 $4 $5 echo "IN MAIN-------->>>>>>>" BUGPAUSE HD_TUNE TUNE_READAHEAD TUNE_TCP_STACK SET_BDFLUSH echo "End run--------------<<<<<<<".