David Bainbridge | cfd7ca1 | 2019-10-06 03:31:41 +0000 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # Copyright 2019 Ciena Corporation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | # This script reads RAW log data collected from the PODS and combines |
| 17 | # this RAW data into a rolloing log file. |
| 18 | |
| 19 | RAW_LOG_DIR=${RAW_LOG_DIR:-./logger/raw} |
| 20 | COMBINED_LOG_DIR=${COMBINED_LOG_DIR:-./logger/combined} |
| 21 | ROLL_AT=${ROLL_AT:-100M} |
| 22 | MAX_LOG_COUNT=${MAX_LOG_COUNT:-5} |
| 23 | PERIOD=${PERIOD:-60} |
| 24 | |
| 25 | # === END OF CONFIGURATION === |
| 26 | |
| 27 | # Convert the ROLL_AT value to byes |
| 28 | ROLL_AT=$(numfmt --from=iec $ROLL_AT) |
| 29 | |
| 30 | # Ensure the combined directory exists |
| 31 | mkdir -p $COMBINED_LOG_DIR |
| 32 | |
| 33 | # Get a working area |
| 34 | WORK=$(mktemp) |
| 35 | |
| 36 | # forever ... |
| 37 | while true; do |
| 38 | |
| 39 | # Iterate over all existing raw entries |
| 40 | for MERGE_DIR in $(ls $RAW_LOG_DIR); do |
| 41 | echo "Merging from $RAW_LOG_DIR/$MERGE_DIR ..." |
| 42 | |
| 43 | # Iterate over each file in the RAW data directory |
| 44 | for FILE_PATH in $(ls $RAW_LOG_DIR/$MERGE_DIR/*.log); do |
| 45 | |
| 46 | # Get the base of the log file |
| 47 | FILE=$(basename $FILE_PATH) |
| 48 | |
| 49 | # Find destination log file with largest index, if none |
| 50 | # exists this will end up with IDX == 1 |
| 51 | IDX=2 |
| 52 | while [ -f $COMBINED_LOG_DIR/$FILE.$(printf "%04d" $IDX) ]; do |
| 53 | IDX=$(expr $IDX + 1) |
| 54 | done |
| 55 | IDX=$(expr $IDX - 1) |
| 56 | |
| 57 | # Get the NAME of the log file to write |
| 58 | NAME=$COMBINED_LOG_DIR/$FILE.$(printf "%04d" $IDX) |
| 59 | |
| 60 | # different behavior if the file exists or not |
| 61 | if [ -f $NAME ]; then |
| 62 | |
| 63 | # if the file exists, check the size of the file and see |
| 64 | # if we need to move to the next index |
| 65 | SIZE=$(stat -c %s $NAME) |
| 66 | if [ $SIZE -gt $ROLL_AT ]; then |
| 67 | |
| 68 | # Combine the exists log file with the new data, this will |
| 69 | # have double entires for the overlap, then run it through |
| 70 | # uniq -D | sort -u to only end up with the overlap entries |
| 71 | cat $NAME $FILE_PATH | sort | uniq -D | sort -u > $WORK |
| 72 | |
| 73 | # time to move |
| 74 | IDX=$(expr $IDX + 1) |
| 75 | |
| 76 | # If the nex IDX is great than the max log file count thebn |
| 77 | # we shift each log file to index - 1, losing the first (.1) |
| 78 | # forever ... |
| 79 | if [ $IDX -gt $MAX_LOG_COUNT ]; then |
| 80 | echo " Shifting log files for $FILE ..." |
| 81 | I=1 |
| 82 | while [ $I -lt $MAX_LOG_COUNT ]; do |
| 83 | rm -f $COMBINED_LOG_DIR/$FILE.$(printf "%04d" $I) |
| 84 | mv $COMBINED_LOG_DIR/$FILE.$(printf "%04d" $(expr $I + 1)) $COMBINED_LOG_DIR/$FILE.$(printf "%04d" $I) |
| 85 | I=$(expr $I + 1) |
| 86 | done |
| 87 | |
| 88 | # Reset the IDX to the MAX |
| 89 | IDX=$MAX_LOG_COUNT |
| 90 | fi |
| 91 | |
| 92 | # |
| 93 | NAME=$COMBINED_LOG_DIR/$FILE.$(printf "%04d" $IDX) |
| 94 | echo " Creating new log file $NAME ..." |
| 95 | |
| 96 | # Combine the list of overlap entries (WORK), i.e. the ones |
| 97 | # that need to be removed from the new data set with the |
| 98 | # new data set so that there will be 2 of each of the overlaps. |
| 99 | # then pipe that to uniq -u to return only the uniq entries. |
| 100 | # this has the affect of removing the overlap from the new |
| 101 | # data set |
| 102 | cat $WORK $FILE_PATH | sort | uniq -u > $NAME |
| 103 | chmod 644 $NAME |
| 104 | else |
| 105 | # Not rolling so a simple combining of the new data set |
| 106 | # with the existing file and sorting to only unique entries |
| 107 | # will do |
| 108 | cat $NAME $FILE_PATH | sort -u > $WORK |
| 109 | rm -f $NAME |
| 110 | mv $WORK $NAME |
| 111 | chmod 644 $NAME |
| 112 | fi |
| 113 | else |
| 114 | # The destination log file does not exist, so just sort the |
| 115 | # the RAW data into the file. This really only happens on the |
| 116 | # first iteration |
| 117 | sort -u $FILE_PATH > $NAME |
| 118 | chmod 644 $NAME |
| 119 | fi |
| 120 | done |
| 121 | |
| 122 | # Remove the RAW data directory so we don't try to merge it again |
| 123 | rm -rf $RAW_LOG_DIR/$MERGE_DIR |
| 124 | done |
| 125 | echo "=====" |
| 126 | echo "Sleep for $PERIOD seconds ..." |
| 127 | sleep $PERIOD |
| 128 | done |