获取DM7000H存储卷性能数据

需求:每5分钟查询获取DM7000H存储172.20.52.30、172.20.52.40卷性能数据,并保存到文件中,每天保存一个文件。

#####优化前脚本内容#####

cat get_storage_stats.sh

#!/usr/bin/expect

set LOG_FILE “/app/log_storage/[exec date +%Y%m%d]_storage_stats.log”

log_file -a $LOG_FILE

spawn ssh admin@172.20.52.30

expect “Password:”

send “V0iCe#Len0325\r”

expect “*>”

send “statistics volume show -sort-key instance_name -iterations 1\r”

expect “*>”

send “exit\r”

spawn ssh admin@172.20.52.40

expect “Password:”

send “V0iCe#Len0425\r”

expect “*>”

send “statistics volume show -sort-key instance_name -iterations 1\r”

expect “*>”

send “exit\r”

log_file

system “chmod 644 $LOG_FILE”

#####优化后脚本内容(密码单独存放)#####

1. 创建密码文件

创建一个仅 root 或当前用户可读的文件(例如 /app/.storage_pwd):

# 文件格式:IP 密码

172.20.52.30 V0iCe#Len0325

172.20.52.40 V0iCe#Len0425

设置严格权限:chmod 600 /app/.storage_pwd

2. 优化后的脚本(通过读取外部文件获取密码)

#!/usr/bin/expect

# 读取密码文件的辅助过程

proc get_password {ip} {

    set fh [open “/app/.storage_pwd” r]

    while {[gets $fh line] >= 0} {

        # 假设文件每行格式为 “IP Password”

        if {[string match “$ip *” $line]} {

            close $fh

            # 返回 IP 后面的部分作为密码

            return [lindex $line 1]

        }

    }

    close $fh

    return “”

}

set LOG_FILE “/app/log_storage/[exec date +%Y%m%d]_storage_stats.log”

log_file -a $LOG_FILE

# — 节点 1 —

set target_ip “172.20.52.30”

set pwd [get_password $target_ip]

spawn ssh admin@$target_ip

expect “Password:”

send “$pwd\r”

expect “*>”

send “statistics volume show -sort-key instance_name -iterations 1\r”

expect “*>”

send “exit\r”

# — 节点 2 —

set target_ip “172.20.52.40”

set pwd [get_password $target_ip]

spawn ssh admin@$target_ip

expect “Password:”

send “$pwd\r”

expect “*>”

send “statistics volume show -sort-key instance_name -iterations 1\r”

expect “*>”

send “exit\r”

log_file

system “chmod 644 $LOG_FILE”