Skip to content

Instantly share code, notes, and snippets.

@suconghou
Last active April 23, 2017 10:57
Show Gist options
  • Save suconghou/c4d26f069997a2f0a87b035fb47b6c11 to your computer and use it in GitHub Desktop.
Save suconghou/c4d26f069997a2f0a87b035fb47b6c11 to your computer and use it in GitHub Desktop.
get server status
#!/bin/bash
# config
apiUrl="http://127.0.0.1:9090"
# 内网IP
if hash ip 2>/dev/null; then
localIp=$(ip addr | grep inet | grep -v inet6 | grep -v 127.0.0.1 | awk '{print $2}' | awk -F '/' '{print $1}' | tr '\n' ' ' )
elif hash ifconfig 2>/dev/null; then
localIp=$(ifconfig | grep inet | grep -v inet6 | grep -v 127.0.0.1 | awk '{print $2}' | tr '\n' ' ' )
else
localIp="Unknow"
fi
if hash curl 2>/dev/null; then
str=$(curl -s myip.ipip.net)
else
echo >&2 "I require curl but it's not installed. Aborting."; exit 1;
fi
# 外网IP
publicIp=$(echo $str | awk -F ":" '{print $2}'|awk '{print $1}')
# 物理地址
location=$(echo $str | awk -F ":" '{print $3}')
uptimeStr=`uptime`
# 在线时间
upDays=$(echo $uptimeStr | awk '{match($0,/up\s+(.*),.*user/,a);print a[1]}')
# 机器名字
hostName=`hostname -s`
cpu=$(grep 'model name' /proc/cpuinfo |uniq |awk -F : '{print $2}' |sed 's/^[ \t]*//g' |sed 's/ \+/ /g')
# CPU个数
physicalNumber=$(grep "physical id" /proc/cpuinfo|sort -u|wc -l)
# 物理核心数
coreNumber=$(grep "cpu cores" /proc/cpuinfo|uniq|awk -F':' '{print $2}'|xargs)
# 逻辑CPU核心数
logicalNumber=$(grep "processor" /proc/cpuinfo|sort -u|wc -l)
# CPU 负载
loadAvg=$(echo $uptimeStr | awk -F "load average: " '{print $2}')
# 内存占用/内存大小
memInfo=$(free -m | awk 'NR==2{printf "%s/%s", $3,$2 }')
# 系统内核
kenVersion=`uname -sr`
# tcp接通的连接数
conn=$(netstat -nt | grep "ESTABLISHED" | wc -l)
# 系统进程总量
process=$(ps -e | wc -l)
ID=$(echo $hostName,$cpu,$physicalNumber,$coreNumber,$logicalNumber,$kenVersion | md5sum | awk '{print $1}')
filePath="/etc/redhat-release"
if [ -f "$filePath" ] ; then
osVersion=$(cat $filePath)
elif [ -e '/usr/bin/lsb_release' ] ; then
osVersion=$(lsb_release -d | awk -F "Description:" '{gsub(/^\s+/, "", $2); print $2}')
elif [ -f '/etc/os-release' ]; then
osVersion=$(cat /etc/os-release | grep "PRETTY_NAME" | awk -F '"' '{print $2}')
elif [ -f '/etc/issue' ]; then
osVersion=$(awk -F '\\\\n' '{print $1}' /etc/issue)
else
osVersion="Unknow"
fi
data="{\"ID\":\"$ID\",\"hostName\":\"$hostName\",\"localIp\":\"$localIp\",\"publicIp\":\"$publicIp\",\"location\":\"$location\",\"upDays\":\"$upDays\",\"cpu\":\"$cpu\",\"coreNumber\":\"$coreNumber\",\"physicalNumber\":\"$physicalNumber\",\"logicalNumber\":\"$logicalNumber\",\"loadAvg\":\"$loadAvg\",\"memInfo\":\"$memInfo\",\"conn\":\"$conn\",\"process\":\"$process\",\"osVersion\":\"$osVersion\",\"kenVersion\":\"$kenVersion\"}"
curl -H "Content-Type: application/json" -X POST -d "$data" $apiUrl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment