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.
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, , 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 = @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_address ⇒ Object (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_path ⇒ Object (readonly)
Returns the value of attribute log_path.
28 29 30 |
# File 'lib/mtr_monitor.rb', line 28 def log_path @log_path end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
30 31 32 |
# File 'lib/mtr_monitor.rb', line 30 def logger @logger end |
#s3_path ⇒ Object (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
#dig ⇒ Object
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 |
#generate ⇒ Object
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_report ⇒ Object
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_s3 ⇒ Object
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 |