Class: MtrMonitor::Report
- Inherits:
-
Object
- Object
- MtrMonitor::Report
- Defined in:
- lib/mtr_monitor.rb
Instance Attribute Summary collapse
-
#host_ip_address ⇒ Object
readonly
Returns the value of attribute host_ip_address.
-
#log_path ⇒ Object
readonly
Returns the value of attribute log_path.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#s3_path ⇒ Object
readonly
Returns the value of attribute s3_path.
Instance Method Summary collapse
- #dig ⇒ Object
- #generate ⇒ Object
- #generate_local_report ⇒ Object
-
#initialize(name, domain, s3_bucket, mtr_options, aws_access_key_id, aws_secret_access_key, dig_ip_address, logger = nil) ⇒ Report
constructor
A new instance of Report.
- #run(cmd) ⇒ Object
- #upload_to_s3 ⇒ Object
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.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/mtr_monitor.rb', line 23 def initialize(name, domain, s3_bucket, , 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 = @host_ip_address = `curl http://ifconfig.co`.strip.gsub(".", "-") @hostname = `hostname`.strip.gsub(".", "-") @dig_ip_address = dig_ip_address @logger = logger || Logger.new(STDOUT) @log_name = "#{@name}/#{Time.now.strftime("%Y-%m-%d")}/#{@host_ip_address}/#{Time.now.strftime("%H-%M")}.log" @log_path = "#{MtrMonitor::REPORTS_PATH}/#{@log_name.gsub("/", "-")}" @s3_path = "s3://#{@s3_bucket}/#{@log_name}" end |
Instance Attribute Details
#host_ip_address ⇒ Object (readonly)
Returns the value of attribute host_ip_address.
21 22 23 |
# File 'lib/mtr_monitor.rb', line 21 def host_ip_address @host_ip_address end |
#log_path ⇒ Object (readonly)
Returns the value of attribute log_path.
18 19 20 |
# File 'lib/mtr_monitor.rb', line 18 def log_path @log_path end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
20 21 22 |
# File 'lib/mtr_monitor.rb', line 20 def logger @logger end |
#s3_path ⇒ Object (readonly)
Returns the value of attribute s3_path.
19 20 21 |
# File 'lib/mtr_monitor.rb', line 19 def s3_path @s3_path end |
Instance Method Details
#dig ⇒ Object
66 67 68 69 |
# File 'lib/mtr_monitor.rb', line 66 def dig logger.info "Dig #{@domain} IP address" run("dig +short #{@domain}").split("\n").first end |
#generate ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/mtr_monitor.rb', line 40 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_report ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mtr_monitor.rb', line 50 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
71 72 73 74 75 |
# File 'lib/mtr_monitor.rb', line 71 def run(cmd) logger.info "Running: #{cmd}" `#{cmd}` end |
#upload_to_s3 ⇒ Object
62 63 64 |
# File 'lib/mtr_monitor.rb', line 62 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 |