• Uncategorized

About linux : Shell-Script-to-Check-if-Linux-Storage-is-Accessible-on-Linux-Host

Question Detail

I have a list of Linux hosts and I want do a script that will check if a a bunch of Linux storages are mounted and accessible on each of these hosts and then echo if the storage was not accissble and on which host. The best way I can think of doing this is using pdsh -w ^hostslist "command" so my script will look something like this:

#!/bin/sh

storages="s1 s2 s3 s4" 
host=`hostname`

for storage in ${storages}; do 
pdsh -w ^hosts "cd /$storage" 2 > &1 #here I want to save the output of what I get from running this command 

Then in the same script, check if it the file output I got is empty, then read it and grep for “no such file or directory” and if the error message was found, then echo that the directory doesn’t exist on that host. So it will be something like this

if [ -s 1 ] 
then
error=`cat 1 | grep "no such file or direcotry`
  if [ -n $error] 
  then 
     echo "there is something wrong with $storage in $host" 
  fi 
fi

But I am not sure if this is the best approach to check if a storage is accessible on a certain host. Is this the best approach, or is there a better way of doing this?

Question Answer

Since you have a list of hosts and seems that you want to do a task in all of them by using pdsh, I would suggest giving a try to ansible

A very basic implementation would be to first create your inventory create a file named my-inventory with your real addresses, for example:

[my-hosts]
10.10.1.1
10.10.1.2
10.10.1.3
10.10.1.4

Then to check only if a file exists you could create this basic playbook a file check-if-storage-exists.yml with this contents:

---
- hosts: my-hosts

  tasks:
    - name: Check if file exists
      stat:
        path: /storage
      register: st

    - debug:
        msg: "path /storage doesn't exist"
      when: not st.stat.exists

Then give a try like this:

ansible-playbook -i my-inventory check-if-storage-exists.yml

It may look a little overkilling but it may worth give a try since you could do much more things, for example, you could use the mount module, etc.

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.