使用jenkins實現監控嵌入式設備穩定性之二----腳本部分

1、Readme:html

一、下面的腳本,是jenkins經過ftp上傳到每臺pbx的 /root/stability路徑下,pbx執行須要用的腳本
二、 para.txt ,是在jenkins構建時,jenkins在PBX上下發命令生成的,生成路徑也在   /root/stability ;無需特意準備 para.txt
三、若是想在csv中多記錄數據,那麼在 stabilitycsv.sh腳本中自定義添加你要記錄的txt文件,而後在記錄csv的腳本中自定義添加你要增長的記錄內容便可
四、以下腳本中有些小細節跟注意事項,搜索關鍵詞 「小細節」 「注意事項」
 
 
2、具體腳本及一些細節、注意事項
df.sh
df腳本,root/dev  在$devroot下正常
  • awk獲取指定行制定列數據:awk 'NR==2{print}'| awk '{print $5}':第2行第5列
#!/bin/sh

df_max=`cat para.txt | grep 'df_max'|awk -F ":" '{print$2}'`     #dev/root百分值的上限,具體數值在同路徑下的 para.txt中獲取
df > df.txt
root=$(cat df.txt | awk 'NR==2{print}'| awk '{print $5}' | awk -F "%" '{print$1}')  #   awk 'NR==2{print}'| awk '{print $5}':第2行第5列

if [ $root -lt $df_max ]  # [] 跟條件語句之間要加 !!空格!!    ;若是當前dev/root百分值大於上限,打印error
then 
   echo `date`" : dev/root OK"
else
   echo `date`" : dev/root false"
fi

 
top.sh
top腳本,asterisk虛擬內存在minvsz-maxvsz之間正常
#!/bin/sh
asterisk_vsz_max=`cat para.txt | grep 'asterisk_vsz_max'|awk -F ":" '{print$2}'`
asterisk_vsz_min=`cat para.txt | grep 'asterisk_vsz_min'|awk -F ":" '{print$2}'`
top -b n1 > top.txt
vsz=$(cat top.txt |grep 'asterisk -vvv'| awk '{print $5}' | awk -F "m" '{print$1}')
#echo $vsz

if [[ $vsz -gt $asterisk_vsz_min ]] && [[ $vsz -lt $asterisk_vsz_max ]]   # 小細節:if 多條件判斷語句,要兩個[] ;參數與[]之間要有空格
   then
      echo `date`" : asterisk vsz :"$vsz", OK"
   else
      echo `date`" : asterisk vsz :"$vsz", false"
fi
 
cpu.sh
CPU腳本,cpu -c 30內,10個>90%
bash的shell腳本for語句有些不支持C語言的寫法: https://blog.csdn.net/liuqinglong_along/article/details/52191382
#!/bin/sh
#set -xv

echo "" > cpu.txt

cpu_max=` cat para.txt | grep 'cpu_max'|awk -F ":" '{print$2}'`      #cpu的上限 ,具體數值在同路徑下的 para.txt中獲取
time=0
times=$cpu_checktime      #要測試的cpu次數,一秒測試一次打印一次cpu值
errortimes=$cpu_allowerrortime  #容許超過指定cpu上限的次數

for i in $(seq 1 $times)
   do
      echo "`cpu -c 1`" >> cpu.txt
      cpu=$(tac cpu.txt | awk "NR==1" | awk '{print $3}' | awk -F "%" '{print$1}')
#      echo "cpu:"$cpu

      if [ $cpu -gt $cpu_max ]   #若是cpu > 指定值maxcpu,次數+1
        then
             time=$(($time+1))
             echo "time:"$time
         else
             time=$time
       fi

   done


if [ $time -gt $errortimes ]     #若是指定次數maxtime內,cpu超過maxcpu次數大於指定值次數errortime;打印error

  then
       echo `date`" : cpu false"

  else
       echo `date`" : cpu OK"

fi

 
 
memory.sh
檢查內存是否泄漏,是否有降低趨勢:連續5次free降低cach上升,判斷爲異常;單次超過極限,也判斷爲異常 
在首次運行時,建立memory.txt,並把memory.txt分別cp到memory1.txt-memory5.txt
在第二次及以後運行時,最新的寫到memory.txt,以前的memory.txt->memory1.txt,memory1.txt->memory2.txt 以此類推
先判斷當前free、cach值是否超過給定的極限,符合給定極限後再判斷free跟前5次比是否連續降低,cach是否連續上升
#!/bin/sh
#set -xv
free_min=`cat para.txt | grep 'free_min'|awk -F ":" '{print$2}'`  #內存的下限
cach_max=`cat para.txt | grep 'cach_max'|awk -F ":" '{print$2}'`   #cached的上限


#free_min=47084
#cach_max=324000


if [ ! -f "memory.txt" ] #判斷memory.txt是否存在---第一次運行該腳本不存在,若不存在則執行then部分
    then  
        cat /proc/meminfo > memory.txt  
        cp memory.txt memory1.txt 
        cp memory.txt memory2.txt
        cp memory.txt memory3.txt
        cp memory.txt memory4.txt
        cp memory.txt memory5.txt
    
    else break;
fi  

a=0
b=0

memfree=`cat memory.txt | awk 'NR==2{print}' | awk '{print$2}'`
cached=`cat memory.txt  | awk 'NR==4{print}' | awk '{print$2}'`
if [[ $memfree -ge $free_min ]] && [[ $cached -lt $cach_max ]]  #判斷當前的free跟cach是否超過限制範圍,若沒超過則執行then部分進一步判斷

   then 
      mv memory4.txt memory5.txt
      mv memory3.txt memory4.txt      
      mv memory2.txt memory3.txt
      mv memory1.txt memory2.txt
      mv memory.txt memory1.txt
      cat /proc/meminfo > memory.txt
      
      for i in $(seq 1 5)  #當前的free跟cach與memory1.txt到memory5.txt對比
          do 
               memfreelast=`cat memory$i.txt | awk 'NR==2{print}' | awk '{print$2}'`
               #echo $memfreelast
               
               cachedlast=`cat memory$i.txt  | awk 'NR==4{print}' | awk '{print$2}'`
               #echo $cachedlast
               
               if [ $memfree -ge $memfreelast ]
                    then 
                            if [ $cached -le $cachedlast ]
                                 then 
                                     echo `date`" : memory and cached OK"
                                      break
                                 else 
                                      b=$(($b+1))
                             fi
                       
                    else 
                        a=$(($a+1))
                fi                
             
                i=$((i+1))
          done
      
            
     if [ $a -eq 5 ];then
          echo `date`" :  memfree continue leak:false"
     fi

     if [ $b -eq 5 ];then
         echo `date`" :  cached continue leak:false"
     fi
    

   else 
      echo `date`"memory overflow,please chech memfree and cached : false "

fi
 
 
 
stabilitycsv.sh
一、須要對shell執行完輸出的txt文件作些改造:
a、在寫CSV文件時,是用逗號做爲換單元格,
b、若是要讓一個單元格內的內容換行的話,須要將這個單元格的內容都用雙引號括起來,如 "aabb",而後在須要換行的位置寫入換行的轉義字符;shell輸出的txt的換行符是 \n,excel支持的換行符是 \r\n
#!/bin/sh

#top.txt
sed -n '1,20p' top.txt > topsed1.txt   #把top.txt的前1-20行另存爲 topsed1.txt(太多行會破壞格式,因此保留的20行)
sed 's/,/;/g ' topsed1.txt > topsed2.txt  #把topsed1.txt中的 「,」都換成「;」另存到 topsed2.txt中 (由於csv是把「,」當成換單元格,因此把內容中的「,」替換掉以保持格式便於查看)
sleep 1
sed '1i"' topsed2.txt > topsed3.txt   #在topsed2.txt的第一行插入符號"  
sleep 1
sed '$a"' topsed3.txt > topsed4.txt  #在topsed3.txt的最後一行插入符號 "  
sed 'H;$!d;${x;s/^\n//;s/\n/\r\n/g}' topsed4.txt > top_final.txt   # 在topsed4.txt 把換行符由原來的「\n」改爲「\r\n」(excel的換行符是\r\n,這樣在window打開能夠保持top.txt輸出的換行效果)

#若是有其餘txt內容須要改造,按照top.txt這個模式copy改造便可,以下

#df.txt
sed 's/,/;/g ' df.txt > dfsed1.txt
sleep 1
sed '1i"' dfsed1.txt > dfsed2.txt
sleep 1
sed '$a"' dfsed2.txt > dfsed3.txt
sed 'H;$!d;${x;s/^\n//;s/\n/\r\n/g}' dfsed3.txt > df_final.txt

#cpu.txt
sed 's/,/;/g ' cpu.txt > cpu1.txt
sleep 1
sed '1i"' cpu1.txt > cpu2.txt
sleep 1
sed '$a"' cpu2.txt > cpu3.txt
sed 'H;$!d;${x;s/^\n//;s/\n/\r\n/g}' cpu3.txt > cpu_final.txt

#memory.txt
sed 's/,/;/g ' memory.txt > memory1.txt
sleep 1
sed '1i"' memory1.txt > memory2.txt
sleep 1
sed '$a"' memory2.txt > memory3.txt
sed 'H;$!d;${x;s/^\n//;s/\n/\r\n/g}' memory3.txt > memory_final.txt

echo "`date`"",""`cat df_final.txt`"",""`cat top_final.txt`"",""`cat calls_final.txt`"",""`cat memory_final.txt`">> stabilitycsv$PBX_IP.csv  #若是要新加入單元格內容,在此添加,記得","換單元格

 

 
ftp_get.sh
執行命令格式  ./ftpget.sh PBX_IP password   (password參數不填表示密碼爲空)
./ftpget.sh 192.168.101.155 1   :ip爲192.168.101.155,密碼爲1
./ftpget.sh 192.168.101.155      :ip爲192.168.101.155,密碼爲空
用戶名代碼固定爲ls@yf
注意點:
一、該腳本用的是expect ,因此 首行是 #!/usr/bin/expect  ;而不是 #!/bin/sh
二、要用 ./ftpget.sh的 格式運行,因此這個腳本要 賦予執行權限 755
三、expect用是的tcl語法, 註釋跟shell不同,需 在#前加 ;
#!/usr/bin/expect

set PBX_IP [lindex $argv 0]  ;#取自命名後帶的第一個參數值,設置爲鏈接的ip,參數值之間以空格隔開; [lindex $argv 0]表示執行命令行帶的第一個參數值
set password [lindex $argv 1] ;#取自命令後待的第二個參數值,設置爲鏈接的密碼;無該參數值表示密碼爲空
spawn ftp $PBX_IP

expect "Name"
send "root\r"

expect "Password:"
send "$password\r"

expect "ftp>"
send "bin\r"

expect "ftp>"
send "get df.txt\r"

expect "ftp>"
send "get cpu.txt\r"

expect "ftp>"
send "bye\r"
相關文章
相關標籤/搜索