1<?php
2/* vim: set expandtab sw=4 ts=4 sts=4: */
3/**
4 * functions for displaying server status
5 *
6 * @usedby  server_status.php
7 *
8 * @package PhpMyAdmin
9 */
10namespace PhpMyAdmin\Server;
11
12use PhpMyAdmin\ReplicationGui;
13use PhpMyAdmin\Server\Status\Data;
14use PhpMyAdmin\Util;
15
16/**
17 * PhpMyAdmin\Server\Status
18 *
19 * @package PhpMyAdmin
20 */
21class Status
22{
23    /**
24     * Prints server status information: processes, connections and traffic
25     *
26     * @param Data $serverStatusData Server status data
27     *
28     * @return string
29     */
30    public static function getHtml(Data $serverStatusData)
31    {
32        //display the server state General Information
33        $retval  = self::getHtmlForServerStateGeneralInfo($serverStatusData);
34
35        //display the server state traffic information
36        $retval .= self::getHtmlForServerStateTraffic($serverStatusData);
37
38        //display the server state connection information
39        $retval .= self::getHtmlForServerStateConnections($serverStatusData);
40
41        // display replication information
42        if ($GLOBALS['replication_info']['master']['status']
43            || $GLOBALS['replication_info']['slave']['status']
44        ) {
45            $retval .= self::getHtmlForReplicationInfo();
46        }
47
48        return $retval;
49    }
50
51    /**
52     * Prints server state General information
53     *
54     * @param Data $serverStatusData Server status data
55     *
56     * @return string
57     */
58    public static function getHtmlForServerStateGeneralInfo(Data $serverStatusData)
59    {
60        $start_time = $GLOBALS['dbi']->fetchValue(
61            'SELECT UNIX_TIMESTAMP() - ' . $serverStatusData->status['Uptime']
62        );
63
64        $retval  = '<h3>';
65        $bytes_received = $serverStatusData->status['Bytes_received'];
66        $bytes_sent = $serverStatusData->status['Bytes_sent'];
67        $retval .= sprintf(
68            __('Network traffic since startup: %s'),
69            implode(
70                ' ',
71                Util::formatByteDown(
72                    $bytes_received + $bytes_sent,
73                    3,
74                    1
75                )
76            )
77        );
78        $retval .= '</h3>';
79        $retval .= '<p>';
80        $retval .= sprintf(
81            __('This MySQL server has been running for %1$s. It started up on %2$s.'),
82            Util::timespanFormat($serverStatusData->status['Uptime']),
83            Util::localisedDate($start_time)
84        ) . "\n";
85        $retval .= '</p>';
86
87        return $retval;
88    }
89
90    /**
91     * Returns HTML to display replication information
92     *
93     * @return string HTML on replication
94     */
95    public static function getHtmlForReplicationInfo()
96    {
97        $retval = '<p class="notice clearfloat">';
98        if ($GLOBALS['replication_info']['master']['status']
99            && $GLOBALS['replication_info']['slave']['status']
100        ) {
101            $retval .= __(
102                'This MySQL server works as <b>master</b> and '
103                . '<b>slave</b> in <b>replication</b> process.'
104            );
105        } elseif ($GLOBALS['replication_info']['master']['status']) {
106            $retval .= __(
107                'This MySQL server works as <b>master</b> '
108                . 'in <b>replication</b> process.'
109            );
110        } elseif ($GLOBALS['replication_info']['slave']['status']) {
111            $retval .= __(
112                'This MySQL server works as <b>slave</b> '
113                . 'in <b>replication</b> process.'
114            );
115        }
116        $retval .= '</p>';
117
118        /*
119         * if the server works as master or slave in replication process,
120         * display useful information
121         */
122        $retval .= '<hr class="clearfloat" />';
123        $retval .= '<h3><a name="replication">';
124        $retval .= __('Replication status');
125        $retval .= '</a></h3>';
126        foreach ($GLOBALS['replication_types'] as $type) {
127            if (isset($GLOBALS['replication_info'][$type]['status'])
128                && $GLOBALS['replication_info'][$type]['status']
129            ) {
130                $retval .= ReplicationGui::getHtmlForReplicationStatusTable($type);
131            }
132        }
133
134        return $retval;
135    }
136
137    /**
138     * Prints server state traffic information
139     *
140     * @param Data $serverStatusData Server status data
141     *
142     * @return string
143     */
144    public static function getHtmlForServerStateTraffic(Data $serverStatusData)
145    {
146        $hour_factor    = 3600 / $serverStatusData->status['Uptime'];
147        $retval  = '<table id="serverstatustraffic" class="width100 data noclick">';
148        $retval .= '<thead>';
149        $retval .= '<tr>';
150        $retval .= '<th>';
151        $retval .= __('Traffic') . '&nbsp;';
152        $retval .=  Util::showHint(
153            __(
154                'On a busy server, the byte counters may overrun, so those statistics '
155                . 'as reported by the MySQL server may be incorrect.'
156            )
157        );
158        $retval .= '</th>';
159        $retval .= '<th>#</th>';
160        $retval .= '<th>&oslash; ' . __('per hour') . '</th>';
161        $retval .= '</tr>';
162        $retval .= '</thead>';
163        $retval .= '<tbody>';
164        $retval .= '<tr>';
165        $retval .= '<th class="name">' . __('Received') . '</th>';
166        $retval .= '<td class="value">';
167        $retval .= implode(
168            ' ',
169            Util::formatByteDown(
170                $serverStatusData->status['Bytes_received'], 3, 1
171            )
172        );
173        $retval .= '</td>';
174        $retval .= '<td class="value">';
175        $retval .= implode(
176            ' ',
177            Util::formatByteDown(
178                $serverStatusData->status['Bytes_received'] * $hour_factor, 3, 1
179            )
180        );
181        $retval .= '</td>';
182        $retval .= '</tr>';
183        $retval .= '<tr>';
184        $retval .= '<th class="name">' . __('Sent') . '</th>';
185        $retval .= '<td class="value">';
186        $retval .= implode(
187            ' ',
188            Util::formatByteDown(
189                $serverStatusData->status['Bytes_sent'], 3, 1
190            )
191        );
192        $retval .= '</td>';
193        $retval .= '<td class="value">';
194        $retval .= implode(
195            ' ',
196            Util::formatByteDown(
197                $serverStatusData->status['Bytes_sent'] * $hour_factor, 3, 1
198            )
199        );
200        $retval .= '</td>';
201        $retval .= '</tr>';
202        $retval .= '<tr>';
203        $retval .= '<th class="name">' . __('Total') . '</th>';
204        $retval .= '<td class="value">';
205        $bytes_received = $serverStatusData->status['Bytes_received'];
206        $bytes_sent = $serverStatusData->status['Bytes_sent'];
207        $retval .= implode(
208            ' ',
209            Util::formatByteDown(
210                $bytes_received + $bytes_sent, 3, 1
211            )
212        );
213        $retval .= '</td>';
214        $retval .= '<td class="value">';
215        $bytes_received = $serverStatusData->status['Bytes_received'];
216        $bytes_sent = $serverStatusData->status['Bytes_sent'];
217        $retval .= implode(
218            ' ',
219            Util::formatByteDown(
220                ($bytes_received + $bytes_sent) * $hour_factor, 3, 1
221            )
222        );
223        $retval .= '</td>';
224        $retval .= '</tr>';
225        $retval .= '</tbody>';
226        $retval .= '</table>';
227        return $retval;
228    }
229
230    /**
231     * Prints server state connections information
232     *
233     * @param Data $serverStatusData Server status data
234     *
235     * @return string
236     */
237    public static function getHtmlForServerStateConnections(Data $serverStatusData)
238    {
239        $hour_factor    = 3600 / $serverStatusData->status['Uptime'];
240        $retval  = '<table id="serverstatusconnections" class="width100 data noclick">';
241        $retval .= '<thead>';
242        $retval .= '<tr>';
243        $retval .= '<th>' . __('Connections') . '</th>';
244        $retval .= '<th>#</th>';
245        $retval .= '<th>&oslash; ' . __('per hour') . '</th>';
246        $retval .= '<th>%</th>';
247        $retval .= '</tr>';
248        $retval .= '</thead>';
249        $retval .= '<tbody>';
250        $retval .= '<tr>';
251        $retval .= '<th class="name">' . __('Max. concurrent connections') . '</th>';
252        $retval .= '<td class="value">';
253        $retval .= Util::formatNumber(
254            $serverStatusData->status['Max_used_connections'], 0
255        );
256        $retval .= '</td>';
257        $retval .= '<td class="value">--- </td>';
258        $retval .= '<td class="value">--- </td>';
259        $retval .= '</tr>';
260        $retval .= '<tr>';
261        $retval .= '<th class="name">' . __('Failed attempts') . '</th>';
262        $retval .= '<td class="value">';
263        $retval .= Util::formatNumber(
264            $serverStatusData->status['Aborted_connects'], 4, 1, true
265        );
266        $retval .= '</td>';
267        $retval .= '<td class="value">';
268        $retval .= Util::formatNumber(
269            $serverStatusData->status['Aborted_connects'] * $hour_factor, 4, 2, true
270        );
271        $retval .= '</td>';
272        $retval .= '<td class="value">';
273        if ($serverStatusData->status['Connections'] > 0) {
274            $abortNum = $serverStatusData->status['Aborted_connects'];
275            $connectNum = $serverStatusData->status['Connections'];
276
277            $retval .= Util::formatNumber(
278                $abortNum * 100 / $connectNum,
279                0, 2, true
280            );
281            $retval .= '%';
282        } else {
283            $retval .= '--- ';
284        }
285        $retval .= '</td>';
286        $retval .= '</tr>';
287        $retval .= '<tr>';
288        $retval .= '<th class="name">' . __('Aborted') . '</th>';
289        $retval .= '<td class="value">';
290        $retval .= Util::formatNumber(
291            $serverStatusData->status['Aborted_clients'], 4, 1, true
292        );
293        $retval .= '</td>';
294        $retval .= '<td class="value">';
295        $retval .= Util::formatNumber(
296            $serverStatusData->status['Aborted_clients'] * $hour_factor, 4, 2, true
297        );
298        $retval .= '</td>';
299        $retval .= '<td class="value">';
300        if ($serverStatusData->status['Connections'] > 0) {
301            $abortNum = $serverStatusData->status['Aborted_clients'];
302            $connectNum = $serverStatusData->status['Connections'];
303
304            $retval .= Util::formatNumber(
305                $abortNum * 100 / $connectNum,
306                0, 2, true
307            );
308            $retval .= '%';
309        } else {
310            $retval .= '--- ';
311        }
312        $retval .= '</td>';
313        $retval .= '</tr>';
314        $retval .= '<tr>';
315        $retval .= '<th class="name">' . __('Total') . '</th>';
316        $retval .= '<td class="value">';
317        $retval .= Util::formatNumber(
318            $serverStatusData->status['Connections'], 4, 0
319        );
320        $retval .= '</td>';
321        $retval .= '<td class="value">';
322        $retval .= Util::formatNumber(
323            $serverStatusData->status['Connections'] * $hour_factor, 4, 2
324        );
325        $retval .= '</td>';
326        $retval .= '<td class="value">';
327        $retval .= Util::formatNumber(100, 0, 2);
328        $retval .= '%</td>';
329        $retval .= '</tr>';
330        $retval .= '</tbody>';
331        $retval .= '</table>';
332
333        return $retval;
334    }
335}
336