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
- #emit_provider_to_logdna ⇒ Object
- #generate ⇒ Object
- #generate_local_report ⇒ Object
- #gtt ⇒ Object
-
#initialize(name, domain, s3_bucket, mtr_options, aws_access_key_id, aws_secret_access_key, dig_ip_address, logdna_ingestion_key, logger = nil) ⇒ Report
constructor
A new instance of Report.
- #run(cmd) ⇒ Object
- #telia ⇒ Object
- #unknown ⇒ 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, logdna_ingestion_key, logger = nil) ⇒ Report
Returns a new instance of Report.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/mtr_monitor.rb', line 34 def initialize(name, domain, s3_bucket, , aws_access_key_id, aws_secret_access_key, dig_ip_address, logdna_ingestion_key, 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) @logdna = Logdna::Ruby.new(logdna_ingestion_key, :ip => @host_ip_address, :app => "MTR monitor", :meta => { :route => @name }, :level => "DEBUG") path = "#{@name}/#{Time.now.strftime("%Y-%m-%d")}/#{@host_ip_address.gsub(".", "-")}/#{Time.now.utc.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.
32 33 34 |
# File 'lib/mtr_monitor.rb', line 32 def host_ip_address @host_ip_address end |
#log_path ⇒ Object (readonly)
Returns the value of attribute log_path.
29 30 31 |
# File 'lib/mtr_monitor.rb', line 29 def log_path @log_path end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
31 32 33 |
# File 'lib/mtr_monitor.rb', line 31 def logger @logger end |
#s3_path ⇒ Object (readonly)
Returns the value of attribute s3_path.
30 31 32 |
# File 'lib/mtr_monitor.rb', line 30 def s3_path @s3_path end |
Instance Method Details
#dig ⇒ Object
111 112 113 114 |
# File 'lib/mtr_monitor.rb', line 111 def dig logger.info "Dig #{@domain} IP address" run("dig +short #{@domain}").split("\n").first end |
#emit_provider_to_logdna ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/mtr_monitor.rb', line 68 def emit_provider_to_logdna mtr_log = File.read(@log_path) if mtr_log.include?("telia.net") telia elsif mtr_log.include?("gtt.net") gtt else unknown end end |
#generate ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mtr_monitor.rb', line 57 def generate logger.info "Local Path: #{@log_path}" logger.info "S3 Path: #{@s3_path}" generate_local_report upload_to_s3 emit_provider_to_logdna MtrMonitor::Metrics.submit(@log_path, @hostname, @name, @logger) end |
#generate_local_report ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/mtr_monitor.rb', line 95 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 |
#gtt ⇒ Object
85 86 87 88 |
# File 'lib/mtr_monitor.rb', line 85 def gtt @logdna.log("GTT network provider") @logger.info("Network provider: GTT -- Emitted to LogDNA") end |
#run(cmd) ⇒ Object
116 117 118 119 120 |
# File 'lib/mtr_monitor.rb', line 116 def run(cmd) logger.info "Running: #{cmd}" `#{cmd}` end |
#telia ⇒ Object
80 81 82 83 |
# File 'lib/mtr_monitor.rb', line 80 def telia @logdna.log("Telia network provider") @logger.info("Network provider: Telia -- Emitted to LogDNA") end |
#unknown ⇒ Object
90 91 92 93 |
# File 'lib/mtr_monitor.rb', line 90 def unknown @logdna.log("Unknown network provider ") @logger.info("Network provider: Unknown -- Emitted to LogDNA") end |
#upload_to_s3 ⇒ Object
107 108 109 |
# File 'lib/mtr_monitor.rb', line 107 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 |