一、Readme:
1、下面的脚本,是jenkins通过ftp上传到每台pbx的 /root/stability路径下,pbx执行需要用的脚本
2、 para.txt ,是在jenkins构建时,jenkins在PBX上下发命令生成的,生成路径也在 /root/stability ;无需特地准备 para.txt
3、如果想在csv中多记录数据,那么在
stabilitycsv.sh脚本中自定义添加你要记录的txt文件,然后在记录csv的脚本中自定义添加你要增加的记录内容即可
4、如下脚本中有些小细节跟注意事项,搜索关键词 “小细节” “注意事项”
二、具体脚本及一些细节、注意事项
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之间正常
- Linux 中shell 脚本if判断多个条件:https://blog.csdn.net/weixin_37569048/article/details/80039941
#!/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
linux下shell的for语句:
https://www.cnblogs.com/EasonJim/p/8315939.html
#!/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
1、需要对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
注意点:
1、该脚本用的是expect ,所以
首行是 #!/usr/bin/expect ;而不是 #!/bin/sh
2、要用 ./ftpget.sh的 格式运行,所以这个脚本要
赋予执行权限 755
3、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"