forked from jeremyd/rest_connection
-
Notifications
You must be signed in to change notification settings - Fork 4
/
log_api_call_parser
executable file
·92 lines (80 loc) · 2.95 KB
/
log_api_call_parser
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/ruby
#--
# Copyright (c) 2012 RightScale, Inc, All Rights Reserved Worldwide.
#
# THIS PROGRAM IS CONFIDENTIAL AND PROPRIETARY TO RIGHTSCALE
# AND CONSTITUTES A VALUABLE TRADE SECRET. Any unauthorized use,
# reproduction, modification, or disclosure of this program is
# strictly prohibited. Any use of this program by an authorized
# licensee is strictly subject to the terms and conditions,
# including confidentiality obligations, set forth in the applicable
# License Agreement between RightScale, Inc. and
# the licensee.
#++
########################################################################################################################
# rest_connection API Call Parser Main program
#
# This program will parse a rest_connection.log produced from any given monkey run and display the:
# - API version
# - Request Type
# - Request
# in tabular format on STDOUT. It will also generate the csv file odf the same information.
########################################################################################################################
# Third party requires
require 'rubygems'
require 'trollop'
require 'terminal-table'
require 'tempfile'
require 'csv'
#
# Parse and validate command line arguments
#
opts = Trollop::options do
banner <<-EOS
Usage:
log_api_call_parser [options]
where [options] are:
EOS
opt :input, "Path to the input file in standard rest_connection logger format", :short => "-i", :type => String, :required => true
opt :csv_file, "Path to the csv output file", :short => "-o", :type => String, :required => true
end
# Open the input file and read it into an array
input_file = File.open(opts[:input])
log_file_array = input_file.readlines
input_file.close
# Loop over the array and for each row, parse out the api version, request type and the request
# and add them to output_array for later sorting and writing.
output_array = []
search_string = "INFO -- :"
log_file_array.each { |element|
if element[0..2] == "I, "
# Find the API version start column as this can vary at least per log file and maybe even per line
api_version_start_column = element.index(search_string)
api_version_start_column = api_version_start_column + search_string.length + 1
# Get the elements
element = element[api_version_start_column..-1]
version = element[5..8]
request_type = element[11..13]
if request_type == "GET" || request_type == "PUT"
request = element[16..-1]
output_array.push "#{version},#{request_type},#{request}"
end
end
}
# Sort the output array by API version
output_array.sort! {|a,b| a <=> b}
# Remove duplicates
output_array.uniq!
# Write out the CSV file
csv_file = File.open(opts[:csv_file], "w")
csv_file_name = csv_file.path()
output_array.each { |element| csv_file.puts element }
csv_file.close
# Generate a table version and write that out to STDOUT
csv_array = CSV.read(csv_file_name)
table = Terminal::Table.new
csv_array.collect do |line|
table << line
table << :separator
end
puts table