需求:
对于linux(EDA)用户,我们会开启disk quota配额,来防止用户过度消耗存储资源,但是一旦用户超额或即将超额会导致disk满无法进行作业,我们通过配置disk quota的告警提示消息,自动告知用户进行disk清理
解决思路:
- 脚本获取存储(netapp,isilon)配额超限用户,USERID,LIMIT,USED,PATH信息,
- yum install -y sshpass
- Isilon上需开启soft limt功能,通过exceeded命令自动筛选出用户
- netapp上需要通过循环命令检索出用户
- 脚本, user_quota_exceeded_list.sh
- 获取超额用户列表,用户已登录则执行notify-send脚本,
- 脚本,quota_notify.sh
- 配置该用户的DISPLAY变量,以便notify-send发送弹窗通知
- 脚本,user_quota_notify.sh
- 统计超额用户quota生成列表,并在服务器上推送
- 脚本,main.sh
- 效果展示:
main.sh
#!/bin/bash
#统计超额用户quota生成列表
/xxx/storage_quota_notify/user_quota_exceeded_list.sh > /xxx/storage_quota_notify/quota_exceeded_list_verbose
grep ‘user’ /xxx/storage_quota_notify/quota_exceeded_list_verbose | awk ‘{printf “%-20s %-20s %-20s %-20s %-20s\n”,$1,$2,$3,$4,$5}’ > /xxx/storage_
quota_notify/quota_exceeded_list
#执行脚本quota_notify脚本
/xxx/storage_quota_notify/quota_notify.sh
user_quota_exceeded_list.sh
#!/bin/bash
# SSH连接信息
username=”xxx”
password=”xxxx”
netapp_host=”xxx”
isilon_host=”xxx”
# 循环替换百分比值并执行命令
for i in {91..99}; do
# netapp quota命令
netapp_quota_command=”quota report -disk-used-pct-disk-limit ${i}%”
# 使用sshpass执行SSH命令
output1=$(sshpass -p “$password” ssh $username@$netapp_host “$netapp_quota_command”| awk ‘{print $2, $3, $1, $5, $4}’)
# 输出每次执行的结果
echo “磁盘使用百分比为 ${i}% 的结果:”
echo “$output1”
echo “————————————————“
done
# isilon quota命令
isilon_quota_command=”isi quota list –exceeded –no-footer -v”
# 使用sshpass执行SSH命令
output2=$(sshpass -p “$password” ssh $username@$isilon_host “$isilon_quota_command” | awk ‘{print $1, $2, $3, $5, $12}’| awk ‘{gsub(“/ifs/”, “”); print}’)
# 输出每次执行的结果
echo “isilon 磁盘quota使用的结果:”
echo “$output2”
echo “————————————————“
quota_notify.sh
#!/bin/bash
#获取超额用户列表
user_list=$(awk ‘{print $2}’ /xxx/storage_quota_notify/quota_exceeded_list)
for user in $user_list;do
#用户已登录则执行脚本
who |grep $user &>>/dev/null && su $user -c “/xxx/storage_quota_notify/user_quota_notify.sh”
done
user_quota_notify.sh
#!/bin/bash
#配置该用户的DISPLAY变量,以便notify-send发送弹窗通知
export DISPLAY=$(who |grep $USER |tail -n1 |grep -oP ‘\(\K[^)]+’)
#发送DISPLAY通知
grep $USER /xxx/storage_quota_notify/quota_exceeded_list |awk ‘{print “Your directory ” $3 ” has exceeded the quota” “(Limit:” $4 “, Used:” $5 “). Please clean up the useless files in this directory in time, otherwise writing will be prohibited.”}’ |xargs -i notify-send -u critical “Storage quota exceeded” “
{}”