55
66namespace ZendDiagnostics \Check ;
77
8- use InvalidArgumentException ;
98use ZendDiagnostics \Result \Failure ;
109use ZendDiagnostics \Result \Skip ;
1110use ZendDiagnostics \Result \Success ;
2019 * license: The PHP License, version 3.01
2120 * copyright: Copyright (c) 2006-2011 The PHP Group
2221 */
23- class ApcMemory extends AbstractCheck implements CheckInterface
22+ class ApcMemory extends AbstractMemoryCheck
2423{
2524 /**
26- * Percentage that will cause a warning.
25+ * APC information
2726 *
28- * @var int
27+ * @var array
2928 */
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 ;
6530
6631 /**
6732 * Perform the check
@@ -83,43 +48,30 @@ public function check()
8348 return new Warning ('APC extension is not available ' );
8449 }
8550
86- if (!$ info = apc_sma_info ()) {
51+ if (!$ this -> apcInfo = apc_sma_info ()) {
8752 return new Warning ('Unable to retrieve APC memory status information. ' );
8853 }
8954
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 ();
10556 }
10657
10758 /**
108- * @param int $bytes
109- * @return string
59+ * Returns the total memory in bytes
60+ *
61+ * @return int
11062 */
111- private function formatBytes ( $ bytes )
63+ protected function getTotalMemory ( )
11264 {
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+ }
12267
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 ' ];
12476 }
12577}
0 commit comments