File:
0.02.18a/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 0;
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: return $elapsedTime;
122: break;
123: case "testloop":
124: return $param;
125: break;
126: }
127: return -1;
128: }
129:
130: function SimpleEval($test,$result)
131: {
132: switch($test)
133: {
134: case "ping":
135: if ($result<=0) return 2;
136: return 0;
137: case "web":
138: if ($result<=0) return 2;
139: return 0;
140: case "tcp":
141: if ($result==1) return 0;
142: return 2;
143: case "wtime":
144: if ($result<0) return 2;
145: return 0;
146: }
147: return -1;
148: }
149:
150: function aText($al)
151: {
152: switch($al)
153: {
154: case -1: return "Untested";
155: case 0: return "Passed";
156: case 1: return "Warning";
157: case 2: return "Failed";
158: default: return "Unknown";
159: }
160: }
161: ?>