Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
pginfo.php
Go to the documentation of this file.
1 <html>
2 <head>
3 <?php
4 print sprintf("<title>%s</title>", isset($_GET['q'])?htmlspecialchars($_GET['q']):'pginfo');
5 ?>
6 <style type="text/css">
7 body {
8  font-family: monospace;
9 }
10 div.menu {
11  background-color: darkgray;
12 }
13 table > thead {
14  background-color: lightgray;
15 }
16 .bar { text-align:right; }
17 </style>
18 </head>
19 <body>
20 <div class="bar"><a href="#" onclick="window.location.reload()">refresh page</a></div>
21 <?php
22 
23 define('PG_PAGE_SIZE', 8 * 1024);
24 
25 include("../../../WHAT/Lib.Common.php");
26 
28 
29 $conn = pg_connect("service='$pgservice_core'");
30 if( $conn === false ) {
31  print sprintf("Error: could not connect to pg service '%s'", $pgservice_core);
32  exit(1);
33 }
34 
35 $query = array();
36 
37 $query['pg_settings'] = sprintf("SELECT name, setting, context, short_desc FROM pg_settings");
38 
39 $query['pg_stat_activity'] = sprintf("SELECT datname, procpid, client_addr, client_port, waiting, query_start, now() - query_start AS time, current_query FROM pg_stat_activity WHERE procpid != pg_backend_pid()");
40 
41 $query['tables'] = sprintf("SELECT s.schemaname, s.relname, c.oid, c.relfilenode, s.seq_scan, s.idx_scan, c.reltuples, c.relpages as pages FROM pg_stat_all_tables as s, pg_class as c WHERE s.relname = c.relname AND s.schemaname IN (SELECT nspname FROM pg_namespace WHERE oid = c.relnamespace) AND ( s.schemaname = 'public' OR s.schemaname = 'pg_toast' )");
42 
43 $query['tables I/O'] = sprintf("SELECT schemaname, relname, heap_blks_read, heap_blks_hit, idx_blks_read, idx_blks_hit, toast_blks_read, toast_blks_hit, tidx_blks_read, tidx_blks_hit FROM pg_statio_all_tables WHERE schemaname IN ('public', 'pg_toast')");
44 
45 $query['indexes'] = sprintf("SELECT s.schemaname, s.relname, s.indexrelname, c.oid, c.relfilenode, s.idx_scan, s.idx_tup_read, s.idx_tup_fetch, c.reltuples, c.relpages as pages FROM pg_stat_all_indexes as s, pg_class as c WHERE s.indexrelname = c.relname AND s.schemaname IN (SELECT nspname FROM pg_namespace WHERE oid = c.relnamespace) AND ( s.schemaname = 'public' OR s.schemaname = 'pg_toast' )");
46 
47 $query['indexes I/O'] = sprintf("SELECT schemaname, relname, indexrelname, idx_blks_read, idx_blks_hit FROM pg_statio_all_indexes WHERE schemaname IN ('public', 'pg_toast')");
48 
49 $query['vacuums'] = sprintf("SELECT s.schemaname, s.relname, c.reltuples, s.n_tup_ins, s.n_tup_upd, s.n_tup_del, s.n_tup_hot_upd, s.n_live_tup, s.n_dead_tup, s.last_vacuum, s.last_autovacuum FROM pg_stat_all_tables as s, pg_class as c WHERE s.relname = c.relname AND s.schemaname IN (SELECT nspname FROM pg_namespace WHERE oid = c.relnamespace) AND ( s.schemaname = 'public' OR s.schemaname = 'pg_toast' )");
50 
51 $query['pg_buffercache'] = sprintf("SELECT c.relname, count(*) AS pages FROM pg_buffercache b INNER JOIN pg_class c ON b.relfilenode = c.relfilenode AND b.reldatabase IN (0, (SELECT oid FROM pg_database WHERE datname = current_database())) GROUP BY c.relname");
52 
53 $parms = array();
54 $parms['q'] = $_GET['q'];
55 $parms['orderby'] = $_GET['orderby'];
56 $parms['desc'] = $_GET['desc'];
57 
58 if( ! isset($query[$parms['q']]) ) {
59  $parms['q'] = key($query);
60 }
61 if( $parms['desc'] != 'desc' && $parms['desc'] != 'asc' ) {
62  $parms['desc'] = 'desc';
63 }
64 
65 $menu = array();
66 foreach( $query as $k => $v ) {
67  $menu [] = '<a href="?q='.htmlspecialchars($k).'">'.htmlspecialchars($k).'</a>';
68 }
69 print '<div class="menu">&nbsp;'.join('&nbsp;|&nbsp;', $menu).'&nbsp;</div>';
70 
71 show_query($conn, $query[$parms['q']], $parms);
72 
73 function show_query(&$conn, &$query, &$parms) {
74  $q = $parms['q'];
75  $orderby = $parms['orderby'];
76  $desc = $parms['desc'];
77 
78  $add = '';
79  if( $orderby != '' ) {
80  $add = sprintf('ORDER BY "%s"', pg_escape_string($orderby));
81  if( $desc != '' ) {
82  $add = sprintf('%s %s', $add, pg_escape_string($desc));
83  }
84  }
85 
86  $res = pg_prepare($conn, 'select', sprintf("%s %s", $query, pg_escape_string($add)));
87  if( $res !== false ) {
88  $res = pg_execute($conn, 'select', array());
89  }
90  if( $res === false ) {
91  print '<div class="error">';
92  print htmlspecialchars(pg_last_error($conn));
93  print '</div>';
94  return false;
95  }
96 
97  $col_num = pg_num_fields($res);
98  $line_num = pg_num_rows($res);
99 
100  $fields = array();
101  for( $i = 0; $i < $col_num; $i++ ) {
102  $fields[$i] = pg_field_name($res, $i);
103  }
104  $desc = ($desc == 'desc') ? 'asc' : 'desc';
105 
106  print '<div class="table">';
107  print '<table>';
108  print '<thead>';
109  foreach( $fields as $i => $name ) {
110  print '<td><a href="?q='.htmlspecialchars($q).'&orderby='.htmlspecialchars($name).'&desc='.htmlspecialchars($desc).'">'.htmlspecialchars($name).'</a></td>';
111  }
112  print '</thead>';
113  print '<tbody>';
114  while( $row = pg_fetch_row($res) ) {
115  print '<tr>';
116  foreach( $fields as $i => $name ) {
117  $v = $row[$i];
118  if( $name == 'pages' ) {
119  $v = prettySize($v * PG_PAGE_SIZE);
120  }
121  print '<td>'.htmlspecialchars($v).'</td>';
122  }
123  print '</tr>';
124  }
125  print '</tbody>';
126  print '</table>';
127  print '</div>';
128 }
129 
130 function prettySize($bytes, $precision = 2) {
131  $units = array('B', 'KB', 'MB', 'GB', 'TB');
132 
133  $bytes = max($bytes, 0);
134  $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
135  $pow = min($pow, count($units) - 1);
136 
137  $bytes = $bytes / pow(1024, $pow);
138 
139  return round($bytes, $precision) . ' ' . $units[$pow];
140 }
141 
142 ?>
143 </body>
144 </html>
$s type
Definition: dav.php:73
const PG_PAGE_SIZE
Definition: pginfo.php:23
if($conn===false) $query
Definition: pginfo.php:35
global $_GET
Definition: wsh.php:37
$conn
Definition: pginfo.php:29
getServiceCore()
Definition: Lib.Common.php:405
print
Definition: checklist.php:49
switch($command) exit
Definition: checkVault.php:46
$pgservice_core
Definition: pginfo.php:27
$parms
Definition: pginfo.php:53
if(!isset($query[$parms['q']])) if($parms['desc']!= 'desc'&&$parms['desc']!= 'asc') $menu
Definition: pginfo.php:65
← centre documentaire © anakeen