#! /bin/bash

# ------------------------------------------------------------------
# FIA driver TCAM driver dump file wrangler script
#
# August  2018, Edward Conger
#
# Copyright (c) 2018 by Cisco Systems, Inc.
# All rights reserved.
#------------------------------------------------------------------


#  CSCvk64963 Fretta Jericho KBP dump logs filling up misc/scratch
# 
# wants:
# 
#    Wants fewer-of/compression-of any kbp_dump_<unit> timestamp files
#     in /misc/scratch.
# 
#  "In the case of kbp abort, the KBP library creates a dump file which
#   saves the device state.  In CSCvk52996,the size of each file is 84 M:
# 
#     % ls -la kbp_dump_1_\ 7252018_161232 
#          87008978 Jul 25 16:33 kbp_dump_1_ 7252018_161232
# 
#   It creates 8 of them for Zermatt.  Also the way the file name is
#   constructed is unique everytime as it concats date and time to the file
#   name.  This makes it overfill /misc/scratch very fast as older dumps
#   aren't cleaned up/overwritten.
# 
#   The same partition is used by dumper to move cores from LC to RP.
#   The absence of space prevents getting a usable core from the dumper.
# 
#   Want to have only the latest dumps from KBP and these are zipped up
#   and moved elsewhere before KBP aborts.  
#   Zipping the above file reduces it from 84M to 12M."
# 


# KBP code did _not_ ensure month-number would have two digits
#  and hence no leading space in the filename(!)  Take care....
#
# # E.g.
#    "./kbp_dump_0_12242018_235959"  ...
#    "./kbp_dump_2_ 7221934_180002"  ....
#



## WORKDIR=/tmp/misc/scratch # for our offworld UT...
WORKDIR=/misc/scratch


MAX_ASICS=$1   # Passed down from fia driver

PAT="kbp_dump"
TSTAMP="tstamp.${PAT}"
TGZNAME=collected.${PAT}.tgz


cd ${WORKDIR}

/bin/touch ${TSTAMP}

/bin/ls -t ${PAT}* 2>/dev/null | \
  /usr/bin/head -${MAX_ASICS}  | \
    /bin/tar zcvvf ${TGZNAME} -T - ${TSTAMP}

/bin/echo
/bin/rm -vf ${PAT}* ${TSTAMP}


exit 0

#

# %: pwd
# /tmp/misc/scratch
#
# %: ls -lt
# total 31500
# -rw-r--r-- 1 root root 1380947 Aug 14 16:33 some_newer_file
# -rw-r--r-- 1 root root 3581459 Aug 14 03:04 kbp_dump_0_12242018_235959
# -rw-r--r-- 1 root root 3581459 Aug 14 03:04 kbp_dump_1_12242018_235958
# -rw-r--r-- 1 root root 3581459 Aug 14 03:04 kbp_dump_2_12242018_235957
# -rw-r--r-- 1 root root 3581459 Aug 14 03:04 kbp_dump_3_12242018_235956
# -rw-rw-r-- 1 root root       0 Jun 27 13:48 some_middling_file
# -rw-r--r-- 1 root root 3581459 Apr 23 13:39 kbp_dump_0_ 7221934_180000
# -rw-r--r-- 1 root root 3581459 Apr 23 13:39 kbp_dump_1_ 7221934_180001
# -rw-r--r-- 1 root root 3581459 Apr 23 13:39 kbp_dump_2_ 7221934_180002
# -rw-r--r-- 1 root root 3581459 Apr 23 13:39 kbp_dump_3_ 7221934_180003
# -rw-rw-r-- 1 root root       0 Nov 21  2017 some_older_file
#
# % kbp_driver_dump_file_wrangler 4
# 
# -rw-r--r-- root/root  3581459 2018-08-14 03:04 kbp_dump_0_12242018_235959
# -rw-r--r-- root/root  3581459 2018-08-14 03:04 kbp_dump_1_12242018_235958
# -rw-r--r-- root/root  3581459 2018-08-14 03:04 kbp_dump_2_12242018_235957
# -rw-r--r-- root/root  3581459 2018-08-14 03:04 kbp_dump_3_12242018_235956
# -rw-rw-r-- root/root        0 2018-08-14 17:58 tstamp.kbp_dump
#
# removed 'kbp_dump_0_ 7221934_180000'
# removed 'kbp_dump_0_12242018_235959'
# removed 'kbp_dump_1_ 7221934_180001'
# removed 'kbp_dump_1_12242018_235958'
# removed 'kbp_dump_2_ 7221934_180002'
# removed 'kbp_dump_2_12242018_235957'
# removed 'kbp_dump_3_ 7221934_180003'
# removed 'kbp_dump_3_12242018_235956'
# removed 'tstamp.kbp_dump'
#
# %

