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
- #destination ⇒ Object
- #dig ⇒ Object
- #generate ⇒ Object
- #generate_local_report ⇒ Object
-
#initialize(options) ⇒ Report
constructor
A new instance of Report.
- #local_log_path ⇒ Object
- #path ⇒ Object
- #run(cmd) ⇒ Object
- #s3_uri ⇒ Object
- #s3_url ⇒ Object
- #upload_to_s3 ⇒ Object
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() @name = .fetch(:name) @domain = .fetch(:mtr_target) @s3_bucket = .fetch(:s3_bucket) @aws_access_key_id = .fetch(:aws_access_key_id) @aws_secret_access_key = .fetch(:aws_secret_access_key) @mtr_options = .fetch(:mtr_options) @dig_ip_address = .fetch(:dig_ip_address) @logger = .fetch(:logger, Logger.new(STDOUT)) @logdna_ingestion_key = .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_address ⇒ Object (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_path ⇒ Object (readonly)
Returns the value of attribute log_path.
30 31 32 |
# File 'lib/mtr_monitor.rb', line 30 def log_path @log_path end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
32 33 34 |
# File 'lib/mtr_monitor.rb', line 32 def logger @logger end |
#s3_path ⇒ Object (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
#destination ⇒ Object
89 90 91 |
# File 'lib/mtr_monitor.rb', line 89 def destination @dig_ip_address ? dig : @domain end |
#dig ⇒ Object
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 |
#generate ⇒ Object
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_report ⇒ Object
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_path ⇒ Object
59 60 61 |
# File 'lib/mtr_monitor.rb', line 59 def local_log_path "#{MtrMonitor::LOCAL_REPORTS_DIR}/#{path.gsub("/", "__")}" end |
#path ⇒ Object
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_uri ⇒ Object
55 56 57 |
# File 'lib/mtr_monitor.rb', line 55 def s3_uri "s3://#{@s3_bucket}/#{path}" end |
#s3_url ⇒ Object
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_s3 ⇒ Object
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 |