Class: MtrMonitor::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/mtr_monitor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, domain, s3_bucket, mtr_options, aws_access_key_id, aws_secret_access_key, dig_ip_address, logger = nil) ⇒ Report

Returns a new instance of Report.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mtr_monitor.rb', line 33

def initialize(name, domain, s3_bucket, mtr_options, aws_access_key_id, aws_secret_access_key, dig_ip_address, logger = nil)
  @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
  @host_ip_address       = `dig +short myip.opendns.com @resolver1.opendns.com`.strip
  @hostname              = `hostname`.strip
  @dig_ip_address        = dig_ip_address
  @logger                = logger || Logger.new(STDOUT)

  path = "#{@name}/#{Time.now.strftime("%Y-%m-%d")}/#{@host_ip_address.gsub(".", "-")}/#{Time.now.strftime("%H-%M")}.log"

  @log_path  = "#{MtrMonitor::REPORTS_PATH}/#{path.gsub("/", "-")}"
  @s3_path   = "s3://#{@s3_bucket}/#{path}"
end

Instance Attribute Details

#host_ip_addressObject (readonly)

Returns the value of attribute host_ip_address.



31
32
33
# File 'lib/mtr_monitor.rb', line 31

def host_ip_address
  @host_ip_address
end

#log_pathObject (readonly)

Returns the value of attribute log_path.



28
29
30
# File 'lib/mtr_monitor.rb', line 28

def log_path
  @log_path
end

#loggerObject (readonly)

Returns the value of attribute logger.



30
31
32
# File 'lib/mtr_monitor.rb', line 30

def logger
  @logger
end

#s3_pathObject (readonly)

Returns the value of attribute s3_path.



29
30
31
# File 'lib/mtr_monitor.rb', line 29

def s3_path
  @s3_path
end

Instance Method Details

#digObject



77
78
79
80
# File 'lib/mtr_monitor.rb', line 77

def dig
  logger.info "Dig #{@domain} IP address"
  run("dig +short #{@domain}").split("\n").first
end

#generateObject



51
52
53
54
55
56
57
58
59
# File 'lib/mtr_monitor.rb', line 51

def generate
  logger.info "Local Path: #{@log_path}"
  logger.info "S3 Path: #{@s3_path}"

  generate_local_report
  upload_to_s3

  MtrMonitor::Metrics.submit(@log_path, @hostname, @name, @logger)
end

#generate_local_reportObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mtr_monitor.rb', line 61

def generate_local_report
  destination = @dig_ip_address ? dig : @domain

  run %Q(sudo mkdir -p /var/log/mtr)
  run %Q(sudo sh -c 'echo "Name: #{@name}" >> #{@log_path}')
  run %Q(sudo sh -c 'echo "Domain: #{@domain}" >> #{@log_path}')
  run %Q(sudo sh -c 'echo "Source IP: #{@host_ip_address}" >> #{@log_path}')
  run %Q(sudo sh -c 'echo "Target IP: #{destination}" >> #{@log_path}')
  run %Q(sudo sh -c 'echo "---------------------------------------" >> #{@log_path}')
  run %Q(sudo sh -c 'mtr --report #{@mtr_options} #{destination} >> #{@log_path}')
end

#run(cmd) ⇒ Object



82
83
84
85
86
# File 'lib/mtr_monitor.rb', line 82

def run(cmd)
  logger.info "Running: #{cmd}"

  `#{cmd}`
end

#upload_to_s3Object



73
74
75
# File 'lib/mtr_monitor.rb', line 73

def upload_to_s3
  run %Q(AWS_ACCESS_KEY_ID='#{@aws_access_key_id}' AWS_SECRET_ACCESS_KEY='#{@aws_secret_access_key}' aws s3 cp #{@log_path} #{@s3_path})
end