Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit a39a48e

Browse files
committed
Merge pull request #39 from kbond/redis-rabbitmq-checks
Add RabbitMQ and Redis checks.
2 parents b8b34f1 + ee9201c commit a39a48e

File tree

6 files changed

+204
-3
lines changed

6 files changed

+204
-3
lines changed

.travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
language: php
22

3+
services:
4+
- redis-server
5+
- rabbitmq
6+
37
php:
48
- 5.3
59
- 5.3.3

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ It currently ships with the following Diagnostic Checks:
2222
* [PhpVersion](#phpversion) - make sure that PHP version matches constraint,
2323
* [PhpFlag](#phpflag) - make sure that given PHP flag (feature) is turned on or off.
2424
* [ProcessRunning](#processrunning) - check if a process with given name or ID is currently running,
25+
* [RabbitMQ](#rabbitmq) - Validate that a RabbitMQ service is running,
26+
* [Redis](#redis) - Validate that a Redis service is running,
2527
* [SecurityAdvisory](#securityadvisory) - check installed composer dependencies against SensioLabs SA database,
2628
* [StreamWrapperExists](#streamwrapperexists) - make sure given stream wrapper is available.
2729

@@ -510,6 +512,28 @@ $checkApache = new ProcessRunning('httpd');
510512
$checkProcess1000 = new ProcessRunning(1000);
511513
````
512514

515+
### RabbitMQ
516+
517+
Validate that a RabbitMQ service is running.
518+
519+
```php
520+
<?php
521+
use ZendDiagnostics\Check\RabbitMQ;
522+
523+
$rabbitMQCheck = new RabbitMQ('localhost', 5672, 'guest', 'guest', '/');
524+
```
525+
526+
### Redis
527+
528+
Validate that a Redis service is running.
529+
530+
```php
531+
<?php
532+
use ZendDiagnostics\Check\Redis;
533+
534+
$redisCheck = new Redis('localhost', 6379);
535+
```
536+
513537
### SecurityAdvisory
514538

515539
Run a security check of libraries locally installed by [Composer](http://getcomposer.org) against

composer.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,19 @@
2020
"require-dev": {
2121
"zendframework/zend-loader" : "2.*",
2222
"sensiolabs/security-checker": "1.3.*@dev",
23-
"symfony/yaml": "v2.3.11",
23+
"symfony/yaml" : "v2.3.11",
2424
"guzzle/http" : "3.*",
25-
"guzzle/plugin-mock" : "3.*"
25+
"guzzle/plugin-mock" : "3.*",
26+
"videlalvaro/php-amqplib" : "2.*",
27+
"predis/predis" : "0.8.*"
2628
},
2729
"suggest" : {
2830
"ext-bcmath" : "Required by Check\\CpuPerformance",
2931
"sensiolabs/security-checker": "Required by Check\\SecurityAdvisory",
3032
"symfony/yaml": "Required by Check\\YamlFile",
31-
"guzzle/http": "Required by Check\\GuzzleHttpService"
33+
"guzzle/http": "Required by Check\\GuzzleHttpService",
34+
"predis/predis": "Required by Check\\Redis",
35+
"videlalvaro/php-amqplib": "Required by Check\\RabbitMQ"
3236
},
3337
"extra": {
3438
"branch-alias": {
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* @license http://framework.zend.com/license/new-bsd New BSD License
4+
*/
5+
6+
namespace ZendDiagnostics\Check;
7+
8+
use PhpAmqpLib\Connection\AMQPConnection;
9+
use ZendDiagnostics\Result\Failure;
10+
use ZendDiagnostics\Result\Success;
11+
12+
/**
13+
* Validate that a RabbitMQ service is running
14+
*/
15+
class RabbitMQ extends AbstractCheck
16+
{
17+
/**
18+
* @var string
19+
*/
20+
protected $host;
21+
22+
/**
23+
* @var integer
24+
*/
25+
protected $port;
26+
27+
/**
28+
* @var string
29+
*/
30+
protected $user;
31+
32+
/**
33+
* @var string
34+
*/
35+
protected $password;
36+
37+
/**
38+
* @var string
39+
*/
40+
protected $vhost;
41+
42+
/**
43+
* @param string $host
44+
* @param integer $port
45+
* @param string $user
46+
* @param string $password
47+
* @param string $vhost
48+
*/
49+
public function __construct(
50+
$host = 'localhost',
51+
$port = 5672,
52+
$user = 'guest',
53+
$password = 'guest',
54+
$vhost = '/'
55+
) {
56+
$this->host = $host;
57+
$this->port = $port;
58+
$this->user = $user;
59+
$this->password = $password;
60+
$this->vhost = $vhost;
61+
}
62+
63+
/**
64+
* Perform the check
65+
*
66+
* @see \ZendDiagnostics\Check\CheckInterface::check()
67+
* @return Failure|Success
68+
*/
69+
public function check()
70+
{
71+
if (!class_exists('PhpAmqpLib\Connection\AMQPConnection')) {
72+
return new Failure('PhpAmqpLib is not installed');
73+
}
74+
75+
$conn = new AMQPConnection(
76+
$this->host,
77+
$this->port,
78+
$this->user,
79+
$this->password,
80+
$this->vhost
81+
);
82+
83+
$conn->channel();
84+
85+
return new Success();
86+
}
87+
}

src/ZendDiagnostics/Check/Redis.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* @license http://framework.zend.com/license/new-bsd New BSD License
4+
*/
5+
6+
namespace ZendDiagnostics\Check;
7+
8+
use Predis\Client;
9+
use ZendDiagnostics\Result\Failure;
10+
use ZendDiagnostics\Result\Success;
11+
12+
/**
13+
* Validate that a Redis service is running
14+
*/
15+
class Redis extends AbstractCheck
16+
{
17+
/**
18+
* @var string
19+
*/
20+
protected $host;
21+
22+
/**
23+
* @var int
24+
*/
25+
protected $port;
26+
27+
/**
28+
* @param string $host
29+
* @param int $port
30+
*/
31+
public function __construct($host = 'localhost', $port = 6379)
32+
{
33+
$this->host = $host;
34+
$this->port = $port;
35+
}
36+
37+
/**
38+
* Perform the check
39+
*
40+
* @see \ZendDiagnostics\Check\CheckInterface::check()
41+
* @return Failure|Success
42+
*/
43+
public function check()
44+
{
45+
if (!class_exists('Predis\Client')) {
46+
return new Failure('Predis is not installed');
47+
}
48+
49+
$client = new Client(array(
50+
'host' => $this->host,
51+
'port' => $this->port,
52+
));
53+
54+
$client->ping();
55+
56+
return new Success();
57+
}
58+
}

tests/ZendDiagnosticsTest/ChecksTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use ZendDiagnostics\Check\PhpFlag;
1616
use ZendDiagnostics\Check\PhpVersion;
1717
use ZendDiagnostics\Check\ProcessRunning;
18+
use ZendDiagnostics\Check\RabbitMQ;
19+
use ZendDiagnostics\Check\Redis;
1820
use ZendDiagnostics\Check\StreamWrapperExists;
1921
use ZendDiagnostics\Check\XmlFile;
2022
use ZendDiagnostics\Check\YamlFile;
@@ -43,6 +45,28 @@ public function testCpuPerformance()
4345
$this->assertInstanceOf('ZendDiagnostics\Result\Failure', $result);
4446
}
4547

48+
public function testRabbitMQ()
49+
{
50+
$check = new RabbitMQ();
51+
$result = $check->check();
52+
$this->assertInstanceOf('ZendDiagnostics\Result\Success', $result);
53+
54+
$check = new RabbitMQ('127.0.0.250', 9999);
55+
$this->setExpectedException('PhpAmqpLib\Exception\AMQPRuntimeException');
56+
$check->check();
57+
}
58+
59+
public function testRedis()
60+
{
61+
$check = new Redis();
62+
$result = $check->check();
63+
$this->assertInstanceOf('ZendDiagnostics\Result\Success', $result);
64+
65+
$check = new Redis('127.0.0.250', 9999);
66+
$this->setExpectedException('Predis\Connection\ConnectionException');
67+
$check->check();
68+
}
69+
4670
public function testClassExists()
4771
{
4872
$check = new ClassExists(__CLASS__);

0 commit comments

Comments
 (0)