blob: 0cdd5de4279c7e87fa804a9521633e07450a26e3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
<?php
///////////////////////////////////////////////////////////////////////////////
//
// pgPhoneHome - Postgres Monitor for iPhone
// Copyright 2008, EnterpriseDB UK Ltd.
// Dave Page (dave.page@enterprisedb.com)
//
// connections.php - Connection info
//
///////////////////////////////////////////////////////////////////////////////
require "global.php";
// Get the server number
if (isset($_GET['s']))
$server = intval($_GET['s']);
else
$server = -1;
// Set the panel name
$panel = "connections" . $server;
if ($server == -1 || $servers[$server]["description"] == "" || $servers[$server]["connstr"] == "")
www_error("Invalid server", "The specified server number ($server) does not have a valid configuration.");
// Connect the database
$db = @pg_connect($servers[$server]["connstr"]);
if ($db === FALSE)
www_error("Couldn't connect to the database.", html_entity_decode($php_errormsg, ENT_QUOTES));
// Get the connections
$sql = "SELECT *, CASE WHEN client_port = -1 THEN 'Unix socket' ELSE client_port::text END AS port FROM pg_stat_activity ORDER BY procpid";
$res = @pg_query($db, $sql);
if ($res === false)
www_error("Query execution error", $php_errormsg);
// Set the message for the top of the page
$rows = pg_num_rows($res);
if ($rows == 0)
$message = $servers[$server]["description"] . ": no connections";
else if ($rows == 1)
$message = $servers[$server]["description"] . ": 1 connection";
else
$message = $servers[$server]["description"] . ": $rows connections";
$message2 = "Server: " . $servers[$server]["description"];
$list = "";
$divs = "";
$rownum = 0;
while ($row = pg_fetch_assoc($res)) {
// Set the sub-panel ID
$subpanel = $panel . "row" . $rownum++;
$list .= " <li class=\"d\"><a href=\"#" . $subpanel . "\">PID: <span class=\"cv\">" . www_clean($row['procpid']) . "</span> Source: <span class=\"cv\">" . www_clean($row['client_addr']) . ":" . www_clean($row['client_port']) . "</span><br />User: <span class=\"cv\">" . www_clean($row['usename']) . "</span> DB: <span class=\"cv\">" . www_clean($row['datname']) . "</span></a></li>\n";
$divs .= "<div id=\"" . $subpanel . "\" class=\"panel\" title=\"Connection\">";
$divs .= "<div class=\"dh\">$message2</div>";
$divs .= "<h2>Process ID</h2><div class=\"vb\">" . www_clean($row['procpid']) . "</div>";
$divs .= "<h2>Source address</h2><div class=\"vb\">" . www_clean($row['client_addr']) . "</div>";
$divs .= "<h2>Source port</h2><div class=\"vb\">" . www_clean($row['port']) . "</div>";
$divs .= "<h2>Database</h2><div class=\"vb\">" . www_clean($row['datname']) . "</div>";
$divs .= "<h2>Username</h2><div class=\"vb\">" . www_clean($row['usename']) . "</div>";
$divs .= "<h2>Connection start</h2><div class=\"vb\">" . www_clean($row['backend_start']) . "</div>";
if (array_key_exists("xact_start", $row))
$divs .= "<h2>Transaction start</h2><div class=\"vb\">" . www_clean($row['xact_start']) . "</div>";
$divs .= "<h2>Query start</h2><div class=\"vb\">" . www_clean($row['query_start']) . "</div>";
$divs .= "<h2>Current query</h2><div class=\"vb\">" . www_clean($row['current_query']) . "</div>";
$divs .= "</div>";
}
pg_free_result($res);
// Output just the HTML snippet to be loaded via AJAX
$text = <<<EOT
<ul id="$panel" title="Connections">
<li class="group">$message</li>
$list
</ul>
$divs
EOT;
echo $text;
exit();
?>
|