1 | <?php |
---|
2 | /**************************************************************************\ |
---|
3 | * eGroupWare API - Ping class * |
---|
4 | * This file written by Joseph Engo <jengo@phpgroupware.org> * |
---|
5 | * Linux only ping class for detecting network connections * |
---|
6 | * Copyright (C) 2001 Joseph Engo * |
---|
7 | * -------------------------------------------------------------------------* |
---|
8 | * This library is part of the eGroupWare API * |
---|
9 | * http://www.egroupware.org/api * |
---|
10 | * ------------------------------------------------------------------------ * |
---|
11 | * This library is free software; you can redistribute it and/or modify it * |
---|
12 | * under the terms of the GNU Lesser General Public License as published by * |
---|
13 | * the Free Software Foundation; either version 2.1 of the License, * |
---|
14 | * or any later version. * |
---|
15 | * This library is distributed in the hope that it will be useful, but * |
---|
16 | * WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
---|
18 | * See the GNU Lesser General Public License for more details. * |
---|
19 | * You should have received a copy of the GNU Lesser General Public License * |
---|
20 | * along with this library; if not, write to the Free Software Foundation, * |
---|
21 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
---|
22 | \**************************************************************************/ |
---|
23 | |
---|
24 | |
---|
25 | class ping |
---|
26 | { |
---|
27 | var $hostname; |
---|
28 | var $packet_tx; |
---|
29 | var $packet_rx; |
---|
30 | var $packet_loss; |
---|
31 | var $reponse_min; |
---|
32 | var $reponse_max; |
---|
33 | var $reponse_avg; |
---|
34 | var $reponse_mdev; |
---|
35 | |
---|
36 | var $raw_array_of_data = array(); |
---|
37 | |
---|
38 | function ping($hostname) |
---|
39 | { |
---|
40 | $this->hostname = $hostname; |
---|
41 | $this->re_ping(); |
---|
42 | } |
---|
43 | |
---|
44 | function clear_values() |
---|
45 | { |
---|
46 | $this->packet_tx = 0; |
---|
47 | $this->packet_rx = 0; |
---|
48 | $this->packet_loss = 0; |
---|
49 | $this->reponse_min = ''; |
---|
50 | $this->reponse_max = ''; |
---|
51 | $this->reponse_avg = ''; |
---|
52 | $this->reponse_mdev = ''; |
---|
53 | $this->raw_array_of_data = array(); |
---|
54 | } |
---|
55 | |
---|
56 | function re_ping() |
---|
57 | { |
---|
58 | $this->clear_values(); |
---|
59 | |
---|
60 | $raw_data = `ping -c 5 $this->hostname`; |
---|
61 | $this->raw_array_of_data = explode("\n",$raw_data); |
---|
62 | |
---|
63 | $this->parse_times(); |
---|
64 | $this->parse_responses(); |
---|
65 | } |
---|
66 | |
---|
67 | function parse_responses() |
---|
68 | { |
---|
69 | $dl = $this->raw_array_of_data[count($this->raw_array_of_data) - 3]; |
---|
70 | $values = explode(',',$dl); |
---|
71 | |
---|
72 | $packet_tx = str_replace(' packets transmitted','',$values[0]); |
---|
73 | $packet_rx = str_replace(' packets received','',$values[1]); |
---|
74 | $packet_loss = preg_replace('/% packet loss/','',$values[2]); |
---|
75 | |
---|
76 | $this->packet_tx = (int)str_replace(' ','',$packet_tx); |
---|
77 | $this->packet_rx = (int)str_replace(' ','',$packet_rx); |
---|
78 | $this->packet_loss = (int)str_replace(' ','',$packet_loss); |
---|
79 | } |
---|
80 | |
---|
81 | function parse_times() |
---|
82 | { |
---|
83 | $tl = $this->raw_array_of_data[count($this->raw_array_of_data) - 2]; |
---|
84 | $times_split = explode(' = ',$tl); |
---|
85 | $raw_times = $times_split[1]; |
---|
86 | $raw_times = str_replace(' ms','',$raw_times); |
---|
87 | $values = explode('/',$raw_times); |
---|
88 | |
---|
89 | $this->response_min = $values[0]; |
---|
90 | $this->response_avg = $values[1]; |
---|
91 | $this->response_max = $values[2]; |
---|
92 | $this->response_mdev = $values[3]; |
---|
93 | } |
---|
94 | |
---|
95 | function debug_output() |
---|
96 | { |
---|
97 | echo '<br><b>Debug output</b><hr width="20%" align="left">'; |
---|
98 | echo '<b>hostname:</b> ' . $this->hostname; |
---|
99 | echo '<br><b>tx:</b> ' . $this->packet_tx; |
---|
100 | echo '<br><b>rx:</b> ' . $this->packet_rx; |
---|
101 | echo '<br><b>loss:</b> ' . $this->packet_loss; |
---|
102 | echo '<br><b>min:</b> ' . $this->response_min; |
---|
103 | echo '<br><b>max:</b> ' . $this->response_max; |
---|
104 | echo '<br><b>avg:</b> ' . $this->response_avg; |
---|
105 | echo '<br><b>mdev:</b> ' . $this->response_mdev; |
---|
106 | } |
---|
107 | } |
---|