主要使用SAR命令(sysstat):执行一次动态采样,获取对应资源的数据
拼接为json串,发送给server端
#!/bin/bash host_id=17 #host_id=1 json="{\"host_id\":\"$host_id\","; cpu=$(sar -u 1 1|awk 'END {print $3}') json="$json\"cpu\":\"$cpu\"," memory=$(sar -r 1 1|awk 'END {print $8}') json="$json\"memory\":\"$memory\"," n=0; sign=1; json="$json\"io\":[" for line in `sar -d -p 1 1|grep -E "秒|Average"|sed '1d'|awk '{print $2,$3,$8}'` do if [ $sign -eq 1 ] && [ $n -eq 0 ];then json="$json{\"name\":\"$line\"," n=$((${n}+1)) elif [ $sign -eq 0 ] && [ $n -eq 0 ];then json="$json,{\"name\":\"$line\"," n=$((${n}+1)) elif [ $sign -eq 0 ] && [ $n -eq 1 ];then json="$json\"tps\":\"$line\"," n=$((${n}+1)) elif [ $sign -eq 0 ] && [ $n -eq 2 ];then json="$json\"await\":\"$line\"}" n=0 fi sign=0; done json="$json]}" curl -H "Content-Type: application/json" -X POST --data "$json" http://10.50.16.201:8090/interface/gethostper
展示: