-
Notifications
You must be signed in to change notification settings - Fork 1
/
discourse-doctor.rb
137 lines (110 loc) · 3.44 KB
/
discourse-doctor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env ruby
Dir.chdir("/") do
require 'bundler/inline'
require 'net/http'
require 'net/smtp'
gemfile(true, ui: false) do
source 'https://rubygems.org'
gem 'dnsruby'
gem 'paint'
gem 'sys-filesystem' # for checking available disk space
end
require 'sys/filesystem'
include Sys
end
DISCOURSE_PATH = "/var/www/discourse"
def log(level, message)
$stderr.puts("#{level} #{message}")
end
def warning(message)
log(Paint['warning', :yellow], message)
end
def error(message, log = nil)
log(Paint['error', :red], message)
exit(1)
end
def info(message)
log(Paint['info', :green], message)
end
def check_env_var(var)
if ENV[var].nil? || ENV[var].empty?
error("#{var} is blank, edit containers/app.yml variables")
end
end
def check_smtp_config
info("Check SMTP configuration...")
check_env_var("DISCOURSE_SMTP_ADDRESS")
check_env_var("DISCOURSE_SMTP_PORT")
check_env_var("DISCOURSE_SMTP_USER_NAME")
check_env_var("DISCOURSE_SMTP_PASSWORD")
begin
Net::SMTP.start(ENV["DISCOURSE_SMTP_ADDRESS"], ENV["DISCOURSE_SMTP_PORT"])
.auth_login(ENV["DISCOURSE_SMTP_USER_NAME"], ENV["DISCOURSE_SMTP_PASSWORD"])
rescue Exception => e
error("Couldn’t connect to SMTP server: #{e}")
end
end
def grep_logs
info("Search logs for errors...")
system("grep -E -w \"error|warning\" \"#{File.join(DISCOURSE_PATH, "log", "production.log")}\" | sort | uniq -c | sort -r")
end
def check_hostname
info("Perform checks on the hostname...")
check_env_var("DISCOURSE_HOSTNAME")
begin
resolver = Dnsruby::Resolver.new
request = resolver.query(ENV["DISCOURSE_HOSTNAME"], Dnsruby::Types.TXT)
answers = request.answer.map(&:to_s)
if answers.select { |a| a.include?("spf") }.empty?
warning("Please check SPF is correctly configured on this domain")
end
if answers.select { |a| a.include?("dkim") }.empty?
warning("Please check DKIM is correctly configured on this domain")
end
rescue Dnsruby::NXDomain => e
error("Non-existent Internet Domain Names Definition (NXDOMAIN) for: #{ENV["DISCOURSE_HOSTNAME"]}")
end
url = URI.parse("http://downforeveryoneorjustme.com/#{ENV["DISCOURSE_HOSTNAME"]}")
request = Net::HTTP.new(url.host, url.port)
result = request.request_get(url.path)
if result.body.include?("It's not just you")
error("The internets can’t reach: #{ENV["DISCOURSE_HOSTNAME"]}")
end
end
def check_plugins
unofficial_plugins = []
base_plugins = %w(
discourse-details
discourse-narrative-bot
discourse-nginx-performance-report
lazyYT
poll
)
Dir.chdir("plugins") do
plugins = Dir.glob('*') - base_plugins
plugins.each do |plugin|
url = URI.parse("https://github.com/discourse/#{plugin}")
request = Net::HTTP.new(url.host, url.port)
request.use_ssl = true
result = request.request_head(url.path)
if result.code == "404"
unofficial_plugins << plugin
end
end
end
unless unofficial_plugins.empty?
warning("If you encounter issues, you might want to consider disabling these unofficial plugins: #{unofficial_plugins.join(',')}")
end
end
def check_available_disk_space
stat = Filesystem.stat(DISCOURSE_PATH)
bytes_available = stat.blocks_available * stat.block_size
if bytes_available < 5 * 1024 * 1024
warning("The available disk space at #{DISCOURSE_PATH} is less than 5 GB")
end
end
check_smtp_config
check_hostname
check_plugins
check_available_disk_space
grep_logs