#!/bin/bash -x

cd /tmp

rm -rf luster-log

mkdir -p luster-log/bootnode
cat /proc/scsi/scsi >luster-log/bootnode/scsi.out
/usr/local/bin/scli -z >luster-log/bootnode/scli.out

if eval ping -c 1 nid00008 2>&1 >/dev/null ; then
    echo looking for lustre log files on nid00008
    mkdir -p luster-log/8
    scp nid00008:/tmp/lustre-log* luster-log/8/
    pdsh -w nid00008 "find /proc/fs/ldiskfs -name mb_history" | dshbak -c
    pdsh -w nid00008 "find /proc/fs/ldiskfs -name mb_history | xargs -t cat 2>&1" | dshbak -c > luster-log/8/ldiskfs_mb_history
    pdsh -w nid00008 "find /proc/fs/lustre/obdfilter -name brw_stats" | dshbak -c
    pdsh -w nid00008 "find /proc/fs/lustre/obdfilter -name brw_stats | xargs -t cat 2>&1" | dshbak -c > luster-log/8/ldiskfs_brw_stats
    pdsh -w nid00008 "lctl dk | sort -t: -k 4 -n > /tmp/dump_lctl"
    scp nid00008:/tmp/dump_lctl luster-log/8/
    pdsh -w nid00008 "cat /proc/scsi/scsi" >luster-log/8/scsi.out
    pdsh -w nid00008 "/usr/local/bin/scli -z" >luster-log/8/scli.out
else
    echo
    echo nid00008 did not respond to a 'ping', skipping
    echo
fi

if eval ping -c 1 nid00011 2>&1 >/dev/null ; then
    echo looking for lustre log files on nid00011
    mkdir -p luster-log/11
    scp nid00011:/tmp/lustre-log* luster-log/11/
    pdsh -w nid00011 "find /proc/fs/ldiskfs -name mb_history" | dshbak -c
    pdsh -w nid00011 "find /proc/fs/ldiskfs -name mb_history | xargs -t cat 2>&1" | dshbak -c > luster-log/11/ldiskfs_mb_history
    pdsh -w nid00011 "find /proc/fs/lustre/obdfilter -name brw_stats" | dshbak -c
    pdsh -w nid00011 "find /proc/fs/lustre/obdfilter -name brw_stats | xargs -t cat 2>&1" | dshbak -c > luster-log/11/ldiskfs_brw_stats
    pdsh -w nid00011 "lctl dk | sort -t: -k 4 -n > /tmp/dump_lctl"
    scp nid00011:/tmp/dump_lctl luster-log/11/
    pdsh -w nid00011 "cat /proc/scsi/scsi" >luster-log/11/scsi.out
    pdsh -w nid00011 "/usr/local/bin/scli -z" >luster-log/11/scli.out
else
    echo
    echo nid00011 did not respond to a 'ping', skipping
    echo
fi

if eval ping -c 1 nid00012 2>&1 >/dev/null ; then
    echo looking for lustre log files on nid00012
    mkdir -p luster-log/12
    scp nid00012:/tmp/lustre-log* luster-log/12/
    pdsh -w nid00012 "find /proc/fs/ldiskfs -name mb_history" | dshbak -c
    pdsh -w nid00012 "find /proc/fs/ldiskfs -name mb_history | xargs -t cat 2>&1" | dshbak -c > luster-log/12/ldiskfs_mb_history
    pdsh -w nid00012 "find /proc/fs/lustre/obdfilter -name brw_stats" | dshbak -c
    pdsh -w nid00012 "find /proc/fs/lustre/obdfilter -name brw_stats | xargs -t cat 2>&1" | dshbak -c > luster-log/12/ldiskfs_brw_stats
    pdsh -w nid00012 "lctl dk | sort -t: -k 4 -n > /tmp/dump_lctl"
    scp nid00012:/tmp/dump_lctl luster-log/12/
    pdsh -w nid00012 "cat /proc/scsi/scsi" >luster-log/12/scsi.out
    pdsh -w nid00012 "/usr/local/bin/scli -z" >luster-log/12/scli.out
else
    echo
    echo nid00012 did not respond to a 'ping', skipping
    echo
fi

if eval ping -c 1 nid00015 2>&1 >/dev/null ; then
    echo looking for lustre log files on nid00015
    mkdir -p luster-log/15
    scp nid00015:/tmp/lustre-log* luster-log/15/
    pdsh -w nid00015 "find /proc/fs/ldiskfs -name mb_history" | dshbak -c
    pdsh -w nid00015 "find /proc/fs/ldiskfs -name mb_history | xargs -t cat 2>&1" | dshbak -c > luster-log/15/ldiskfs_mb_history
    pdsh -w nid00015 "find /proc/fs/lustre/obdfilter -name brw_stats" | dshbak -c
    pdsh -w nid00015 "find /proc/fs/lustre/obdfilter -name brw_stats | xargs -t cat 2>&1" | dshbak -c > luster-log/15/ldiskfs_brw_stats
    pdsh -w nid00015 "lctl dk | sort -t: -k 4 -n > /tmp/dump_lctl"
    scp nid00015:/tmp/dump_lctl luster-log/15/
    pdsh -w nid00015 "cat /proc/scsi/scsi" >luster-log/15/scsi.out
    pdsh -w nid00015 "/usr/local/bin/scli -z" >luster-log/15/scli.out
else
    echo
    echo nid00015 did not respond to a 'ping', skipping
    echo
fi

for FILE in $(find luster-log -type f -print | grep lustre-log-nid); do
    lctl df ${FILE} > ${FILE}.lctl
    rm -f ${FILE}
done

find luster-log -type d -print | xargs chmod 755
find luster-log -type f -print | xargs chmod 644
ls -lR luster-log
rm -f luster-log.tar.gz
tar cvf luster-log.tar luster-log
chmod 644 luster-log.tar
ls -l luster-log.tar
gzip luster-log.tar
chmod 644 luster-log.tar.gz
ls -l luster-log.tar.gz
gunzip -c luster-log.tar.gz | tar tvf -
