Class: MtrMonitor::Report
- Inherits:
-
Object
- Object
- MtrMonitor::Report
- Defined in:
- lib/mtr_monitor.rb
Overview
Runs a mtr trace toward a domain and saves the report to /var/log/mtr/<domain>_timestamp.log , uploads to s3 and reports metrics to statsd.
Instance Method Summary collapse
- #generate ⇒ Object
- #hops ⇒ Object
- #host_ip_address ⇒ Object
-
#initialize(name, domain, s3_bucket, mtr_options, aws_access_key_id, aws_secret_access_key) ⇒ Report
constructor
A new instance of Report.
- #log_path ⇒ Object
- #run(cmd) ⇒ Object
- #submit_metrics ⇒ Object
Constructor Details
#initialize(name, domain, s3_bucket, mtr_options, aws_access_key_id, aws_secret_access_key) ⇒ Report
Returns a new instance of Report.
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/mtr_monitor.rb', line 11 def initialize(name, domain, s3_bucket, , aws_access_key_id, aws_secret_access_key) @name = name @domain = domain @s3_bucket = s3_bucket @aws_access_key_id = aws_access_key_id @aws_secret_access_key = aws_secret_access_key = @log_name = "#{@name}/#{Time.now.strftime("%Y-%m-%d")}/#{host_ip_address}/#{Time.now.strftime("%H-%M")}.log" @log_path = "/var/log/mtr/#{@log_name.gsub("/", "-")}" @s3_path = "s3://#{@s3_bucket}/#{@log_name}" end |
Instance Method Details
#generate ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/mtr_monitor.rb', line 24 def generate puts "Local Path : #{@log_path}" puts "S3 Path : #{@s3_path}" run "sudo mkdir -p /var/log/mtr" run "sudo bash -c 'mtr --report #{@mtr_options} #{@domain} > #{@log_path}'" run "AWS_ACCESS_KEY_ID='#{@aws_access_key_id}' AWS_SECRET_ACCESS_KEY='#{@aws_secret_access_key}' ~/.local/bin/aws s3 cp #{@log_path} #{@s3_path}" end |
#hops ⇒ Object
48 49 50 |
# File 'lib/mtr_monitor.rb', line 48 def hops `sudo cat #{@log_path}`.split("\n").map { |line| Hop.parse(line) }.compact end |
#host_ip_address ⇒ Object
52 53 54 |
# File 'lib/mtr_monitor.rb', line 52 def host_ip_address `/sbin/ip -o -4 addr list eth0 | awk '{print $4}' | cut -d/ -f1`.strip.gsub(".", "-") end |
#log_path ⇒ Object
56 57 58 |
# File 'lib/mtr_monitor.rb', line 56 def log_path @log_path end |
#run(cmd) ⇒ Object
60 61 62 |
# File 'lib/mtr_monitor.rb', line 60 def run(cmd) `#{cmd}` end |
#submit_metrics ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/mtr_monitor.rb', line 33 def submit_metrics hops.each do |hop| hop_name = hop.name.gsub(".", "-").gsub("?", "q") = [`hostname`.strip, @name, hop_name] Watchman.submit("network.mtr.loss", hop.loss, :gauge, :tags => ) Watchman.submit("network.mtr.snt", hop.snt, :gauge, :tags => ) Watchman.submit("network.mtr.last", hop.last, :gauge, :tags => ) Watchman.submit("network.mtr.avg", hop.avg, :gauge, :tags => ) Watchman.submit("network.mtr.best", hop.best, :gauge, :tags => ) Watchman.submit("network.mtr.worst", hop.worst, :gauge, :tags => ) Watchman.submit("network.mtr.std_dev", hop.std_dev, :gauge, :tags => ) end end |