Last active
November 13, 2018 14:08
-
-
Save kerkenit/926a6f3658a8fa02bb684bca75e8acc4 to your computer and use it in GitHub Desktop.
Test CPU
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Detects which OS and if it is Linux then it will detect which Linux | |
# Distribution. | |
# -e option instructs bash to immediately exit if any command [1] has a non-zero exit status | |
# We do not want users to end up with a partially working install, so we exit the script | |
# instead of continuing the installation with something broken | |
set -e | |
# Find the rows and columns will default to 80x24 if it can not be detected | |
screen_size=$(stty size 2>/dev/null || echo 24 80) | |
rows=$(echo "${screen_size}" | awk '{print $1}') | |
columns=$(echo "${screen_size}" | awk '{print $2}') | |
# Divide by two so the dialogs take up half of the screen, which looks nice. | |
r=$(( rows / 2 )) | |
c=$(( columns / 2 )) | |
# Unless the screen is tiny | |
r=$(( r < 20 ? 20 : r )) | |
c=$(( c < 70 ? 70 : c )) | |
# Compatibility | |
distro_check() { | |
# If apt-get is installed, then we know it's part of the Debian family | |
if command -v apt-get &> /dev/null; then | |
# Set some global variables here | |
# We don't set them earlier since the family might be Red Hat, so these values would be different | |
PKG_MANAGER="apt-get" | |
# A variable to store the command used to update the package cache | |
UPDATE_PKG_CACHE="${PKG_MANAGER} update" | |
# An array for something... | |
PKG_INSTALL=(${PKG_MANAGER} --yes --no-install-recommends install) | |
# grep -c will return 1 retVal on 0 matches, block this throwing the set -e with an OR TRUE | |
PKG_COUNT="${PKG_MANAGER} -s -o Debug::NoLocking=true upgrade | grep -c ^Inst || true" | |
# Some distros vary slightly so these fixes for dependencies may apply | |
# Since our install script is so large, we need several other programs to successfully get a machine provisioned | |
# These programs are stored in an array so they can be looped through later | |
INSTALLER_DEPS=(whiptail sysbench mailutils) | |
# A function to check... | |
test_dpkg_lock() { | |
# An iterator used for counting loop iterations | |
i=0 | |
# fuser is a program to show which processes use the named files, sockets, or filesystems | |
# So while the command is true | |
while fuser /var/lib/dpkg/lock >/dev/null 2>&1 ; do | |
# Wait half a second | |
sleep 0.5 | |
# and increase the iterator | |
((i=i+1)) | |
done | |
# Always return success, since we only return if there is no | |
# lock (anymore) | |
return 0 | |
} | |
# If apt-get is not found, check for rpm to see if it's a Red Hat family OS | |
elif command -v rpm &> /dev/null; then | |
# Then check if dnf or yum is the package manager | |
if command -v dnf &> /dev/null; then | |
PKG_MANAGER="dnf" | |
else | |
PKG_MANAGER="yum" | |
fi | |
# Fedora and family update cache on every PKG_INSTALL call, no need for a separate update. | |
UPDATE_PKG_CACHE=":" | |
PKG_INSTALL=(${PKG_MANAGER} install -y) | |
PKG_COUNT="${PKG_MANAGER} check-update | egrep '(.i686|.x86|.noarch|.arm|.src)' | wc -l" | |
INSTALLER_DEPS=(sysbench mail) | |
if grep -qi 'centos' /etc/redhat-release; then | |
# CPU Test currently supports CentOS 7+ with PHP7+ | |
SUPPORTED_CENTOS_VERSION=7 | |
SUPPORTED_CENTOS_PHP_VERSION=7 | |
# Check current CentOS major release version | |
CURRENT_CENTOS_VERSION=$(rpm -q --queryformat '%{VERSION}' centos-release) | |
# Check if CentOS version is supported | |
if [[ $CURRENT_CENTOS_VERSION -lt $SUPPORTED_CENTOS_VERSION ]]; then | |
echo -e " ${CROSS} CentOS $CURRENT_CENTOS_VERSION is not suported." | |
echo -e " Please update to CentOS release $SUPPORTED_CENTOS_VERSION or later" | |
# exit the installer | |
exit | |
fi | |
# on CentOS we need to add the EPEL repository to gain access to Fedora packages | |
EPEL_PKG="epel-release" | |
rpm -q ${EPEL_PKG} &> /dev/null || rc=$? | |
if [[ $rc -ne 0 ]]; then | |
echo -e " ${INFO} Enabling EPEL package repository (https://fedoraproject.org/wiki/EPEL)" | |
"${PKG_INSTALL[@]}" ${EPEL_PKG} &> /dev/null | |
echo -e " ${TICK} Installed ${EPEL_PKG}" | |
fi | |
else | |
# If not a supported version of Fedora or CentOS, | |
echo -e " ${CROSS} Unsupported RPM based distribution" | |
# exit the installer | |
exit | |
fi | |
# If neither apt-get or rmp/dnf are found | |
else | |
# it's not an OS we can support, | |
echo -e " ${CROSS} OS distribution not supported" | |
# so exit the installer | |
exit | |
fi | |
} | |
install_dependent_packages() { | |
# Local, named variables should be used here, especially for an iterator | |
# Add one to the counter | |
counter=$((counter+1)) | |
# If it equals 1, | |
if [[ "${counter}" == 1 ]]; then | |
# | |
echo -e " ${INFO} Installer Dependency checks..." | |
else | |
# | |
echo -e " ${INFO} Main Dependency checks..." | |
fi | |
# Install packages passed in via argument array | |
# No spinner - conflicts with set -e | |
declare -a argArray1=("${!1}") | |
declare -a installArray | |
# Debian based package install - debconf will download the entire package list | |
# so we just create an array of packages not currently installed to cut down on the | |
# amount of download traffic. | |
# NOTE: We may be able to use this installArray in the future to create a list of package that were | |
# installed by us, and remove only the installed packages, and not the entire list. | |
if command -v debconf-apt-progress &> /dev/null; then | |
# For each package, | |
for i in "${argArray1[@]}"; do | |
echo -ne " ${INFO} Checking for $i..." | |
if dpkg-query -W -f='${Status}' "${i}" 2>/dev/null | grep "ok installed" &> /dev/null; then | |
echo -e "${OVER} ${TICK} Checking for $i" | |
else | |
echo -e "${OVER} ${INFO} Checking for $i (will be installed)" | |
installArray+=("${i}") | |
fi | |
done | |
if [[ "${#installArray[@]}" -gt 0 ]]; then | |
test_dpkg_lock | |
debconf-apt-progress -- "${PKG_INSTALL[@]}" "${installArray[@]}" | |
return | |
fi | |
echo "" | |
return 0 | |
fi | |
# Install Fedora/CentOS packages | |
for i in "${argArray1[@]}"; do | |
echo -ne " ${INFO} Checking for $i..." | |
if ${PKG_MANAGER} -q list installed "${i}" &> /dev/null; then | |
echo -e "${OVER} ${TICK} Checking for $i" | |
else | |
echo -e "${OVER} ${INFO} Checking for $i (will be installed)" | |
installArray+=("${i}") | |
fi | |
done | |
if [[ "${#installArray[@]}" -gt 0 ]]; then | |
"${PKG_INSTALL[@]}" "${installArray[@]}" &> /dev/null | |
return | |
fi | |
echo "" | |
return 0 | |
} | |
main() { | |
######## FIRST CHECK ######## | |
# Must be root to install | |
local str="Root user check" | |
echo "" | |
# Find the rows and columns will default to 80x24 if it can not be detected | |
screen_size=$(stty size 2>/dev/null || echo 24 80) | |
rows=$(echo "${screen_size}" | awk '{print $1}') | |
columns=$(echo "${screen_size}" | awk '{print $2}') | |
# Divide by two so the dialogs take up half of the screen, which looks nice. | |
r=$(( rows / 2 )) | |
c=$(( columns / 2 )) | |
# Unless the screen is tiny | |
r=$(( r < 20 ? 20 : r )) | |
c=$(( c < 70 ? 70 : c )) | |
OS=`uname -s` | |
REV=`uname -r` | |
MACH=`uname -m` | |
GetVersionFromFile() | |
{ | |
VERSION=`cat $1 | tr "\n" ' ' | sed s/.*VERSION.*=\ // ` | |
} | |
if [ "${OS}" = "SunOS" ] ; then | |
OS=Solaris | |
ARCH=`uname -p` | |
OSSTR="${OS} ${REV}(${ARCH} `uname -v`)" | |
elif [ "${OS}" = "AIX" ] ; then | |
OSSTR="${OS} `oslevel` (`oslevel -r`)" | |
elif [ "${OS}" = "Linux" ] ; then | |
KERNEL=`uname -r` | |
if [ -f /etc/redhat-release ] ; then | |
DIST='RedHat' | |
PSUEDONAME=`cat /etc/redhat-release | sed s/.*\(// | sed s/\)//` | |
REV=`cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//` | |
elif [ -f /etc/SuSE-release ] ; then | |
DIST=`cat /etc/SuSE-release | tr "\n" ' '| sed s/VERSION.*//` | |
REV=`cat /etc/SuSE-release | tr "\n" ' ' | sed s/.*=\ //` | |
elif [ -f /etc/mandrake-release ] ; then | |
DIST='Mandrake' | |
PSUEDONAME=`cat /etc/mandrake-release | sed s/.*\(// | sed s/\)//` | |
REV=`cat /etc/mandrake-release | sed s/.*release\ // | sed s/\ .*//` | |
elif [ -f /etc/debian_version ] ; then | |
DIST="Debian `cat /etc/debian_version`" | |
REV="" | |
fi | |
if [ -f /etc/UnitedLinux-release ] ; then | |
DIST="${DIST}[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]" | |
fi | |
OSSTR="${OS} ${DIST} ${REV}(${PSUEDONAME} ${KERNEL} ${MACH})" | |
fi | |
# If the user's id is zero, | |
if [[ "${EUID}" -eq 0 ]]; then | |
# they are root and all is good | |
echo -e " ${TICK} ${str}" | |
# Otherwise, | |
else | |
# They do not have enough privileges, so let the user know | |
echo -e " ${CROSS} ${str}" | |
echo -e " ${INFO} ${COL_LIGHT_RED}Script called with non-root privileges${COL_NC}" | |
echo -e " ${INFO} The CPU test requires elevated privileges to install and run" | |
echo -e " ${INFO} Please check the installer for any concerns regarding this requirement" | |
echo -e " ${INFO} Make sure to download this script from a trusted source\\n" | |
echo -ne " ${INFO} Sudo utility check" | |
# If the sudo command exists, | |
if command -v sudo &> /dev/null; then | |
echo -e "${OVER} ${TICK} Sudo utility check" | |
# Download the install script and run it with admin rights | |
exec curl -sSL https://cpu.mvtk.nl | sudo bash "$@" | |
exit $? | |
# Otherwise, | |
else | |
# Let them know they need to run it as root | |
echo -e "${OVER} ${CROSS} Sudo utility check" | |
echo -e " ${INFO} Sudo is needed for install packages\\n" | |
echo -e " ${INFO} ${COL_LIGHT_RED}Please re-run this installer as root${COL_NC}" | |
exit 1 | |
fi | |
fi | |
# Check for supported distribution | |
distro_check | |
# Install packages used by this installation script | |
install_dependent_packages INSTALLER_DEPS[@] | |
email=$(whiptail --title "Send results to the desired e-mail adres." --inputbox "Enter your e-mail address" ${r} ${c} 3>&1 1>&2 2>&3) | |
exitstatus=$? | |
if [ $exitstatus = 0 ] && [ -n "$email" ]; then | |
echo $(sysbench --test=cpu --num-threads=$(nproc --all) --cpu-max-prime=23456 run) | mail -s "${OSSTR}" ${email} | |
else | |
sysbench --test=cpu --num-threads=$(nproc --all) --cpu-max-prime=23456 run | |
fi | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment