Class: MtrMonitor::Report

Inherits:
Object
  • Object
show all
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

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, mtr_options, 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
  @mtr_options = mtr_options

  @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

#generateObject



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

#hopsObject



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_addressObject



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_pathObject



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_metricsObject



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")
    tags = [`hostname`.strip, @name, hop_name]

    Watchman.submit("network.mtr.loss", hop.loss, :gauge, :tags => tags)
    Watchman.submit("network.mtr.snt", hop.snt, :gauge, :tags => tags)
    Watchman.submit("network.mtr.last", hop.last, :gauge, :tags => tags)
    Watchman.submit("network.mtr.avg", hop.avg, :gauge, :tags => tags)
    Watchman.submit("network.mtr.best", hop.best, :gauge, :tags => tags)
    Watchman.submit("network.mtr.worst", hop.worst, :gauge, :tags => tags)
    Watchman.submit("network.mtr.std_dev", hop.std_dev, :gauge, :tags => tags)
  end
end