This repository was archived by the owner on Jan 31, 2020. It is now read-only.
File tree 2 files changed +66
-0
lines changed
src/ZendDiagnostics/Check
2 files changed +66
-0
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ It currently ships with the following Diagnostic Checks:
22
22
* [ GuzzleHttpService] ( #guzzlehttpservice ) - check if given http host is responding using Guzzle,
23
23
* [ HttpService] ( #httpservice ) - check if given http host is responding,
24
24
* [ Memcache] ( #memcache ) - check if memcache extension is loaded and given server is reachable,
25
+ * [ Mongo] ( #mongodb ) - check if connection to MongoDb is possible,
25
26
* [ OpCacheMemory] ( #opcachememory ) - check if the OpCache memory usage is below warning/critical thresholds,
26
27
* [ PDOCheck] ( #pdocheck ) - check if connection is possible,
27
28
* [ PhpVersion] ( #phpversion ) - make sure that PHP version matches constraint,
@@ -487,6 +488,18 @@ $checkLocal = new Memcache('127.0.0.1'); // default port
487
488
$checkBackup = new Memcache('10.0.30.40', 11212);
488
489
````
489
490
491
+ ### MongoDb
492
+ Check if connection to MongoDb is possible
493
+
494
+ ```` php
495
+ <?php
496
+ use ZendDiagnostics\Check\Mongo;
497
+
498
+ $mongoCheck = new Mongo('mongodb://127.0.0.1:27017');
499
+ // and with user/password
500
+ $mongoCheck = new Mongo('mongodb://user:
[email protected] :27017');
501
+ ````
502
+
490
503
### PhpVersion
491
504
492
505
Check if current PHP version matches the given requirement. The test accepts 2 parameters - baseline version and
Original file line number Diff line number Diff line change
1
+ <?php
2
+ namespace ZendDiagnostics \Check ;
3
+
4
+ use ZendDiagnostics \Result \Success ;
5
+ use ZendDiagnostics \Result \Failure ;
6
+
7
+ class Mongo extends AbstractCheck
8
+ {
9
+ /**
10
+ * @var string
11
+ */
12
+ private $ connectionUri ;
13
+
14
+ /**
15
+ * @param string $connectionUri
16
+ */
17
+ public function __construct ($ connectionUri )
18
+ {
19
+ $ this ->connectionUri = $ connectionUri ;
20
+ }
21
+
22
+ /**
23
+ * {@inheritdoc}
24
+ */
25
+ public function check ()
26
+ {
27
+ try {
28
+ $ this ->getListDBs ();
29
+ } catch (\Exception $ e ) {
30
+ return new Failure (sprintf ('Failed to connect to MongoDB server. Reason: `%s` ' , $ e ->getMessage ()));
31
+ }
32
+
33
+ return new Success ();
34
+ }
35
+
36
+ /**
37
+ * @return array|\Iterator
38
+ *
39
+ * @throws \RuntimeException
40
+ * @throws \MongoDB\Driver\Exception
41
+ * @throws \MongoConnectionException
42
+ */
43
+ private function getListDBs ()
44
+ {
45
+ if (class_exists ('\MongoDB\Client ' )) {
46
+ return (new \MongoDB \Client ($ this ->connectionUri ))->listDatabases ();
47
+ } elseif (class_exists ('\MongoClient ' )) {
48
+ return (new \MongoClient ($ this ->server ))->listDBs ();
49
+ }
50
+
51
+ throw new \RuntimeException ('Neither the mongo extension or mongodb are installed ' );
52
+ }
53
+ }
You can’t perform that action at this time.
0 commit comments