Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple bash script for checking system readiness for launching an application. It checks if necessary softwares/tools installed, required modules enabled, recommended ini settings, depending servers are up and required ports are listening.
#!/usr/bin/env bash
# Author : Anis Uddin Ahmad <anis.prorgrammer@gmail.com>
# http://ajaxray.com
# How to use : https://cl.ly/1f1L0K413g1x
# List command line tools
dependencies=(httpd php mysql beanstalkd convert)
# If php application, list required modules
php_modules=(curl PDO pdo_mysql session Reflection hash json sockets oci8 imagick)
# List required recommended ini settings as 'option value' (separated by space)
php_settings=('max_input_vars 5001' 'post_max_size 1024M' 'upload_max_filesize 100M' 'memory_limit 1024M')
# list servers and required ports as 'IP-Address PORT' (separated by space)
listeners=('127.0.0.1 3306' '127.0.0.1 80' '127.0.0.1 11300')
message=''
fg_red=`tput setaf 1`
fg_green=`tput setaf 2`
reset=`tput sgr0`
# Common messages
YES="${fg_green}OK${reset}"
NO="${fg_red}NO${reset}"
NOT="${fg_red}NOT FOUND${reset}"
check_program() {
printf "%-40s" "Checking if ${1} installed..."
command -v $1 >/dev/null 2>&1 && echo ${YES} || { echo ${NOT}; message+="${1} not installed. "; }
}
check_port() {
# expected args: host port
printf "%-40s" "Listening ${1}:${2}..."
# Check first if server is up
ping -c 2 ${1} > /dev/null 2>&1
if [ $? -eq 0 ] ; then
# Check if port is listening
nc -w2 ${1} ${2} &> /dev/null 2>&1
[[ $? -eq 0 ]] && echo ${YES} || { echo ${NO}; message+="${1}:${2} NOT LISTENING. "; }
else
{ echo "${fg_red}NOT REACHABLE${reset}"; message+="Server ${1} not reachable. "; }
fi
}
check_php_module() {
printf "%-40s" "Checking PHP module ${1} ..."
PHP_MODS_ENABLED="${PHP_MODS_ENABLED}:-`php -m`"
[[ ${PHP_MODS_ENABLED} =~ ${1} ]] && echo ${YES} || { echo ${NOT}; message+="PHP module ${1} not enabled. "; }
}
check_php_ini() {
# expected args: host port
printf "%-25s %-15s" ${1} ${2}
[[ `php -i | grep ${1}` =~ "=> ${2} =>" ]] && echo ${YES} || echo ${NO}
}
echo "=> Checking required dependencies are installed:"
for i in "${dependencies[@]}" ; do
check_program $i
done
echo "=> Checking required server/ports are listening:"
for j in "${listeners[@]}" ; do
check_port $j
done
# Check modules and ini settings ONLY if PHP application
if [[ `check_program php` =~ 'OK' ]] ; then
echo "=> Checking required PHP Modules are enabled:"
for k in "${php_modules[@]}" ; do
check_php_module $k
done
echo "=> Checking recommended PHP ini settings:"
for l in "${php_settings[@]}" ; do
check_php_ini $l
done
fi
if [[ -z ${message} ]] ; then
echo "${fg_green}SUCCESS: System is ready!${reset}"
exit 0
else
echo "${fg_red}WARNING: System may be NOT ready yet!${reset}"
echo ${message};
exit 1
fi
@ajaxray

This comment has been minimized.

Owner

ajaxray commented Jan 29, 2017

This post describes usages of this script - https://cl.ly/1f1L0K413g1x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment