File:
0.02.52a/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 Foobar. If not, see www.gnu.org/licenses
19:
20: For more information see www.purplepixie.org/freenats
21: -------------------------------------------------------------- */
22:
23: function icmpChecksum($data)
24: {
25: if (strlen($data)%2)
26: $data .= "\x00";
27:
28: $bit = unpack('n*', $data);
29: $sum = array_sum($bit);
30:
31: while ($sum >> 16)
32: $sum = ($sum >> 16) + ($sum & 0xffff);
33:
34: return pack('n*', ~$sum);
35: }
36: function PingTest($host)
37: {
38: // Make Package
39: $type= "\x08";
40: $code= "\x00";
41: $checksum= "\x00\x00";
42: $identifier = "\x00\x00";
43: $seqNumber = "\x00\x00";
44: $data= "FreeNATS";
45: $package = $type.$code.$checksum.$identifier.$seqNumber.$data;
46: $checksum = icmpChecksum($package); // Calculate the checksum
47: $package = $type.$code.$checksum.$identifier.$seqNumber.$data;
48:
49: // Create Socket
50: $socket = @socket_create(AF_INET, SOCK_RAW, 1);
51: //or die(socket_strerror(socket_last_error()));
52: if (!$socket) return 0;
53:
54: // Set Non-Blocking
55: @socket_set_nonblock($socket);
56:
57: // Connect Socket
58: $sconn=@socket_connect($socket, $host, null);
59: if (!$sconn) return 0;
60:
61: // Start Timer
62: $startTime = microtime(true);
63:
64: // Send Data
65: @socket_send($socket, $package, strLen($package), 0);
66:
67: // Read Data
68: $keepon=true;
69: if (isset($NATS)) $timeout=$NATS->Cfg->Get("test.icmp.timeout",10);
70: else $timeout=10;
71: while( (!(@socket_read($socket, 255))) && $keepon)
72: { // basically just kill time
73: // consider putting some sort of sleepy thing here to lower load but would f* with figures!
74:
75: if ( (microtime(true) - $startTime) > $timeout )
76: $keepon=false;
77: }
78:
79: if ($keepon) // didn't time out - read data
80: {
81: @socket_close($socket);
82: $ret=round(microtime(true) - $startTime, 4);
83: if ($ret==0) return "0.0001";
84: else return $ret;
85: }
86:
87: // Socket timed out
88: @socket_close($socket);
89: return 0;
90: }
91:
92: function WebTest($url)
93: {
94: $fp=@fopen($url,"r");
95: if ($fp<=0) return -1;
96: $ctr=0;
97: while ($body=@fgets($fp,1024)) $ctr+=sizeof($body);
98: @fclose($fp);
99: return $ctr;
100: }
101:
102: function DoTest($test,$param,$hostname="")
103: {
104: switch ($test)
105: {
106: case "web":
107: return WebTest($param);
108: break;
109: case "tcp":
110: $fp=@fsockopen($hostname,$param);
111: if ($fp<=0) return 0;
112: @fclose($fp);
113: return 1;
114: break;
115: case "wtime":
116: $startTime=microtime(true);
117: $r=WebTest($param);
118: $elapsedTime=round(microtime(true)-$startTime,4);
119: if ($r<=0) return -1;
120: //if ($r==0) return 0.0001;
121: if ($elapsedTime==0) return 0.0001;
122: return $elapsedTime;
123: break;
124: case "testloop":
125: return $param;
126: break;
127:
128: case "testrand":
129: mt_srand(microtime()*1000000);
130: if ( ($param=="") || ($param==0) ) $param=100;
131: return mt_rand(0,$param);
132: break;
133:
134: case "ping":
135: return PingTest($param);
136: break;
137: }
138: return -1;
139: }
140:
141: function SimpleEval($test,$result)
142: {
143: switch($test)
144: {
145: case "ping": // handles both types of simple evaluation (inbuilt ICMP and remote ping)
146: if ($result<=0) return 2;
147: return 0;
148: case "web":
149: if ($result<=0) return 2;
150: return 0;
151: case "tcp":
152: if ($result==1) return 0;
153: return 2;
154: case "wtime":
155: if ($result<0) return 2;
156: return 0;
157: case "testloop":
158: return 0;
159: case "testrand":
160: return 0;
161: }
162: return -1;
163: }
164:
165: function aText($al)
166: {
167: switch($al)
168: {
169: case -1: return "Untested";
170: case 0: return "Passed";
171: case 1: return "Warning";
172: case 2: return "Failed";
173: default: return "Unknown";
174: }
175: }
176: ?>