#! /bin/bash # function library for data retrieval # v. 1.3.10 # Function list: # k_detect_vm() - function detects if script is ran on the VM. Returns VM_DETECTED=1 if so. VM_TYPE="string" with the name of the VM Type # k_detect_os() - function detects OS. Returns $OS and $DIST. Example: centos 7, debian 10 # k_centos_version() - function returns CentOS version in variable 'centos_version' # k_mysql_version() - function returns mysql version, MYSQL_VERSION - full x.y.z, MYSQL_VERSION1 - x, MYSQL_VERSION2 - y, MYSQL_VERSION3 - z # k_system_detect() - function detects if MOR/M4 is installed in the system # k_config_details() - function retrieves configuration values from configuration file, provides: SYSTEM_CONFIG (/etc/(mor|m2)/system.conf), SYSTEM_CONFIG_DIR (mor|m2) # k_are_we_root() - function checks if we are root # k_default_interface_ip() - function retrieves DEFAULT_INTERFACE, DEFAULT_IP, DEFAULT_IP2, DEFAULT_INTERFACE_MAC, NIC_MODEL, EXTERNAL_IP # k_cpu_thread_count() - function returns CPU_THREAD_COUNT # k_iptables_locking_option() - function returns iptables locking option, usage: iptables $l_opt ... # k_we_are_inside_screen() - function checks if shell is running in 'screen', use in if k_we_are_inside_screen; then, or: if ! k_we_are_inside_screen; then # k_script_version() - function returns C script version # Function naming rules # k__ where either object or action are not mandatory (only object or only action is necessary, could be both also) # function detects if script is ran on the VM. # returns VM_DETECTED=1 if so. VM_TYPE="string" with the name of the VM Type k_detect_vm() { # Author: Mindaugas Mardosas # Company: Kolmisoft # Year: 2011-2013 # About: This script detects if the machine is a Virtual Machine. # # Returns: # 0 - VM detected # 1 - VM not detected # # Variable: # VM_DETECTED=[0 - not detected, 1 - detected] VM_DETECTED=1 if [ -f /proc/sys/xen ] || [ -f /sys/bus/xen ] || [ -f /proc/xen ]; then VM_TYPE="XEN" return 0; fi if [ ! -f /sbin/lspci ]; then yum -y install pciutils fi if [ ! -f /usr/sbin/virt-what ]; then k_detect_os # no virt-what on c6 :( if (( centos_version != 6 )); then yum -y install virt-what fi fi if [ -f /proc/vz/veinfo ]; then VM_TYPE="OpenVZ" return 0; fi local KVM=`lspci 2> /dev/null | grep RAM | grep Qumranet | wc -l` if [ "$KVM" == "1" ]; then VM_TYPE="KVM" return 0; fi if [ -f /var/log/dmesg ]; then if cat /var/log/dmesg | grep -q kvm; then VM_TYPE="KVM" return 0; fi fi #--- VMware local VMware=`lspci | grep VMware | wc -l` if [ "$VMware" != "0" ]; then VM_TYPE="VMWARE" return 0; fi #--- VirtualBox --- local VirtualBox=`lspci | grep VirtualBox | wc -l` if [ "$VirtualBox" != "0" ]; then VM_TYPE="VirtualBox" return 0; fi #--- Qemu --- local QEMU=`cat /proc/cpuinfo | grep -F QEMU | wc -l` if [ "$QEMU" != "0" ]; then VM_TYPE="QEMU" return 0; fi #--- LXC --- if [ -f "/proc/1/cgroup" ]; then local LXC=`grep lxc /proc/1/cgroup | wc -l` if [ "$LXC" != "0" ]; then VM_TYPE="LXC" return 0; fi fi #--- Azure --- FILE="/var/log/waagent.log" if [ -f $FILE ]; then if cat $FILE | zgrep -q "Azure\|azure"; then VM_TYPE="Azure" return 0; fi fi # detects Microsoft Hyper-V, parallels, powervm_lx86, qemu, virtualpc, xen-hvm, uml, openvz, linux_vserver, ibm_systemz # https://www.cyberciti.biz/faq/linux-determine-virtualization-technology-command/ FILE="/usr/sbin/virt-what" if [ -f $FILE ]; then VM_TYPE=`virt-what` if [[ ${#VM_TYPE} > 0 ]]; then return 0; fi fi VM_DETECTED=0 return 1 # VM not detected } # function detects OS. Returns $OS and $DIST. Example: centos 7 k_detect_os() { if [[ ( -z "${OS}" ) && ( -z "${DIST}" ) ]]; then if [ -e /etc/os-release ]; then # This will overwrite script VERSION variable, so save it and restore it later SCRIPT_VERSION=${VERSION} . /etc/os-release os=${ID} if [ "${os}" = "poky" ]; then dist=`echo ${VERSION_ID}` elif [ "${os}" = "sles" ]; then dist=`echo ${VERSION_ID}` elif [ "${os}" = "opensuse" ]; then dist=`echo ${VERSION_ID}` elif [ "${os}" = "opensuse-leap" ]; then os=opensuse dist=`echo ${VERSION_ID}` else dist=`echo ${VERSION_ID} | awk -F '.' '{ print $1 }'` fi VERSION=${SCRIPT_VERSION} elif [ `which lsb_release 2>/dev/null` ]; then # get major version (e.g. '5' or '6') dist=`lsb_release -r | cut -f2 | awk -F '.' '{ print $1 }'` # get os (e.g. 'centos', 'redhatenterpriseserver', etc) os=`lsb_release -i | cut -f2 | awk '{ print tolower($1) }'` elif [ -e /etc/oracle-release ]; then dist=`cut -f5 --delimiter=' ' /etc/oracle-release | awk -F '.' '{ print $1 }'` os='ol' elif [ -e /etc/fedora-release ]; then dist=`cut -f3 --delimiter=' ' /etc/fedora-release` os='fedora' elif [ -e /etc/redhat-release ]; then os_hint=`cat /etc/redhat-release | awk '{ print tolower($1) }'` if [ "${os_hint}" = "centos" ]; then dist=`cat /etc/redhat-release | awk '{ print $3 }' | awk -F '.' '{ print $1 }'` os='centos' elif [ "${os_hint}" = "scientific" ]; then dist=`cat /etc/redhat-release | awk '{ print $4 }' | awk -F '.' '{ print $1 }'` os='scientific' else dist=`cat /etc/redhat-release | awk '{ print tolower($7) }' | cut -f1 --delimiter='.'` os='redhatenterpriseserver' fi else aws=`grep -q Amazon /etc/issue` if [ "$?" = "0" ]; then dist='6' os='aws' else unknown_os fi fi fi if [[ ( -z "${os}" ) || ( -z "${dist}" ) ]]; then unknown_os fi # remove whitespace from OS and dist name OS="${os// /}" DIST="${dist// /}" if [[ $OS == "centos" ]]; then centos_version=$DIST fi ROCKY9=0 if [[ $OS == "rocky" && $DIST == "9" ]]; then ROCKY9=1 fi # echo "Detected operating system as ${OS}/${DIST}." } # function returns CentOS version k_centos_version() { k_detect_os if [[ "$OS" == "centos" ]]; then centos_version=$DIST else report "OS $OS $DIST, not Centos!" 1 EXIT_CODE=1 fi } # function returns mysql version # MYSQL_VERSION - full x.y.z MYSQL_VERSION1 - x MYSQL_VERSION2 - y MYSQL_VERSION3 - z k_mysql_version() { if [ ! -f /usr/bin/mysql ]; then report "MySQL not installed" 2 return fi local mv=$(mysql -V) if echo $mv | grep -q "Ver 14.14"; then MYSQL_VERSION=`echo $mv | awk '{print $5}' | sed 's/,$//' | xargs` elif echo $mv | grep -q "Ver 8."; then MYSQL_VERSION=`echo $mv | awk '{print $3}' | xargs` else MYSQL_VERSION="unknown" report "MYSQL_VERSION unknown: $mv" 1 report " fix the script /kf/date_get.sh k_mysql_version()" fix EXIT_CODE=1 return fi MYSQL_VERSION1=`echo $MYSQL_VERSION | awk -F "." '{print $1}'` MYSQL_VERSION2=`echo $MYSQL_VERSION | awk -F "." '{print $2}'` MYSQL_VERSION3=`echo $MYSQL_VERSION | awk -F "." '{print $3}'` } # function detects if MOR/M4 is installed in the system k_system_detect() { if [ -f /etc/mor/system.conf ]; then : elif [ -f /etc/m2/system.conf ]; then : else report "Can't detect MOR or M4 system.conf in /etc" 1 EXIT_CODE=1 fi } # function retrieves configuration values from configuration file k_config_details() { local no_mysql=$1 if [ -f /etc/mor/system.conf ]; then local config="/etc/mor/system.conf" SYSTEM_CONFIG_DIR="mor" elif [ -f /etc/m2/system.conf ]; then local config="/etc/m2/system.conf" SYSTEM_CONFIG_DIR="m2" else report "Can't detect MOR or M4 system.conf in /etc" 1 k_exit 1 fi if [ -r "$config" ]; then DB_HOST=`sed 's/ //g' $config | grep dbhost | awk -F"=" '{print $2}' | xargs`; DB_NAME=`sed 's/ //g' $config | grep dbname | awk -F"=" '{print $2}' | xargs`; DB_USERNAME=`sed 's/ //g' $config | grep dbuser | awk -F"=" '{print $2}' | xargs`; DB_PASSWORD=`sed 's/ //g' $config | grep dbsecret | awk -F"=" '{print $2}' | xargs`; if [ -f /usr/bin/mysql ]; then P_OPT="" if /usr/bin/timeout 5 /usr/bin/mysql -V | grep -Eq 'Distrib 5\.[15]'; then P_OPT="--password=$DB_PASSWORD" fi fi SERVER_ID=`sed 's/ //g' $config | grep server_id | awk -F"=" '{print $2}' | xargs`; DB_PRESENT=`sed 's/ //g' $config | grep DB_PRESENT | awk -F"=" '{print $2}' | xargs`; GUI_PRESENT=`sed 's/ //g' $config | grep GUI_PRESENT | awk -F"=" '{print $2}' | xargs`; ES_PRESENT=`sed 's/ //g' $config | grep ES_PRESENT | awk -F"=" '{print $2}' | xargs`; PROXY_PRESENT=`sed 's/ //g' $config | grep PROXY_PRESENT | awk -F"=" '{print $2}' | xargs`; MAIN_DB=`sed 's/ //g' $config | grep -v MAIN_DB_IP | grep MAIN_DB | awk -F"=" '{print $2}' | xargs`; VIRTUAL_IP=`sed 's/ //g' $config | grep VIRTUAL_IP | awk -F"=" '{print $2}' | xargs`; # MOR MOR_VERSION=`sed 's/ //g' $config | grep MOR_VERSION | awk -F"=" '{print $2}' | xargs`; MAIN_GUI=`sed 's/ //g' $config | grep MAIN_GUI | awk -F"=" '{print $2}' | xargs`; ASTERISK_PRESENT=`sed 's/ //g' $config | grep ASTERISK_PRESENT | awk -F"=" '{print $2}' | xargs`; HEARTBEAT_PRESENT=`sed 's/ //g' $config | grep HEARTBEAT_PRESENT | awk -F"=" '{print $2}' | xargs`; BACKUP_SYSTEM=`sed 's/ //g' $config | grep BACKUP_SYSTEM | awk -F"=" '{print $2}' | xargs`; # M2 FREESWITCH_PRESENT=`sed 's/ //g' $config | grep FREESWITCH_PRESENT | awk -F"=" '{print $2}' | xargs`; RADIUS_PRESENT=`sed 's/ //g' $config | grep RADIUS_PRESENT | awk -F"=" '{print $2}' | xargs`; CFG_START_TIMEOUT=`sed 's/ //g' $config | grep start_timeout | awk -F"=" '{print $2}' | xargs`; # M4 PROXY_PRESENT=`sed 's/ //g' $config | grep PROXY_PRESENT | awk -F"=" '{print $2}' | xargs`; B2BUA_PRESENT=`sed 's/ //g' $config | grep B2BUA_PRESENT | awk -F"=" '{print $2}' | xargs`; MEDIA_PRESENT=`sed 's/ //g' $config | grep MEDIA_PRESENT | awk -F"=" '{print $2}' | xargs`; RTPE_LOOP_PROTECT=`sed 's/ //g' $config | grep RTPE_LOOP_PROTECT | awk -F"=" '{print $2}' | xargs`; SYSTEM_CONFIG=$config if [ "$no_mysql" != "no_mysql" ]; then if [ -f /usr/bin/mysql ]; then M4_ACTIVE=`MYSQL_PWD=$DB_PASSWORD /usr/bin/mysql -h $DB_HOST -u $DB_USERNAME $P_OPT $DB_NAME --silent -e "SELECT value FROM conflines WHERE name = 'M4_Functionality';" | grep -v value` REDIS_ENABLED=`MYSQL_PWD=$DB_PASSWORD /usr/bin/mysql -h $DB_HOST -u $DB_USERNAME $P_OPT $DB_NAME --silent -e "SELECT value FROM conflines WHERE name = 'Use_Redis'" | grep -v value` fi fi REDIS_ENABLED_CORE=`sed 's/ //g' $config | grep redis_enabled | awk -F"=" '{print $2}' | xargs`; # failover # pcsd - pacemaker/corosync configuration system daemon if k_service_is_running pcsd; then PCSD_ACTIVE=1 if [ "$VIRTUAL_IP" != "" ]; then if ip addr | grep -q $VIRTUAL_IP; then PCSD_MAIN=1 # system is in active failover state (usually active MAIN server) else PCSD_MAIN=0 # system is in passive failover state (usually passive BACKUP server) fi fi else PCSD_ACTIVE=0 PCSD_MAIN=-1 # undefined fi else report "Can't read $config" 1 k_exit 1 fi } # function checks if we are root k_are_we_root() { local weroot=`id -u` if [[ $weroot != 0 ]];then return else return 0 fi } # function retrieves DEFAULT_INTERFACE, DEFAULT_IP, DEFAULT_INTERFACE_MAC, EXTERNAL_IP # exporting variables to the environment to avoid wasting time retrieving them in the other scripts k_default_interface_ip() { if [[ $NIC_MODEL != "" ]]; then return fi export DEFAULT_INTERFACE=`/bin/netstat -nr | (read; cat) | (read; cat) | grep "^0.0.0.0" | awk '{ print $8}' | head -n 1` #Gets kernel routing table, when skips 2 first lines, when grep's the default path and finally prints the interface name export DEFAULT_IP=`/sbin/ip addr show $DEFAULT_INTERFACE | grep "inet " | awk -F" " '{print $2}' | awk -F"/" '{print $1}' | tail -1` export DEFAULT_IP2=`/sbin/ip addr show $DEFAULT_INTERFACE | grep "inet " | awk -F" " '{print $2}' | awk -F"/" '{print $1}' | head -1` export DEFAULT_INTERFACE_MAC=`/sbin/ifconfig | grep eth | awk -F'HWaddr' '{print $2}' | sed 's/ //g'` local nic_model_short=`lshw -class network -short | grep "$DEFAULT_INTERFACE" | sed -n -e 's/^.*network//p' | xargs` export NIC_MODEL=`lspci | grep "$nic_model_short" | head -1 | awk -F "ller:" '{print $2}' | xargs` if [[ $NIC_MODEL == "" ]]; then export NIC_MODEL=`ethtool -i $DEFAULT_INTERFACE | grep driver | awk -F ":" '{print $2}' | xargs` fi # get external server ip export EXTERNAL_IP=`curl -s --connect-timeout 2 http://whatismyip.akamai.com/` #report "External IP: $EXTERNAL_IP by http://whatismyip.akamai.com" 0 } # function returns CPU_THREAD_COUNT k_cpu_thread_count() { CPU_THREAD_COUNT=`grep -c ^processor /proc/cpuinfo` } # function returns iptables locking option, usage: iptables $l_opt ... k_iptables_locking_option() { # Return l_opt for iptables # Usage: # get_iptables_locking_option # /sbin/iptables $l_opt ... # Since iptables v 1.4.10, iptables use locks to prevent concurrent access # In case of concurent access, iptables will fail with error message: Another app is currently holding the xtables lock. # -w option will wait for lock, but it is available only in versions which supports locking local iptables_version=$(/sbin/iptables --version | grep -Eo '[1-9.]+' ) local major=$(echo $iptables_version | awk -F'.' '{print $1}') local minor=$(echo $iptables_version | awk -F'.' '{print $2}') local patch=$(echo $iptables_version | awk -F'.' '{print $3}') if ((major > 1)); then l_opt="-w" else if ((minor > 4)); then l_opt="-w" elif ((minor == 4)); then if ((patch >= 10)); then l_opt="-w" else l_opt="" fi else l_opt="" fi fi export l_opt } # function checks if shell is running in 'screen', use in if k_we_are_inside_screen; then, or: if ! k_we_are_inside_screen; then k_we_are_inside_screen() { if [ "$TERM" == "screen" ] || [ "$TERM" == "screen-256color" ]; then return 0 else return fi } # function returns C script version k_script_version() { script=$1 date_check="2021-11-22 00:00:00" # (constant) date when all C scripts started to produce version output (until this date, all scripts are considered as outdated) if [ ! -e $script ]; then echo "" return fi # get script modify date (time when it was installed) script_installed_at=$(stat $script | grep Modify | cut -c9-27) # convert dates to seconds (for easy comparison) date_check_seconds=$(date -d "$date_check" +%s) script_installed_at_seconds=$(date -d "$script_installed_at" +%s) if [ $date_check_seconds -ge $script_installed_at_seconds ]; then echo "" return fi $script -v }