5
5
6
6
namespace ZendDiagnostics \Check ;
7
7
8
- use InvalidArgumentException ;
9
8
use ZendDiagnostics \Result \Failure ;
10
9
use ZendDiagnostics \Result \Skip ;
11
10
use ZendDiagnostics \Result \Success ;
20
19
* license: The PHP License, version 3.01
21
20
* copyright: Copyright (c) 2006-2011 The PHP Group
22
21
*/
23
- class ApcMemory extends AbstractCheck implements CheckInterface
22
+ class ApcMemory extends AbstractMemoryCheck
24
23
{
25
24
/**
26
- * Percentage that will cause a warning.
25
+ * APC information
27
26
*
28
- * @var int
27
+ * @var array
29
28
*/
30
- protected $ warningThreshold ;
31
-
32
- /**
33
- * Percentage that will cause a fail.
34
- *
35
- * @var int
36
- */
37
- protected $ criticalThreshold ;
38
-
39
- /**
40
- * @param int $warningThreshold A number between 0 and 100
41
- * @param int $criticalThreshold A number between 0 and 100
42
- * @throws InvalidArgumentException
43
- */
44
- public function __construct ($ warningThreshold , $ criticalThreshold )
45
- {
46
- if (!is_numeric ($ warningThreshold )) {
47
- throw new InvalidArgumentException ('Invalid warningThreshold argument - expecting an integer ' );
48
- }
49
-
50
- if (!is_numeric ($ criticalThreshold )) {
51
- throw new InvalidArgumentException ('Invalid criticalThreshold argument - expecting an integer ' );
52
- }
53
-
54
- if ($ warningThreshold > 100 || $ warningThreshold < 0 ) {
55
- throw new InvalidArgumentException ('Invalid warningThreshold argument - expecting an integer between 1 and 100 ' );
56
- }
57
-
58
- if ($ criticalThreshold > 100 || $ criticalThreshold < 0 ) {
59
- throw new InvalidArgumentException ('Invalid criticalThreshold argument - expecting an integer between 1 and 100 ' );
60
- }
61
-
62
- $ this ->warningThreshold = (int )$ warningThreshold ;
63
- $ this ->criticalThreshold = (int )$ criticalThreshold ;
64
- }
29
+ private $ apcInfo ;
65
30
66
31
/**
67
32
* Perform the check
@@ -83,43 +48,30 @@ public function check()
83
48
return new Warning ('APC extension is not available ' );
84
49
}
85
50
86
- if (!$ info = apc_sma_info ()) {
51
+ if (!$ this -> apcInfo = apc_sma_info ()) {
87
52
return new Warning ('Unable to retrieve APC memory status information. ' );
88
53
}
89
54
90
- $ size = $ info ['num_seg ' ] * $ info ['seg_size ' ];
91
- $ available = $ info ['avail_mem ' ];
92
- $ used = $ size - $ available ;
93
- $ percentUsed = ($ used / $ size ) * 100 ;
94
- $ message = sprintf ('%.0f%% of available %s memory used. ' , $ percentUsed , $ this ->formatBytes ($ size ));
95
-
96
- if ($ percentUsed > $ this ->criticalThreshold ) {
97
- return new Failure ($ message );
98
- }
99
-
100
- if ($ percentUsed > $ this ->warningThreshold ) {
101
- return new Warning ($ message );
102
- }
103
-
104
- return new Success ($ message );
55
+ return parent ::check ();
105
56
}
106
57
107
58
/**
108
- * @param int $bytes
109
- * @return string
59
+ * Returns the total memory in bytes
60
+ *
61
+ * @return int
110
62
*/
111
- private function formatBytes ( $ bytes )
63
+ protected function getTotalMemory ( )
112
64
{
113
- $ size = 'B ' ;
114
-
115
- foreach (array ('B ' ,'KB ' ,'MB ' ,'GB ' ) as $ size ) {
116
- if ($ bytes < 1024 ) {
117
- break ;
118
- }
119
-
120
- $ bytes /= 1024 ;
121
- }
65
+ return $ this ->apcInfo ['num_seg ' ] * $ this ->apcInfo ['seg_size ' ];
66
+ }
122
67
123
- return sprintf ("%.0f %s " , $ bytes , $ size );
68
+ /**
69
+ * Returns the used memory in bytes
70
+ *
71
+ * @return int
72
+ */
73
+ protected function getUsedMemory ()
74
+ {
75
+ return $ this ->getTotalMemory () - $ this ->apcInfo ['avail_mem ' ];
124
76
}
125
77
}
0 commit comments