1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AWSCustomMetric\Plugin; |
4
|
|
|
|
5
|
|
|
use AWSCustomMetric\DI; |
6
|
|
|
use AWSCustomMetric\Metric; |
7
|
|
|
|
8
|
|
|
class RedisCheck extends BaseMetricPlugin implements MetricPluginInterface |
9
|
|
|
{ |
10
|
|
|
private $server; |
11
|
|
|
private $port; |
12
|
|
|
private $keys = array(); |
13
|
|
|
|
14
|
|
|
public function __construct(DI $diObj, $namespace = null, $cronExpression = '') |
15
|
|
|
{ |
16
|
|
|
parent::__construct($diObj, $namespace, $cronExpression); |
17
|
|
|
$this->server = 'localhost'; |
18
|
|
|
$this->port = '6379'; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return mixed |
23
|
|
|
*/ |
24
|
|
|
public function getServer() |
25
|
|
|
{ |
26
|
|
|
return $this->server; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $server |
31
|
|
|
*/ |
32
|
|
|
public function setServer($server) |
33
|
|
|
{ |
34
|
|
|
$this->server = $server; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public function getPort() |
41
|
|
|
{ |
42
|
|
|
return $this->port; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $port |
47
|
|
|
*/ |
48
|
|
|
public function setPort($port) |
49
|
|
|
{ |
50
|
|
|
$this->port = $port; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function getServerPortArgs() |
54
|
|
|
{ |
55
|
|
|
return '-h ' . $this->getServer() . ' -p ' . $this->getPort(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function getRedisCliCmd() |
59
|
|
|
{ |
60
|
|
|
return '/usr/bin/redis-cli ' . $this->getServerPortArgs(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
public function getKeys() |
67
|
|
|
{ |
68
|
|
|
return $this->keys; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param array $keys |
73
|
|
|
*/ |
74
|
|
|
public function setKeys(array $keys) |
75
|
|
|
{ |
76
|
|
|
$this->keys = $keys; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return array|bool |
81
|
|
|
*/ |
82
|
|
|
private function findKeys() |
83
|
|
|
{ |
84
|
|
|
try { |
85
|
|
|
$this->diObj->getCommandRunner()->execute($this->getRedisCliCmd() . ' --raw dbsize'); |
86
|
|
|
$retVal = $this->diObj->getCommandRunner()->getReturnCode(); |
87
|
|
|
$redisKeyCount = $this->diObj->getCommandRunner()->getOutput(); |
88
|
|
|
error_log('redisKeyCount: ' . json_encode($redisKeyCount).PHP_EOL, 3, '/tmp/php_error.log'); |
89
|
|
|
|
90
|
|
View Code Duplication |
if ($retVal!==0) { |
|
|
|
|
91
|
|
|
if ($this->diObj->getLogger()) { |
92
|
|
|
$this->diObj->getLogger()->error( |
93
|
|
|
'Redis KEyCount cmd failed!, RETVAL: ' . $retVal |
94
|
|
|
. ', OUT: ' . implode('|', $redisKeyCount) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
if ($redisKeyCount[0]>0) { |
100
|
|
|
$this->diObj->getCommandRunner()->execute($this->getRedisCliCmd() . ' --raw keys *'); |
101
|
|
|
$retVal = $this->diObj->getCommandRunner()->getReturnCode(); |
102
|
|
|
$redisKeyList = $this->diObj->getCommandRunner()->getOutput(); |
103
|
|
View Code Duplication |
if ($retVal!==0) { |
|
|
|
|
104
|
|
|
if ($this->diObj->getLogger()) { |
105
|
|
|
$this->diObj->getLogger()->error( |
106
|
|
|
'Redis KeyList cmd failed!, RETVAL: ' . $retVal |
107
|
|
|
. ', OUT: ' . implode('|', $redisKeyList) |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
return $redisKeyList; |
113
|
|
|
} else { |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
} catch (\Exception $e) { |
117
|
|
|
if ($this->diObj->getLogger()) { |
118
|
|
|
$this->diObj->getLogger()->error('Redis client thrown exception! ExcpMsg: ' . $e->getMessage()); |
119
|
|
|
} |
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return Metric[]|null|bool |
126
|
|
|
*/ |
127
|
|
|
public function getMetrics() |
128
|
|
|
{ |
129
|
|
|
try { |
130
|
|
|
$redisKeyList = count($this->keys)==0?$this->findKeys():$this->keys; |
131
|
|
|
$totalLen = 0; |
132
|
|
|
if ($redisKeyList && is_array($redisKeyList)) { |
|
|
|
|
133
|
|
|
foreach ($redisKeyList as $redisKey) { |
134
|
|
|
$this->diObj->getCommandRunner()->execute( |
135
|
|
|
$this->getRedisCliCmd() . ' --raw llen ' . trim($redisKey) |
136
|
|
|
); |
137
|
|
|
$retVal = $this->diObj->getCommandRunner()->getReturnCode(); |
138
|
|
|
$redisKeyLen = $this->diObj->getCommandRunner()->getOutput(); |
139
|
|
|
if ($retVal===0) { |
140
|
|
|
$totalLen += intval($redisKeyLen[0]); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
return [ |
145
|
|
|
$this->createNewMetric('RedisKeysLen', 'Count', $totalLen) |
146
|
|
|
]; |
147
|
|
|
} catch (\Exception $e) { |
148
|
|
|
if ($this->diObj->getLogger()) { |
149
|
|
|
$this->diObj->getLogger()->error('Redis client thrown exception! ExcpMsg: ' . $e->getMessage()); |
150
|
|
|
} |
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.