Skip to content

Commit

Permalink
Update the log collection policy of Fabric && expand functions
Browse files Browse the repository at this point in the history
  • Loading branch information
doubletao318 committed Aug 5, 2020
1 parent d5b7b46 commit 643912f
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 118 deletions.
233 changes: 137 additions & 96 deletions scripts/project002/collect/collectLog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,129 +5,170 @@
#time: 2020-03-17
############################################################################################

echo "**********************Clollect Switch Log***************************"
echo "** Type "1" for collect logs using supportsave. ******"
echo "** Type "2" for collect logs using supportshow. ******"
echo "** Type "3" quit. ******"
echo "********************************************************************"

switch_user='admin'
#操作类型,支持“save”和“show”,分别对应supportsave和supportshow
collectType=$1
#交换机IP地址
switchIP=$2
#交换机访问账号,如果参数为空,则默认值为admin
switch_user=$3
#交换机访问密码信息,可从堡垒机获取
switch_pwd=''
logs_path='/home/switchLogs'
ftp_save_path='/backup/switchsavelogs'

#定义sftp服务器信息
sftp_IP=''
sftp_User=''
sftp_Pass=''

#定义在sftp服务器侧的show交换机日志文件根目录,需要手动创建
logs_path_show='/backup/switchLogs/show'
#定义在sftp服务器侧的save交换机日志文件根目录,需要手动创建
logs_path_save='/backup/switchLogs/save'
#定义本次任务执行的具体目录,实际该目录由脚本自动创建(交换机IP地址+日期信息)
logs_path=''

#获取系统时间,用于追加在日志目录
time=$(date "+%Y%m%d_%H-%M-%S")
#定义使用supportshow获取的日志文件名称
logFile="${time}_supportshowLog.log"

readOperationType()
current_Path=$(pwd)

verifyPara()
{

while true
do

read -p "Please input Operation type[1,2,3]: " operation
#判断日志收集类型合法性
if [[ "${collectType}" != "save" ]] && [[ "${collectType}" != "show" ]]
then
echo "Operation type is invalid."
exit 1
fi

if [ "$operation" = 1 ]
then
collectLogUsingSave
break

elif [ "$operation" = 2 ]
then
collectLogUsingShow
break

elif [ "$operation" = 3 ]
#判断IP地址合法性
ip=${switchIP}
echo $ip
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
then
ip=(${ip//\./ })
if ! [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
then
exit
else
echo "Operation Type Error"
echo "Switch ip: $switchIP is invalid."
exit 1
fi

done
else
echo "Switch ip: $switchIP is invalid."
exit 1
fi

#如果参数中账号为空,则默认使用admin账号
if [[ "${switch_user}" == "" ]]
then
switch_user='admin'
fi
}

readSwitchIP()
checkresult()
{
local ip
while true
do
read -p "Please input IP Address: " switchIP
ip=${switchIP}
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
then
ip=(${ip//\./ })
if [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
then
break
else
echo "$switchIP is not valid"
fi
else
echo "$switchIP is not valid"
fi
done
if [[ $? != 0 ]]; then
echo $1
exit 1
fi
}

getswpwd()
{
# 调用客户的SDK,从堡垒机获取交换机鉴权信息
# java路径替换为环境上的路径
# Test.jar替换为客户的可执行jar
# $@为命令行参数:ip username
result=`/opt/oss/rtsp/jre-2.22.12.1/bin/java -jar Test.jar $1 ${switch_user}`

code=""
msg=""

# 从result变量中逐行读取信息
while read -r line
do
# 如果该行以‘code:’开始,则提取code
if [[ "$line" == "code:"* ]]
then
code=${line#code:}
continue
fi
# 调用客户的SDK,从堡垒机获取交换机鉴权信息
# java路径替换为环境上的路径
# Test.jar替换为客户的可执行jar
# $@为命令行参数:ip username
result=`/opt/oss/rtsp/jre-9.50.8.2/bin/java -jar /yunwei/usp4o.jar $1 $2`
local pass

code=""
msg=""

# 如果该行以‘msg:’开始,则提取msg
if [[ "$line" == "msg:"* ]]
# 从result变量中逐行读取信息
while read -r line
do
# 如果该行以‘code:’开始,则提取code
if [[ "$line" == "code:"* ]]
then
code=${line#code:}
continue
fi

# 如果该行以‘msg:’开始,则提取msg
if [[ "$line" == "msg:"* ]]
then
msg=${line#msg:}
continue
fi

# 如果该行以‘passwd:’开始,则提取passwd
if [[ "$line" == "passwd:"* ]]
then
pass=${line#passwd:}
continue
fi
done <<<"$result"

# 如果code为非‘0000’,则退出执行
if [[ "$code" != "0000" ]]
then
msg=${line#msg:}
continue
echo "Get password from bastion host failed. Code:$code, msg:$msg"
exit 1
fi

# 如果该行以‘passwd:’开始,则提取passwd
if [[ "$line" == "passwd:"* ]]
then
switch_pwd=${line#passwd:}
continue
echo ${pass}
}

getSftpServerPwd()
{
if [[ -z ${sftp_Pass} ]]; then
sftp_Pass=`getswpwd ${sftp_IP} ${sftp_User}`
checkresult "Get sftp password from bastion host failed."
fi
done <<<"$result"

# 如果code为非‘0000’,则退出执行
if [[ "$code" != "0000" ]]
then
echo "{\"result\":\"fail\",\"msg\":\"code:"$code", msg:"$msg"\"}"
exit 1
fi
}

getSwtichPwd()
{
switch_pwd=`getswpwd ${switchIP} ${switch_user}`
checkresult "Get sftp password from bastion host failed."
}

collectLogUsingSave()
{
readSwitchIP
switch_pwd=`getswpwd`
expect collectLog_Save.exp ${switchIP} ${switch_user} ${switch_pwd} ${ftp_save_path}
getSwtichPwd
expect collectLog_Save.exp ${switch_user} ${switch_pwd} ${switchIP} ${sftp_User} ${sftp_Pass} ${sftp_IP} ${logs_path}

exit 0
}

collectLogUsingShow()
{
readSwitchIP
createPath
switch_pwd=`getswpwd`
expect collectLog_Show.exp ${switchIP} ${switch_user} ${switch_pwd} > ${logs_path}/${switchIP}_${logFile}
getSwtichPwd
expect collectLog_Show.exp ${switchIP} ${switch_user} ${switch_pwd} > ${current_Path}/${switchIP}_${logFile}

expect send_ShowFile.exp ${sftp_IP} ${sftp_User} ${sftp_Pass} ${current_Path} ${logs_path} ${switchIP}_${logFile}

rm ${switchIP}_${logFile}

exit 0
}
createPath()

collectSwitchLog()
{
logs_path=${logs_path}/${switchIP}_${time}
mkdir ${logs_path}
verifyPara
getSftpServerPwd

if [[ ${collectType} == "save" ]]
then
logs_path=${logs_path_save}/${switchIP}_${time}
expect create_Path.exp ${sftp_IP} ${sftp_User} ${sftp_Pass} ${logs_path}
collectLogUsingSave
else
logs_path=${logs_path_show}/${switchIP}_${time}
expect create_Path.exp ${sftp_IP} ${sftp_User} ${sftp_Pass} ${logs_path}
collectLogUsingShow
fi
}
readOperationType

collectSwitchLog
19 changes: 9 additions & 10 deletions scripts/project002/collect/collectLog_Save.exp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
#function: Collect Brocade Switch Logs using supportsave
#time: 2020-03-17
############################################################################################
if {$argc < 3} {
send_user "Usage: expect collectLog_Save.exp switchIP switch_user switch_pwd.\n"
if {$argc < 7} {
send_user "Usage: expect collectLog_Save.exp switch_user switch_pwd switchIP ftp_user ftp_pwd ftp_ip ftp_path.\n"
exit
}

set switch_IP [lindex $argv 0]
set switch_user [lindex $argv 1]
set switch_pwd [lindex $argv 2]
set ftp_path [lindex $argv 3]

set ftp_user
set ftp_pwd
set ftp_ip
set switch_user [lindex $argv 0]
set switch_pwd [lindex $argv 1]
set switch_IP [lindex $argv 2]
set ftp_user [lindex $argv 3]
set ftp_pwd [lindex $argv 4]
set ftp_ip [lindex $argv 5]
set ftp_path [lindex $argv 6]

set timeout -1;
spawn ssh -o StrictHostKeyChecking=no ${switch_user}@${switch_IP}
Expand Down
22 changes: 22 additions & 0 deletions scripts/project002/collect/create_Path.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/expect
############################################################################################
#Program name: createPath.exp
#function: Create path for logs
#time: 2020-03-17
############################################################################################
if {$argc < 4} {
send_user "Usage: expect createPath.exp sftp_IP sftp_User sftp_Pass logs_path.\n"
exit
}

set sftp_IP [lindex $argv 0]
set sftp_User [lindex $argv 1]
set sftp_Pass [lindex $argv 2]
set log_Path [lindex $argv 3]

set timeout -1;
spawn sftp -o StrictHostKeyChecking=no ${sftp_User}@${sftp_IP}
expect "*assword:" {send "${sftp_Pass}\r"}
expect "*sftp>" {send "mkdir ${log_Path}\r"}
expect "*sftp>" {send "bye\r"}
exit 0
26 changes: 26 additions & 0 deletions scripts/project002/collect/send_ShowFile.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/expect
############################################################################################
#Program name: send_ShowFile.exp
#function: Send supportshow file to sftp
#time: 2020-03-17
############################################################################################
if {$argc < 6} {
send_user "Usage: expect send_ShowFile.exp sftp_IP sftp_User sftp_Pass local_Path sftp_Path log_File.\n"
exit
}

set sftp_IP [lindex $argv 0]
set sftp_User [lindex $argv 1]
set sftp_Pass [lindex $argv 2]
set local_Path [lindex $argv 3]
set sftp_Path [lindex $argv 4]
set log_File [lindex $argv 5]

set timeout -1;
spawn sftp -o StrictHostKeyChecking=no ${sftp_User}@${sftp_IP}
expect "*assword:" {send "${sftp_Pass}\r"}
expect "*sftp>" {send "cd ${sftp_Path}\r"}
expect "*sftp>" {send "lcd ${local_Path}\r"}
expect "*sftp>" {send "put ${log_File}\r"}
expect "*sftp>" {send "bye\r"}
exit 0
Loading

0 comments on commit 643912f

Please sign in to comment.