fauguste /
cloudwatch-script-php
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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); |
||
|
0 ignored issues
–
show
|
|||
| 50 | } |
||
| 51 | |||
| 52 | public function getUnit($alarmName=null) |
||
| 53 | { |
||
| 54 | if($alarmName === null) { |
||
| 55 | return array('queue' => 'Count', 'process' => 'None', 'buffer' => 'None'); |
||
|
0 ignored issues
–
show
The return type of
return array('queue' => ...', 'buffer' => 'None'); (array<string,string>) is incompatible with the return type declared by the abstract method CloudWatchScript\AbstractMonitoring::getUnit of type integer.
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: class Author {
private $name;
public function __construct($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
abstract class Post {
public function getAuthor() {
return 'Johannes';
}
}
class BlogPost extends Post {
public function getAuthor() {
return new Author('Johannes');
}
}
class ForumPost extends Post { /* ... */ }
function my_function(Post $post) {
echo strtoupper($post->getAuthor());
}
Our function Loading history...
|
|||
| 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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.