Completed
Push — master ( dc77f2...9e17e8 )
by Fatih
05:11
created

RedisCheck::getServerPortArgs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $redisKeyList of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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