Skip to content

Commit 6676e7c

Browse files
committed
Initial commit.
0 parents  commit 6676e7c

39 files changed

+2395
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.sass-cache
2+
**/.DS_Store

Gemfile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
source "https://rubygems.org"
2+
3+
gem "sinatra"
4+
gem "redis"
5+
gem "sass"

Gemfile.lock

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
rack (1.6.6)
5+
rack-protection (1.5.3)
6+
rack
7+
redis (3.3.3)
8+
sass (3.4.23)
9+
sinatra (1.4.8)
10+
rack (~> 1.5)
11+
rack-protection (~> 1.4)
12+
tilt (>= 1.3, < 3)
13+
tilt (2.0.7)
14+
15+
PLATFORMS
16+
ruby
17+
18+
DEPENDENCIES
19+
redis
20+
sass
21+
sinatra
22+
23+
BUNDLED WITH
24+
1.15.0

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Redis Dashboard
2+
3+
It's a Sinatra web app showing monitoring informations about your Redis server.
4+
You can run it in standalone or inside your Rails app.
5+
6+
## Installation inside a Rails app
7+
8+
Add this line in your Gemfile:
9+
```ruby
10+
gem "redis_dashboard"
11+
```
12+
13+
In your terminal run the following command:
14+
```shell
15+
bundle install
16+
```
17+
18+
Then mount the app from `config/routes.rb`:
19+
```ruby
20+
match "/redis_dashboard" => RedisDashboard::Application
21+
```
22+
23+
RedisDashboard.urls = ["redis://localhost"]
24+
25+
## Authentication
26+
27+
To protect your dashboard you can setup a basic HTTP authentication:
28+
29+
```ruby
30+
RedisDashboard::Application.use(Rack::Auth::Basic) do |user, password|
31+
user == "USER" && password == "PASSWORD"
32+
end
33+
```

lib/redis_dashboard.rb

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module RedisDashboard
2+
def self.urls=(array)
3+
@urls = array
4+
end
5+
6+
def self.urls
7+
@urls ||= ["redis://localhost"]
8+
end
9+
end
10+
11+
require "redis_dashboard/client"
12+
require "redis_dashboard/command"
13+
require "redis_dashboard/application"

lib/redis_dashboard/application.rb

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require "sinatra/base"
2+
require "redis"
3+
4+
class RedisDashboard::Application < Sinatra::Base
5+
after { client.close }
6+
7+
get "/" do
8+
erb(:index, locals: {clients: clients})
9+
end
10+
11+
get "/info" do
12+
erb(:info, locals: {info: client.info})
13+
end
14+
15+
get "/config" do
16+
erb(:config, locals: {config: client.config})
17+
end
18+
19+
get "/clients" do
20+
erb(:clients, locals: {clients: client.clients})
21+
end
22+
23+
get "/slowlog" do
24+
erb(:slowlog, locals: {commands: client.slow_commands})
25+
end
26+
27+
get "/application.css" do
28+
scss(:application, style: :expanded)
29+
end
30+
31+
def client
32+
@client ||= RedisDashboard::Client.new(RedisDashboard.urls[redis_id])
33+
end
34+
35+
def clients
36+
RedisDashboard.urls.map { |url| RedisDashboard::Client.new(url) }
37+
end
38+
39+
helpers do
40+
def epoch_to_short_date_time(epoch)
41+
Time.at(epoch).strftime("%b %d %H:%M")
42+
end
43+
44+
def redis_id
45+
params[:id].to_i
46+
end
47+
end
48+
end

lib/redis_dashboard/client.rb

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class RedisDashboard::Client
2+
attr_reader :url, :connection
3+
4+
def initialize(url)
5+
@url = url
6+
end
7+
8+
def clients
9+
connection.client.call([:client, "list"]).split("\n").map do |line|
10+
line.split(" ").reduce({}) do |hash, str|
11+
pair = str.split("=")
12+
hash[pair[0]] = pair[1]
13+
hash
14+
end
15+
end
16+
end
17+
18+
def config
19+
hash = {}
20+
array = connection.config("get", "*")
21+
while (pair = array.slice!(0, 2)).any?
22+
hash[pair.first] = pair.last
23+
end
24+
hash
25+
end
26+
27+
def info
28+
connection.info
29+
end
30+
31+
def close
32+
connection.close if connection
33+
end
34+
35+
def slow_commands(length = 128) # 128 is the default slowlog-max-len
36+
connection.slowlog("get", length).map do |entry|
37+
cmd = RedisDashboard::Command.new
38+
cmd.id = entry[0]
39+
cmd.timestamp = entry[1]
40+
cmd.microseconds = entry[2]
41+
cmd.command = entry[3]
42+
cmd
43+
end.sort{ |left, right| right.microseconds <=> left.microseconds }
44+
end
45+
46+
private
47+
48+
def connection
49+
@connection ||= Redis.new(url: url)
50+
end
51+
end

lib/redis_dashboard/command.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class RedisDashboard::Command
2+
attr_accessor :id, :timestamp, :microseconds, :command
3+
end

0 commit comments

Comments
 (0)