#!/bin/bash -x

cd /tmp

rm -rf lustre-log

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

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

if eval ping -c 1 nid00008 2>&1 >/dev/null ; then
    echo looking for lustre log files on nid00008
    mkdir -p lustre-log/8
    scp nid00008:/tmp/lustre-log* lustre-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 > lustre-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 > lustre-log/8/ldiskfs_brw_stats
    pdsh -w nid00008 "lctl dk | sort -t: -k 4 -n > /tmp/dump_lctl"
    scp nid00008:/tmp/dump_lctl lustre-log/8/
    pdsh -w nid00008 "cat /proc/scsi/scsi" >lustre-log/8/scsi.out
    pdsh -w nid00008 "/usr/local/bin/scli -z" >lustre-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 lustre-log/11
    scp nid00011:/tmp/lustre-log* lustre-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 > lustre-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 > lustre-log/11/ldiskfs_brw_stats
    pdsh -w nid00011 "lctl dk | sort -t: -k 4 -n > /tmp/dump_lctl"
    scp nid00011:/tmp/dump_lctl lustre-log/11/
    pdsh -w nid00011 "cat /proc/scsi/scsi" >lustre-log/11/scsi.out
    pdsh -w nid00011 "/usr/local/bin/scli -z" >lustre-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 lustre-log/12
    scp nid00012:/tmp/lustre-log* lustre-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 > lustre-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 > lustre-log/12/ldiskfs_brw_stats
    pdsh -w nid00012 "lctl dk | sort -t: -k 4 -n > /tmp/dump_lctl"
    scp nid00012:/tmp/dump_lctl lustre-log/12/
    pdsh -w nid00012 "cat /proc/scsi/scsi" >lustre-log/12/scsi.out
    pdsh -w nid00012 "/usr/local/bin/scli -z" >lustre-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 lustre-log/15
    scp nid00015:/tmp/lustre-log* lustre-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 > lustre-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 > lustre-log/15/ldiskfs_brw_stats
    pdsh -w nid00015 "lctl dk | sort -t: -k 4 -n > /tmp/dump_lctl"
    scp nid00015:/tmp/dump_lctl lustre-log/15/
    pdsh -w nid00015 "cat /proc/scsi/scsi" >lustre-log/15/scsi.out
    pdsh -w nid00015 "/usr/local/bin/scli -z" >lustre-log/15/scli.out
else
    echo
    echo nid00015 did not respond to a 'ping', skipping
    echo
fi

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

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

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

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

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

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

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

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