postgres_monitor 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 29ff454384341fa953a1ad421cd4f721fb91dd05
4
- data.tar.gz: a81b7f4ff4e01d1f09cb192aa043c158a44caf32
3
+ metadata.gz: 3f36aa6bad2a4377b290bbc990ebbe038408b825
4
+ data.tar.gz: 7b98230e7f7f00f24cf402cbc06eb0ff278b86f1
5
5
  SHA512:
6
- metadata.gz: 1ad9882350ef1698530d23e473f5ff08b6a83b3a7826279ceaad27469444bae602f6d6995382b25a01be553d86b5ed43cbb91d495b5df72b0efbe423eff853d4
7
- data.tar.gz: fcec22dcca7f3eb0f8beb6c9a345f7fca7e49e33afc5e5da702124bdf66ce5e0b3a8e7a85a1caf0b484a6e7f2b9c6c5f1fa626ded3a35894e6f860accc692419
6
+ metadata.gz: f4ce5bc9bd330bef5ba935ecded7b35ce19d01eb21dafa68b9d5fae1155e18d204fcd3c845dd481001701c47d4bb2e9b9535ac19318b490836cd64b2b67006f4
7
+ data.tar.gz: 9e4e929e59f44a7cb8dbe7816e796db13a38d4b585578133cd25b5a92104b449303724ec78eb2b6ee5cd4b05d15830e7732a68619b8f2c765c70a16add6e43c4
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ *.gem
data/README.md CHANGED
@@ -27,6 +27,8 @@ Or install it yourself as:
27
27
  ### Connection to the Database
28
28
  To create a connection you will need to supply the host name, user, password (can be nil), and dbname. Port will default to 5432 if it is not provided. sslmode will default to required if it is not supplied.
29
29
 
30
+ Additionally there is a variable for configuring the long running query threshold. It defaults to '5 seconds' and follows the Postgres format for time intervals (e.g. '1 minute', '20 seconds')
31
+
30
32
  #### Example
31
33
 
32
34
  ```ruby
@@ -37,6 +39,7 @@ To create a connection you will need to supply the host name, user, password (ca
37
39
  password: 'password',
38
40
  sslmode: 'allow',
39
41
  dbname: 'postrges'
42
+ long_query_threshold: '1 second'
40
43
  }
41
44
 
42
45
  @monitor = PostgresMonitor::Monitor.new(@connection_params)
@@ -8,6 +8,8 @@ module PostgresMonitor
8
8
  @sslmode = connection_params[:sslmode] ? connection_params[:sslmode] : 'require'
9
9
  @dbname = connection_params[:dbname]
10
10
 
11
+ @long_query_threshold = connection_params[:long_query_threshold] ? connection_params[:long_query_threshold] : '5 seconds'
12
+
11
13
  @connection = self.connect
12
14
  end
13
15
 
@@ -50,8 +52,18 @@ module PostgresMonitor
50
52
  execute_sql 'SELECT datname FROM pg_database WHERE datistemplate = false;'
51
53
  end
52
54
 
55
+ # list connection states and count
56
+ def connection_counts
57
+ execute_sql "SELECT #{state_column}, COUNT(*) FROM pg_stat_activity GROUP BY #{state_column};"
58
+ end
59
+
60
+ ### DEPRECATION WARNING
61
+ # This seems to have an issue with returning multiple duplicate results;
62
+ # Deprecating in favor of connection_counts
53
63
  # returns Active and Idle connections from DB
54
64
  def backend_query
65
+ warn 'DEPRECATED. Please use connection_counts instead'
66
+
55
67
  sql = %Q(
56
68
  SELECT ( SELECT count(*) FROM pg_stat_activity WHERE
57
69
  #{
@@ -75,6 +87,21 @@ module PostgresMonitor
75
87
  execute_sql(sql)
76
88
  end
77
89
 
90
+ # get database sizes
91
+ def get_database_sizes
92
+ sql = %q(
93
+ SELECT
94
+ t1.datname AS db_name,
95
+ pg_size_pretty(pg_database_size(t1.datname)) as db_size
96
+ FROM
97
+ pg_database t1
98
+ ORDER BY
99
+ pg_database_size(t1.datname) desc;
100
+ )
101
+
102
+ execute_sql(sql)
103
+ end
104
+
78
105
  # calculates your cache hit rate (effective databases are at 99% and up)
79
106
  def cache_hit
80
107
  sql = %q(
@@ -242,7 +269,7 @@ module PostgresMonitor
242
269
  AND c.relkind='i';
243
270
  )
244
271
 
245
- self.execute(sql)
272
+ execute_sql(sql)
246
273
  end
247
274
 
248
275
  # show the size of indexes, descending by size
@@ -429,7 +456,7 @@ module PostgresMonitor
429
456
  "AND current_query <> '<IDLE>'"
430
457
  end
431
458
  }
432
- AND now() - pg_stat_activity.query_start > interval '5 minutes'
459
+ AND now() - pg_stat_activity.query_start > interval '#{@long_query_threshold}'
433
460
  ORDER BY
434
461
  now() - pg_stat_activity.query_start DESC;
435
462
  )
@@ -506,6 +533,10 @@ module PostgresMonitor
506
533
  nine_two? ? 'pid' : 'procpid'
507
534
  end
508
535
 
536
+ def state_column
537
+ nine_two? ? 'state' : 'current_query'
538
+ end
539
+
509
540
  def extension_loaded?(extname)
510
541
  @connection.exec("SELECT count(*) FROM pg_extension WHERE extname = '#{extname}'") do |result|
511
542
  result[0]['count'] == '1'
@@ -1,3 +1,3 @@
1
1
  module PostgresMonitor
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postgres_monitor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Shea
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-13 00:00:00.000000000 Z
11
+ date: 2015-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg
@@ -119,4 +119,3 @@ signing_key:
119
119
  specification_version: 4
120
120
  summary: Gem to help monitor Postgres Instances
121
121
  test_files: []
122
- has_rdoc: