Operator Menu
To make my servers easier to administer within my group (who are not all *NIX guys), I made an operator script that can be used to manipulate the server.
The main features I wanted to make easier for other members of the IT group are:
1. Update the operating system (manually)
2. Disk Status
3. Memory Status
4. Stop/Restart primary services
5. Cleanly shutdown services and reboot server
I accomplished this by creating a script and making a shortcut to it so the only thing they need to type on each server is "opm" which is short for "Operator Menu"
Here is how to do something similar for your servers.
Create the script file and set the permissions so that only the root user can run the script and make a shortcut for it:
Code:
touch /var/scripts/prod/opm.sh
chmod 0700 /var/scripts/prod/opm.sh
ln -s /var/scripts/prod/opm.sh /usr/sbin/opm
You will need a program called "dialog" installed for this menu to work.
Code:
aptitude install dialog
Now open the script in your favorite editor and add the following:
/var/scripts/prod/opm.sh
Code:
#!/bin/bash
#############################################
## Name : opm.sh
## Version : 1.0
## Date : 2013-01-07
## Author : LHammonds
## Compatibility : Ubuntu Server 12.04-14.04 LTS
## Requirements : dialog (apt-get dialog) and root privileges
## Purpose : Display menu to control the server
## Run Frequency : As needed
## Exit Codes : None
## SymLink Cmd : ln -s /var/scripts/prod/opm.sh /usr/sbin/opm
################ CHANGE LOG #################
## DATE WHO WHAT WAS CHANGED
## ---------- ---- ----------------------------
## 2013-01-07 LTH Created script.
#############################################
## Store menu options selected by the user.
TEMPDIR="/tmp"
SCRIPTDIR="/var/scripts/prod"
INPUT="${TEMPDIR}/opm-input.$$"
## Storage file for displaying cal and date command output.
OUTPUT="${TEMPDIR}/opm-output.$$"
## Get text editor or fall back to vi_editor.
vi_editor=${EDITOR-vi}
## Requirement Check: Script must run as root user.
if [ "$(id -u)" != "0" ]; then
## FATAL ERROR DETECTED: Document problem and terminate script.
echo -e "\nERROR: Root user required to run this script.\n"
echo -e "Type 'sudo su' to temporarily become root user.\n"
exit
fi
## Trap and delete temp files.
trap "rm $OUTPUT; rm $INPUT; exit" SIGHUP SIGINT SIGTERM
function f_display_output(){
## Purpose - display output using msgbox
## $1 -> set msgbox height
## $2 -> set msgbox width
## $3 -> set msgbox title
local h=${1-10} ## box height default 10
local w=${2-41} ## box width default 41
local t=${3-Output} ## box title
dialog --backtitle "KDC Operator Menu for $(hostname -f)" --title "${t}" --clear --msgbox "$(<$OUTPUT)" ${h} ${w}
}
function f_showdate(){
## Purpose - display current system date & time
echo "Today is $(date) @ $(hostname -f)." >$OUTPUT
f_display_output 6 60 "Date and Time"
}
function f_checkdisk(){
## Purpose: Display disk status.
clear
echo -e "df --block-size=M\n"
df --block-size=M
echo ""
read -p "Press [Enter] key to continue..."
}
## Loop the menu display.
while true
do
## Display main menu.
dialog --clear --no-cancel --backtitle "KDC Operator Menu for $(hostname -f)" \
--title "[ M A I N - M E N U ]" \
--menu "You can use the UP/DOWN arrow keys, the first \n\
letter of the choice as a hot key, or the \n\
number keys 1-9 to choose an option.\n\
Choose the TASK" 19 50 7 \
Exit "Exit menu" \
OSUpdate "Update Operating System" \
CheckDisk "Check Disk Status" \
MEMCheck "Look at running processes" \
ServiceRestart "Stop/Start Main Services" \
Reboot "Cleanly reboot server" \
Date/time "Displays date and time" 2>"${INPUT}"
menuitem=$(<"${INPUT}")
## Make decision.
case $menuitem in
OSUpdate) ${SCRIPTDIR}/kdc-apt-upgrade.sh;;
CheckDisk) f_checkdisk;;
MEMCheck) htop;;
Reboot) ${SCRIPTDIR}/reboot.sh;;
ServiceRestart) ${SCRIPTDIR}/servicerestart.sh;;
Date/time) f_showdate;;
Exit) clear; echo "Clean menu exit."; break;;
esac
done
## Delete temp files.
[ -f $OUTPUT ] && rm $OUTPUT
[ -f $INPUT ] && rm $INPUT
I like the opm script to as static as possible when copying among the many servers I have so I push the reboot and service restart "logic" into their own scripts which can be unique to each server.
On a MySQL server, the service restart script would look something like this:
/var/scripts/prod/servicerestart.sh
Code:
#!/bin/bash
#############################################
## Name : servicerestart.sh
## Version : 1.0
## Date : 2013-01-08
## Author : LHammonds
## Compatibility : Ubuntu Server 12.04-14.04 LTS
## Requirements : None
## Purpose : Stop/Start primary services.
## Run Frequency : As needed
## Exit Codes : None
################ CHANGE LOG #################
## DATE WHO WHAT WAS CHANGED
## ---------- ---- ----------------------------
## 2013-01-08 LTH Created script.
#############################################
clear
echo ""
echo "Stopping services..."
service mysql stop
sleep 1
echo "Starting services..."
service mysql start
sleep 1
On a MySQL server, the reboot script would look something like this:
/var/scripts/prod/reboot.sh
Code:
#!/bin/bash
#############################################
## Name : reboot.sh
## Version : 1.0
## Date : 2013-01-08
## Author : LHammonds
## Compatibility : Ubuntu Server 12.04-14.04 LTS
## Requirements : Run as root
## Purpose : Stop services and reboot server.
## Run Frequency : As needed
## Exit Codes : None
################ CHANGE LOG #################
## DATE WHO WHAT WAS CHANGED
## ---------- ---- ----------------------------
## 2013-01-07 LTH Created script.
#############################################
## Requirement Check: Script must run as root user.
if [ "$(id -u)" != "0" ]; then
## FATAL ERROR DETECTED: Document problem and terminate script.
echo -e "\nERROR: Root user required to run this script.\n"
echo -e "Type 'sudo su' to temporarily become root user.\n"
exit
fi
clear
echo ""
echo "Stopping MySQL service..."
service mysql stop
echo "Rebooting..."
echo "3"
sleep 1
echo "2"
sleep 1
echo "1"
sleep 1
shutdown -r now
Customizing Operator Menu
Let's say you have "mytop" installed on your MySQL server and you want that added to your menu. This is how you would do that.
Edit the opm.sh file and find this section:
Code:
OSUpdate "Update Operating System" \
CheckDisk "Check Disk Status" \
MEMCheck "Look at running processes" \
ServiceRestart "Stop/Start Main Services" \
Reboot "Cleanly reboot server" \
Date/time "Displays date and time" 2>"${INPUT}"
Now add the following line so it shows up in your menu:
Code:
MySQLCheck "Look at MySQL processes" \
End result looking like this:
Code:
OSUpdate "Update Operating System" \
CheckDisk "Check Disk Status" \
MEMCheck "Look at running processes" \
MySQLCheck "Look at MySQL processes" \
ServiceRestart "Stop/Start Main Services" \
Reboot "Cleanly reboot server" \
Date/time "Displays date and time" 2>"${INPUT}"
Now that the menu entry is there, we need the menu item to actually do something so find this section below:
Code:
OSUpdate) ${SCRIPTDIR}/kdc-apt-upgrade.sh;;
CheckDisk) f_checkdisk;;
MEMCheck) htop;;
Reboot) ${SCRIPTDIR}/reboot.sh;;
ServiceRestart) ${SCRIPTDIR}/servicerestart.sh;;
Date/time) f_showdate;;
Exit) clear; echo "Clean menu exit."; break;;
Add the following line:
Code:
MySQLCheck) mytop;;
The end result looking like this:
Code:
OSUpdate) ${SCRIPTDIR}/kdc-apt-upgrade.sh;;
CheckDisk) f_checkdisk;;
MEMCheck) htop;;
MySQLCheck) mytop;;
Reboot) ${SCRIPTDIR}/reboot.sh;;
ServiceRestart) ${SCRIPTDIR}/servicerestart.sh;;
Date/time) f_showdate;;
Exit) clear; echo "Clean menu exit."; break;;
Save and close the script.
Now when you run opm, you will see the MySQL entry which will run mytop when you select it.
With this menu example, I have given you 3 example types of how you can do something. You can call the program directly, like mytop or htop, or you can call a function to process several lines of script like f_checkdisk or f_showdate or you can call an external script like reboot.sh or servicerestart.sh which can further isolate individual server needs in other scripts.
Bookmarks