Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # NOTE: Before submission, change default/nargs values!!!!!!!
- # This script detects backdating in a CSV log
- # To view script input and options use -h
- __author__ = 'XX'
- __date__ = '20190419'
- __version__ = '1.0'
- # Import the necessary packages
- import argparse
- import os
- from dateutil.parser import parse
- # Declare variable to count detections of backdating
- detection_counter = 0
- # Read commandline arguments
- ap = argparse.ArgumentParser()
- ap.add_argument("lf", nargs='?', default='logfile.csv',
- help='Specify file path to CSV log to check')
- ap.add_argument('-n', type=int, default=0,
- help='To sort log file first, provide column number of column that contains the record numbers')
- ap.add_argument('-t', type=int, default=3,
- help='Column number of column that contains record timestamps. Default is column 1')
- ap.add_argument('-s', action='store_true',
- help='Set flag if log has a header')
- args = vars(ap.parse_args())
- # Create non-array variables with values of parsed arguments
- fp_log_file = args['lf']
- column_seq_nr = args['n']
- column_timestamp = args['t']
- header = args['s']
- # Determine first record in the list
- if header:
- first_record_in_list = 1
- else:
- first_record_in_list = 0
- # Log file file path sanity checks
- if fp_log_file is not None and not os.path.isfile(fp_log_file):
- print("ERROR: Log file specified does not exist.")
- exit(1)
- if not fp_log_file.endswith('.csv'):
- print("ERROR: Log file specified is not a .csv file.")
- exit(1)
- # Copy entries from the log file to an array
- log_file = open(fp_log_file)
- record_list = log_file.read().splitlines()
- log_file.close()
- # When required, sort log file on sequence number (does not work yet)
- if column_seq_nr != 0:
- log_list = sorted(record_list, key=lambda row: float(row[column_seq_nr - 1].strip(',')))
- print('INFO: Log sorted on column', column_seq_nr)
- # Get first record in list for the first check in the loop
- previous_record = record_list[first_record_in_list]
- print('INFO: Checking if timestamps increase in column', column_timestamp)
- # Loop through entries in the log file, start at second record
- for record in record_list[first_record_in_list + 1:]:
- # Timestamp column validity check
- try:
- timestamp = parse(record.split(',')[column_timestamp - 1])
- previous_timestamp = parse(previous_record.split(',')[column_timestamp - 1])
- except ValueError:
- print('ERROR: Column', column_timestamp, 'does not contain valid timestamp values')
- exit(1)
- # Check for the actual backdating
- if timestamp < previous_timestamp:
- detection_counter += 1
- print('[', detection_counter, '] BACKDATING DETECTED')
- print('Clock was set back between following records:')
- print(previous_record)
- print(record)
- # Safe current record to check next record against
- previous_record = record
- # Print summary
- if detection_counter == 0:
- print('DONE: NO BACKDATING DETECTED')
- exit(0)
- else:
- print('DONE: BACKDATING WAS DETECTED', detection_counter, 'TIMES')
- exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement