Shell脚本采集基础监控项数据(含采集周期)
生活随笔
收集整理的這篇文章主要介紹了
Shell脚本采集基础监控项数据(含采集周期)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
每10秒為周期,輸出一次采集的數據;
第一次,獲取相關數據,寫入文件中;
第二次以后,從文件中獲取前一次的數據,并將這次的數據替換進文件中,并計算此次和前一次的差值,并輸出
#!/bin/bashSNIC_DATA_FILE='/home/cenquanyu/bbc_snic_data_10.txt'# CPU數據收集 function cpu_idle_w_cal() {# CPU空閑率core=${1}total=0cpu_stat=`cat /proc/stat | head -${core}`for i in {2..9}; doarr[$i]=`echo $cpu_stat | awk -v t="${i}" '{print $t}'`if [ $i -eq 5 ]; thenidle=$((arr[$i]))fitotal=$((total + arr[$i]))done# core=1代表總的cpu核的數據if [ $core -eq 1 ]; thenecho cpu_total:$total >> $SNIC_DATA_FILEecho cpu_idle:$idle >> $SNIC_DATA_FILEelse# 每個核的數據,core要減2,當前的core代表行數,-2后為核數core=$((core-2))echo cpu_${core}_total:$total >> $SNIC_DATA_FILEecho cpu_${core}_idle:$idle >> $SNIC_DATA_FILEfi } # 第一次數據寫入文件中 function cpu_core_idle_w() {cpu_core_num=$(cat /proc/stat | grep 'cpu' | wc -l)a=1# 每個核的數據寫入文件中while ((a <= cpu_core_num)); docpu_idle_w_cal $a((a++))done } # 從文件中讀出數據并計算 function cpu_idle_r_cal() {# CPU空閑率core=${1}total=0cpu_stat=`cat /proc/stat | head -${core}`for i in {2..9}; doarr[$i]=`echo $cpu_stat | awk -v t="${i}" '{print $t}'`if [ $i -eq 5 ]; thenidle=$((arr[$i]))fitotal=$((total + arr[$i]))donecpu_idle_2=$idlecpu_total_2=$totalif [ $core -eq 1 ]; thencpu_idle_1=`cat "${SNIC_DATA_FILE}" | grep cpu_idle | grep -Po "(\d+(\.\d+)?)"`cpu_total_1=`cat "${SNIC_DATA_FILE}" | grep cpu_total | grep -Po "(\d+(\.\d+)?)"`elsecorenum=$((core-2))cpu_idle_1=`cat "${SNIC_DATA_FILE}" | grep "cpu_${corenum}_idle" | grep -Po "(\d+(\.\d+)?)" | sed -n '2p'`cpu_total_1=`cat "${SNIC_DATA_FILE}" | grep "cpu_${corenum}_total" | grep -Po "(\d+(\.\d+)?)" | sed -n '2p'`ficpu_idle_val=$((cpu_idle_2-cpu_idle_1))cpu_total=$((cpu_total_2-cpu_total_1))cpu_idle=`awk 'BEGIN{printf "%.3f\n",'$cpu_idle_val'/'$cpu_total'}'`if [ $core -eq 1 ]; then# 讀出數據的同時,替換對應行的值sed -i 's/^.*'cpu_idle'.*$/cpu_idle:'${cpu_idle_2}'/' $SNIC_DATA_FILEsed -i 's/^.*'cpu_total'.*$/cpu_total:'${cpu_total_2}'/' $SNIC_DATA_FILEecho cpu_idle:$cpu_idleelsesed -i 's/^.*'cpu_${corenum}_idle'.*$/'cpu_${corenum}_idle:${cpu_idle_2}'/g' $SNIC_DATA_FILEsed -i 's/^.*'cpu_${corenum}_total'.*$/'cpu_${corenum}_total:${cpu_total_2}'/g' $SNIC_DATA_FILEecho cpu_${corenum}_idle:$cpu_idlefi }function cpu_core_idle_r() {cpu_core_num=`cat /proc/stat | grep 'cpu' | wc -l`b=1# 讀出每個核的數據while ((b <= cpu_core_num)); docpu_idle_r_cal $b((b++))done }function cpu_server_loadavg() {# CPU負載--最近一分鐘,最近五分鐘,最近十五分鐘cpu_server_loadavg_1=$(cat /proc/loadavg | awk '{print $1}')cpu_server_loadavg_5=$(cat /proc/loadavg | awk '{print $2}')cpu_server_loadavg_15=$(cat /proc/loadavg | awk '{print $3}')echo cpu_server_loadavg_1:$cpu_server_loadavg_1echo cpu_server_loadavg_5:$cpu_server_loadavg_5echo cpu_server_loadavg_15:"$cpu_server_loadavg_15" }# 內存數據收集 function mem_data_collect() {# 內存空閑量-MEM_FREE,內存總量-MEM_TOTAL,內存使用量-MEM_USED,內存使用率-MEM_USED_PERCENTmem_total=$(cat /proc/meminfo | grep 'MemTotal' | awk '{print $2}')mem_buffers=$(cat /proc/meminfo | grep 'Buffers' | awk '{print $2}')mem_cached=$(cat /proc/meminfo | grep 'Cached' | head -1 | awk '{print $2}')mem_free=$((mem_free + mem_buffers + mem_cached))mem_used=$((mem_total - mem_free))mem_used_percent=$(awk 'BEGIN{printf "%.2f\n",'$mem_used'/ '$mem_total'}')echo mem_free:$mem_freeecho mem_total:$mem_totalecho mem_used:$mem_usedecho mem_used_percent:$mem_used_percent}# 磁盤數據收集 function disk_data_collect() {num=`df -BK | grep -v 'none\|nodedev\|tmpfs\|devtmpfs' | awk '{print $2}' | tail -n +2 | grep -Po "(\d+(\.\d+)?)" | sed '/^$/d' | wc -l`total_info=`df -BK | grep -v 'none\|nodedev\|tmpfs\|devtmpfs' | awk '{print $2}' | tail -n +2 | grep -Po "(\d+(\.\d+)?)"`free_info=`df -BK | grep -v 'none\|nodedev\|tmpfs\|devtmpfs' | awk '{print $4}' | grep -Po "(\d+(\.\d+)?)"`used_info=`df -BK | grep -v 'none\|nodedev\|tmpfs\|devtmpfs' | awk '{print $3}' | grep -Po "(\d+(\.\d+)?)"`while ((i <= num)); dodisk_used_size=`echo $used_info | awk -v t="${i}" '{print $t}'`disk_total_used=$((disk_total_used + disk_used_size))disk_free_size=`echo $used_info | awk -v t="${i}" '{print $t}'`disk_total_free=$((disk_total_free + disk_free_size))disk_total_size=`echo $total_info | awk -v t="${i}" '{print $t}'`disk_total_total=$((disk_total_total + disk_total_size))((i++))doneecho disk_total_used:$disk_total_useddisk_total_used_percent=$(awk 'BEGIN{printf "%.3f\n",'$disk_total_used'/ '$disk_total_total'}')echo disk_total_used_percent:$disk_total_used_percent}function net_total_bitps_w() {# 整機網卡總接收比特速率 -- 寫入NIC_NUM=`cat /proc/net/dev | tail -n +3 | grep -v 'lo:' | wc -l` # 網卡數量,除去lo網卡inbit_total_val=`cat /proc/net/dev | tail -n +3 | grep -v 'lo:' | awk '{print $2}'`i=1while ((i <= NIC_NUM)); doinbit_val=`echo $inbit_total_val | awk -v t="${i}" '{print $t}'`echo inbit_nic_name_${i}:$inbit_val >> $SNIC_DATA_FILE((i++))done# 整機網卡總發送比特速率 -- 寫入NIC_NUM=`cat /proc/net/dev | tail -n +3 | grep -v 'lo:' | wc -l`outbit_total_val=`cat /proc/net/dev | tail -n +3 | grep -v 'lo:' | awk '{print $10}'`i=1while ((i <= NIC_NUM)); dooutbit_val=$(echo $outbit_total_val | awk -v t="${i}" '{print $t}')echo outbit_nic_name_${i}:$outbit_val >> $SNIC_DATA_FILE((i++))done }function net_total_bitps_r() {# 整機網卡總接收比特速率 -- 讀取NIC_NUM=`cat /proc/net/dev | tail -n +3 | grep -v 'lo:' | wc -l`inbit_total_val=`cat /proc/net/dev | tail -n +3 | grep -v 'lo:' | awk '{print $2}'`i=1while ((i <= NIC_NUM)); doinbit_val_2=`echo $inbit_total_val | awk -v t="${i}" '{print $t}'`inbit_val_1=`cat $SNIC_DATA_FILE | grep inbit_nic_name_${i} | grep -Po "(\d+(\.\d+)?)" | sed -n '2p'`sed -i 's/^.*'inbit_nic_name_${i}'.*$/'inbit_nic_name_${i}:${inbit_val_2}'/g' $SNIC_DATA_FILEinbps_bytes=$((inbit_val_2 - inbit_val_1))net_total_in_bitps=$((net_total_in_bitps + 8 * inbps_bytes / 10))((i++))doneecho net_total_in_bitps:$net_total_in_bitps# 整機網卡總發送比特速率 -- 讀取outbit_total_val=`cat /proc/net/dev | tail -n +3 | grep -v 'lo:' | awk '{print $10}'`i=1while ((i <= NIC_NUM)); dooutbit_val_2=`echo $outbit_total_val | awk -v t="${i}" '{print $t}'`outbps_val_1=`cat $SNIC_DATA_FILE | grep outbit_nic_name_${i} | grep -Po "(\d+(\.\d+)?)" | sed -n '2p'`sed -i 's/^.*'outbit_nic_name_${i}'.*$/'outbit_nic_name_${i}:${outbit_val_2}'/g' $SNIC_DATA_FILEoutbps_bytes=$((outbit_val_2-outbps_val_1))net_total_out_bitps=$((net_total_out_bitps + 8 * outbps_bytes / 10))((i++))doneecho net_total_out_bitps:$net_total_out_bitps }if [ ! -f $SNIC_DATA_FILE ]; thentouch $SNIC_DATA_FILEecho 1 > $SNIC_DATA_FILEcpu_core_idle_wnet_total_bitps_w# 寫完內容,更改首行的1為2sed -i '1s/1/2/' $SNIC_DATA_FILE elsecpu_core_idle_rcpu_server_loadavgmem_data_collectdisk_data_collectnet_total_bitps_r fi?
每60秒為周期,輸出一次采集的數據;
第一次,獲取相關數據,寫入文件中;
第二次以后,從文件中獲取前一次的數據,并將這次的數據替換進文件中,并計算此次和前一次的差值,并輸出
#!/bin/bashSNIC_DATA_FILE='/home/cenquanyu/bbc_snic_data_60.txt' # 網卡相關參數(60s周期) function net_other_w() {# 1.整機網卡總接收包速率NIC_NUM=`cat /proc/net/dev | tail -n +3 | grep -v 'lo:' | wc -l`inpkg_total_val=`cat /proc/net/dev | tail -n +3 | grep -v 'lo:' | awk '{print $3}'`i=1while ((i <= NIC_NUM)); doinpkg_val=`echo $inpkg_total_val | awk -v t="${i}" '{print $t}'`echo inpkg_nic_name_${i}:$inpkg_val >> $SNIC_DATA_FILE((i++))done# 2.整機網卡總發送包速率outpkg_total_val=`cat /proc/net/dev | tail -n +3 | grep -v 'lo:' | awk '{print $11}'`i=1while ((i <= NIC_NUM)); dooutpkg_val=`echo $outpkg_total_val | awk -v t="${i}" '{print $t}'`echo outpkg_nic_name_${i}:$outpkg_val >> $SNIC_DATA_FILE((i++))done# 3.TCP接收包速率tcp_in_segs=`cat /proc/net/snmp | grep 'Tcp:' | tail -1 | awk '{print $11}'`echo tcp_in_segs:$tcp_in_segs >> $SNIC_DATA_FILE# 4.TCP發送包速率tcp_out_segs=`cat /proc/net/snmp | grep 'Tcp:' | tail -1 | awk '{print $12}'`echo tcp_out_segs:$tcp_out_segs >> $SNIC_DATA_FILE# 5.TCP丟包速率tcp_loss=`cat /proc/net/netstat | awk '(f==0) {name=$1; i=2; \while ( i<=NF) {n[i] = $i; i++ };f=1; next} (f==1){ i=2; \while ( i<=NF){ printf "%s%s = %d\n", name, n[i], $i; i++}; f=0}' | grep 'TCPLossFailures' | awk '{print $3}'`echo tcp_loss:$tcp_loss >> $SNIC_DATA_FILE# 6.當前建立的TCP連接數 -- NET_TCP_CURR_ESTABnet_tcp_curr_estab=`cat /proc/net/snmp | grep 'Tcp:' | tail -1 | awk '{print $10}'`echo net_tcp_curr_estab:$net_tcp_curr_estab >> $SNIC_DATA_FILE }function net_other_r() {# 1.整機網卡總接收包速率NIC_NUM=`cat /proc/net/dev | tail -n +3 | grep -v 'lo:' | wc -l`inpkg_total_val=`cat /proc/net/dev | tail -n +3 | grep -v 'lo:' | awk '{print $3}'`i=1while ((i <= NIC_NUM)); doinpkg_val_2=`echo $inpkg_total_val | awk -v t="${i}" '{print $t}'`inpkg_val_1=`cat $SNIC_DATA_FILE | grep inpkg_nic_name_${i} | grep -Po "(\d+(\.\d+)?)" | sed -n '2p'`sed -i 's/^.*'inpkg_nic_name_${i}'.*$/'inpkg_nic_name_${i}:${inpkg_val_2}'/g' $SNIC_DATA_FILEinpkg_val=$((inpkg_val_2 - inpkg_val_1))net_total_in_pkg=$((net_total_in_pkg + inpkg_val / 10))((i++))doneecho net_total_in_pkg:$net_total_in_pkg# 2.整機網卡總發送包速率outpkg_total_val=`cat /proc/net/dev | tail -n +3 | grep -v 'lo:' | awk '{print $11}'`i=1while ((i <= NIC_NUM)); dooutpkg_val_2=`echo $outpkg_total_val | awk -v t="${i}" '{print $t}'`outpkg_val_1=`cat $SNIC_DATA_FILE | grep outpkg_nic_name_${i} | grep -Po "(\d+(\.\d+)?)" | sed -n '2p'`sed -i 's/^.*'outpkg_nic_name_${i}'.*$/'outpkg_nic_name_${i}:${outpkg_val_2}'/g' $SNIC_DATA_FILEoutpkg_val=$((outpkg_val_2 - outpkg_val_1))net_total_out_pkg=$((net_total_out_pkg + outpkg_val / 10))((i++))doneecho net_total_out_pkg:$net_total_out_pkg# 3.TCP接收包速率tcp_in_segs_2=`cat /proc/net/snmp | grep 'Tcp:' | tail -1 | awk '{print $11}'`tcp_in_segs_1=`cat $SNIC_DATA_FILE | grep tcp_in_segs | grep -Po "(\d+(\.\d+)?)"`sed -i 's/^.*'tcp_in_segs'.*$/'tcp_in_segs:${tcp_in_segs_2}'/g' $SNIC_DATA_FILEtcp_in_segs=$((tcp_in_segs_2 - tcp_in_segs_1))net_tcp_in_segs=$((tcp_in_segs / 60))echo net_tcp_in_segs:$net_tcp_in_segs# 4.TCP發送包速率tcp_out_segs_2=`cat /proc/net/snmp | grep 'Tcp:' | tail -1 | awk '{print $12}'`tcp_out_segs_1=`cat $SNIC_DATA_FILE | grep tcp_out_segs | grep -Po "(\d+(\.\d+)?)"`sed -i 's/^.*'tcp_out_segs'.*$/'tcp_out_segs:${tcp_out_segs_2}'/g' $SNIC_DATA_FILEtcp_out_segs=$((tcp_out_segs_2 - tcp_out_segs_1))net_tcp_out_segs=$((tcp_out_segs / 60))echo net_tcp_out_segs:$net_tcp_out_segs# 5.TCP丟包速率period=60tcp_loss_2=`cat /proc/net/netstat | awk '(f==0) {name=$1; i=2; \while ( i<=NF) {n[i] = $i; i++ };f=1; next} (f==1){ i=2; \while ( i<=NF){ printf "%s%s = %d\n", name, n[i], $i; i++}; f=0}' | grep 'TCPLossFailures' | awk '{print $3}'`tcp_loss_1=`cat $SNIC_DATA_FILE | grep tcp_loss | grep -Po "(\d+(\.\d+)?)"`sed -i 's/^.*'tcp_loss'.*$/'tcp_loss:${tcp_loss_2}'/g' $SNIC_DATA_FILEtcp_loss_val=$(((tcp_loss_2 - tcp_loss_1)))net_tcp_loss=`awk 'BEGIN{printf "%.3f\n",'$tcp_loss_val'/ '$period'}'`echo net_tcp_loss:$net_tcp_loss }if [ ! -f $SNIC_DATA_FILE ]; thentouch $SNIC_DATA_FILEecho 1 > $SNIC_DATA_FILEnet_other_w# 寫完內容,更改首行的1為2sed -i '1s/1/2/' $SNIC_DATA_FILE elsenet_other_r fi總結
以上是生活随笔為你收集整理的Shell脚本采集基础监控项数据(含采集周期)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: The History Began fr
- 下一篇: Uni-app APP开发、适配指北