用于验证 Linux 服务器中挂载点状态的 Shell 脚本

用于验证 Linux 服务器中挂载点状态的 Shell 脚本

我需要一个应该按如下方式工作的脚本。

一个脚本,它将验证该特定安装点当前是否已安装在服务器上。它将搜索安装点名称,/etc/fstab然后从df -h输出进行验证或/proc/mounts检查其是否安装在服务器上(如果您有更好的方法来验证也可以)。

重新启动后,如果未安装,则会触发电子邮件。

对于一台服务器来说还可以,但这将用于验证超过 1000 台服务器,因此脚本将是更好的解决方案。

因此,该脚本将在一台服务器上执行,并检查另一台 1000 台服务器中的挂载点状态。

服务器中的挂载点名称为/mount1、等/mount2/mount3我们只需验证该特定挂载点名称即可,可以忽略其他操作系统相关的 FS。

到目前为止我所拥有的:

#!/bin/bash

# Grep for word mountpoint name ie "mount" 
awk '{print $2}' /etc/fstab | grep -i "mount" > mntpoint.txt
exec 3< mntpoint.txt

while read mount <&3
do
# Search for present mountpoint in file /prod/mounts.
# I'm using /proc/mounts here to validate

grep -iw $mount /proc/mounts > /dev/null

if [ $? -eq 0 ]
then
    echo $mount "is mounted"
else
    echo $mount "is not mounted needs manual intervention"
fi
done

答案1

我建议在 python 中尝试这个。内置的 os.path 模块有一个非常简单的 ismount 功能。

$ cat ismount.py 
import os
mp = '/mount1'
if os.path.ismount(mp):
    print('{0} is mounted'.format(mp))
else:
    print('{0} is NOT mounted'.format(mp))
$ python ismount.py 
/mount1 is NOT mounted

答案2

尝试这样的事情。首先创建一个服务器 IP 列表(假设您设置了无密码 ssh 并且可以以 root 身份连接到所有服务器),然后运行这个小脚本(复制粘贴到命令行中):

while read ip;
do
    echo "connecting to $ip";
    ssh root@$ip "until mount | grep -w \"$MOUNT\" >/dev/null;
     do echo mounting \"$MOUNT\"; mount \"$MOUNT\"; sleep 1; done && 
     echo Mounted on $ip"
done < ips.txt

这需要一个文件,ips.txt每行调用一个 IP。它将ssh进入 IP,并且当$MOUNT未安装挂载时,它将尝试这样做。确保替换$MOUNT为您感兴趣的安装点,它应该完全像它一样出现/etc/fstab。例如:

while read ip;
do
    echo "connecting to $ip";
    ssh root@$ip "until mount | grep -w \"/mnt/data\" >/dev/null;
     do echo mounting \"/mnt/data\"; mount \"/mnt/data\"; sleep 1; done && 
     echo Mounted on $ip"
done < ips.txt

答案3

#!/bin/bash

A=``awk '{print $2}' /etc/fstab | grep -i "^/" | egrep -v '/etc/fstab|proc|sys|shm|pts`'`

`for i in $A; do
grep $i /proc/mounts > /dev/null`

`if [ $? -eq 0 ]; then
    echo $i "is mounted"
else
    echo $i "is not mounted needs manual intervention"
fi
done`

答案4

当您将 for 循环中的 grep 更改为:

grep "$i " /proc/mounts.

在这种情况下,它不会返回误报。

相关内容