#! /bin/bash # function library for data retrieval # v. 1.5.4 # 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_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_time() - function returns CURRENT_TIME and CURRENT_DATE in readable ISO 8601 format # k_confline_get() - function retrieves $CONFLINE_VALUE # 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 os=$(awk -F'=' '/^ID=/ {print $2}' /etc/os-release | tr -d '"') VERSION_ID=$(awk -F'=' '/^VERSION_ID=/ {print $2}' /etc/os-release | tr -d '"') 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 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 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 gets value from variable in given config file k_get_conf_var() { local conf_file=$1 local variable=$2 local value="" value=$(grep -P "^${variable} ?= ?" $conf_file | awk -F'=' '{print $2}' | xargs) echo "$value" } # function retrieves configuration values from configuration file k_config_details() { k_detect_os local no_mysql=$1 if [ -f /etc/mor/system.conf ]; then local config="/etc/mor/system.conf" SYSTEM_CONFIG_DIR="mor" SYSTEM_GUI_DIR="mor" elif [ -f /etc/m2/system.conf ]; then local config="/etc/m2/system.conf" SYSTEM_CONFIG_DIR="m2" if [[ $ROCKY9 == 1 ]]; then SYSTEM_GUI_DIR="m4" else SYSTEM_GUI_DIR="m2" fi else report "Can't detect MOR or M4 system.conf in /etc" 1 k_exit 1 fi if [ -r "$config" ]; then DB_HOST=$(k_get_conf_var $config dbhost) DB_NAME=$(k_get_conf_var $config dbname) DB_USERNAME=$(k_get_conf_var $config dbuser) DB_PASSWORD=$(k_get_conf_var $config dbsecret) 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=$(k_get_conf_var $config server_id) DB_PRESENT=$(k_get_conf_var $config DB_PRESENT) GUI_PRESENT=$(k_get_conf_var $config GUI_PRESENT) ES_PRESENT=$(k_get_conf_var $config ES_PRESENT) PROXY_PRESENT=$(k_get_conf_var $config PROXY_PRESENT) MAIN_DB=$(k_get_conf_var $config MAIN_DB) VIRTUAL_IP=$(k_get_conf_var $config VIRTUAL_IP) REPLICATION_M=$(k_get_conf_var $config REPLICATION_M) REPLICATION_S=$(k_get_conf_var $config REPLICATION_S) # MOR MOR_VERSION=$(k_get_conf_var $config MOR_VERSION) MAIN_GUI=$(k_get_conf_var $config MAIN_GUI) ASTERISK_PRESENT=$(k_get_conf_var $config ASTERISK_PRESENT) HEARTBEAT_PRESENT=$(k_get_conf_var $config HEARTBEAT_PRESENT) BACKUP_SYSTEM=$(k_get_conf_var $config BACKUP_SYSTEM) # M2 FREESWITCH_PRESENT=$(k_get_conf_var $config FREESWITCH_PRESENT) RADIUS_PRESENT=$(k_get_conf_var $config RADIUS_PRESENT) CFG_START_TIMEOUT=$(k_get_conf_var $config start_timeout) # M4 PROXY_PRESENT=$(k_get_conf_var $config PROXY_PRESENT) B2BUA_PRESENT=$(k_get_conf_var $config B2BUA_PRESENT) MEDIA_PRESENT=$(k_get_conf_var $config MEDIA_PRESENT) RTPE_LOOP_PROTECT=$(k_get_conf_var $config RTPE_LOOP_PROTECT) REDIS_ENABLED_CORE=$(k_get_conf_var $config redis_enabled) SYSTEM_CONFIG=$config table_exists=$(MYSQL_PWD="$DB_PASSWORD" mysql -h $DB_HOST -u"$DB_USERNAME" -D"$DB_NAME" -e "SHOW TABLES LIKE 'conflines';" 2>/dev/null) if [ -z "$table_exists" ]; then : #report "Table 'conflines' not found in DB - k_config_details" 2 else if [[ "$no_mysql" != "no_mysql" || "$DB_HOST" == "" ]]; 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 fi # failover # pcsd - pacemaker/corosync configuration system daemon if k_service_is_running pcsd; then PCSD_ACTIVE=1 if [ "$VIRTUAL_IP" != "" ]; then if /usr/sbin/ip -o -f inet addr show | grep -Eq "inet[[:space:]]${VIRTUAL_IP}[[:space:]/]"; 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'` if [[ "$DEFAULT_INTERFACE_MAC" == "" ]]; then export DEFAULT_INTERFACE_MAC=$(ip addr show "$DEFAULT_INTERFACE" | awk '/ether/ {print $2}') fi 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" ] || [ "$TERM" == "screen.xterm-256color" ]; then return 0 else return fi } # function returns CURRENT_TIME and CURRENT_DATE in readable ISO 8601 format k_time() { CURRENT_TIME=`date +%Y\.%-m\.%-d\-%-k\-%-M\-%-S` CURRENT_DATE=`date +%Y\.%-m\.%-d` } # function retrieves $CONFLINE_VALUE k_confline_get() { local confline_name="$1" local confline_value="" k_config_details table_exists=$(MYSQL_PWD="$DB_PASSWORD" mysql -h $DB_HOST -u"$DB_USERNAME" -D"$DB_NAME" -e "SHOW TABLES LIKE 'conflines';" 2>/dev/null) if [ -z "$table_exists" ]; then report "Table 'conflines' not found in DB - k_confline_get $1" 2 return fi confline_value=$(MYSQL_PWD="$DB_PASSWORD" /usr/bin/mysql -h "$DB_HOST" -u "$DB_USERNAME" "$DB_NAME" -sNe "select value from conflines where name = '$confline_name'") CONFLINE_VALUE=$confline_value } # True if exist, false otherwise k_iptables_chain_exist() { k_iptables_locking_option local chain="$1" /sbin/iptables $l_opt -t filter -S "$chain" 1 &> /dev/null }