Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby1.9
- #daniel watson
- #2009.09.04
- #
- require 'net/smtp'
- require 'thread'
- require 'fileutils'
- require 'VueSim/SimpleGateway'
- require 'VueSim/SimplePortal'
- require 'VueSim/SimplePacket'
- require 'VueSim/SimpleCommons'
- require 'inspector'
- require 'logger'
- require 'authentication_header'
- require 'vue_parse'
- require 'iprofiletools'
- include SimpleCommons
- Log_dir = "logs"
- DC_log_dir = File.join(Log_dir, "dc")
- Log_ext = "log"
- class String; def soap_entity_size; 1; end; end
- class Array; def soap_entity_size; size; end; end
- Email_to = "xxxxxx@example.com"
- Txt_to = "xxxxxxxx@example.com"
- class Vue_Monitor
- def portal_start
- tries = 0
- begin
- @portal.connect
- rescue Errno::ECONNREFUSED => e
- if tries < 5
- sleep(60*(2**tries))
- tries += 1
- retry
- else
- @log.warn("too many portal balance refusals. giving up")
- raise e
- end
- end
- @portal_thread = Thread.new{@portal.listen}
- end
- def gw_stop
- @gw.stop
- @gw_thread.kill
- @gw_thread.join
- end
- def portal_stop
- @portal.stop
- @portal_thread.kill
- @portal_thread.join
- end
- def output_results
- @results = get_results
- output_to_disk
- output_to_terminal
- should_send_email = should_send_email?
- should_send_txt = should_send_txt?
- send_email if should_send_email
- #TODO: take out 'false' condition when vetted by email
- send_txt if (should_send_txt and false)
- puts "should send txt = #{should_send_txt}"
- puts "should send email = #{should_send_email}"
- end
- def output_to_terminal
- puts "results:"
- puts @results.join('')
- puts "txt:"
- puts txt_format
- end
- def output_to_disk
- n = Time.now
- log_base = File.join(DC_log_dir, "%02X"% @id)
- most_recent = "#{log_base}.#{Log_ext}"
- write_with_dir(most_recent, 'a', @results)
- individual_fn = "#{log_base + n.strftime("_%Y-%m-%d_%H-%M-%S")}.#{Log_ext}"
- write_with_dir(individual_fn, 'w', @results)
- end
- def write_with_dir filepath, mode, content
- retried = false
- begin
- File.open(filepath, mode){ |file| file.puts content}
- rescue Errno::ENOENT => e
- unless retried
- directory = File.dirname(filepath)
- FileUtils.mkdir_p(directory)
- puts e.inspect
- msg = "creating directory #{directory}"; puts msg; @log.debug msg
- retried = true
- retry
- else
- puts "still getting #{e.class}, unable to handle"
- raise e
- end
- end
- end
- def send_email(body=@results.join(''))
- _send_email(body)
- end
- def send_txt(txt=txt_format)
- _send_email(txt, Txt_to)
- end
- def txt_format
- unless @videos_countable then
- "videos uncountable"
- else
- "Old video count == #{@old_video_count}, " +
- "current video count == #{@current_video_count}"
- end
- end
- def should_send_email?
- ! reports_counts_match_most_common?
- end
- def should_send_txt?
- #if we can't count videos, then we can't send a txt due to wrong video count
- !(correct_video_count? or !(@videos_countable))
- end
- def correct_video_count?
- if @videos_countable then
- @old_video_count = @current_video_count
- @current_video_count = get_video_count
- (@old_video_count + 1) == @current_video_count
- else
- false
- end
- end
- def reports_counts_match_most_common?
- current_inspector = Inspector.new @results
- match_most_common = @reports.all? do |report_type, occurence_hash|
- Dir[File.join('logs','dc','*.log')].each do |filename|
- directory_inspector = Inspector.new File.readlines(filename)
- key = eval "directory_inspector.#{report_type}.size"
- occurence_hash[key] = (occurence_hash[key] || 0) + 1
- end
- kwlv = key_with_largest_value(occurence_hash)
- cirts = eval("current_inspector.#{report_type}.size")
- puts "(most common #{report_type}) count = #{kwlv}," +
- " (current #{report_type}) count = #{cirts}"
- (kwlv - cirts).abs <= 5
- end
- end
- def get_results
- line_number = 0
- File.readlines(@log_filename).select do |l|
- line_number += 1
- line_number > @old_results_line_count
- end
- end
- def email_format from, to, subject, body
- <<END_OF_MESSAGE
- From: #{from}
- To: #{to}
- Subject: #{subject}
- #{body}
- END_OF_MESSAGE
- end
- def initialize options
- @send_email = options[:send_email]
- @log_filename = options[:log_filename]
- @log = Logger.new(@log_filename)
- @id = options[:id]
- setup_id(@id) #from VueSim/SimpleCommons
- setup_gw(options)
- setup_portal(options)
- @reports = Logger::Severity.constants.inject({}) do |s, e|
- s["#{e.downcase}s"] = {}
- s
- end
- setup_soap(options)
- #set to number of lines in the master log, 0 if the file doesn't exist
- @old_results_line_count =
- begin; File.readlines(@log_filename).size; rescue Errno::ENOENT; 0; end
- @current_video_count = get_video_count if @videos_countable
- end
- def generate_events
- #generate events to populate the log
- @gw_thread = Thread.new{@gw.start}
- sleep 15
- portal_start
- sleep 1
- Thread.new{@portal.request_burst 0}
- sleep 0.334
- Thread.new{@portal.request_start_recording}
- sleep 0.334
- Thread.new{@portal.request_stop_recording}
- sleep 0.334
- Thread.new{@portal.cancel_video}
- sleep 1
- portal_stop
- gw_stop
- sleep 1
- end
- private
- def setup_gw(options)
- gw_url = options[:gw_url]
- gw_port = options[:gw_port]
- gw_options = {
- :url => gw_url,
- :id => @id,
- :port => gw_port,
- :log => @log,
- }
- @gw = SimpleGateway.new gw_options
- end
- def setup_portal(options)
- portal_balancer_url = options[:portal_balancer_url]
- portal_port = options[:portal_port]
- portal_options = {
- :load_balancer_url => portal_balancer_url,
- :id => @id,
- :load_balancer_port => portal_port,
- :log => @log,
- }
- @portal = SimplePortal.new portal_options
- end
- def setup_soap(options)
- @iProfileTools = IProfileTools.new
- @videos_countable = true
- end
- def _send_email(body, to=Email_to, host='example.com',
- port=587, relay_domain='example.com', account='example',
- pw='example', auth_type=:login)
- from = "#{account}@example.com"
- subject = "dc logging"
- msg = email_format(from, to, subject, body)
- smtp = Net::SMTP.new host, port
- smtp.enable_starttls
- smtp.start(relay_domain, account, pw, auth_type) do |server|
- server.send_message(msg, from, to) if @send_email
- end
- end
- def key_with_largest_value hash
- key_with_largest_value, largest_value = hash.first
- hash.each do |key, value|
- if largest_value < value
- key_with_largest_value, largest_value = key, value
- end
- end
- key_with_largest_value
- end
- def get_video_count
- mac_val = @mac.inject(0){|s,e|s*(2**8) + e}
- sensor_id_val = @sensor_id.inject(0){|s,e|s*(2**8) + e}
- video_count = @iProfileTools.video_count(mac_val, sensor_id_val)
- @videos_countable = @iProfileTools.videos_countable?
- video_count
- end
- end
- if __FILE__ == $0 then
- options = Vue_parse.parse(Array.new(ARGV))
- vue_monitor = Vue_Monitor.new options
- begin
- vue_monitor.generate_events
- rescue => e
- =begin
- vue_monitor.send_txt(
- "vue service monitor problem. check #{Email_to} for more info"
- )
- =end
- vue_monitor.send_email(
- "error\n#{e.inspect}\nresults\n#{vue_monitor.get_results}"
- )
- end
- vue_monitor.output_results
- end
Add Comment
Please, Sign In to add comment