Guest User

Untitled

a guest
Jul 22nd, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 KB | None | 0 0
  1. #!/usr/bin/env ruby1.9
  2. #daniel watson
  3. #2009.09.04
  4. #
  5.  
  6. require 'net/smtp'
  7. require 'thread'
  8. require 'fileutils'
  9.  
  10. require 'VueSim/SimpleGateway'
  11. require 'VueSim/SimplePortal'
  12. require 'VueSim/SimplePacket'
  13. require 'VueSim/SimpleCommons'
  14. require 'inspector'
  15. require 'logger'
  16. require 'authentication_header'
  17. require 'vue_parse'
  18. require 'iprofiletools'
  19.  
  20. include SimpleCommons
  21.  
  22. Log_dir = "logs"
  23. DC_log_dir = File.join(Log_dir, "dc")
  24. Log_ext = "log"
  25.  
  26. class String; def soap_entity_size; 1; end; end
  27. class Array; def soap_entity_size; size; end; end
  28.  
  29. Email_to = "xxxxxx@example.com"
  30. Txt_to = "xxxxxxxx@example.com"
  31.  
  32. class Vue_Monitor
  33. def portal_start
  34. tries = 0
  35. begin
  36. @portal.connect
  37. rescue Errno::ECONNREFUSED => e
  38. if tries < 5
  39. sleep(60*(2**tries))
  40. tries += 1
  41. retry
  42. else
  43. @log.warn("too many portal balance refusals. giving up")
  44. raise e
  45. end
  46. end
  47. @portal_thread = Thread.new{@portal.listen}
  48. end
  49.  
  50. def gw_stop
  51. @gw.stop
  52. @gw_thread.kill
  53. @gw_thread.join
  54. end
  55.  
  56. def portal_stop
  57. @portal.stop
  58. @portal_thread.kill
  59. @portal_thread.join
  60. end
  61.  
  62. def output_results
  63. @results = get_results
  64. output_to_disk
  65. output_to_terminal
  66.  
  67. should_send_email = should_send_email?
  68. should_send_txt = should_send_txt?
  69.  
  70. send_email if should_send_email
  71. #TODO: take out 'false' condition when vetted by email
  72. send_txt if (should_send_txt and false)
  73.  
  74. puts "should send txt = #{should_send_txt}"
  75. puts "should send email = #{should_send_email}"
  76. end
  77.  
  78. def output_to_terminal
  79. puts "results:"
  80. puts @results.join('')
  81. puts "txt:"
  82. puts txt_format
  83. end
  84.  
  85. def output_to_disk
  86. n = Time.now
  87. log_base = File.join(DC_log_dir, "%02X"% @id)
  88. most_recent = "#{log_base}.#{Log_ext}"
  89. write_with_dir(most_recent, 'a', @results)
  90. individual_fn = "#{log_base + n.strftime("_%Y-%m-%d_%H-%M-%S")}.#{Log_ext}"
  91. write_with_dir(individual_fn, 'w', @results)
  92. end
  93.  
  94. def write_with_dir filepath, mode, content
  95. retried = false
  96. begin
  97. File.open(filepath, mode){ |file| file.puts content}
  98. rescue Errno::ENOENT => e
  99. unless retried
  100. directory = File.dirname(filepath)
  101. FileUtils.mkdir_p(directory)
  102.  
  103. puts e.inspect
  104. msg = "creating directory #{directory}"; puts msg; @log.debug msg
  105.  
  106. retried = true
  107. retry
  108. else
  109. puts "still getting #{e.class}, unable to handle"
  110. raise e
  111. end
  112. end
  113. end
  114.  
  115. def send_email(body=@results.join(''))
  116. _send_email(body)
  117. end
  118.  
  119. def send_txt(txt=txt_format)
  120. _send_email(txt, Txt_to)
  121. end
  122.  
  123. def txt_format
  124. unless @videos_countable then
  125. "videos uncountable"
  126. else
  127. "Old video count == #{@old_video_count}, " +
  128. "current video count == #{@current_video_count}"
  129. end
  130. end
  131.  
  132. def should_send_email?
  133. ! reports_counts_match_most_common?
  134. end
  135.  
  136. def should_send_txt?
  137. #if we can't count videos, then we can't send a txt due to wrong video count
  138. !(correct_video_count? or !(@videos_countable))
  139. end
  140.  
  141. def correct_video_count?
  142. if @videos_countable then
  143. @old_video_count = @current_video_count
  144. @current_video_count = get_video_count
  145. (@old_video_count + 1) == @current_video_count
  146. else
  147. false
  148. end
  149. end
  150.  
  151. def reports_counts_match_most_common?
  152. current_inspector = Inspector.new @results
  153. match_most_common = @reports.all? do |report_type, occurence_hash|
  154. Dir[File.join('logs','dc','*.log')].each do |filename|
  155. directory_inspector = Inspector.new File.readlines(filename)
  156. key = eval "directory_inspector.#{report_type}.size"
  157. occurence_hash[key] = (occurence_hash[key] || 0) + 1
  158. end
  159. kwlv = key_with_largest_value(occurence_hash)
  160. cirts = eval("current_inspector.#{report_type}.size")
  161. puts "(most common #{report_type}) count = #{kwlv}," +
  162. " (current #{report_type}) count = #{cirts}"
  163. (kwlv - cirts).abs <= 5
  164. end
  165. end
  166.  
  167. def get_results
  168. line_number = 0
  169. File.readlines(@log_filename).select do |l|
  170. line_number += 1
  171. line_number > @old_results_line_count
  172. end
  173. end
  174.  
  175. def email_format from, to, subject, body
  176. <<END_OF_MESSAGE
  177. From: #{from}
  178. To: #{to}
  179. Subject: #{subject}
  180.  
  181. #{body}
  182. END_OF_MESSAGE
  183. end
  184.  
  185. def initialize options
  186. @send_email = options[:send_email]
  187.  
  188. @log_filename = options[:log_filename]
  189. @log = Logger.new(@log_filename)
  190.  
  191. @id = options[:id]
  192. setup_id(@id) #from VueSim/SimpleCommons
  193.  
  194. setup_gw(options)
  195.  
  196. setup_portal(options)
  197.  
  198. @reports = Logger::Severity.constants.inject({}) do |s, e|
  199. s["#{e.downcase}s"] = {}
  200. s
  201. end
  202.  
  203. setup_soap(options)
  204.  
  205. #set to number of lines in the master log, 0 if the file doesn't exist
  206. @old_results_line_count =
  207. begin; File.readlines(@log_filename).size; rescue Errno::ENOENT; 0; end
  208. @current_video_count = get_video_count if @videos_countable
  209. end
  210.  
  211. def generate_events
  212. #generate events to populate the log
  213. @gw_thread = Thread.new{@gw.start}
  214. sleep 15
  215. portal_start
  216. sleep 1
  217. Thread.new{@portal.request_burst 0}
  218. sleep 0.334
  219. Thread.new{@portal.request_start_recording}
  220. sleep 0.334
  221. Thread.new{@portal.request_stop_recording}
  222. sleep 0.334
  223. Thread.new{@portal.cancel_video}
  224. sleep 1
  225. portal_stop
  226. gw_stop
  227. sleep 1
  228. end
  229.  
  230. private
  231. def setup_gw(options)
  232. gw_url = options[:gw_url]
  233. gw_port = options[:gw_port]
  234. gw_options = {
  235. :url => gw_url,
  236. :id => @id,
  237. :port => gw_port,
  238. :log => @log,
  239. }
  240. @gw = SimpleGateway.new gw_options
  241. end
  242.  
  243. def setup_portal(options)
  244. portal_balancer_url = options[:portal_balancer_url]
  245. portal_port = options[:portal_port]
  246. portal_options = {
  247. :load_balancer_url => portal_balancer_url,
  248. :id => @id,
  249. :load_balancer_port => portal_port,
  250. :log => @log,
  251. }
  252. @portal = SimplePortal.new portal_options
  253. end
  254.  
  255. def setup_soap(options)
  256. @iProfileTools = IProfileTools.new
  257. @videos_countable = true
  258. end
  259.  
  260. def _send_email(body, to=Email_to, host='example.com',
  261. port=587, relay_domain='example.com', account='example',
  262. pw='example', auth_type=:login)
  263.  
  264. from = "#{account}@example.com"
  265. subject = "dc logging"
  266. msg = email_format(from, to, subject, body)
  267.  
  268. smtp = Net::SMTP.new host, port
  269. smtp.enable_starttls
  270. smtp.start(relay_domain, account, pw, auth_type) do |server|
  271. server.send_message(msg, from, to) if @send_email
  272. end
  273. end
  274.  
  275. def key_with_largest_value hash
  276. key_with_largest_value, largest_value = hash.first
  277. hash.each do |key, value|
  278. if largest_value < value
  279. key_with_largest_value, largest_value = key, value
  280. end
  281. end
  282. key_with_largest_value
  283. end
  284.  
  285. def get_video_count
  286. mac_val = @mac.inject(0){|s,e|s*(2**8) + e}
  287. sensor_id_val = @sensor_id.inject(0){|s,e|s*(2**8) + e}
  288. video_count = @iProfileTools.video_count(mac_val, sensor_id_val)
  289. @videos_countable = @iProfileTools.videos_countable?
  290. video_count
  291. end
  292. end
  293.  
  294. if __FILE__ == $0 then
  295. options = Vue_parse.parse(Array.new(ARGV))
  296. vue_monitor = Vue_Monitor.new options
  297. begin
  298. vue_monitor.generate_events
  299. rescue => e
  300. =begin
  301. vue_monitor.send_txt(
  302. "vue service monitor problem. check #{Email_to} for more info"
  303. )
  304. =end
  305. vue_monitor.send_email(
  306. "error\n#{e.inspect}\nresults\n#{vue_monitor.get_results}"
  307. )
  308. end
  309. vue_monitor.output_results
  310. end
Add Comment
Please, Sign In to add comment