Statistics.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import os
  2. import time
  3. import ID2TLib.libpcapreader as pr
  4. from ID2TLib.PcapFile import PcapFile
  5. from ID2TLib.StatsDatabase import StatsDatabase
  6. class Statistics:
  7. def __init__(self, pcap_file: PcapFile):
  8. """
  9. Creates a new Statistics object.
  10. :param pcap_file: A reference to the PcapFile object
  11. """
  12. # Fields
  13. self.pcap_filepath = pcap_file.pcap_file_path
  14. self.pcap_processor = None
  15. pcap_file_hash = pcap_file.get_file_hash()
  16. # Create folder for statistics database if required
  17. self.path_db = pcap_file.get_db_path()
  18. path_dir = os.path.dirname(self.path_db)
  19. if not os.path.isdir(path_dir):
  20. os.makedirs(path_dir)
  21. # Class instances
  22. self.stats_db = StatsDatabase(self.path_db)
  23. def load_pcap_statistics(self, flag_write_file: bool, flag_recalculate_stats: bool, flag_print_statistics: bool):
  24. """
  25. Loads the PCAP statistics for the file specified by pcap_filepath. If the database is not existing yet, the
  26. statistics are calculated by the PCAP file processor and saved into the newly created database. Otherwise the
  27. statistics are gathered directly from the existing database.
  28. :param flag_write_file: Indicates whether the statistics should be written addiotionally into a text file (True)
  29. or not (False)
  30. :param flag_recalculate_stats: Indicates whether eventually existing statistics should be recalculated
  31. :param flag_print_statistics: Indicates whether the gathered basic statistics should be printed to the terminal
  32. """
  33. # Load pcap and get loading time
  34. time_start = time.clock()
  35. # Inform user about recalculation of statistics and its reason
  36. if flag_recalculate_stats:
  37. print("Flag -r/--recalculate found. Recalculating statistics.")
  38. # Recalculate statistics if database not exists OR param -r/--recalculate was provided
  39. if (not self.stats_db.get_db_exists()) or flag_recalculate_stats:
  40. self.pcap_processor = pr.pcap_processor(self.pcap_filepath)
  41. self.pcap_processor.collect_statistics()
  42. self.pcap_processor.write_to_database(self.path_db)
  43. outstring_datasource = "by PCAP file processor."
  44. else:
  45. outstring_datasource = "from statistics database."
  46. # Load statistics from database
  47. self.file_info = self.stats_db.get_file_info()
  48. time_end = time.clock()
  49. print("Loaded file statistics in " + str(time_end - time_start)[:4] + " sec " + outstring_datasource)
  50. # Write statistics if param -e/--export provided
  51. if flag_write_file:
  52. self.write_statistics_to_file()
  53. # Print statistics if param -s/--statistics provided
  54. if flag_print_statistics:
  55. self.print_statistics()
  56. def get_file_information(self):
  57. """
  58. Returns a list of tuples, each containing a information of the file.
  59. :return: a list of tuples, each consisting of (description, value, unit), where unit is optional.
  60. """
  61. return [("Pcap file", self.pcap_filepath),
  62. ("#Packets", self.get_packet_count(), "packets"),
  63. ("Capture length", self.get_capture_duration(), "seconds"),
  64. ("Capture start", self.get_pcap_timestamp_start()),
  65. ("Capture end", self.get_pcap_timestamp_end())]
  66. def get_general_file_statistics(self):
  67. """
  68. Returns a list of tuples, each containing a file statistic.
  69. :return: a list of tuples, each consisting of (description, value, unit).
  70. """
  71. return [("Avg. packet rate", self.file_info['avgPacketRate'], "packets/sec"),
  72. ("Avg. packet size", self.file_info['avgPacketSize'], "kbytes"),
  73. ("Avg. packets sent", self.file_info['avgPacketsSentPerHost'], "packets"),
  74. ("Avg. bandwidth in", self.file_info['avgBandwidthIn'], "kbit/s"),
  75. ("Avg. bandwidth out", self.file_info['avgBandwidthOut'], "kbit/s")]
  76. @staticmethod
  77. def _write_list(desc_val_unit_list, func, line_ending="\n"):
  78. """
  79. Takes a list of tuples (statistic name, statistic value, unit) as input, generates a string of these three values
  80. and applies the function func on this string.
  81. Before generating the string, it identifies text containing a float number, casts the string to a
  82. float and rounds the value to two decimal digits.
  83. :param desc_val_unit_list: The list of tuples consisting of (description, value, unit)
  84. :param func: The function to be applied to each generated string
  85. :param line_ending: The formatting string to be applied at the end of each string
  86. """
  87. for entry in desc_val_unit_list:
  88. # Convert text containing float into float
  89. (description, value) = entry[0:2]
  90. if isinstance(value, str) and "." in value:
  91. try:
  92. value = float(value)
  93. except ValueError:
  94. pass # do nothing -> value was not a float
  95. # round float
  96. if isinstance(value, float):
  97. value = round(value, 2)
  98. # write into file
  99. if len(entry) == 3:
  100. unit = entry[2]
  101. func(description + ":\t" + str(value) + " " + unit + line_ending)
  102. else:
  103. func(description + ":\t" + str(value) + line_ending)
  104. def print_statistics(self):
  105. """
  106. Prints the basic file statistics to the terminal.
  107. """
  108. print("\nPCAP FILE INFORMATION ------------------------------")
  109. Statistics._write_list(self.get_file_information(), print, "")
  110. print("\nGENERAL FILE STATISTICS ----------------------------")
  111. Statistics._write_list(self.get_general_file_statistics(), print, "")
  112. print("\n")
  113. def write_statistics_to_file(self):
  114. """
  115. Writes the calculated basic statistics into a file.
  116. """
  117. def _write_header(title: str):
  118. """
  119. Writes the section header into the open file.
  120. :param title: The section title
  121. """
  122. target.write("====================== \n")
  123. target.write(title + " \n")
  124. target.write("====================== \n")
  125. target = open(self.pcap_filepath + ".stat", 'w')
  126. target.truncate()
  127. _write_header("PCAP file information")
  128. Statistics._write_list(self.get_file_information(), target.write)
  129. _write_header("General statistics")
  130. Statistics._write_list(self.get_general_file_statistics(), target.write)
  131. target.close()
  132. def get_capture_duration(self):
  133. """
  134. :return: The duration of the capture in seconds
  135. """
  136. return self.file_info['captureDuration']
  137. def get_pcap_timestamp_start(self):
  138. """
  139. :return: The timestamp of the first packet in the PCAP file
  140. """
  141. return self.file_info['timestampFirstPacket']
  142. def get_pcap_timestamp_end(self):
  143. """
  144. :return: The timestamp of the last packet in the PCAP file
  145. """
  146. return self.file_info['timestampLastPacket']
  147. def get_pps_sent(self, ip_address: str):
  148. """
  149. Calculates the sent packets per seconds for a given IP address.
  150. :param ip_address: The IP address whose packets per second should be calculated
  151. :return: The sent packets per seconds for the given IP address
  152. """
  153. packets_sent = self.stats_db.process_db_query("SELECT pktsSent from ip_statistics WHERE ipAddress=?", False,
  154. (ip_address,))
  155. capture_duration = float(self.get_capture_duration())
  156. return int(float(packets_sent) / capture_duration)
  157. def get_packet_count(self):
  158. """
  159. :return: The number of packets in the loaded PCAP file
  160. """
  161. return self.file_info['packetCount']
  162. def get_statistics_database(self):
  163. """
  164. :return: A reference to the statistics database object
  165. """
  166. return self.stats_db
  167. def process_db_query(self, query_string_in: str, print_results: bool = False):
  168. """
  169. Executes a string identified previously as a query. This can be a standard SQL SELECT/INSERT query or a named
  170. query.
  171. :param query_string_in: The query to be processed
  172. :param print_results: Indicates whether the results should be printed to terminal
  173. :return: The result of the query
  174. """
  175. return self.stats_db.process_db_query(query_string_in, print_results)
  176. def is_query(self, value: str):
  177. """
  178. Checks whether the given string is a standard SQL query (SELECT, INSERT) or a named query.
  179. :param value: The string to be checked
  180. :return: True if the string is recognized as a query, otherwise False.
  181. """
  182. is_scalar_value = type(value) in (int, float)
  183. return not is_scalar_value and (
  184. any(x in value.lower().strip() for x in self.stats_db.get_all_named_query_keywords()) or
  185. any(x in value.lower().strip() for x in self.stats_db.get_all_sql_query_keywords()))