#!/bin/sh

# Copyright C 2002-2013 by Isilon Systems LLC. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
#
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
#
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems.
#
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

#set -x

get_idi_logs_for_job()
{
  echo "Running job <$jobid>"
  isi_for_array "mkdir -p $workdir"
  if [ ! -z "$reset" ]
  then
    echo "Moving the logs"
    isi_for_array "newsyslog -F /var/log/idi.log"
    isi_for_array "mv /var/log/idi.log.* $workdir"
  else
    echo "Copying the logs"
    isi_for_array "cp /var/log/idi.log.* $workdir"
    isi_for_array "cp /var/log/idi.log $workdir"
  fi
  mkdir -p $jobdir

  isi_for_array -s "ls $workdir/idi.log.*.gz 2>/dev/null" >$jobdir/listlog 2>/dev/null
  isi_for_array -s "ls $workdir/idi.log.[0-9] 2>/dev/null" >>$jobdir/listlog 2>/dev/null
  isi_for_array -s "ls $workdir/idi.log 2>/dev/null" >>$jobdir/listlog 2>/dev/null

  isi_for_array -s "zcat $workdir/idi.log.*.gz 2>/dev/null" >$jobdir/log 2>/dev/null
  isi_for_array -s "cat $workdir/idi.log.[0-9] 2>/dev/null" >>$jobdir/log 2>/dev/null
  isi_for_array -s "cat $workdir/idi.log 2>/dev/null" >>$jobdir/log 2>/dev/null

  touch $jobdir/done
  echo "Ran $jobid in $workdir to $jobdir"
}

clean_old_reports()
{
  echo "Cleaning up old reports"
  echo isi_for_array "rm -rf $varlog/idi/* 2>/dev/null"
  isi_for_array "rm -rf $varlog/idi/*"
  echo rm -rf $ifsvar/idi/*
  rm -rf $ifsvar/idi/*
}

process_log()
{
  echo "Process Log"
  sed 's/.*\(\[IDI_VERIFY=.*\]\).*/\1/'
}

parse_lins()
{
  while read cnt lin
  do
    if [ "$lin" != "None" ] && [ "$lin" != "1" ]
    then
      #echo lin=$lin $cnt
      file=`isi get -L $lin | sed 's/.* //' 2>/dev/null` 
      if [ -n "$file" ]
      then
        echo $file corrupted $cnt times
        echo "$lin $file" >> $log.linfiles
        isi get -DDD $file > $log.$lin.isiget
      else
        echo $lin not found, corrupted $cnt times
        echo "$lin NA" >> $log.linfiles
      fi
    fi
  done
}

process_lins()
{
  sed 's/LIN=//' | parse_lins
}

process_linsnaps()
{
  sed 's/LINSNAP=//' | parse_lins
}

process_baddrs()
{
  echo "Process BADDRs"
  tot=0
  echo "Baddr Report"
  while read lin baddr
  do
    ((tot+=1))
    echo $baddr was reported
  done
  echo
  echo Total $tot BADDRs were reported
  echo
}

show_report()
{
  log=$1

  #echo "Show report $log"
  #egrep 'IDI_VERIFY=' $log |
  #process_log > $log.repo

  cat $log |
  /usr/libexec/isilon/isi_iscan_log_process> $log.repo

  cat $log.repo | grep  'LINSNAP=' |
  sed 's/.*\(LINSNAP=[^|]*\) |.*/\1/' | sort | uniq -c > $log.linsnaps
  cat $log.repo | grep  'LIN=' |
  sed 's/.*\(LIN=[^|]*\) |.*/\1/' | sort | uniq -c > $log.lins
  cat $log.repo | grep  'SIN=' |
  sed 's/.*\(SIN=[^|]*\) |.*/\1/' | sort | uniq -c > $log.sins
  cat $log.repo | grep  'BADDR=' |
  sed 's/.*\(BADDR=[^|]*\) |.*/\1/' | sort | uniq > $log.baddrs
  cat $log.repo | grep  'SHADDR=' |
  sed 's/.*\(SHADDR=[^|]*\) |.*/\1/' | sort | uniq > $log.shaddrs
  cat $log.repo | grep  'BSET=' |
  sed 's/.*\(BSET=[^|]*\) |.*/\1/' | sort | uniq > $log.bsets

  echo "Process LINs"
  echo "Lin Report"
  echo -n "" > $log.linfiles

  cat $log.lins | process_lins
  cat $log.linsnaps | process_linsnaps

  tot=`wc -l $log.linfiles`
  echo
  echo Total $tot LINs were reported
  echo
}

report_is()
{
  echo "Reporting IS"
  get_idi_logs_for_job
  if [ ! -z "$reset" ]
  then
    reset_idi_logs
  fi
  show_report $jobdir/log
}

show_usage()
{
  echo "Usage: $0"
  echo "isi_iscan_report is a tool to collate the errors from IDI log"
  echo "files generated on different nodes. It generates a report file "
  echo "which can be used as an input to 'isi_iscan_query' tool. "
  echo "Additionally, it reports the number of errors seen for each "
  echo "file containing IDI errors."
  echo ""
  echo "At the end of the run, a report file can be found at "
  echo "/ifs/.ifsvar/idi/tjob.<pid>/log.repo. Use this as the input file "
  echo "for 'isi_iscan_query'"
}

getargs()
{
  echo "Init"
  ifsvar=/ifs/.ifsvar
  varlog=/var/log
  echo "Processing args $*"
  jobid=
  reset=
  while getopts ":rcj:l:" opt; do
    case $opt in
      r)
        #echo "-r was triggered!"
        reset=1
        ;;
      j)
        #echo "-j was triggered!"
        jobid=job.$OPTARG
        ;;
      c)
        #echo "-c was triggered!"
        clean_old_reports
        exit
        ;;
      l)
        echo "-l was triggered!"
        show_report $OPTARG
        exit
        ;;
      *)
        show_usage
        exit
        ;;
    esac
  done
  if [ -z "$jobid" ]
  then
    jobid=tjob.$$
    echo "Setting jobid=$jobid"
  fi
  jobdir=$ifsvar/idi/$jobid
  workdir=$varlog/idi/$jobid
}

main()
{
  echo "Integrity Scan Report"

  #If the second option does not begin with "-", then getopts cannot use it
  if [ ! -z "$args"  -a "$(echo $args | head -c 1)" != "-" ]
  then
    show_usage
    exit
  fi

  getargs $args
  cd
  report_is
}

args=$*
main
