Class: Amiok

Inherits:
Object
  • Object
show all
Defined in:
lib/amiok.rb

Constant Summary collapse

POSSIBLE_LOCATIONS =
%w(
  /etc/apache2
  /etc/httpd
)
GOOD_RESPONSES =
%w(200 301 302 401 403)

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.outputObject

Returns the value of attribute output.



9
10
11
# File 'lib/amiok.rb', line 9

def output
  @output
end

Class Method Details

.run(argv) ⇒ Object



75
76
77
# File 'lib/amiok.rb', line 75

def self.run(argv)
  new.run
end

Instance Method Details

#_failedObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/amiok.rb', line 47

def _failed
  domains.inject([]) do |failed, domain|
    status_code, status_message = status(domain)
    write('.')
    unless GOOD_RESPONSES.include?(status_code)
      failed << { 'domain' => domain, 'status_code' => status_code, 'status_message' => status_message }
    end
    failed
  end
end

#apache_directoryObject



13
14
15
16
17
# File 'lib/amiok.rb', line 13

def apache_directory
  POSSIBLE_LOCATIONS.find do |path|
    File.exist?(path)
  end
end

#curl(domain) ⇒ Object



34
35
36
# File 'lib/amiok.rb', line 34

def curl(domain)
  `curl --connect-timeout 2 --max-time 10 -s -I http://#{domain}`
end

#domainsObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/amiok.rb', line 23

def domains
  grep.split("\n").inject([]) do |domains, line|
    config = line.split(':', 2)[-1].strip
    domain = config.split(' ')[-1]
    unless config.start_with?('#') or domain.start_with?('_') or domain.include?('example.com')
      domains << domain
    end
    domains
  end
end

#failedObject



58
59
60
# File 'lib/amiok.rb', line 58

def failed
  @failed ||= _failed
end

#grepObject



19
20
21
# File 'lib/amiok.rb', line 19

def grep
  `grep -r -i -e 'server\\(name\\|alias\\)' #{apache_directory} 2> /dev/null`
end

#runObject



62
63
64
65
66
67
68
# File 'lib/amiok.rb', line 62

def run
  just = failed.map { |f| f['domain'].length }.max
  write("\n")
  failed.each do |f|
    write("#{f['domain'].ljust(just + 5)}#{f['status_message']}\n")
  end
end

#status(domain) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/amiok.rb', line 38

def status(domain)
  if status_line = curl(domain).split("\n")[0]
    status = status_line.split(' ', 2)[-1]
    status.split(' ', 2)
  else
    ['', "Can't find server"]
  end
end

#write(str) ⇒ Object



70
71
72
73
# File 'lib/amiok.rb', line 70

def write(str)
  self.class.output.write(str)
  self.class.output.flush
end