1
|
|
|
<?php |
2
|
|
|
namespace CloudWatchScript\Plugins; |
3
|
|
|
|
4
|
|
|
use CloudWatchScript\AbstractMonitoring; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Check Solr using ping URL. |
8
|
|
|
* Add and configure the folliwong lines to the config file |
9
|
|
|
* "Solr" : { |
10
|
|
|
* "name" : "Name of metric and alarm", |
11
|
|
|
* "url": "http://localhost:8080/solr/collection1/", |
12
|
|
|
* "namespace": "Metric/Namespace", |
13
|
|
|
* "description": "Descrption" |
14
|
|
|
* } |
15
|
|
|
*/ |
16
|
|
|
class SolrCdcrMonitoring extends AbstractMonitoring |
17
|
|
|
{ |
18
|
|
|
private $solrUrl; |
19
|
|
|
/** |
20
|
|
|
* @param array $config |
21
|
|
|
* @param String $name |
22
|
|
|
*/ |
23
|
|
|
public function __construct($config, $name) |
24
|
|
|
{ |
25
|
|
|
parent::__construct($config, $name); |
26
|
|
|
$this->solrUrl = $this->config->url; |
27
|
|
|
} |
28
|
|
|
/** |
29
|
|
|
* Check solr cdcr : queue size, process and buffer. |
30
|
|
|
* @return metric 0 Ok, 1 KO |
31
|
|
|
*/ |
32
|
|
|
public function getMetric() |
33
|
|
|
{ |
34
|
|
|
$queue = -1; |
35
|
|
|
$process = 0; |
36
|
|
|
$buffer = 0; |
37
|
|
|
$cdcrQueues = @file_get_contents($this->solrUrl . "cdcr?action=QUEUES&wt=json"); |
38
|
|
|
if($cdcrQueues !== false) { |
39
|
|
|
$cdcrQueues = json_decode($cdcrQueues, true); |
40
|
|
|
$queue = $cdcrQueues['queues'][1][1][1]; |
41
|
|
|
} |
42
|
|
|
$cdcrStatus = @file_get_contents($this->solrUrl . "cdcr?action=STATUS&wt=json"); |
43
|
|
|
if($cdcrStatus !== false) { |
44
|
|
|
$cdcrStatus = json_decode($cdcrStatus, true); |
45
|
|
|
$process = $cdcrStatus['status'][1]==='started'?1:0; |
46
|
|
|
$buffer = $cdcrStatus['status'][3]==='enabled'?1:0; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return array('queue' => $queue, 'process'=> $process, 'buffer' => $buffer); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getUnit($alarmName=null) |
53
|
|
|
{ |
54
|
|
|
if($alarmName === null) { |
55
|
|
|
return array('queue' => 'Count', 'process' => 'None', 'buffer' => 'None'); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
else { |
58
|
|
|
switch ($alarmName) { |
59
|
|
|
case $this->name . " synchronisation stop": |
60
|
|
|
return "Count"; |
61
|
|
|
case $this->name . " synchronisation delay (1000)": |
62
|
|
|
return "Count"; |
63
|
|
|
case $this->name . " buffer": |
64
|
|
|
return "None"; |
65
|
|
|
case $this->name . " process": |
66
|
|
|
return "None"; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
/** |
71
|
|
|
* Alarm if : |
72
|
|
|
* - queue size set to -1 |
73
|
|
|
* - queue size great than 1000 |
74
|
|
|
* - process is not started |
75
|
|
|
* - buffer is not enabled |
76
|
|
|
*/ |
77
|
|
|
public function getAlarms() |
78
|
|
|
{ |
79
|
|
|
return array( |
80
|
|
|
array("ComparisonOperator" => "LessThanThreshold", |
81
|
|
|
"Threshold" => -1, |
82
|
|
|
"Name" => $this->name . " synchronisation stop" |
83
|
|
|
), |
84
|
|
|
array("ComparisonOperator" => "GreaterThanThreshold", |
85
|
|
|
"Threshold" => 1000, |
86
|
|
|
"Name" => $this->name . " synchronisation delay (1000)" |
87
|
|
|
), |
88
|
|
|
array("ComparisonOperator" => "LessThanThreshold", |
89
|
|
|
"Threshold" => 1, |
90
|
|
|
"Name" => $this->name . " process" |
91
|
|
|
), |
92
|
|
|
array("ComparisonOperator" => "LessThanThreshold", |
93
|
|
|
"Threshold" => 1, |
94
|
|
|
"Name" => $this->name . " buffer" |
95
|
|
|
) |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
public function getMetricName($alarm) { |
99
|
|
|
switch ($alarm) { |
100
|
|
|
case $this->name . " synchronisation stop": |
101
|
|
|
return $this->name . " queue"; |
102
|
|
|
case $this->name . " synchronisation delay (1000)": |
103
|
|
|
return $this->name . " queue"; |
104
|
|
|
case $this->name . " buffer": |
105
|
|
|
return $this->name . " buffer"; |
106
|
|
|
case $this->name . " process": |
107
|
|
|
return $this->name . " process"; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.