From b1af814eabbcfbdd23f7bcb17723c82580135e63 Mon Sep 17 00:00:00 2001 From: Vishnu Narayanan Date: Mon, 30 Jan 2023 18:37:51 +0530 Subject: [PATCH] feat: add chatwoot instance status in superadmin (#6045) * feat: add chatwoot instance status in superadmin * feat: add redis metrics to instance health page * chore: fix rubocop * chore: rescue redis no connection * chore: add rspec * chore: refactor * feat: add instance health to /api * chore: rescue postgres * chore: fix spec --- app/controllers/api_controller.rb | 20 ++++++++- .../instance_statuses_controller.rb | 44 +++++++++++++++++++ .../application/_navigation.html.erb | 7 ++- .../instance_statuses/show.html.erb | 23 ++++++++++ config/routes.rb | 1 + spec/controllers/api_controller_spec.rb | 2 + .../instance_statuses_controller_spec.rb | 25 +++++++++++ 7 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 app/controllers/super_admin/instance_statuses_controller.rb create mode 100644 app/views/super_admin/instance_statuses/show.html.erb create mode 100644 spec/controllers/super_admin/instance_statuses_controller_spec.rb diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb index a7764cbc9..23178b5f3 100644 --- a/app/controllers/api_controller.rb +++ b/app/controllers/api_controller.rb @@ -2,6 +2,24 @@ class ApiController < ApplicationController skip_before_action :set_current_user, only: [:index] def index - render json: { version: Chatwoot.config[:version], timestamp: Time.now.utc.to_formatted_s(:db) } + render json: { version: Chatwoot.config[:version], + timestamp: Time.now.utc.to_formatted_s(:db), + queue_services: redis_status, + data_services: postgres_status } + end + + private + + def redis_status + r = Redis.new(Redis::Config.app) + return 'ok' if r.ping + rescue Redis::CannotConnectError + 'failing' + end + + def postgres_status + ActiveRecord::Base.connection.active? ? 'ok' : 'failing' + rescue ActiveRecord::ConnectionNotEstablished + 'failing' end end diff --git a/app/controllers/super_admin/instance_statuses_controller.rb b/app/controllers/super_admin/instance_statuses_controller.rb new file mode 100644 index 000000000..5a99582b6 --- /dev/null +++ b/app/controllers/super_admin/instance_statuses_controller.rb @@ -0,0 +1,44 @@ +class SuperAdmin::InstanceStatusesController < SuperAdmin::ApplicationController + def show + @metrics = {} + chatwoot_version + sha + postgres_status + redis_metrics + end + + def chatwoot_version + @metrics['Chatwoot version'] = Chatwoot.config[:version] + end + + def sha + sha = `git rev-parse HEAD` + @metrics['Git SHA'] = sha.presence || 'n/a' + end + + def postgres_status + @metrics['Postgres alive'] = if ActiveRecord::Base.connection.active? + 'true' + else + 'false' + end + end + + def redis_metrics + r = Redis.new(Redis::Config.app) + if r.ping == 'PONG' + redis_server = r.info + @metrics['Redis alive'] = 'false' + @metrics['Redis version'] = redis_server['redis_version'] + @metrics['Redis number of connected clients'] = redis_server['connected_clients'] + @metrics["Redis 'maxclients' setting"] = redis_server['maxclients'] + @metrics['Redis memory used'] = redis_server['used_memory_human'] + @metrics['Redis memory peak'] = redis_server['used_memory_peak_human'] + @metrics['Redis total memory available'] = redis_server['total_system_memory_human'] + @metrics["Redis 'maxmemory' setting"] = redis_server['maxmemory'] + @metrics["Redis 'maxmemory_policy' setting"] = redis_server['maxmemory_policy'] + end + rescue Redis::CannotConnectError + @metrics['Redis alive'] = false + end +end diff --git a/app/views/super_admin/application/_navigation.html.erb b/app/views/super_admin/application/_navigation.html.erb index 73e7c29af..e09ebb277 100644 --- a/app/views/super_admin/application/_navigation.html.erb +++ b/app/views/super_admin/application/_navigation.html.erb @@ -40,7 +40,7 @@ as defined by the routes in the `admin/` namespace <% Administrate::Namespace.new(namespace).resources.each do |resource| %> - <% next if ["account_users", "dashboard", "devise/sessions", "app_configs" ].include? resource.resource %> + <% next if ["account_users", "dashboard", "devise/sessions", "app_configs", "instance_statuses" ].include? resource.resource %> <% end %> + +