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(options) ⇒ Report

Returns a new instance of Report.



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

def initialize(options)
  @name                  = options.fetch(:name)
  @domain                = options.fetch(:mtr_target)
  @s3_bucket             = options.fetch(:s3_bucket)
  @aws_access_key_id     = options.fetch(:aws_access_key_id)
  @aws_secret_access_key = options.fetch(:aws_secret_access_key)
  @mtr_options           = options.fetch(:mtr_options)
  @dig_ip_address        = options.fetch(:dig_ip_address)
  @logger                = options.fetch(:logger, Logger.new(STDOUT))
  @logdna_ingestion_key  = options.fetch(:logdna_ingestion_key)

  @host_ip_address       = `dig +short myip.opendns.com @resolver1.opendns.com`.strip
  @hostname              = `hostname`.strip
  @created_at            = Time.now.utc
end

Instance Attribute Details

#host_ip_addressObject (readonly)

Returns the value of attribute host_ip_address.



33
34
35
# File 'lib/mtr_monitor.rb', line 33

def host_ip_address
  @host_ip_address
end

#log_pathObject (readonly)

Returns the value of attribute log_path.



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

def log_path
  @log_path
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#s3_pathObject (readonly)

Returns the value of attribute s3_path.



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

def s3_path
  @s3_path
end

Instance Method Details

#destinationObject



89
90
91
# File 'lib/mtr_monitor.rb', line 89

def destination
  @dig_ip_address ? dig : @domain
end

#digObject



97
98
99
100
# File 'lib/mtr_monitor.rb', line 97

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

#generateObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/mtr_monitor.rb', line 67

def generate
  logger.info "Local Path: #{local_log_path}"
  logger.info "S3 URI: #{s3_uri}"

  generate_local_report
  upload_to_s3

  MtrMonitor::LogDna.submit(@name, @logdna_ingestion_key, local_log_path, @host_ip_address, s3_url, @hostname, @logger)
  MtrMonitor::Metrics.submit(local_log_path, @hostname, @name, @logger)
end

#generate_local_reportObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/mtr_monitor.rb', line 78

def generate_local_report
  run %Q(sudo mkdir -p /var/log/mtr)
  run %Q(sudo sh -c 'echo "Version:   #{MtrMonitor::VERSION.ljust(30)}" >> #{local_log_path}')
  run %Q(sudo sh -c 'echo "Name:      #{@name.ljust(30)}" >> #{local_log_path}')
  run %Q(sudo sh -c 'echo "Domain:    #{@domain.ljust(30)}" >> #{local_log_path}')
  run %Q(sudo sh -c 'echo "Source IP: #{@host_ip_address.ljust(30)}" >> #{local_log_path}')
  run %Q(sudo sh -c 'echo "Target IP: #{destination.ljust(30)}" >> #{local_log_path}')
  run %Q(sudo sh -c 'echo "-----------------------------------------" >> #{local_log_path}')
  run %Q(sudo sh -c 'mtr --report --report-wide #{@mtr_options} #{destination} >> #{local_log_path}')
end

#local_log_pathObject



59
60
61
# File 'lib/mtr_monitor.rb', line 59

def local_log_path
  "#{MtrMonitor::LOCAL_REPORTS_DIR}/#{path.gsub("/", "__")}"
end

#pathObject



63
64
65
# File 'lib/mtr_monitor.rb', line 63

def path
  "#{@name}/#{@created_at.strftime("%Y-%m-%d_%H-%M")}/#{@host_ip_address.gsub(".", "-")}_to_#{@domain.gsub(".", "-")}.txt"
end

#run(cmd) ⇒ Object



102
103
104
105
106
# File 'lib/mtr_monitor.rb', line 102

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

  `#{cmd}`
end

#s3_uriObject



55
56
57
# File 'lib/mtr_monitor.rb', line 55

def s3_uri
  "s3://#{@s3_bucket}/#{path}"
end

#s3_urlObject



51
52
53
# File 'lib/mtr_monitor.rb', line 51

def s3_url
  "https://s3.eu-central-1.amazonaws.com/#{@s3_bucket}/#{path}"
end

#upload_to_s3Object



93
94
95
# File 'lib/mtr_monitor.rb', line 93

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 #{local_log_path} #{s3_uri})
end