1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of PhuninNode. |
6
|
|
|
* |
7
|
|
|
** (c) 2013 - 2016 Cees-Jan Kiewiet |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace WyriHaximus\PhuninNode\Plugins; |
14
|
|
|
|
15
|
|
|
use React\Promise\PromiseInterface; |
16
|
|
|
use WyriHaximus\PhuninNode\Configuration; |
17
|
|
|
use WyriHaximus\PhuninNode\Metric; |
18
|
|
|
use WyriHaximus\PhuninNode\Node; |
19
|
|
|
use WyriHaximus\PhuninNode\PluginInterface; |
20
|
|
|
use function React\Promise\resolve; |
21
|
|
|
use function WyriHaximus\PhuninNode\metricPromisesToObjectStorage; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Uptime |
25
|
|
|
* @package WyriHaximus\PhuninNode\Plugins |
26
|
|
|
*/ |
27
|
|
|
class Uptime implements PluginInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Seconds in a day |
31
|
|
|
*/ |
32
|
|
|
const DAY_IN_SECONDS = 86400; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Node |
36
|
|
|
*/ |
37
|
|
|
private $node; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Cached configuration state |
41
|
|
|
* |
42
|
|
|
* @var Configuration |
43
|
|
|
*/ |
44
|
|
|
private $configuration; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Start time of this instance |
48
|
|
|
* |
49
|
|
|
* @var int |
50
|
|
|
*/ |
51
|
|
|
private $startTime = 0; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Save the time this instance started |
55
|
|
|
*/ |
56
|
8 |
|
public function __construct() |
57
|
|
|
{ |
58
|
8 |
|
$this->startTime = time(); |
59
|
8 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
8 |
|
public function setNode(Node $node) |
65
|
|
|
{ |
66
|
8 |
|
$this->node = $node; |
67
|
8 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
1 |
|
public function getSlug(): string |
73
|
|
|
{ |
74
|
1 |
|
return 'uptime'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
1 |
|
public function getCategorySlug(): string |
81
|
|
|
{ |
82
|
1 |
|
return 'phunin_node'; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
2 |
View Code Duplication |
public function getConfiguration(): PromiseInterface |
|
|
|
|
89
|
|
|
{ |
90
|
2 |
|
if ($this->configuration instanceof Configuration) { |
91
|
1 |
|
return resolve($this->configuration); |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
$this->configuration = new Configuration(); |
95
|
2 |
|
$this->configuration->setPair('graph_category', 'phunin_node'); |
96
|
2 |
|
$this->configuration->setPair('graph_title', 'Uptime'); |
97
|
2 |
|
$this->configuration->setPair('graph_args', '--base 1000 -l 0'); |
98
|
2 |
|
$this->configuration->setPair('graph_vlabel', 'uptime in days'); |
99
|
2 |
|
$this->configuration->setPair('uptime.label', 'uptime'); |
100
|
2 |
|
$this->configuration->setPair('uptime.draw', 'AREA'); |
101
|
|
|
|
102
|
2 |
|
return resolve($this->configuration); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
2 |
|
public function getValues(): PromiseInterface |
109
|
|
|
{ |
110
|
2 |
|
return metricPromisesToObjectStorage([ |
111
|
2 |
|
$this->getUptimeValue(), |
112
|
|
|
]); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return Metric |
117
|
|
|
*/ |
118
|
2 |
|
private function getUptimeValue(): Metric |
119
|
|
|
{ |
120
|
2 |
|
$value = new Metric( |
121
|
2 |
|
'uptime', |
122
|
2 |
|
((time() - $this->startTime) / self::DAY_IN_SECONDS) |
123
|
|
|
); |
124
|
2 |
|
return $value; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
1 |
|
public function getCapabilities(): array |
131
|
|
|
{ |
132
|
1 |
|
return []; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
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.