Skip to content

Commit 57bdedc

Browse files
Merge pull request erikdubbelboer#42 from ptdev/master
Better error detection
2 parents a651558 + fa4082a commit 57bdedc

File tree

3 files changed

+158
-144
lines changed

3 files changed

+158
-144
lines changed

includes/common.inc.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@
8484

8585
// Setup a connection to Redis.
8686
$redis = !$server['port'] ? new Predis\Client($server['host']) : new Predis\Client('tcp://'.$server['host'].':'.$server['port']);
87+
try {
88+
$redis->connect();
89+
} catch (Predis\CommunicationException $exception) {
90+
$redis = false;
91+
}
8792

8893
if (isset($server['auth'])) {
8994
if (!$redis->auth($server['auth'])) {

index.php

Lines changed: 114 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,125 @@
22

33
require_once 'includes/common.inc.php';
44

5+
if($redis) {
56

6-
try {
7-
// Get keys from Redis according to server-config.
8-
$keys = $redis->keys($server['filter']);
9-
} catch (Exception $e) {
10-
die('ERROR: Can\'t connect to Redis');
11-
}
7+
$keys = $redis->keys($server['filter']);
128

13-
sort($keys);
9+
sort($keys);
1410

15-
$namespaces = array(); // Array to hold our top namespaces.
11+
$namespaces = array(); // Array to hold our top namespaces.
1612

17-
// Build an array of nested arrays containing all our namespaces and containing keys.
18-
foreach ($keys as $key) {
19-
// Ignore keys that are to long (Redis supports keys that can be way to long to put in an url).
20-
if (strlen($key) > $config['maxkeylen']) {
21-
continue;
22-
}
13+
// Build an array of nested arrays containing all our namespaces and containing keys.
14+
foreach ($keys as $key) {
15+
// Ignore keys that are to long (Redis supports keys that can be way to long to put in an url).
16+
if (strlen($key) > $config['maxkeylen']) {
17+
continue;
18+
}
2319

24-
$key = explode($server['seperator'], $key);
20+
$key = explode($server['seperator'], $key);
2521

26-
// $d will be a reference to the current namespace.
27-
$d = &$namespaces;
22+
// $d will be a reference to the current namespace.
23+
$d = &$namespaces;
2824

29-
// We loop though all the namespaces for this key creating the array for each.
30-
// Each time updating $d to be a reference to the last namespace so we can create the next one in it.
31-
for ($i = 0; $i < (count($key) - 1); ++$i) {
32-
if (!isset($d[$key[$i]])) {
33-
$d[$key[$i]] = array();
34-
}
25+
// We loop though all the namespaces for this key creating the array for each.
26+
// Each time updating $d to be a reference to the last namespace so we can create the next one in it.
27+
for ($i = 0; $i < (count($key) - 1); ++$i) {
28+
if (!isset($d[$key[$i]])) {
29+
$d[$key[$i]] = array();
30+
}
3531

36-
$d = &$d[$key[$i]];
37-
}
32+
$d = &$d[$key[$i]];
33+
}
3834

39-
// Nodes containing an item named __phpredisadmin__ are also a key, not just a directory.
40-
// This means that creating an actual key named __phpredisadmin__ will make this bug.
41-
$d[$key[count($key) - 1]] = array('__phpredisadmin__' => true);
35+
// Nodes containing an item named __phpredisadmin__ are also a key, not just a directory.
36+
// This means that creating an actual key named __phpredisadmin__ will make this bug.
37+
$d[$key[count($key) - 1]] = array('__phpredisadmin__' => true);
4238

43-
// Unset $d so we don't accidentally overwrite it somewhere else.
44-
unset($d);
45-
}
39+
// Unset $d so we don't accidentally overwrite it somewhere else.
40+
unset($d);
41+
}
42+
43+
// Recursive function used to print the namespaces.
44+
function print_namespace($item, $name, $fullkey, $islast) {
45+
global $config, $server, $redis;
46+
47+
// Is this also a key and not just a namespace?
48+
if (isset($item['__phpredisadmin__'])) {
49+
// Unset it so we won't loop over it when printing this namespace.
50+
unset($item['__phpredisadmin__']);
51+
52+
$type = $redis->type($fullkey);
53+
$class = array();
54+
$len = false;
55+
56+
if (isset($_GET['key']) && ($fullkey == $_GET['key'])) {
57+
$class[] = 'current';
58+
}
59+
if ($islast) {
60+
$class[] = 'last';
61+
}
62+
63+
// Get the number of items in the key.
64+
if (!isset($config['faster']) || !$config['faster']) {
65+
switch ($type) {
66+
case 'hash':
67+
$len = $redis->hLen($fullkey);
68+
break;
69+
70+
case 'list':
71+
$len = $redis->lLen($fullkey);
72+
break;
73+
74+
case 'set':
75+
// This is currently the only way to do this, this can be slow since we need to retrieve all keys
76+
$len = count($redis->sMembers($fullkey));
77+
break;
78+
79+
case 'zset':
80+
// This is currently the only way to do this, this can be slow since we need to retrieve all keys
81+
$len = count($redis->zRange($fullkey, 0, -1));
82+
break;
83+
}
84+
}
85+
86+
87+
?>
88+
<li<?php echo empty($class) ? '' : ' class="'.implode(' ', $class).'"'?>>
89+
<a href="?view&amp;s=<?php echo $server['id']?>&amp;key=<?php echo urlencode($fullkey)?>"><?php echo format_html($name)?><?php if ($len !== false) { ?><span class="info">(<?php echo $len?>)</span><?php } ?></a>
90+
</li>
91+
<?php
92+
}
4693

94+
// Does this namespace also contain subkeys?
95+
if (count($item) > 0) {
96+
?>
97+
<li class="folder<?php echo ($fullkey === '') ? '' : ' collapsed'?><?php echo $islast ? ' last' : ''?>">
98+
<div class="icon"><?php echo format_html($name)?>&nbsp;<span class="info">(<?php echo count($item)?>)</span>
99+
<?php if ($fullkey !== '') { ?><a href="delete.php?s=<?php echo $server['id']?>&amp;tree=<?php echo urlencode($fullkey)?>:" class="deltree"><img src="images/delete.png" width="10" height="10" title="Delete tree" alt="[X]"></a><?php } ?>
100+
</div><ul>
101+
<?php
102+
103+
$l = count($item);
104+
105+
foreach ($item as $childname => $childitem) {
106+
// $fullkey will be empty on the first call.
107+
if ($fullkey === '') {
108+
$childfullkey = $childname;
109+
} else {
110+
$childfullkey = $fullkey.$server['seperator'].$childname;
111+
}
112+
113+
print_namespace($childitem, $childname, $childfullkey, (--$l == 0));
114+
}
115+
116+
?>
117+
</ul>
118+
</li>
119+
<?php
120+
}
121+
}
122+
123+
} // if redis
47124

48125

49126
// This is basically the same as the click code in index.js.
@@ -62,92 +139,6 @@
62139

63140

64141

65-
66-
67-
68-
// Recursive function used to print the namespaces.
69-
function print_namespace($item, $name, $fullkey, $islast) {
70-
global $config, $server, $redis;
71-
72-
// Is this also a key and not just a namespace?
73-
if (isset($item['__phpredisadmin__'])) {
74-
// Unset it so we won't loop over it when printing this namespace.
75-
unset($item['__phpredisadmin__']);
76-
77-
$type = $redis->type($fullkey);
78-
$class = array();
79-
$len = false;
80-
81-
if (isset($_GET['key']) && ($fullkey == $_GET['key'])) {
82-
$class[] = 'current';
83-
}
84-
if ($islast) {
85-
$class[] = 'last';
86-
}
87-
88-
// Get the number of items in the key.
89-
if (!isset($config['faster']) || !$config['faster']) {
90-
switch ($type) {
91-
case 'hash':
92-
$len = $redis->hLen($fullkey);
93-
break;
94-
95-
case 'list':
96-
$len = $redis->lLen($fullkey);
97-
break;
98-
99-
case 'set':
100-
// This is currently the only way to do this, this can be slow since we need to retrieve all keys
101-
$len = count($redis->sMembers($fullkey));
102-
break;
103-
104-
case 'zset':
105-
// This is currently the only way to do this, this can be slow since we need to retrieve all keys
106-
$len = count($redis->zRange($fullkey, 0, -1));
107-
break;
108-
}
109-
}
110-
111-
112-
?>
113-
<li<?php echo empty($class) ? '' : ' class="'.implode(' ', $class).'"'?>>
114-
<a href="?view&amp;s=<?php echo $server['id']?>&amp;key=<?php echo urlencode($fullkey)?>"><?php echo format_html($name)?><?php if ($len !== false) { ?><span class="info">(<?php echo $len?>)</span><?php } ?></a>
115-
</li>
116-
<?php
117-
}
118-
119-
// Does this namespace also contain subkeys?
120-
if (count($item) > 0) {
121-
?>
122-
<li class="folder<?php echo ($fullkey === '') ? '' : ' collapsed'?><?php echo $islast ? ' last' : ''?>">
123-
<div class="icon"><?php echo format_html($name)?>&nbsp;<span class="info">(<?php echo count($item)?>)</span>
124-
<?php if ($fullkey !== '') { ?><a href="delete.php?s=<?php echo $server['id']?>&amp;tree=<?php echo urlencode($fullkey)?>:" class="deltree"><img src="images/delete.png" width="10" height="10" title="Delete tree" alt="[X]"></a><?php } ?>
125-
</div><ul>
126-
<?php
127-
128-
$l = count($item);
129-
130-
foreach ($item as $childname => $childitem) {
131-
// $fullkey will be empty on the first call.
132-
if ($fullkey === '') {
133-
$childfullkey = $childname;
134-
} else {
135-
$childfullkey = $fullkey.$server['seperator'].$childname;
136-
}
137-
138-
print_namespace($childitem, $childname, $childfullkey, (--$l == 0));
139-
}
140-
141-
?>
142-
</ul>
143-
</li>
144-
<?php
145-
}
146-
}
147-
148-
149-
150-
151142
$page['css'][] = 'index';
152143
$page['js'][] = 'index';
153144

@@ -166,6 +157,8 @@ function print_namespace($item, $name, $fullkey, $islast) {
166157
</select>
167158
</p>
168159

160+
<?php if($redis): ?>
161+
169162
<p>
170163
<?php if (isset($login)) { ?>
171164
<a href="logout.php"><img src="images/logout.png" width="16" height="16" title="Logout" alt="[L]"></a>
@@ -192,6 +185,10 @@ function print_namespace($item, $name, $fullkey, $islast) {
192185
</ul>
193186
</div><!-- #keys -->
194187

188+
<?php else: ?>
189+
<div style="color:red">Can't connect to this server</div>
190+
<?php endif; ?>
191+
195192
<div id="frame">
196193
<iframe src="<?php echo format_html($iframe)?>" id="iframe" frameborder="0" scrolling="0"></iframe>
197194
</div><!-- #frame -->

overview.php

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,45 @@
1212
$server['db'] = 0;
1313
}
1414

15-
// Setup a connection to this Redis server.
16-
$redis = new Predis\Client('tcp://'.$server['host'].':'.$server['port']);
17-
18-
19-
if (isset($server['auth'])) {
20-
if (!$redis->auth($server['auth'])) {
21-
die('ERROR: Authentication failed ('.$server['host'].':'.$server['port'].')');
22-
}
15+
// Setup a connection to Redis.
16+
$redis = !$server['port'] ? new Predis\Client($server['host']) : new Predis\Client('tcp://'.$server['host'].':'.$server['port']);
17+
try {
18+
$redis->connect();
19+
} catch (Predis\CommunicationException $exception) {
20+
$redis = false;
2321
}
2422

25-
26-
if ($server['db'] != 0) {
27-
if (!$redis->select($server['db'])) {
28-
die('ERROR: Selecting database failed ('.$server['host'].':'.$server['port'].','.$server['db'].')');
29-
}
23+
if(!$redis) {
24+
$info[$i] = false;
25+
} else {
26+
if (isset($server['auth'])) {
27+
if (!$redis->auth($server['auth'])) {
28+
die('ERROR: Authentication failed ('.$server['host'].':'.$server['port'].')');
29+
}
30+
}
31+
if ($server['db'] != 0) {
32+
if (!$redis->select($server['db'])) {
33+
die('ERROR: Selecting database failed ('.$server['host'].':'.$server['port'].','.$server['db'].')');
34+
}
35+
}
36+
37+
$info[$i] = $redis->info();
38+
$info[$i]['size'] = $redis->dbSize();
39+
40+
if (!isset($info[$i]['Server'])) {
41+
$info[$i]['Server'] = array(
42+
'redis_version' => $info[$i]['redis_version'],
43+
'uptime_in_seconds' => $info[$i]['uptime_in_seconds']
44+
);
45+
}
46+
if (!isset($info[$i]['Memory'])) {
47+
$info[$i]['Memory'] = array(
48+
'used_memory' => $info[$i]['used_memory']
49+
);
50+
}
3051
}
3152

3253

33-
$info[$i] = $redis->info();
34-
$info[$i]['size'] = $redis->dbSize();
35-
36-
if (!isset($info[$i]['Server'])) {
37-
$info[$i]['Server'] = array(
38-
'redis_version' => $info[$i]['redis_version'],
39-
'uptime_in_seconds' => $info[$i]['uptime_in_seconds']
40-
);
41-
}
42-
if (!isset($info[$i]['Memory'])) {
43-
$info[$i]['Memory'] = array(
44-
'used_memory' => $info[$i]['used_memory']
45-
);
46-
}
4754
}
4855

4956

@@ -60,6 +67,10 @@
6067
<div class="server">
6168
<h2><?php echo isset($server['name']) ? $server['name'] : format_html($server['host'])?></h2>
6269

70+
<?php if(!$info[$i]): ?>
71+
<div style="text-align:center;color:red">Server Down</div>
72+
<?php else: ?>
73+
6374
<table>
6475

6576
<tr><td><div>Redis version:</div></td><td><div><?php echo $info[$i]['Server']['redis_version']?></div></td></tr>
@@ -73,6 +84,7 @@
7384
<tr><td><div>Last save:</div></td><td><div><?php if (isset($info[$i]['Persistence']['rdb_last_save_time'])) { echo format_ago(time() - $info[$i]['Persistence']['rdb_last_save_time'], true); } else { echo 'never'; } ?> <a href="save.php?s=<?php echo $i?>"><img src="images/save.png" width="16" height="16" title="Save Now" alt="[S]" class="imgbut"></a></div></td></tr>
7485

7586
</table>
87+
<?php endif; ?>
7688
</div>
7789
<?php } ?>
7890

0 commit comments

Comments
 (0)