#!/bin/bash hostArchVersion="" hostArch="" #### CollectSystemInfo #### serverName="" serverIpPbV4="" serverIpInV4="" serverIpPbV6="" serverIpInV6="" location="" provider="" managePort="" cpuBrand="" cpuCore="" memoryTotal="" diskTotal="" diskUsage="" osInfo="" osKernelInfo="" tcpControl="" virtualization="" ioSpeed="" machineId="" archInfo="" agentVersion="" ### tmp usage ioavg="" public_ipv4="" inner_ipv4="" public_ipv6="" inner_ipv6="" country="" region="" city="" org="" managePort="" #### CollectSystemInfo #### command_exists() { command -v "$@" >/dev/null 2>&1 } ####### 获取系统版本及64位或32位信息 check_sys() { # 获取当前终端的宽度,动态调整分割线的长度 shellwidth=$(stty size | awk '{print $2}') SplitLine=$(yes "-" | sed ${shellwidth}'q' | tr -d '\n') sys_bit=$(uname -m) case $sys_bit in i[36]86) os_bit="32" hostArch="386" ;; x86_64) os_bit="64" hostArch="amd64" ;; *armv6*) os_bit="arm" hostArch="arm6" ;; *armv7*) os_bit="arm" hostArch="arm7" ;; *aarch64* | *armv8*) os_bit="arm64" hostArch="arm64" ;; *) error " 哈哈……这个 辣鸡脚本 不支持你的系统。 (-_-) \n 备注: 仅支持 Ubuntu 16+ / Debian 8+ / CentOS 7+ 系统 " && exit 1 ;; esac ## 判定Linux的发行版本 if [ -f /etc/redhat-release ]; then hostArchVersion="centos" elif cat /etc/issue | grep -Eqi "debian"; then hostArchVersion="debian" elif cat /etc/issue | grep -Eqi "ubuntu"; then hostArchVersion="ubuntu" elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then hostArchVersion="centos" elif cat /proc/version | grep -Eqi "debian"; then hostArchVersion="debian" elif cat /proc/version | grep -Eqi "ubuntu"; then hostArchVersion="ubuntu" elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then hostArchVersion="centos" else hostArchVersion="" fi # 判断系统的包管理工具 apt, yum, or zypper getPackageManageTool() { if [[ -n $(command -v apt-get) ]]; then CMD_INSTALL="apt-get -y -qq install" CMD_UPDATE="apt-get -qq update" CMD_REMOVE="apt-get -y remove" elif [[ -n $(command -v yum) ]]; then CMD_INSTALL="yum -y -q install" CMD_UPDATE="yum -q makecache" CMD_REMOVE="yum -y remove" elif [[ -n $(command -v zypper) ]]; then CMD_INSTALL="zypper -y install" CMD_UPDATE="zypper ref" CMD_REMOVE="zypper -y remove" else return 1 fi return 0 } # 检查系统包管理方式,更新包 getPackageManageTool if [[ $? -eq 1 ]]; then error "系统的包管理不是 APT or YUM, 请手动安装所需要的软件." return 1 fi return 0 } ####################################### # description # Arguments: # 1 ####################################### GoIOTest() { (LANG=C dd if=/dev/zero of=benchtest_$$ bs=512k count=$1 conv=fdatasync && rm -f benchtest_$$) 2>&1 | awk -F, '{io=$NF} END { print io}' | sed 's/^[ \t]*//;s/[ \t]*$//' } ####################################### # description # Arguments: # 1 # Returns: # ... ####################################### calc_size() { local raw=$1 local total_size=0 local num=1 local unit="KB" if ! [[ ${raw} =~ ^[0-9]+$ ]]; then echo "" return fi if [ "${raw}" -ge 1073741824 ]; then num=1073741824 unit="TB" elif [ "${raw}" -ge 1048576 ]; then num=1048576 unit="GB" elif [ "${raw}" -ge 1024 ]; then num=1024 unit="MB" elif [ "${raw}" -eq 0 ]; then echo "${total_size}" return fi total_size=$(awk 'BEGIN{printf "%.1f", '$raw' / '$num'}') echo "${total_size} ${unit}" } ####################################### # description # Arguments: # None # Returns: # ... ####################################### GethostArchInfo() { [ -f /etc/redhat-release ] && awk '{print $0}' /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 } ####################################### # description # Globals: # freespace # io1 # io2 # io3 # ioall # ioavg # ioraw1 # ioraw2 # ioraw3 # writemb # Arguments: # None ####################################### StartIOTest() { echo "start IO speed test !" freespace=$(df -m . | awk 'NR==2 {print $4}') if [ -z "${freespace}" ]; then freespace=$(df -m . | awk 'NR==3 {print $3}') fi if [ ${freespace} -gt 1024 ]; then writemb=2048 io1=$(GoIOTest ${writemb}) echo "I/O Speed(1st run) : $io1)" io2=$(GoIOTest ${writemb}) echo "I/O Speed(2st run) : $io2)" io3=$(GoIOTest ${writemb}) echo "I/O Speed(3st run) : $io3)" 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 "I/O Speed(average) : $ioavg MB/s)" else echo " $(_red "Not enough space for I/O Speed test!")" fi } ####################################### # description # Arguments: # None ####################################### GetAgentLatestVersion() { ls /octopus-agent | grep "octopus-agent" | cut -d "-" -f3- | cut -d"." -f1 } ####################################### # description # Globals: # cpuName # sys_manu # sys_product # sys_ver # virt # virtualx # Arguments: # None ####################################### Check_Virtualization() { echo "start to check host virtualization !" command_exists "dmesg" && virtualx="$(dmesg 2>/dev/null)" if command_exists "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 -qa docker /proc/1/cgroup; then virt="Docker" elif grep -qa lxc /proc/1/cgroup; then virt="LXC" elif grep -qa container=lxc /proc/1/environ; then virt="LXC" elif [[ -f /proc/user_beancounters ]]; then virt="OpenVZ" elif [[ ${virtualx} == *kvm-clock* ]]; then virt="KVM" elif [[ ${sys_product} == *KVM* ]]; then virt="KVM" elif [[ ${cpuName} == *KVM* ]]; then virt="KVM" elif [[ ${cpuName} == *QEMU* ]]; then virt="KVM" elif [[ ${virtualx} == *"VMware Virtual Platform"* ]]; then virt="VMware" elif [[ ${sys_product} == *"VMware Virtual Platform"* ]]; then virt="VMware" elif [[ ${virtualx} == *"Parallels Software International"* ]]; then virt="Parallels" elif [[ ${virtualx} == *VirtualBox* ]]; then virt="VirtualBox" elif [[ -e /proc/xen ]]; then if grep -q "control_d" "/proc/xen/capabilities" 2>/dev/null; then virt="Xen-Dom0" else virt="Xen-DomU" fi elif [ -f "/sys/hypervisor/type" ] && grep -q "xen" "/sys/hypervisor/type"; then virt="Xen" elif [[ ${sys_manu} == *"Microsoft Corporation"* ]]; then if [[ ${sys_product} == *"Virtual Machine"* ]]; then if [[ ${sys_ver} == *"7.0"* || ${sys_ver} == *"Hyper-V" ]]; then virt="Hyper-V" else virt="Microsoft Virtual Machine" fi fi else virt="Dedicated" fi } GetManagePort() { tmpManagePorts=$(netstat -ntulp | grep sshd | grep -w tcp | grep -v "127.0.0.1" | awk '{print$4}' | cut -d":" -f2) managePort=$(echo $tmpManagePorts | tr '\n' ' ') echo "manage Port is => $managePort" } ####################################### # description # Globals: # city # country # org # public_ipv4 # region # Arguments: # None ####################################### GetIpv4Info() { echo "start to get system public ip info !" # public ip info org="$(wget -q -T10 -O- ipinfo.io/org)" city="$(wget -q -T10 -O- ipinfo.io/city)" country="$(wget -q -T10 -O- ipinfo.io/country)" region="$(wget -q -T10 -O- ipinfo.io/region)" public_ipv4="$(wget -q -T10 -O- ipinfo.io/ip)" public_ipv6="$(curl -q --max-time 5 -6 https://ifconfig.co/ip)" if [ -z "$public_ipv4" ]; then public_ipv4=" " fi if [ -z "$public_ipv6" ]; then public_ipv6=" " fi # inner ipinfo export interface_prefix=("[[:space:]]eth[0-9]{1,2}" "[[:space:]]ens[0-9]{1,3}" "[[:space:]]eno[0-9]{1,3}" "[[:space:]]enp[0-9]{1,2}" "[[:space:]]enp[0-9]{1,2}s[0-9]{1,3}") local real_interface="eth0" for interface in "${interface_prefix[@]}"; do echo $(ip link show) | grep -oE ${interface} | head -1 if [[ $? -eq 0 ]]; then real_interface=$(echo $(ip link show) | grep -oE ${interface} | head -1 | cut -d" " -f2) echo "当前主机的真实内网网卡为 => [ $real_interface ]" break fi done # 提取IPv4地址(CIDR格式) ipv4_regex="inet[[:space:]](25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/[0-9]{1,2}" # 提取IPv6地址(CIDR格式) ipv6_regex="inet6[[:space:]]([0-9a-fA-F]{0,4}(:[0-9a-fA-F]{0,4}){1,7})\/[0-9]{1,3}" # 查找IPv4地址 inner_ipv4=$(echo $(ip addr show $real_interface) | grep -oE $ipv4_regex | grep -v 127 | cut -d" " -f2) echo "Interface: $real_interface, IPv4 Address: $inner_ipv4" # 查找IPv6地址 inner_ipv6=$(echo $(ip addr show $real_interface) | grep -oE $ipv6_regex | grep -v "::1/" | cut -d" " -f2) echo "Interface: $real_interface, IPv6 Address: $inner_ipv6" } echo "start to collect system info !" check_sys cpuName=$(awk -F: '/model name/ {name=$2} END {print name}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//') cores=$(awk -F: '/processor/ {core++} END {print core}' /proc/cpuinfo) freq=$(awk -F'[ :]' '/cpu MHz/ {print $4;exit}' /proc/cpuinfo) ccache=$(awk -F: '/cache size/ {cache=$2} END {print cache}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//') cpu_aes=$(grep -i 'aes' /proc/cpuinfo) cpu_virt=$(grep -Ei 'vmx|svm' /proc/cpuinfo) tram=$( LANG=C free | awk '/Mem/ {print $2}' ) tram=$(calc_size $tram) uram=$( LANG=C free | awk '/Mem/ {print $3}' ) uram=$(calc_size $uram) swap=$( LANG=C free | awk '/Swap/ {print $2}' ) swap=$(calc_size $swap) uswap=$( LANG=C free | awk '/Swap/ {print $3}' ) uswap=$(calc_size $uswap) 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) if command_exists "w"; then load=$( LANG=C w | head -1 | awk -F'load average:' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//' ) elif command_exists "uptime"; then load=$( LANG=C uptime | head -1 | awk -F'load average:' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//' ) fi opsy=$(GethostArchInfo) arch=$(uname -m) if command_exists "getconf"; then lbit=$(getconf LONG_BIT) else echo ${arch} | grep -q "64" && lbit="64" || lbit="32" fi kern=$(uname -r) disk_total_size=$( LANG=C df -t simfs -t ext2 -t ext3 -t ext4 -t btrfs -t xfs -t vfat -t ntfs -t swap --total 2>/dev/null | grep total | awk '{ print $2 }' ) disk_total_size=$(calc_size $disk_total_size) disk_used_size=$( LANG=C df -t simfs -t ext2 -t ext3 -t ext4 -t btrfs -t xfs -t vfat -t ntfs -t swap --total 2>/dev/null | grep total | awk '{ print $3 }' ) disk_used_size=$(calc_size $disk_used_size) tcpctrl=$(sysctl net.ipv4.tcp_congestion_control | awk -F ' ' '{print $3}') # todo # StartIOTest GetManagePort GetIpv4Info Check_Virtualization machineNumber="" if [[ $(cat /etc/hostname | cut -d"-" -f 3 | grep -c '^[0-9][0-9]') -gt 0 ]]; then machineNumber=$(cat /etc/hostname | cut -d"-" -f 3) else machineNumber=99 fi export agentServerInfoFile="/octopus-agent/octopus-agent.conf" #cat >/etc/environment.d/octopus-agent.conf <"$agentServerInfoFile" < $agentServerInfoFile"