Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. # NOTE: Before submission, change default/nargs values!!!!!!!
  2.  
  3. # This script detects backdating in a CSV log
  4. # To view script input and options use -h
  5.  
  6. __author__ = 'XX'
  7. __date__ = '20190419'
  8. __version__ = '1.0'
  9.  
  10. # Import the necessary packages
  11. import argparse
  12. import os
  13. from dateutil.parser import parse
  14.  
  15. # Declare variable to count detections of backdating
  16. detection_counter = 0
  17.  
  18. # Read commandline arguments
  19. ap = argparse.ArgumentParser()
  20. ap.add_argument("lf", nargs='?', default='logfile.csv',
  21. help='Specify file path to CSV log to check')
  22. ap.add_argument('-n', type=int, default=0,
  23. help='To sort log file first, provide column number of column that contains the record numbers')
  24. ap.add_argument('-t', type=int, default=3,
  25. help='Column number of column that contains record timestamps. Default is column 1')
  26. ap.add_argument('-s', action='store_true',
  27. help='Set flag if log has a header')
  28. args = vars(ap.parse_args())
  29.  
  30. # Create non-array variables with values of parsed arguments
  31. fp_log_file = args['lf']
  32. column_seq_nr = args['n']
  33. column_timestamp = args['t']
  34. header = args['s']
  35.  
  36. # Determine first record in the list
  37. if header:
  38. first_record_in_list = 1
  39. else:
  40. first_record_in_list = 0
  41.  
  42. # Log file file path sanity checks
  43. if fp_log_file is not None and not os.path.isfile(fp_log_file):
  44. print("ERROR: Log file specified does not exist.")
  45. exit(1)
  46. if not fp_log_file.endswith('.csv'):
  47. print("ERROR: Log file specified is not a .csv file.")
  48. exit(1)
  49.  
  50. # Copy entries from the log file to an array
  51. log_file = open(fp_log_file)
  52. record_list = log_file.read().splitlines()
  53. log_file.close()
  54.  
  55. # When required, sort log file on sequence number (does not work yet)
  56. if column_seq_nr != 0:
  57. log_list = sorted(record_list, key=lambda row: float(row[column_seq_nr - 1].strip(',')))
  58. print('INFO: Log sorted on column', column_seq_nr)
  59.  
  60. # Get first record in list for the first check in the loop
  61. previous_record = record_list[first_record_in_list]
  62.  
  63. print('INFO: Checking if timestamps increase in column', column_timestamp)
  64.  
  65. # Loop through entries in the log file, start at second record
  66. for record in record_list[first_record_in_list + 1:]:
  67.  
  68. # Timestamp column validity check
  69. try:
  70. timestamp = parse(record.split(',')[column_timestamp - 1])
  71. previous_timestamp = parse(previous_record.split(',')[column_timestamp - 1])
  72. except ValueError:
  73. print('ERROR: Column', column_timestamp, 'does not contain valid timestamp values')
  74. exit(1)
  75.  
  76. # Check for the actual backdating
  77. if timestamp < previous_timestamp:
  78. detection_counter += 1
  79. print('[', detection_counter, '] BACKDATING DETECTED')
  80. print('Clock was set back between following records:')
  81. print(previous_record)
  82. print(record)
  83.  
  84. # Safe current record to check next record against
  85. previous_record = record
  86.  
  87. # Print summary
  88. if detection_counter == 0:
  89. print('DONE: NO BACKDATING DETECTED')
  90. exit(0)
  91. else:
  92. print('DONE: BACKDATING WAS DETECTED', detection_counter, 'TIMES')
  93. exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement