pghero 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of pghero might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dc8ca524f848315726b63a987ca3a39ca7768d13
4
- data.tar.gz: 230b213962882f7a62615f90fc9bc6b45cc97da5
3
+ metadata.gz: 408d3e02f0d03c8da95d11eaeea8a6c0fb8be19e
4
+ data.tar.gz: 557d0b3c72e111fe2355b80eda417ad731d2a345
5
5
  SHA512:
6
- metadata.gz: 74956a91d498cf2b68d7f3dd261d7b727b7695d84a89851514ab8d31ce617f0c6005e18901a792aeb92c12e718ea4847518553ca74c6304f31f4b9aab7af2483
7
- data.tar.gz: 0f63ae5e71393fab956c8d2df185acb4abbe3ce1aaba3b0fed637fc7404011f538b7070c8a0598824968fc7243bdce9845f8cf837b255a64cee3cc7ee5c2eae6
6
+ metadata.gz: e480e555168eaea71a6eada2f669058ee6097d0ac54e1812648025deeae343727a35f7c30287af8abb5c451d2de6d481fd9c099cf96e76fc92b7becd82740e00
7
+ data.tar.gz: 8bd10dc018837518f2f623575d2b6f1c2f975b493b1434800fa31a2f999b933bc70318751041431e7ae7d63dc83602c2982b439e6c569dcd0dfd4de7442ef566
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.1.6
2
+
3
+ - Added methods to create and drop users
4
+ - Added locks
5
+
1
6
  ## 0.1.5
2
7
 
3
8
  - Added system stats for Amazon RDS
data/README.md CHANGED
@@ -8,6 +8,8 @@ Postgres insights made easy
8
8
 
9
9
  Supports PostgreSQL 9.2+
10
10
 
11
+ :speech_balloon: Get [handcrafted updates](http://chartkick.us7.list-manage.com/subscribe?u=952c861f99eb43084e0a49f98&id=6ea6541e8e&group[0][32]=true) for new features
12
+
11
13
  For pure SQL, check out [PgHero.sql](https://github.com/ankane/pghero.sql)
12
14
 
13
15
  A big thanks to [Craig Kerstiens](http://www.craigkerstiens.com/2013/01/10/more-on-postgres-performance/) and [Heroku](https://blog.heroku.com/archives/2013/5/10/more_insight_into_your_database_with_pgextras) for the initial queries :clap:
@@ -41,12 +43,18 @@ PgHero.database_size
41
43
  PgHero.relation_sizes
42
44
  PgHero.index_hit_rate
43
45
  PgHero.table_hit_rate
46
+ ```
44
47
 
45
- # kill queries
48
+ Kill queries
49
+
50
+ ```ruby
46
51
  PgHero.kill(pid)
47
52
  PgHero.kill_all
53
+ ```
54
+
55
+ Query stats
48
56
 
49
- # query stats
57
+ ```ruby
50
58
  PgHero.query_stats_enabled?
51
59
  PgHero.enable_query_stats
52
60
  PgHero.disable_query_stats
@@ -55,6 +63,35 @@ PgHero.query_stats
55
63
  PgHero.slow_queries
56
64
  ```
57
65
 
66
+ ## Users
67
+
68
+ Create a user
69
+
70
+ ```ruby
71
+ PgHero.create_user("link")
72
+ # {password: "zbTrNHk2tvMgNabFgCo0ws7T"}
73
+ ```
74
+
75
+ This generates and returns a secure password. The user has full access to the `public` schema.
76
+
77
+ Read-only access
78
+
79
+ ```ruby
80
+ PgHero.create_user("epona", readonly: true)
81
+ ```
82
+
83
+ Set the password
84
+
85
+ ```ruby
86
+ PgHero.create_user("zelda", password: "hyrule")
87
+ ```
88
+
89
+ Drop a user
90
+
91
+ ```ruby
92
+ PgHero.drop_user("ganondorf")
93
+ ```
94
+
58
95
  ## Security
59
96
 
60
97
  #### Basic Authentication
@@ -123,7 +160,7 @@ SELECT pg_stat_statements_reset();
123
160
 
124
161
  ## System Stats
125
162
 
126
- System stats are available for Amazon RDS. Add these lines to your application’s Gemfile:
163
+ CPU usage is available for Amazon RDS. Add these lines to your application’s Gemfile:
127
164
 
128
165
  ```ruby
129
166
  gem 'aws-sdk'
@@ -346,12 +346,12 @@
346
346
  <ul class="nav">
347
347
  <!-- poor man's active_link_to -->
348
348
  <li class="<%= controller.action_name == "index" ? "active" : "" %>"><%= link_to "Status", root_path %></li>
349
- <% if @query_stats_enabled %>
350
- <li class="<%= controller.action_name == "query_stats" ? "active" : "" %>"><%= link_to "Queries", query_stats_path %></li>
351
- <% end %>
352
349
  <% if @system_stats_enabled %>
353
350
  <li class="<%= controller.action_name == "system_stats" ? "active" : "" %>"><%= link_to "System", system_stats_path %></li>
354
351
  <% end %>
352
+ <% if @query_stats_enabled %>
353
+ <li class="<%= controller.action_name == "query_stats" ? "active" : "" %>"><%= link_to "Queries", query_stats_path %></li>
354
+ <% end %>
355
355
  <li class="<%= controller.action_name == "indexes" ? "active" : "" %>"><%= link_to "Indexes", indexes_path %></li>
356
356
  <li class="<%= controller.action_name == "space" ? "active" : "" %>"><%= link_to "Space", space_path %></li>
357
357
  <li class="<%= controller.action_name == "explain" ? "active" : "" %>"><%= link_to "Explain", explain_path %></li>
@@ -2,5 +2,5 @@
2
2
  <h1>CPU Usage</h1>
3
3
 
4
4
  <%= javascript_include_tag "//www.google.com/jsapi", "chartkick" %>
5
- <div style="margin-bottom: 20px;"><%= line_chart @cpu_usage %></div>
5
+ <div style="margin-bottom: 20px;"><%= line_chart @cpu_usage, max: 101 %></div>
6
6
  </div>
data/lib/pghero.rb CHANGED
@@ -53,6 +53,26 @@ module PgHero
53
53
  }
54
54
  end
55
55
 
56
+ def locks
57
+ select_all %Q{
58
+ SELECT DISTINCT ON (pid)
59
+ pg_stat_activity.pid,
60
+ pg_stat_activity.query,
61
+ age(now(), pg_stat_activity.query_start) AS age
62
+ FROM
63
+ pg_stat_activity
64
+ INNER JOIN
65
+ pg_locks ON pg_locks.pid = pg_stat_activity.pid
66
+ WHERE
67
+ pg_stat_activity.query <> '<insufficient privilege>'
68
+ AND pg_locks.mode = 'ExclusiveLock'
69
+ AND pg_stat_activity.pid <> pg_backend_pid()
70
+ ORDER BY
71
+ pid,
72
+ query_start
73
+ }
74
+ end
75
+
56
76
  def index_hit_rate
57
77
  select_all(%Q{
58
78
  SELECT
@@ -297,6 +317,70 @@ module PgHero
297
317
  !!(defined?(AWS) && access_key_id && secret_access_key && db_instance_identifier)
298
318
  end
299
319
 
320
+ def random_password
321
+ require "securerandom"
322
+ SecureRandom.base64(40).delete("+/=")[0...24]
323
+ end
324
+
325
+ def create_user(user, options = {})
326
+ password = options[:password] || random_password
327
+ schema = options[:schema] || "public"
328
+ database = options[:database] || Connection.connection_config[:database]
329
+
330
+ commands =
331
+ [
332
+ "CREATE ROLE #{user} LOGIN PASSWORD #{Connection.connection.quote(password)}",
333
+ "GRANT CONNECT ON DATABASE #{database} TO #{user}",
334
+ "GRANT USAGE ON SCHEMA #{schema} TO #{user}"
335
+ ]
336
+ if options[:readonly]
337
+ commands << "GRANT SELECT ON ALL TABLES IN SCHEMA #{schema} TO #{user}"
338
+ commands << "ALTER DEFAULT PRIVILEGES IN SCHEMA #{schema} GRANT SELECT ON TABLES TO #{user}"
339
+ else
340
+ commands << "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA #{schema} TO #{user}"
341
+ commands << "GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA #{schema} TO #{user}"
342
+ commands << "ALTER DEFAULT PRIVILEGES IN SCHEMA #{schema} GRANT ALL PRIVILEGES ON TABLES TO #{user}"
343
+ commands << "ALTER DEFAULT PRIVILEGES IN SCHEMA #{schema} GRANT ALL PRIVILEGES ON SEQUENCES TO #{user}"
344
+ end
345
+
346
+ # run commands
347
+ Connection.transaction do
348
+ commands.each do |command|
349
+ execute command
350
+ end
351
+ end
352
+
353
+ {password: password}
354
+ end
355
+
356
+ def drop_user(user, options = {})
357
+ schema = options[:schema] || "public"
358
+ database = options[:database] || Connection.connection_config[:database]
359
+
360
+ # thanks shiftb
361
+ commands =
362
+ [
363
+ "REVOKE CONNECT ON DATABASE #{database} FROM #{user}",
364
+ "REVOKE USAGE ON SCHEMA #{schema} FROM #{user}",
365
+ "REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA #{schema} FROM #{user}",
366
+ "REVOKE ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA #{schema} FROM #{user}",
367
+ "ALTER DEFAULT PRIVILEGES IN SCHEMA #{schema} REVOKE SELECT ON TABLES FROM #{user}",
368
+ "ALTER DEFAULT PRIVILEGES IN SCHEMA #{schema} REVOKE SELECT ON SEQUENCES FROM #{user}",
369
+ "ALTER DEFAULT PRIVILEGES IN SCHEMA #{schema} REVOKE ALL ON SEQUENCES FROM #{user}",
370
+ "ALTER DEFAULT PRIVILEGES IN SCHEMA #{schema} REVOKE ALL ON TABLES FROM #{user}",
371
+ "DROP ROLE #{user}"
372
+ ]
373
+
374
+ # run commands
375
+ Connection.transaction do
376
+ commands.each do |command|
377
+ execute command
378
+ end
379
+ end
380
+
381
+ true
382
+ end
383
+
300
384
  def access_key_id
301
385
  ENV["PGHERO_ACCESS_KEY_ID"] || ENV["AWS_ACCESS_KEY_ID"]
302
386
  end
@@ -1,3 +1,3 @@
1
1
  module PgHero
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pghero
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-04 00:00:00.000000000 Z
11
+ date: 2014-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -141,4 +141,3 @@ summary: Database insights made easy
141
141
  test_files:
142
142
  - test/pghero_test.rb
143
143
  - test/test_helper.rb
144
- has_rdoc: