Skip to content

Instantly share code, notes, and snippets.

@mattfitzgerald
Last active December 27, 2015 17:59
Show Gist options
  • Save mattfitzgerald/7366268 to your computer and use it in GitHub Desktop.
Save mattfitzgerald/7366268 to your computer and use it in GitHub Desktop.
Log file parser
require 'time'
# TODO:
#
# add enquire links (done)
# and enquire link responses (done)
# add restart events on a thread (done)
#
#
class Pdu
attr :id
attr :remote_id
attr :time
attr :thread_name
def initialize log_line
@time = Time.parse(log_line[0..23])
# @thread_name = log_line[/\[(.*?)\]/]
@thread_name = log_line.scan(/.*\[(.*)\]/)[0][0]
end
def to_s
"#{time} #{thread_name} #{direction} #{self.class} #{id} #{remote_id}"
end
end
class SubmitSm < Pdu
def initialize log_line
super(log_line)
@id = log_line.split.last
end
def direction
'->'
end
end
class SubmitSmResp < Pdu
def initialize log_line
super(log_line)
@id = log_line[149..157]
@remote_id = log_line[172..182]
end
def direction
'<-'
end
end
class DeliverSm < Pdu
def initialize log_line
super(log_line)
@remote_id = log_line[182..190]
end
def direction
'<-'
end
end
class EnquireLink < Pdu
def initialize log_line
super(log_line)
end
def direction
'->'
end
end
class EnquireLinkResp < Pdu
def initialize log_line
super(log_line)
end
def direction
'<-'
end
end
class Restart
attr :time
def initialize log_line
@time = Time.parse(log_line[0..23])
end
def to_s
"#{time} RESTART"
end
end
class Logan
# Logan the log analyzer, handles single connection
attr :grep_limit
attr :sender_thread
attr :file_name
PDUS = %w(submit_sm submit_sm_resp deliver_sm enquire_link).map{|e| e.to_sym}
GREP_LIMIT = nil#'2'
def initialize file_name, sender_thread_number
@file_name = file_name
@sender_thread = "mblox_uk-#{sender_thread_number}"
@sender_thread_number = sender_thread_number
end
def receiver_thread
# find the first request, matching response, that's your receiver thread
@receiver_thread ||= begin
first_submit = SubmitSm.new(grep_for_event(:submit_sm, '1').first)
submit_sm_resp = find_submit_sm_resp_by_id(first_submit.id)
submit_sm_resp.thread_name
# "Receiver-#{@sender_thread_number+1}"# receiver_thread
end
end
def find_submit_sm_resp_by_id id
s = "egrep 'submit_sm_resp pdu(.*)#{id}' #{file_name}"
SubmitSmResp.new(`#{s}`.split("\n").first)
end
def all_events
events = []
events = PDUS.inject([]){|agg,type| agg + pdus(type) }
events += restarts
events.sort_by(&:time)
end
def pdus type
cls = Object.const_get(type.to_s.split('_').map{|w| w.capitalize}.join)
grep_for_event(type, GREP_LIMIT).map{|l| cls.new(l)}
end
def restarts
grep_for_event(:restart, GREP_LIMIT).map{|l| Restart.new(l)}
end
private
def grep_for_event type, grep_limit
s = "egrep #{'-m '+grep_limit if grep_limit} '#{event_regex(type)}' #{file_name}"
#puts s
`#{s}`.split("\n")
#{}`egrep #{'-m '+GREP_LIMIT if GREP_LIMIT} '#{pdu_regex(type)}' #{file_name}`.split("\n")
end
def event_regex type
# messy logic here reflects log format
# egrep -m 3 "SmppSender(.*)sending submit_sm" mblox_uk_original.txt
# egrep -m 3 "SmppSender-mblox_uk-1(.*)sending submit_sm" mblox_uk_original.txt
# egrep -m 30 'Receiver-1(.*)- insert into delivery_reports' mblox_uk_original.txt
case type
when :submit_sm
"#{sender_thread}(.*)sending submit_sm"
when :submit_sm_resp
"#{receiver_thread}(.*)submit_sm_resp pdu"
when :deliver_sm
"#{receiver_thread}(.*)- insert into delivery_reports"
when :enquire_link
"#{sender_thread}(.*)sendEnquiryLink"
when :enquire_link_resp
"#{receiver_thread}(.*)enquire_link_resp"
when :enquire_link_resp
"#{receiver_thread}(.*)enquire_link_resp"
when :restart
"#{sender_thread}(.*)Starting SmppSender"
end
end
end
Logan.new("mblox_uk_original.txt", 0).all_events.each{|p| puts p.to_s}
# Logan.new("mblox_uk_original.txt", 0).pdus(:submit_sm).each{|p| puts p.to_s}
# Logan.new("mblox_uk_original.txt", 0).pdus(:enquire_link).each{|p| puts p.to_s}
# Logan.new("mblox_uk_original.txt", 0).restarts.each{|p| puts p.to_s}
# (0..16).each do |thread|
# puts "Thread #{thread}"
# events = Logan.new("mblox_uk_original.txt", thread).all_events
# events.each{|p| puts p.to_s}
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment