From 643912f808996c48ec991862ef9f4c254724d2d4 Mon Sep 17 00:00:00 2001 From: doubletao318 Date: Tue, 4 Aug 2020 20:36:07 -0700 Subject: [PATCH] Update the log collection policy of Fabric && expand functions --- scripts/project002/collect/collectLog.sh | 233 ++++++++++-------- .../project002/collect/collectLog_Save.exp | 19 +- scripts/project002/collect/create_Path.exp | 22 ++ scripts/project002/collect/send_ShowFile.exp | 26 ++ scripts/project002/todo/executeTodo.sh | 35 ++- scripts/project002/todo/executeTodoGroup.py | 37 ++- 6 files changed, 254 insertions(+), 118 deletions(-) create mode 100644 scripts/project002/collect/create_Path.exp create mode 100644 scripts/project002/collect/send_ShowFile.exp diff --git a/scripts/project002/collect/collectLog.sh b/scripts/project002/collect/collectLog.sh index 1ad5d55..2152464 100644 --- a/scripts/project002/collect/collectLog.sh +++ b/scripts/project002/collect/collectLog.sh @@ -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 \ No newline at end of file + +collectSwitchLog \ No newline at end of file diff --git a/scripts/project002/collect/collectLog_Save.exp b/scripts/project002/collect/collectLog_Save.exp index a474efd..5e8c4ef 100644 --- a/scripts/project002/collect/collectLog_Save.exp +++ b/scripts/project002/collect/collectLog_Save.exp @@ -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} diff --git a/scripts/project002/collect/create_Path.exp b/scripts/project002/collect/create_Path.exp new file mode 100644 index 0000000..cf8f2a0 --- /dev/null +++ b/scripts/project002/collect/create_Path.exp @@ -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 \ No newline at end of file diff --git a/scripts/project002/collect/send_ShowFile.exp b/scripts/project002/collect/send_ShowFile.exp new file mode 100644 index 0000000..d1e7eed --- /dev/null +++ b/scripts/project002/collect/send_ShowFile.exp @@ -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 \ No newline at end of file diff --git a/scripts/project002/todo/executeTodo.sh b/scripts/project002/todo/executeTodo.sh index 526e58a..81adc6f 100644 --- a/scripts/project002/todo/executeTodo.sh +++ b/scripts/project002/todo/executeTodo.sh @@ -1,9 +1,9 @@ #!/bin/bash # please modify USER、IP、PORT、USP4O -USER="gatewayUser" -IP="" -PORT="26335" -USP4O="/yunwei/usp4o.jar" +export API_USER=gatewayUser +export IP="" +export PORT="26335" +export USP4O="/yunwei/usp4o.jar" TODO_TASK_NAME=$1 @@ -17,13 +17,32 @@ then exit 1 fi -PASSWORD=`su ossadm -c "source /opt/oss/manager/bin/engr_profile.sh;$JAVA_HOME/bin/java -jar ${USP4O} ${IP} ${USER}"` -if [[ $? -ne 0 ]]; then - echo "Query password from bastion host failed." +if [[ -z "${TODO_TASK_NAME}" ]] +then + echo "Todo task group name is empty, Please input todo task group name." exit 1 fi -su ossadm -c "source /opt/oss/manager/bin/engr_profile.sh;python -c \"from executeTodoGroup import TodoExecutor;executor=TodoExecutor('${IP}','${PORT}','${USER}','${PASSWORD}');executor.execute_todo_group_by_name('${TODO_TASK_NAME}')\"" +result=`su ossadm -c 'source /opt/oss/manager/bin/engr_profile.sh;$JAVA_HOME/bin/java -jar ${USP4O} ${IP} ${API_USER}'` +while read -r line +do + if [[ "$line" == "code:"* ]] + then + CODE=${line#code:} + continue + fi + if [[ "$line" == "passwd:"* ]] + then + PASSWORD=${line#passwd:} + continue + fi +done <<<"$result" + +if [[ "$CODE" != "0000" ]]; then + echo "Query password from bastion host failed." + exit 1 +fi +su ossadm -c "source /opt/oss/manager/bin/engr_profile.sh;python -c \"from executeTodoGroup import TodoExecutor;executor=TodoExecutor('${IP}','${PORT}','${API_USER}','${PASSWORD}');executor.execute_todo_group_by_name('${TODO_TASK_NAME}')\"" 2>/dev/null if [[ $? -ne 0 ]]; then echo "Execute todo task failed." exit 1 diff --git a/scripts/project002/todo/executeTodoGroup.py b/scripts/project002/todo/executeTodoGroup.py index 53e4f28..b54687c 100644 --- a/scripts/project002/todo/executeTodoGroup.py +++ b/scripts/project002/todo/executeTodoGroup.py @@ -4,6 +4,7 @@ import urllib.error import urllib.parse import ssl +import time class TodoExecutor: @@ -84,14 +85,42 @@ def _get_group_by_key(self, key, value): def _execute_todo_group(self, group): status = str(group.get("status")) + group_id = group.get("id") if status == '0': - self._todo_group_action(group.get("id"), "confirm") - elif status in ('1', '3'): - self._todo_group_action(group.get("id"), "execute") + self._todo_group_action(group_id, "confirm") + print("Timed task group confirm success.") + elif status == '1': + self._todo_group_action(group_id, "execute") + self._check_result(group_id) else: - print("Todo group status could not be run.") + print("Todo group status could not be executed.") raise Exception + def _check_result(self, group_id): + print("Wait task group finish...") + sec = 0 + while True: + print("Wait task group finish...%ss" % sec) + try: + group = self._get_group_by_key("group_id", group_id) + status = str(group.get("status")) + if status == '3': + print("Task group execute finished.") + break + except Exception: + print("Query task group failed, continue...") + + time.sleep(10) + sec += 10 + + for item in group.get("items", []): + if item.get(status) != 3: + print("Some sub task execution failed, please access " + "DME Storage website for more information.") + raise Exception("Sub task not success.") + + print("Task group was successfully executed.") + def execute_todo_group_by_name(self, group_name): group = self._get_group_by_key("name", group_name) self._execute_todo_group(group)