From aa6e87c5c17bd0122972548f214cf52986c77336 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Fri, 22 Mar 2013 15:47:25 -0400 Subject: [PATCH] Dashboard memory warning --- app/models/admin_dashboard_data.rb | 8 +++++++- config/locales/server.en.yml | 1 + lib/mem_info.rb | 15 +++++++++++++++ spec/models/admin_dashboard_data_spec.rb | 19 +++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 lib/mem_info.rb diff --git a/app/models/admin_dashboard_data.rb b/app/models/admin_dashboard_data.rb index 09e7b5af642..9db1e700bd1 100644 --- a/app/models/admin_dashboard_data.rb +++ b/app/models/admin_dashboard_data.rb @@ -1,3 +1,5 @@ +require_dependency 'mem_info' + class AdminDashboardData REPORTS = ['visits', 'signups', 'topics', 'posts', 'flags', 'users_by_trust_level', 'likes', 'emails'] @@ -9,7 +11,7 @@ class AdminDashboardData def as_json @json ||= { reports: REPORTS.map { |type| Report.find(type) }, - problems: [rails_env_check, host_names_check, gc_checks, sidekiq_check || clockwork_check].compact + problems: [rails_env_check, host_names_check, gc_checks, sidekiq_check || clockwork_check, ram_check].compact }.merge( SiteSetting.version_checks? ? {version_check: DiscourseUpdates.check_version} : {} ) @@ -35,4 +37,8 @@ class AdminDashboardData def clockwork_check I18n.t('dashboard.clockwork_warning') unless Jobs::ClockworkHeartbeat.is_clockwork_running? end + + def ram_check + I18n.t('dashboard.memory_warning') if MemInfo.new.mem_total and MemInfo.new.mem_total < 1_000_000 + end end \ No newline at end of file diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index b18bc11da35..75851f0ce88 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -286,6 +286,7 @@ en: gc_warning: 'Your server is using default ruby garbage collection parameters, which will not give you the best performance. Read this topic on performance tuning: Tuning Ruby and Rails for Discourse.' clockwork_warning: 'Clockwork is not running. Ensure that a clockwork process is always running so that important jobs can be scheduled. Learn about clockwork here.' sidekiq_warning: 'Sidekiq is not running. Many tasks, like sending emails, are executed asynchronously by sidekiq. Please ensure at least one sidekiq process is running. Learn about Sidekiq here.' + memory_warning: 'Your server is running with less than 1 GB of total memory. At least 1 GB of memory is recommended.' site_settings: default_locale: "The default language of this Discourse instance (ISO 639-1 Code)" diff --git a/lib/mem_info.rb b/lib/mem_info.rb new file mode 100644 index 00000000000..d03ea6999a6 --- /dev/null +++ b/lib/mem_info.rb @@ -0,0 +1,15 @@ +class MemInfo + + # Total memory in kb. Only works on systems with /proc/meminfo. + # Returns nil if it cannot be determined. + def mem_total + @mem_total ||= begin + if s = `grep MemTotal /proc/meminfo` + /(\d+)/.match(s)[0].try(:to_i) + else + nil + end + end + end + +end \ No newline at end of file diff --git a/spec/models/admin_dashboard_data_spec.rb b/spec/models/admin_dashboard_data_spec.rb index 5c4c64e6fd0..0fbfccaaead 100644 --- a/spec/models/admin_dashboard_data_spec.rb +++ b/spec/models/admin_dashboard_data_spec.rb @@ -102,4 +102,23 @@ describe AdminDashboardData do end end + describe 'ram_check' do + subject { AdminDashboardData.new.ram_check } + + it 'returns nil when total ram is 1 GB' do + MemInfo.any_instance.stubs(:mem_total).returns(1025272) + subject.should be_nil + end + + it 'returns nil when total ram cannot be determined' do + MemInfo.any_instance.stubs(:mem_total).returns(nil) + subject.should be_nil + end + + it 'returns a string when total ram is less than 1 GB' do + MemInfo.any_instance.stubs(:mem_total).returns(512636) + subject.should_not be_nil + end + end + end \ No newline at end of file