diff --git a/shell/check-container-ip.sh b/shell/check-container-ip.sh new file mode 100644 index 0000000..d7e59c1 --- /dev/null +++ b/shell/check-container-ip.sh @@ -0,0 +1,300 @@ +#!/usr/bin/env bash +################################################################### +#Script Name : check-container-ip.sh +#Description : Detect container ip addresses and dynamically update nginx upstream configuration +#Create Date : 2021-06-25 +#Author : lework +#Email : lework@yeah.net +################################################################### + + +[[ -n $DEBUG ]] && set -x +set -o errtrace # Make sure any error trap is inherited +set -o nounset # Disallow expansion of unset variables +set -o pipefail # Use last non-zero exit code in a pipeline + + +###################################################################################################### +# environment configuration +###################################################################################################### + +RETRIES="${RETRIES:-9223372036854775807}" +WAIT="${WAIT:-5}" + +CONTAINER_NAME="${CONTAINER_NAME:-}" +CONFIG_FILE="${CONFIG_FILE:-}" +CONFIG_FILE_NAME="${CONFIG_FILE_NAME:-}" +UPSTREAM_NAME="${UPSTREAM_NAME:-}" +SERVICE_PORT="${SERVICE_PORT:-}" +CONTAINER_IP="${CONTAINER_IP:-}" +CHECK_IP="${CHECK_IP:-}" +NGINX_STATUS=0 # 为1时reload nginx +NOTICE_TYPE="${NOTICE_TYPE:-feishu}" +NOTICE_MESSAGE="${NOTICE_MESSAGE:-}" +NOTICE_TOKEN="${NOTICE_TOKEN:-}" + +TMP_DIR="$(rm -rf /tmp/check-container-ip* && mktemp -d -t check-container-ip.XXXXXXXXXX)" +TMP_CONFIG_FILE="${TMP_DIR}/${CONFIG_FILE_NAME}_$(date +%s)_temp.conf" + +NGINX_BACKUP_PATH="${NGINX_BACKUP_PATH:-/tmp}" + +trap trap::info 1 2 3 15 EXIT + +###################################################################################################### +# function +###################################################################################################### + +function trap::info() { + # 信号处理 + + [ ${n:-0} -gt 0 ] && log::info "[stop]" "check total: ${n}" + + trap '' EXIT + exit +} + + +function log::error() { + # 错误日志 + + printf "[%s]: \033[31mERROR: \033[0m%s\n" "$(date +'%Y-%m-%dT%H:%M:%S.%N%z')" "$*" +} + + +function log::info() { + # 基础日志 + + printf "[%s]: \033[32mINFO: \033[0m%s\n" "$(date +'%Y-%m-%dT%H:%M:%S.%N%z')" "$*" +} + + +function log::warning() { + # 警告日志 + + printf "[%s]: \033[33mWARNING: \033[0m%s\n" "$(date +'%Y-%m-%dT%H:%M:%S.%N%z')" "$*" +} + + +function container::get_ip() { + # 获取容器 ip地址 + + log::info "[container]" "Get container ip address." + container_id=$(docker ps -a --no-trunc --filter=status=running --format "table {{.ID}}\t{{.Names}}" | awk "/${CONTAINER_NAME}/{print \$1}") + CONTAINER_IP=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${container_id} 2>/dev/null | tr '\n' ' ' ) + if [[ "$?" != "0" || "${CONTAINER_IP}" == "" ]]; then + log::error "[container]" "Get container ip address error." + exit 1 + fi + + log::info "[container]" "IP: ${CONTAINER_IP}" +} + +function container::check_ip() { + # 检测容器ip地址 + + log::info "[container]" "Check container service status." + CHECK_IP="" + for ip in ${CONTAINER_IP}; do + curl -s --output /dev/null "http://${ip}:${SERVICE_PORT}" && CHECK_IP="${CHECK_IP} ${ip}" + done + log::info "[container]" "Check success: ${CHECK_IP}" +} + +function nginx::get_upstream_ip() { + # 获取 nginx upstream ip地址 + + log::info "[nginx]" "Get nginx upstream ${UPSTREAM_NAME} host." + upstream_ip=$(awk '-F *|:' " +/upstream ${UPSTREAM_NAME}/ { + app = 1 + next + } +/server/ { + if (app) print \$3 + } +/}/ { + if (app) exit + } +" "${CONFIG_FILE}" | tr '\n' ' ') + log::info "[nginx]" "Upstream host: ${upstream_ip}" +} + +function nginx::config() { + # 配置 nginx + + log::info "[nginx]" "Config nginx upstream ${UPSTREAM_NAME} host." + + cp "${CONFIG_FILE}" "${TMP_CONFIG_FILE}" + for ip in ${CHECK_IP}; do + if ! grep "${ip}" "${TMP_CONFIG_FILE}" >/dev/null 2>&1; then + sed -i "/upstream ${UPSTREAM_NAME} {/a\ server ${ip}:${SERVICE_PORT};" "${TMP_CONFIG_FILE}" && NGINX_STATUS=1 + log::info "[nginx]" "IP: ${ip} add to upstream hosts." + NOTICE_MESSAGE="${NOTICE_MESSAGE}**Add**: ${ip} add to upstream hosts.\n" + fi + done + + for ip in ${upstream_ip}; do + if [[ "$CHECK_IP" == *${ip}* ]]; then + log::info "[nginx]" "IP: ${ip} ip already exists on the upstream host." + else + log::info "[nginx]" "IP: ${ip} delete from upstream host." + NOTICE_MESSAGE="${NOTICE_MESSAGE}**Del**: ${ip} delete from upstream host.\n" + sed -i "/server ${ip:-11111111111}:${SERVICE_PORT}/d" "${TMP_CONFIG_FILE}" && NGINX_STATUS=1 + fi + done + + if [[ $NGINX_STATUS == 1 ]]; then + log::info "[nginx]" "Backup nginx config to ${NGINX_BACKUP_PATH}/${CONFIG_FILE_NAME}-$(date +%s)" + cp -f "$CONFIG_FILE" "${NGINX_BACKUP_PATH}/${CONFIG_FILE_NAME}-$(date +%s)" + log::info "[nginx]" "Apply nginx config file." + mv -f "${TMP_CONFIG_FILE}" "$CONFIG_FILE" + fi +} + + +function nginx::reload() { + # 重载 nginx + + if [[ $NGINX_STATUS == 1 ]]; then + log::info "[nginx]" "Reload nginx." + local level=red + if nginx -t >/dev/null 2>&1; then + if nginx -s reload >/dev/null 2>&1; then + log::info "[nginx]" "Reload nginx success." + NOTICE_MESSAGE="${NOTICE_MESSAGE}**Reload**: nginx success.\n" + level=green + else + log::error "[nginx]" "Reload nginx error." + NOTICE_MESSAGE="${NOTICE_MESSAGE}**Reload**: nginx error.\n" + fi + else + log::error "[nginx]" "test nginx config error." + NOTICE_MESSAGE="${NOTICE_MESSAGE}**Test**: nginx config error.\n" + exit 1 + fi + notice "$level" "${NOTICE_MESSAGE}" + fi +} + +function notice() { + # 通知 + + log::info "[notice]" "select ${NOTICE_TYPE}" + if [ -z $NOTICE_TOKEN ];then + log::warning "[notice]" "please set NOTICE_TOKEN" + return + fi + notice::${NOTICE_TYPE} $@ +} + +function notice::feishu() { + # 飞书通知 + + local level="${1:-green}" + local message="${@:2}" + local now_date="$(date +'%Y-%m-%d %T')" + local title="[Upstream] check container ip" + local host="$(ip a | grep glo | awk '{print $2}' | head -1 | cut -f1 -d/)" + + local template="{\"msg_type\":\"interactive\",\"card\":{\"config\":{\"wide_screen_mode\":true,\"enable_forward\":true},\"header\":{\"title\":{\"content\":\"$title\",\"tag\":\"plain_text\"},\"template\":\"${level}\"},\"elements\":[{\"tag\":\"div\",\"text\":{\"content\":\"**发生时间:**\",\"tag\":\"lark_md\"},\"fields\":[{\"is_short\":false,\"text\":{\"tag\":\"lark_md\",\"content\":\"${now_date}\"}}]},{\"tag\":\"div\",\"text\":{\"content\":\"**节点:**\",\"tag\":\"lark_md\"},\"fields\":[{\"is_short\":false,\"text\":{\"tag\":\"lark_md\",\"content\":\"${host}\"}}]},{\"tag\":\"div\",\"text\":{\"content\":\"**详细信息:**\",\"tag\":\"lark_md\"},\"fields\":[{\"is_short\":false,\"text\":{\"tag\":\"lark_md\",\"content\":\"${message}\"}}]},{\"tag\":\"hr\"},{\"tag\":\"note\",\"elements\":[{\"tag\":\"plain_text\",\"content\":\"by lework\"}]}]}}" + + if curl -s -X POST -H "Content-Type: application/json" -d "${template}" "https://open.feishu.cn/open-apis/bot/v2/hook/${NOTICE_TOKEN}" | grep success >/dev/null 2>&1; then + log::info "[notice]" "send feishu success." + else + log::error "[notice]" "send feishu error." + fi +} + +function check::start() { + # 检测流程 + + echo + log::info "[check]" "Start inspection." + + container::get_ip + container::check_ip + nginx::get_upstream_ip + + nginx::config + nginx::reload + + NGINX_STATUS=0 + NOTICE_MESSAGE="" +} + +function help::usage { + # 使用帮助 + + cat << EOF + +Detect container ip addresses and dynamically update nginx upstream configuration. + +Usage: + $(basename "$0") [options] + +Options: + --container-name Docker container name + --config-file Nginx conf path + --upstream-name Nginx upstream name + --service-port Service port + --retries Retries number, default:${RETRIES} + --wait Retries wait time, default:${WAIT} + -h,--help View help + +Example: + $0 --container-name api-test \\ + --config-file /etc/nginx/conf.d/api.conf \\ + --upstream-name api \\ + --service-port 8000 \\ + --retries 20 \\ + --wait 5 + +EOF + exit 1 +} + + + +###################################################################################################### +# main +###################################################################################################### + +[ "$#" == "0" ] && help::usage + +while [ "${1:-}" != "" ]; do + case $1 in + --container-name ) shift + CONTAINER_NAME=${1:-$CONTAINER_NAME} + ;; + --config-file ) shift + CONFIG_FILE=${1:-$CONFIG_FILE} + CONFIG_FILE_NAME=$(basename ${CONFIG_FILE}) + ;; + --upstream-name ) shift + UPSTREAM_NAME=${1:-$UPSTREAM_NAME} + ;; + --service-port ) shift + SERVICE_PORT=${1:-$SERVICE_PORT} + ;; + --retries ) shift + RETRIES=${1:-$RETRIES} + ;; + --wait ) shift + WAIT=${1:-$WAIT} + ;; + -h | --help ) help::usage + ;; + * ) help::usage + exit 1 + esac + shift +done + +n=0 +while [[ $n -lt ${RETRIES} ]] +do + check::start + sleep ${WAIT} + let n++ +done + diff --git a/shell/other.sh b/shell/other.sh new file mode 100644 index 0000000..76e629c --- /dev/null +++ b/shell/other.sh @@ -0,0 +1,24 @@ + +## 获取主机的免密主机列表 + +KEYS=$(find ~/ /root /home -maxdepth 2 -name 'id_rsa*' | grep -vw pub) +KEYS2=$(cat ~/.ssh/config /home/*/.ssh/config /root/.ssh/config | grep IdentityFile | awk -F "IdentityFile" '{print $2 }') +KEYS3=$(find ~/ /root /home -maxdepth 3 -name '*.pem' | uniq) +HOSTS=$(cat ~/.ssh/config /home/*/.ssh/config /root/.ssh/config | grep HostName | awk -F "HostName" '{print $2}') +HOSTS2=$(cat ~/.bash_history /home/*/.bash_history /root/.bash_history | grep -E "(ssh|scp)" | grep -oP "([0-9]{1,3}\.){3}[0-9]{1,3}") +HOSTS3=$(cat ~/*/.ssh/known_hosts /home/*/.ssh/known_hosts /root/.ssh/known_hosts | grep -oP "([0-9]{1,3}\.){3}[0-9]{1,3}" | uniq) +USERZ=$( + echo "root" + find ~/ /root /home -maxdepth 2 -name '\.ssh' | uniq | xargs find | awk '/id_rsa/' | awk -F'/' '{print $3}' | uniq | grep -v "\.ssh" +) +userlist=$(echo $USERZ | tr ' ' '\n' | nl | sort -u -k2 | sort -n | cut -f2-) +hostlist=$(echo "$HOSTS $HOSTS2 $HOSTS3" | grep -vw 127.0.0.1 | tr ' ' '\n' | nl | sort -u -k2 | sort -n | cut -f2-) +keylist=$(echo "$KEYS $KEYS2 $KEYS3" | tr ' ' '\n' | nl | sort -u -k2 | sort -n | cut -f2-) +for user in $userlist; do + for host in $hostlist; do + for key in $keylist; do + chmod +r $key; chmod 400 $key + ssh -oStrictHostKeyChecking=no -oBatchMode=yes -oConnectTimeout=5 -i $key $user@$host "ifconfig" + done + done +done diff --git a/shell/docker_hub_speed_test.sh b/shell/test/docker_hub_speed_test.sh similarity index 100% rename from shell/docker_hub_speed_test.sh rename to shell/test/docker_hub_speed_test.sh diff --git a/shell/os_repo_speed_test.sh b/shell/test/os_repo_speed_test.sh similarity index 100% rename from shell/os_repo_speed_test.sh rename to shell/test/os_repo_speed_test.sh diff --git a/shell/test/superbench.sh b/shell/test/superbench.sh new file mode 100644 index 0000000..68424bd --- /dev/null +++ b/shell/test/superbench.sh @@ -0,0 +1,604 @@ +#!/usr/bin/env bash +# +# Description: Auto system info & I/O test & network to China script +# +# Copyright (C) 2017 - 2020 Oldking +# +# Thanks: Bench.sh +# +# URL: https://www.oldking.net/350.html +# + +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[0;33m' +SKYBLUE='\033[0;36m' +PLAIN='\033[0m' + +about() { + echo "" + echo " ========================================================= " + echo " \ Superbench.sh Script / " + echo " \ Basic system info, I/O test and speedtest / " + echo " \ v1.1.7 (7 Apr 2020) / " + echo " \ Created by Oldking / " + echo " ========================================================= " + echo "" + echo " Intro: https://www.oldking.net/350.html" + echo " Copyright (C) 2020 Oldking oooldking@gmail.com" + echo "" +} + +cancel() { + echo "" + next; + echo " Abort ..." + echo " Cleanup ..." + cleanup; + echo " Done" + exit +} + +trap cancel SIGINT + +benchinit() { + if [ -f /etc/redhat-release ]; then + release="centos" + elif cat /etc/issue | grep -Eqi "debian"; then + release="debian" + elif cat /etc/issue | grep -Eqi "ubuntu"; then + release="ubuntu" + elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then + release="centos" + elif cat /proc/version | grep -Eqi "debian"; then + release="debian" + elif cat /proc/version | grep -Eqi "ubuntu"; then + release="ubuntu" + elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then + release="centos" + fi + + [[ $EUID -ne 0 ]] && echo -e "${RED}Error:${PLAIN} This script must be run as root!" && exit 1 + + if [ ! -e '/usr/bin/python' ]; then + echo " Installing Python ..." + if [ "${release}" == "centos" ]; then + yum update > /dev/null 2>&1 + yum -y install python > /dev/null 2>&1 + else + apt-get update > /dev/null 2>&1 + apt-get -y install python > /dev/null 2>&1 + fi + + fi + + if [ ! -e '/usr/bin/curl' ]; then + echo " Installing Curl ..." + if [ "${release}" == "centos" ]; then + yum update > /dev/null 2>&1 + yum -y install curl > /dev/null 2>&1 + else + apt-get update > /dev/null 2>&1 + apt-get -y install curl > /dev/null 2>&1 + fi + fi + + if [ ! -e '/usr/bin/wget' ]; then + echo " Installing Wget ..." + if [ "${release}" == "centos" ]; then + yum update > /dev/null 2>&1 + yum -y install wget > /dev/null 2>&1 + else + apt-get update > /dev/null 2>&1 + apt-get -y install wget > /dev/null 2>&1 + fi + fi + + if [ ! -e './speedtest-cli/speedtest' ]; then + echo " Installing Speedtest-cli ..." + wget --no-check-certificate -qO speedtest.tgz https://cdn.jsdelivr.net/gh/oooldking/script@1.1.7/speedtest_cli/ookla-speedtest-1.0.0-$(uname -m)-linux.tgz > /dev/null 2>&1 + fi + mkdir -p speedtest-cli && tar zxvf speedtest.tgz -C ./speedtest-cli/ > /dev/null 2>&1 && chmod a+rx ./speedtest-cli/speedtest + + if [ ! -e 'tools.py' ]; then + echo " Installing tools.py ..." + wget --no-check-certificate https://cdn.jsdelivr.net/gh/oooldking/script@1.1.7/tools.py > /dev/null 2>&1 + fi + chmod a+rx tools.py + + if [ ! -e 'fast_com.py' ]; then + echo " Installing Fast.com-cli ..." + wget --no-check-certificate https://cdn.jsdelivr.net/gh/sanderjo/fast.com@master/fast_com.py > /dev/null 2>&1 + wget --no-check-certificate https://cdn.jsdelivr.net/gh/sanderjo/fast.com@master/fast_com_example_usage.py > /dev/null 2>&1 + fi + chmod a+rx fast_com.py + chmod a+rx fast_com_example_usage.py + + sleep 5 + + start=$(date +%s) +} + +get_opsy() { + [ -f /etc/redhat-release ] && awk '{print ($1,$3~/^[0-9]/?$3:$4)}' /etc/redhat-release && return + [ -f /etc/os-release ] && awk -F'[= "]' '/PRETTY_NAME/{print $3,$4,$5}' /etc/os-release && return + [ -f /etc/lsb-release ] && awk -F'[="]+' '/DESCRIPTION/{print $2}' /etc/lsb-release && return +} + +next() { + printf "%-70s\n" "-" | sed 's/\s/-/g' | tee -a $log +} + +speed_test(){ + if [[ $1 == '' ]]; then + speedtest-cli/speedtest -p no --accept-license > $speedLog 2>&1 + is_upload=$(cat $speedLog | grep 'Upload') + result_speed=$(cat $speedLog | awk -F ' ' '/Result/{print $3}') + if [[ ${is_upload} ]]; then + local REDownload=$(cat $speedLog | awk -F ' ' '/Download/{print $3}') + local reupload=$(cat $speedLog | awk -F ' ' '/Upload/{print $3}') + local relatency=$(cat $speedLog | awk -F ' ' '/Latency/{print $2}') + + temp=$(echo "$relatency" | awk -F '.' '{print $1}') + if [[ ${temp} -gt 50 ]]; then + relatency="(*)"${relatency} + fi + local nodeName=$2 + + temp=$(echo "${REDownload}" | awk -F ' ' '{print $1}') + if [[ $(awk -v num1=${temp} -v num2=0 'BEGIN{print(num1>num2)?"1":"0"}') -eq 1 ]]; then + printf "${YELLOW}%-18s${GREEN}%-18s${RED}%-20s${SKYBLUE}%-12s${PLAIN}\n" " ${nodeName}" "${reupload} Mbit/s" "${REDownload} Mbit/s" "${relatency} ms" | tee -a $log + fi + else + local cerror="ERROR" + fi + else + speedtest-cli/speedtest -p no -s $1 --accept-license > $speedLog 2>&1 + is_upload=$(cat $speedLog | grep 'Upload') + if [[ ${is_upload} ]]; then + local REDownload=$(cat $speedLog | awk -F ' ' '/Download/{print $3}') + local reupload=$(cat $speedLog | awk -F ' ' '/Upload/{print $3}') + local relatency=$(cat $speedLog | awk -F ' ' '/Latency/{print $2}') + local nodeName=$2 + + temp=$(echo "${REDownload}" | awk -F ' ' '{print $1}') + if [[ $(awk -v num1=${temp} -v num2=0 'BEGIN{print(num1>num2)?"1":"0"}') -eq 1 ]]; then + printf "${YELLOW}%-18s${GREEN}%-18s${RED}%-20s${SKYBLUE}%-12s${PLAIN}\n" " ${nodeName}" "${reupload} Mbit/s" "${REDownload} Mbit/s" "${relatency} ms" | tee -a $log + fi + else + local cerror="ERROR" + fi + fi +} + +print_speedtest() { + printf "%-18s%-18s%-20s%-12s\n" " Node Name" "Upload Speed" "Download Speed" "Latency" | tee -a $log + speed_test '' 'Speedtest.net' + speed_fast_com + speed_test '27377' 'Beijing 5G CT' + speed_test '26352' 'Nanjing 5G CT' + speed_test '17145' 'Hefei 5G CT' + speed_test '27594' 'Guangzhou 5G CT' + speed_test '27154' 'TianJin 5G CU' + speed_test '24447' 'Shanghai 5G CU' + speed_test '26678' 'Guangzhou 5G CU' + speed_test '17184' 'Tianjin 5G CM' + speed_test '26850' 'Wuxi 5G CM' + speed_test '27249' 'Nanjing 5G CM' + speed_test '26404' 'Hefei 5G CM' + speed_test '28491' 'Changsha 5G CM' + + rm -rf speedtest* +} + +print_speedtest_fast() { + printf "%-18s%-18s%-20s%-12s\n" " Node Name" "Upload Speed" "Download Speed" "Latency" | tee -a $log + speed_test '' 'Speedtest.net' + speed_fast_com + speed_test '27377' 'Beijing 5G CT' + speed_test '24447' 'ShangHai 5G CU' + speed_test '27249' 'Nanjing 5G CM' + + rm -rf speedtest* +} + +speed_fast_com() { + temp=$(python fast_com_example_usage.py 2>&1) + is_down=$(echo "$temp" | grep 'Result') + if [[ ${is_down} ]]; then + temp1=$(echo "$temp" | awk -F ':' '/Result/{print $2}') + temp2=$(echo "$temp1" | awk -F ' ' '/Mbps/{print $1}') + local REDownload="$temp2 Mbit/s" + local reupload="0.00 Mbit/s" + local relatency="-" + local nodeName="Fast.com" + + printf "${YELLOW}%-18s${GREEN}%-18s${RED}%-20s${SKYBLUE}%-12s${PLAIN}\n" " ${nodeName}" "${reupload}" "${REDownload}" "${relatency}" | tee -a $log + else + local cerror="ERROR" + fi + rm -rf fast_com_example_usage.py + rm -rf fast_com.py + +} + +io_test() { + (LANG=C dd if=/dev/zero of=test_file_$$ bs=512K count=$1 conv=fdatasync && rm -f test_file_$$ ) 2>&1 | awk -F, '{io=$NF} END { print io}' | sed 's/^[ \t]*//;s/[ \t]*$//' +} + +calc_disk() { + local total_size=0 + local array=$@ + for size in ${array[@]} + do + [ "${size}" == "0" ] && size_t=0 || size_t=`echo ${size:0:${#size}-1}` + [ "`echo ${size:(-1)}`" == "K" ] && size=0 + [ "`echo ${size:(-1)}`" == "M" ] && size=$( awk 'BEGIN{printf "%.1f", '$size_t' / 1024}' ) + [ "`echo ${size:(-1)}`" == "T" ] && size=$( awk 'BEGIN{printf "%.1f", '$size_t' * 1024}' ) + [ "`echo ${size:(-1)}`" == "G" ] && size=${size_t} + total_size=$( awk 'BEGIN{printf "%.1f", '$total_size' + '$size'}' ) + done + echo ${total_size} +} + +power_time() { + + result=$(smartctl -a $(result=$(cat /proc/mounts) && echo $(echo "$result" | awk '/data=ordered/{print $1}') | awk '{print $1}') 2>&1) && power_time=$(echo "$result" | awk '/Power_On/{print $10}') && echo "$power_time" +} + +install_smart() { + if [ ! -e '/usr/sbin/smartctl' ]; then + echo "Installing Smartctl ..." + if [ "${release}" == "centos" ]; then + yum update > /dev/null 2>&1 + yum -y install smartmontools > /dev/null 2>&1 + else + apt-get update > /dev/null 2>&1 + apt-get -y install smartmontools > /dev/null 2>&1 + fi + fi +} + +ip_info4(){ + ip_date=$(curl -4 -s http://api.ip.la/en?json) + echo $ip_date > ip_json.json + isp=$(python tools.py geoip isp) + as_tmp=$(python tools.py geoip as) + asn=$(echo $as_tmp | awk -F ' ' '{print $1}') + org=$(python tools.py geoip org) + if [ -z "ip_date" ]; then + echo $ip_date + echo "hala" + country=$(python tools.py ipip country_name) + city=$(python tools.py ipip city) + countryCode=$(python tools.py ipip country_code) + region=$(python tools.py ipip province) + else + country=$(python tools.py geoip country) + city=$(python tools.py geoip city) + countryCode=$(python tools.py geoip countryCode) + region=$(python tools.py geoip regionName) + fi + if [ -z "$city" ]; then + city=${region} + fi + + echo -e " ASN & ISP : ${SKYBLUE}$asn, $isp${PLAIN}" | tee -a $log + echo -e " Organization : ${YELLOW}$org${PLAIN}" | tee -a $log + echo -e " Location : ${SKYBLUE}$city, ${YELLOW}$country / $countryCode${PLAIN}" | tee -a $log + echo -e " Region : ${SKYBLUE}$region${PLAIN}" | tee -a $log + + rm -rf tools.py + rm -rf ip_json.json +} + +virt_check(){ + if hash ifconfig 2>/dev/null; then + eth=$(ifconfig) + fi + + virtualx=$(dmesg) 2>/dev/null + + if [ $(which dmidecode) ]; then + sys_manu=$(dmidecode -s system-manufacturer) 2>/dev/null + sys_product=$(dmidecode -s system-product-name) 2>/dev/null + sys_ver=$(dmidecode -s system-version) 2>/dev/null + else + sys_manu="" + sys_product="" + sys_ver="" + fi + + if grep docker /proc/1/cgroup -qa; then + virtual="Docker" + elif grep lxc /proc/1/cgroup -qa; then + virtual="Lxc" + elif grep -qa container=lxc /proc/1/environ; then + virtual="Lxc" + elif [[ -f /proc/user_beancounters ]]; then + virtual="OpenVZ" + elif [[ "$virtualx" == *kvm-clock* ]]; then + virtual="KVM" + elif [[ "$cname" == *KVM* ]]; then + virtual="KVM" + elif [[ "$cname" == *QEMU* ]]; then + virtual="KVM" + elif [[ "$virtualx" == *"VMware Virtual Platform"* ]]; then + virtual="VMware" + elif [[ "$virtualx" == *"Parallels Software International"* ]]; then + virtual="Parallels" + elif [[ "$virtualx" == *VirtualBox* ]]; then + virtual="VirtualBox" + elif [[ -e /proc/xen ]]; then + virtual="Xen" + elif [[ "$sys_manu" == *"Microsoft Corporation"* ]]; then + if [[ "$sys_product" == *"Virtual Machine"* ]]; then + if [[ "$sys_ver" == *"7.0"* || "$sys_ver" == *"Hyper-V" ]]; then + virtual="Hyper-V" + else + virtual="Microsoft Virtual Machine" + fi + fi + else + virtual="Dedicated" + fi +} + +power_time_check(){ + echo -ne " Power time of disk : " + install_smart + ptime=$(power_time) + echo -e "${SKYBLUE}$ptime Hours${PLAIN}" +} + +freedisk() { + freespace=$( df -m . | awk 'NR==2 {print $4}' ) + if [[ $freespace == "" ]]; then + $freespace=$( df -m . | awk 'NR==3 {print $3}' ) + fi + if [[ $freespace -gt 1024 ]]; then + printf "%s" $((1024*2)) + elif [[ $freespace -gt 512 ]]; then + printf "%s" $((512*2)) + elif [[ $freespace -gt 256 ]]; then + printf "%s" $((256*2)) + elif [[ $freespace -gt 128 ]]; then + printf "%s" $((128*2)) + else + printf "1" + fi +} + +print_io() { + if [[ $1 == "fast" ]]; then + writemb=$((128*2)) + else + writemb=$(freedisk) + fi + + writemb_size="$(( writemb / 2 ))MB" + if [[ $writemb_size == "1024MB" ]]; then + writemb_size="1.0GB" + fi + + if [[ $writemb != "1" ]]; then + echo -n " I/O Speed( $writemb_size ) : " | tee -a $log + io1=$( io_test $writemb ) + echo -e "${YELLOW}$io1${PLAIN}" | tee -a $log + echo -n " I/O Speed( $writemb_size ) : " | tee -a $log + io2=$( io_test $writemb ) + echo -e "${YELLOW}$io2${PLAIN}" | tee -a $log + echo -n " I/O Speed( $writemb_size ) : " | tee -a $log + io3=$( io_test $writemb ) + echo -e "${YELLOW}$io3${PLAIN}" | tee -a $log + ioraw1=$( echo $io1 | awk 'NR==1 {print $1}' ) + [ "`echo $io1 | awk 'NR==1 {print $2}'`" == "GB/s" ] && ioraw1=$( awk 'BEGIN{print '$ioraw1' * 1024}' ) + ioraw2=$( echo $io2 | awk 'NR==1 {print $1}' ) + [ "`echo $io2 | awk 'NR==1 {print $2}'`" == "GB/s" ] && ioraw2=$( awk 'BEGIN{print '$ioraw2' * 1024}' ) + ioraw3=$( echo $io3 | awk 'NR==1 {print $1}' ) + [ "`echo $io3 | awk 'NR==1 {print $2}'`" == "GB/s" ] && ioraw3=$( awk 'BEGIN{print '$ioraw3' * 1024}' ) + ioall=$( awk 'BEGIN{print '$ioraw1' + '$ioraw2' + '$ioraw3'}' ) + ioavg=$( awk 'BEGIN{printf "%.1f", '$ioall' / 3}' ) + echo -e " Average I/O Speed : ${YELLOW}$ioavg MB/s${PLAIN}" | tee -a $log + else + echo -e " ${RED}Not enough space!${PLAIN}" + fi +} + +print_system_info() { + echo -e " CPU Model : ${SKYBLUE}$cname${PLAIN}" | tee -a $log + echo -e " CPU Cores : ${YELLOW}$cores Cores ${SKYBLUE}$freq MHz $arch${PLAIN}" | tee -a $log + echo -e " CPU Cache : ${SKYBLUE}$corescache ${PLAIN}" | tee -a $log + echo -e " OS : ${SKYBLUE}$opsy ($lbit Bit) ${YELLOW}$virtual${PLAIN}" | tee -a $log + echo -e " Kernel : ${SKYBLUE}$kern${PLAIN}" | tee -a $log + echo -e " Total Space : ${SKYBLUE}$disk_used_size GB / ${YELLOW}$disk_total_size GB ${PLAIN}" | tee -a $log + echo -e " Total RAM : ${SKYBLUE}$uram MB / ${YELLOW}$tram MB ${SKYBLUE}($bram MB Buff)${PLAIN}" | tee -a $log + echo -e " Total SWAP : ${SKYBLUE}$uswap MB / $swap MB${PLAIN}" | tee -a $log + echo -e " Uptime : ${SKYBLUE}$up${PLAIN}" | tee -a $log + echo -e " Load Average : ${SKYBLUE}$load${PLAIN}" | tee -a $log + echo -e " TCP CC : ${YELLOW}$tcpctrl${PLAIN}" | tee -a $log +} + +print_end_time() { + end=$(date +%s) + time=$(( $end - $start )) + if [[ $time -gt 60 ]]; then + min=$(expr $time / 60) + sec=$(expr $time % 60) + echo -ne " Finished in : ${min} min ${sec} sec" | tee -a $log + else + echo -ne " Finished in : ${time} sec" | tee -a $log + fi + + printf '\n' | tee -a $log + + bj_time=$(curl -s http://cgi.im.qq.com/cgi-bin/cgi_svrtime) + + if [[ $(echo $bj_time | grep "html") ]]; then + bj_time=$(date -u +%Y-%m-%d" "%H:%M:%S -d '+8 hours') + fi + echo " Timestamp : $bj_time GMT+8" | tee -a $log + echo " Results : $log" +} + +get_system_info() { + cname=$( awk -F: '/model name/ {name=$2} END {print name}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//' ) + cores=$( awk -F: '/model name/ {core++} END {print core}' /proc/cpuinfo ) + freq=$( awk -F: '/cpu MHz/ {freq=$2} END {print freq}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//' ) + corescache=$( awk -F: '/cache size/ {cache=$2} END {print cache}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//' ) + tram=$( free -m | awk '/Mem/ {print $2}' ) + uram=$( free -m | awk '/Mem/ {print $3}' ) + bram=$( free -m | awk '/Mem/ {print $6}' ) + swap=$( free -m | awk '/Swap/ {print $2}' ) + uswap=$( free -m | awk '/Swap/ {print $3}' ) + up=$( awk '{a=$1/86400;b=($1%86400)/3600;c=($1%3600)/60} {printf("%d days %d hour %d min\n",a,b,c)}' /proc/uptime ) + load=$( w | head -1 | awk -F'load average:' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//' ) + opsy=$( get_opsy ) + arch=$( uname -m ) + lbit=$( getconf LONG_BIT ) + kern=$( uname -r ) + + disk_size1=$( LANG=C df -hPl | grep -wvE '\-|none|tmpfs|overlay|shm|udev|devtmpfs|by-uuid|chroot|Filesystem' | awk '{print $2}' ) + disk_size2=$( LANG=C df -hPl | grep -wvE '\-|none|tmpfs|overlay|shm|udev|devtmpfs|by-uuid|chroot|Filesystem' | awk '{print $3}' ) + disk_total_size=$( calc_disk ${disk_size1[@]} ) + disk_used_size=$( calc_disk ${disk_size2[@]} ) + + tcpctrl=$( sysctl net.ipv4.tcp_congestion_control | awk -F ' ' '{print $3}' ) + + virt_check +} + +print_intro() { + printf ' Superbench.sh -- https://www.oldking.net/350.html\n' | tee -a $log + printf " Mode : \e${GREEN}%s\e${PLAIN} Version : \e${GREEN}%s${PLAIN}\n" $mode_name 1.1.7 | tee -a $log + printf ' Usage : wget -qO- sb.oldking.net | bash\n' | tee -a $log +} + +sharetest() { + echo " Share result:" | tee -a $log + echo " · $result_speed" | tee -a $log + log_preupload + case $1 in + 'ubuntu') + share_link="https://paste.ubuntu.com"$( curl -v --data-urlencode "content@$log_up" -d "poster=superbench.sh" -d "syntax=text" "https://paste.ubuntu.com" 2>&1 | \ + grep "Location" | awk '{print $3}' );; + 'haste' ) + share_link=$( curl -X POST -s -d "$(cat $log)" https://hastebin.com/documents | awk -F '"' '{print "https://hastebin.com/"$4}' );; + 'clbin' ) + share_link=$( curl -sF 'clbin=<-' https://clbin.com < $log );; + 'ptpb' ) + share_link=$( curl -sF c=@- https://ptpb.pw/?u=1 < $log );; + esac + + echo " · $share_link" | tee -a $log + next + echo "" + rm -f $log_up + +} + +log_preupload() { + log_up="$HOME/superbench_upload.log" + true > $log_up + $(cat superbench.log 2>&1 | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" > $log_up) +} + +cleanup() { + rm -f test_file_* + rm -rf speedtest* + rm -f fast_com* + rm -f tools.py + rm -f ip_json.json +} + +bench_all(){ + mode_name="Standard" + about; + benchinit; + clear + next; + print_intro; + next; + get_system_info; + print_system_info; + ip_info4; + next; + print_io; + next; + print_speedtest; + next; + print_end_time; + next; + cleanup; + sharetest ubuntu; +} + +fast_bench(){ + mode_name="Fast" + about; + benchinit; + clear + next; + print_intro; + next; + get_system_info; + print_system_info; + ip_info4; + next; + print_io fast; + next; + print_speedtest_fast; + next; + print_end_time; + next; + cleanup; +} + +log="./superbench.log" +true > $log +speedLog="./speedtest.log" +true > $speedLog + +case $1 in + 'info'|'-i'|'--i'|'-info'|'--info' ) + about;sleep 3;next;get_system_info;print_system_info;next;; + 'version'|'-v'|'--v'|'-version'|'--version') + next;about;next;; + 'io'|'-io'|'--io'|'-drivespeed'|'--drivespeed' ) + next;print_io;next;; + 'speed'|'-speed'|'--speed'|'-speedtest'|'--speedtest'|'-speedcheck'|'--speedcheck' ) + about;benchinit;next;print_speedtest;next;cleanup;; + 'ip'|'-ip'|'--ip'|'geoip'|'-geoip'|'--geoip' ) + about;benchinit;next;ip_info4;next;cleanup;; + 'bench'|'-a'|'--a'|'-all'|'--all'|'-bench'|'--bench' ) + bench_all;; + 'about'|'-about'|'--about' ) + about;; + 'fast'|'-f'|'--f'|'-fast'|'--fast' ) + fast_bench;; + 'share'|'-s'|'--s'|'-share'|'--share' ) + bench_all; + is_share="share" + if [[ $2 == "" ]]; then + sharetest ubuntu; + else + sharetest $2; + fi + ;; + 'debug'|'-d'|'--d'|'-debug'|'--debug' ) + get_ip_whois_org_name;; +*) + bench_all;; +esac + +if [[ ! $is_share == "share" ]]; then + case $2 in + 'share'|'-s'|'--s'|'-share'|'--share' ) + if [[ $3 == '' ]]; then + sharetest ubuntu; + else + sharetest $3; + fi + ;; + esac +fi \ No newline at end of file