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
|
|
|
* "Elastics" : { |
10
|
|
|
* "name" : "Name of metric and alarm", |
11
|
|
|
* "url": "http://127.0.0.1:9200/_cluster/health", |
12
|
|
|
* "namespace": "Metric/Namespace", |
13
|
|
|
* "description": "Description" |
14
|
|
|
* } |
15
|
|
|
*/ |
16
|
|
|
class ElasticsMonitoring extends AbstractMonitoring |
17
|
|
|
{ |
18
|
|
|
private $elasticsUrl; |
19
|
|
|
/** |
20
|
|
|
* @param array $config |
21
|
|
|
* @param String $name |
22
|
|
|
*/ |
23
|
|
|
public function __construct($config, $name) |
24
|
|
|
{ |
25
|
|
|
parent::__construct($config, $name); |
26
|
|
|
$this->elasticsUrl = $this->config->url; |
27
|
|
|
} |
28
|
|
|
/* |
29
|
|
|
* Check Elastics health check |
30
|
|
|
* @return array |
31
|
|
|
*/ |
32
|
|
|
public function getMetric() |
33
|
|
|
{ |
34
|
|
|
$elasticsCheck = @file_get_contents($this->elasticsUrl); |
35
|
|
|
if ($elasticsCheck === false) { |
36
|
|
|
return 0; |
37
|
|
|
} |
38
|
|
|
$elasticsCheck = json_decode($elasticsCheck, true); |
39
|
|
|
$status = array("green" => 2, "yellow" => 1, "red" => 0); |
40
|
|
|
return array( |
|
|
|
|
41
|
|
|
"status" => $status[$elasticsCheck['status']], |
42
|
|
|
"number_of_nodes" => $elasticsCheck['number_of_nodes'], |
43
|
|
|
"number_of_data_nodes" => $elasticsCheck['number_of_data_nodes'], |
44
|
|
|
"active_primary_shards" => $elasticsCheck['active_primary_shards'], |
45
|
|
|
"active_shards" => $elasticsCheck['active_shards'], |
46
|
|
|
"unassigned_shards" => $elasticsCheck['unassigned_shards'], |
47
|
|
|
"active_shards_percent_as_number" => $elasticsCheck['active_shards_percent_as_number'] |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function getUnit($alarmName=null) |
55
|
|
|
{ |
56
|
|
|
if($alarmName === null) { |
57
|
|
|
return array( |
|
|
|
|
58
|
|
|
"status" => "None", |
59
|
|
|
"number_of_nodes" => "Count", |
60
|
|
|
"number_of_data_nodes" => "Count", |
61
|
|
|
"active_primary_shards" => "Count", |
62
|
|
|
"active_shards" => "Count", |
63
|
|
|
"unassigned_shards" => "Count", |
64
|
|
|
"active_shards_percent_as_number" => "Percent" |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
else { |
68
|
|
|
switch ($alarmName) { |
69
|
|
|
case $this->name . " status warning": |
70
|
|
|
return "None"; |
71
|
|
|
case $this->name . " status error": |
72
|
|
|
return "None"; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return integer 1 |
79
|
|
|
*/ |
80
|
|
|
public function getAlarms() |
81
|
|
|
{ |
82
|
|
|
return array( |
83
|
|
|
array("ComparisonOperator" => "LessThanThreshold", |
84
|
|
|
"Threshold" => 2, |
85
|
|
|
"Name" => $this->name . " status warning" |
86
|
|
|
), |
87
|
|
|
array("ComparisonOperator" => "LessThanThreshold", |
88
|
|
|
"Threshold" => 1, |
89
|
|
|
"Name" => $this->name . " status error" |
90
|
|
|
) |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return The metrics name associate to an alarm name. |
96
|
|
|
*/ |
97
|
|
|
public function getMetricName($alarm) { |
98
|
|
|
switch ($alarm) { |
99
|
|
|
case $this->name . " status warning": |
100
|
|
|
return $this->name . " status"; |
101
|
|
|
case $this->name . " status error": |
102
|
|
|
return $this->name . " status"; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
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.