File:
0.02.76a/server/base/tests.inc.php (
View as Code)
1: 2: /* -------------------------------------------------------------
3: This file is part of FreeNATS
4:
5: FreeNATS is (C) Copyright 2008 PurplePixie Systems
6:
7: FreeNATS is free software: you can redistribute it and/or modify
8: it under the terms of the GNU General Public License as published by
9: the Free Software Foundation, either version 3 of the License, or
10: (at your option) any later version.
11:
12: FreeNATS is distributed in the hope that it will be useful,
13: but WITHOUT ANY WARRANTY; without even the implied warranty of
14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: GNU General Public License for more details.
16:
17: You should have received a copy of the GNU General Public License
18: along with FreeNATS. If not, see www.gnu.org/licenses
19:
20: For more information see www.purplepixie.org/freenats
21: -------------------------------------------------------------- */
22:
23:
24:
25: function icmpChecksum($data)
26: {
27: if (strlen($data)%2)
28: $data .= "\x00";
29:
30: $bit = unpack('n*', $data);
31: $sum = array_sum($bit);
32:
33: while ($sum >> 16)
34: $sum = ($sum >> 16) + ($sum & 0xffff);
35:
36: return pack('n*', ~$sum);
37: }
38:
39: function PingTest($host,$ctimeout=-1)
40: {
41: global $NATS;
42: $FreeNATS_Timer=new TFNTimer();
43: // Make Package
44: $type= "\x08";
45: $code= "\x00";
46: $checksum= "\x00\x00";
47: $identifier = "\x00\x00";
48: $seqNumber = "\x00\x00";
49: $data= "FreeNATS";
50: $package = $type.$code.$checksum.$identifier.$seqNumber.$data;
51: $checksum = icmpChecksum($package); // Calculate the checksum
52: $package = $type.$code.$checksum.$identifier.$seqNumber.$data;
53:
54: // Return s or ms(s*1000)
55: $returnsecs=true;
56: if (isset($NATS))
57: {
58: if ($NATS->Cfg->Get("test.icmp.returnms",0)==1) $returnsecs=false;
59: }
60:
61: // Timeout Values
62: if (isset($NATS)) $timeout=$NATS->Cfg->Get("test.icmp.timeout",10);
63: else $timeout=10;
64: if ($ctimeout>0) $timeout=$ctimeout; // use custom timeout if passed
65: if ($timeout<=0) $timeout=10; // catch-all for defaults bug
66:
67: // Create Socket
68: $socket = @socket_create(AF_INET, SOCK_RAW, 1);
69: //or die(socket_strerror(socket_last_error()));
70: if (!$socket) return 0;
71:
72: // Set Non-Blocking
73: @socket_set_nonblock($socket);
74:
75: // Connect Socket
76: $sconn=@socket_connect($socket, $host, null);
77: if (!$sconn) return 0;
78:
79: // Send Data
80: @socket_send($socket, $package, strLen($package), 0);
81:
82: // Start Timer
83: $FreeNATS_Timer->Start();
84: $startTime = microtime(true); // need this for the looping section
85:
86:
87: // Read Data
88: $keepon=true;
89:
90: while( (!(@socket_read($socket, 255))) && $keepon)
91: { // basically just kill time
92: // consider putting some sort of sleepy thing here to lower load but would f* with figures!
93:
94: if ( (microtime(true) - $startTime) > $timeout )
95: $keepon=false;
96: }
97:
98: if ($keepon) // didn't time out - read data
99: {
100: $elapsed=$FreeNATS_Timer->Stop();
101: if ($returnsecs) $elapsed=round($elapsed,4);
102: else $elapsed=round( ($elapsed*1000),4 );
103: @socket_close($socket);
104: // $ret=round(microtime(true) - $startTime, 4); -- old method
105: if ($elapsed<=0) $elapsed="0.0001"; // safety catch-all
106: return $elapsed;
107: /*
108: if ($ret==0) return "0.0001";
109: else
110: {
111: if ($ret<=0) return "0.0001";
112: else return $ret;
113: }
114: */
115: }
116:
117: // Socket timed out
118: @socket_close($socket);
119: return 0;
120: }
121:
122: function WebTest($url,$timeout=-1)
123: {
124: global $NATS;
125: if ($timeout<=0) // use NATS or env
126: {
127: if (isset($NATS))
128: {
129: $nto=$NATS->Cfg->Get("test.http.timeout",-1);
130: if ($nto>0) $timeout=$nto; // use NATS timeout
131: }
132: }
133: if ($timeout>0) // use the set timeout
134: $oldtimeout=ini_set("default_socket_timeout",$timeout);
135: $fp=@fopen($url,"r");
136: if ($fp<=0)
137: {
138: if ($timeout>0) ini_set("default_socket_timeout",$oldtimeout);
139: return -1;
140: }
141: $ctr=0;
142: while ($body=@fgets($fp,1024)) $ctr+=sizeof($body);
143: @fclose($fp);
144: if ($timeout>0) ini_set("default_socket_timeout",$oldtimeout);
145: return $ctr;
146: }
147:
148: function DoTest($test,$param,$hostname="",$timeout=-1)
149: {
150: switch ($test)
151: {
152: case "web":
153: return WebTest($param,$timeout);
154: break;
155: case "tcp": // nb TCP does not support timeouts currently
156: $fp=@fsockopen($hostname,$param);
157: if ($fp<=0) return 0;
158: @fclose($fp);
159: return 1;
160: break;
161: case "wtime":
162: $startTime=microtime(true);
163: $r=WebTest($param,$timeout);
164: $elapsedTime=round(microtime(true)-$startTime,4);
165: if ($r<0) return -1; // fopen failed
166: if ($r==0) return -2; // no chars shown as returned
167: if ($elapsedTime<=0) return 0.0001;
168: return $elapsedTime;
169: break;
170: case "testloop":
171: return $param;
172: break;
173:
174: case "testrand":
175: mt_srand(microtime()*1000000);
176: if ( ($param=="") || ($param==0) ) $param=100;
177: return mt_rand(0,$param);
178: break;
179:
180: case "ping":
181: return PingTest($param,$timeout);
182: break;
183: }
184: return -1;
185: }
186:
187: function SimpleEval($test,$result)
188: {
189: switch($test)
190: {
191: case "ping": // handles both types of simple evaluation (inbuilt ICMP and remote ping)
192: if ($result<=0) return 2;
193: return 0;
194: case "web":
195: if ($result<=0) return 2;
196: return 0;
197: case "tcp":
198: if ($result==1) return 0;
199: return 2;
200: case "wtime":
201: if ($result<0) return 2;
202: return 0;
203: case "testloop":
204: return 0;
205: case "testrand":
206: return 0;
207: }
208: return -1;
209: }
210:
211: function aText($al)
212: {
213: switch($al)
214: {
215: case -1: return "Untested";
216: case 0: return "Passed";
217: case 1: return "Warning";
218: case 2: return "Failed";
219: default: return "Unknown";
220: }
221: }
222: ?>