代码拉取完成,页面将自动刷新
#!/bin/bash
#########################################################################
# File Name: status.sh
# Author: nian
# Blog: https://whoisnian.com
# Mail: zhuchangbao1998@gmail.com
# Created Time: 2017年10月30日 星期一 19时29分39秒
#########################################################################
#
# hostname
#
hostname=${HOSTNAME}
#
# ip addr
#
ipv4_addr=$(curl -s ipv4.whoisnian.com)
ipv6_addr=$(curl -s ipv6.whoisnian.com)
#
# uptime
#
uptime=$(awk '{printf "%d", $1}' /proc/uptime)
min=$((${uptime} / 60 % 60))
hour=$((${uptime} / 3600 % 24))
day=$((${uptime} / 86400))
uptime="${min}m"
if [ "${hour}" -ne 0 ]; then
uptime="${hour}h ${uptime}"
fi
if [ "${day}" -ne 0 ]; then
uptime="${day}d ${uptime}"
fi
#
# disk usage
#
disk_info=$(df -h -x devtmpfs -x tmpfs --total)
disk_name=($(awk 'NR > 1 {print $1}' <<< ${disk_info}))
disk_total=($(awk 'NR > 1 {print $2}' <<< ${disk_info}))
disk_used=($(awk 'NR > 1 {print $3}' <<< ${disk_info}))
disk_usage=($(awk 'NR > 1 {print $5}' <<< ${disk_info}))
disk_mount=($(awk 'NR > 1 {print $6}' <<< ${disk_info}))
#
# memory
#
# MemUsed = Memtotal + Shmem - MemFree - Buffers - Cached - SReclaimable
mem_info=$(</proc/meminfo)
mem_total=$(awk '$1=="MemTotal:" {print $2}' <<< ${mem_info})
mem_used=$((${mem_total} + $(awk '$1=="Shmem:" {print $2}' <<< ${mem_info})))
mem_used=$((${mem_used} - $(awk '$1=="MemFree:" {print $2}' <<< ${mem_info})))
mem_used=$((${mem_used} - $(awk '$1=="Buffers:" {print $2}' <<< ${mem_info})))
mem_used=$((${mem_used} - $(awk '$1=="Cached:" {print $2}' <<< ${mem_info})))
mem_used=$((${mem_used} - $(awk '$1=="SReclaimable:" {print $2}' <<< ${mem_info})))
mem_total=$((mem_total / 1024))
mem_used=$((mem_used / 1024))
mem_usage=$((100 * ${mem_used} / ${mem_total}))
#
# cpunum
#
cpunum=$(grep -c '^processor' /proc/cpuinfo)
#
# load average
#
load_average=$(uptime | grep -Eo '\average:.*' | awk '{print $2,$3,$4}')
#
# json
#
echo '{
"hostname": "'$hostname'",
"ipv4_addr": "'$ipv4_addr'",
"ipv6_addr": "'$ipv6_addr'",
"uptime": "'$uptime'",
"disk_usage": ['
for ((i=0;i<${#disk_name[*]};i++))
do
if [ $i -gt 0 ]
then
echo ','
fi
echo -e ' {
"disk_name": "'${disk_name[$i]}'",
"disk_total": "'${disk_total[$i]}'",
"disk_used": "'${disk_used[$i]}'",
"disk_usage": "'${disk_usage[$i]}'",
"disk_mount": "'${disk_mount[$i]}'"
}\c'
done
echo '
],
"mem_usage": {
"mem_total": "'$mem_total'MiB",
"mem_used": "'$mem_used'MiB",
"mem_usage": "'$mem_usage'%"
},
"cpunum": "'$cpunum'",
"load_average": "'$load_average'"
}'
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。