File: 1.13.3b/server/base/tests.inc.php (View as Code)

1: 2: /* ------------------------------------------------------------- 3: This file is part of FreeNATS 4: 5: FreeNATS is (C) Copyright 2008-2010 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: if (!isset($BaseDir)) $BaseDir="../base/"; // 24: require_once($BaseDir."timer.inc.php"); // just in case standalone 25: 26: // FreeNATS_Local_Test Base Class - Local Tests (run from FreeNATS server) must extend from this 27: 28: class FreeNATS_Local_Test 29: { 30: function DoTest($testname,$param,$hostname="",$timeout=-1,$params=false) 31: { 32: return 0; 33: } 34: 35: function Evaluate($result) 36: { 37: return -1; 38: } 39: 40: function DisplayForm(&$row) 41: { 42: return false; 43: } 44: 45: function ProtectOutput(&$row) 46: { 47: return false; 48: } 49: } 50: 51: // ------------------------------------------------------------------------------------------------ 52: require($BaseDir."tests/tcp.inc.php"); 53: require($BaseDir."tests/udp.inc.php"); 54: require($BaseDir."tests/mysql.inc.php"); 55: require($BaseDir."tests/imap.inc.php"); 56: require($BaseDir."tests/smtp.inc.php"); 57: 58: require($BaseDir."tests/dns.inc.php"); 59: require($BaseDir."tests/nats-dns.inc.php"); // the wrapper module 60: 61: require($BaseDir."tests/smb.inc.php"); 62: 63: require($BaseDir."tests/ppping.inc.php"); 64: 65: require($BaseDir."tests/ldap.inc.php"); 66: 67: function ip_lookup($hostname) // "safe" DNS lookup function to call with a hostname, URL or IP - returns 0 if unsuccessful 68: { 69: // Is it already an IP adress? 70: $out=str_replace(".","",$hostname); 71: if (is_numeric($out)) return $hostname; // yes it is 72: 73: // No it is not 74: $ip=@gethostbyname($hostname); 75: if ($ip==$hostname) return 0; // unmodified host - lookup failed 76: 77: return $ip; 78: } 79: 80: function next_run_x($interval) 81: { 82: if ($interval<1) return time(); 83: return time()+(($interval)*60)-30; 84: } 85: 86: function test_sleep() 87: { 88: global $NATS; 89: if (!isset($NATS)) return false; 90: $sleep=$NATS->Cfg->Get("test.interval",0); 91: if ($sleep<=0) return false; 92: $sleep=$sleep*1000000; // convert to usec 93: usleep($sleep); 94: return true; 95: } 96: 97: function url_lookup($url) 98: { 99: // Sod regular expressions here as we'd have to do it twice or with cleverness I lack 100: // Is it a URL? 101: $colon=strpos($url,":"); 102: if ($colon != 0) // exists so it a URL 103: { 104: $out=preg_match("@^(?:http[s]*://)?([^/|\?|:]+)@i",$url,$matches); 105: $hostname=$matches[1]; 106: } 107: else $hostname=$url; // try direct 108: 109: return ip_lookup($hostname); 110: } 111: 112: 113: function bin_str_dump($s,$count=0) 114: { 115: //$s = base_convert($s,10,2); 116: $data = unpack('C*',$s); 117: foreach($data as $item) 118: { 119: echo ord($item)." "; 120: } 121: echo "\n"; 122: } 123: 124: 125: 126: function PingTest($host,$ctimeout=-1) 127: { 128: global $NATS; 129: 130: $returnsecs=true; 131: if (isset($NATS)) 132: { 133: if ($NATS->Cfg->Get("test.icmp.returnms",0)==1) $returnsecs=false; 134: } 135: 136: // Timeout Values 137: if (isset($NATS)) $timeout=$NATS->Cfg->Get("test.icmp.timeout",10); 138: else $timeout=10; 139: if ($ctimeout>0) $timeout=$ctimeout; // use custom timeout if passed 140: if ($timeout<=0) $timeout=10; // catch-all for defaults bug 141: 142: $ping = new PPPing(); 143: $ping->hostname = $host; 144: $ping->timeout = $timeout; 145: 146: $result=$ping->Ping(); 147: 148: if ($result<0) // error condition 149: { 150: return $result; 151: } 152: else if ($result==0) // zero time 153: { 154: $result="0.0001"; 155: } 156: 157: if ($returnsecs) 158: { 159: $result = round($result/1000,3); // convert to seconds 160: if ($result==0) $result=0.0001; 161: } 162: 163: return $result; 164: } 165: 166: function WebTest($url,$timeout=-1) 167: { 168: global $NATS; 169: if ($timeout<=0) // use NATS or env 170: { 171: if (isset($NATS)) 172: { 173: $nto=$NATS->Cfg->Get("test.http.timeout",-1); 174: if ($nto>0) $timeout=$nto; // use NATS timeout 175: } 176: } 177: if ($timeout>0) // use the set timeout 178: $oldtimeout=ini_set("default_socket_timeout",$timeout); 179: 180: if (function_exists("curl_getinfo")) // use CURL if present 181: { 182: $ch=curl_init(); 183: curl_setopt($ch,CURLOPT_URL,$url); 184: curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 185: curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0); 186: curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0); 187: curl_setopt($ch,CURLOPT_HEADER,1); 188: if ($timeout>0) curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); 189: if ($timeout>0) curl_setopt($ch,CURLOPT_TIMEOUT,$timeout); 190: if (!$output=curl_exec($ch)) 191: { 192: $ctr=-1; // failed 193: } 194: else $ctr=round(curl_getinfo($ch,CURLINFO_SIZE_DOWNLOAD)/1024,2); 195: curl_close($ch); 196: 197: if ($ctr==0) $ctr="0.0001"; 198: 199: } 200: else 201: { // no CURL - use fopen() 202: $oldagent=ini_set("user_agent","MSIE 4\.0b2;"); // MSIE 4.0b2 is HTTP/1.0 only just like fopen http wrapper 203: $fp=@fopen($url,"r"); 204: if ($fp<=0) 205: { 206: if ($timeout>0) ini_set("default_socket_timeout",$oldtimeout); 207: ini_set("user_agent",$oldagent); 208: return -1; 209: } 210: $ctr=0; 211: while ($body=@fgets($fp,1024)) $ctr+=sizeof($body); 212: @fclose($fp); 213: ini_set("user_agent",$oldagent); 214: } 215: 216: 217: 218: if ($timeout>0) ini_set("default_socket_timeout",$oldtimeout); 219: return $ctr; 220: } 221: 222: function DoTest($test,$param,$hostname="",$timeout=-1,$params=0,$nodeid="") 223: { 224: global $NATS; 225: if (!is_array($params)) 226: { 227: $params=array(); 228: for ($a=0; $a<10; $a++) $params[$a]=""; 229: } 230: 231: switch ($test) 232: { 233: case "web": case "wsize": 234: // Don't bother with pre-resolution as size only 235: return WebTest($param,$timeout); 236: break; 237: /* -- modularised 238: case "tcp": // nb TCP does not support timeouts currently 239: $ip=ip_lookup($hostname); 240: if ($ip=="0") return 0; 241: $fp=@fsockopen($ip,$param); 242: if ($fp<=0) return 0; 243: @fclose($fp); 244: return 1; 245: break; 246: */ 247: case "wtime": 248: $timer=new TFNTimer(); 249: // Do a pre-lookup 250: $ip=url_lookup($param); 251: if ($ip=="0") return -1; // dns lookup failed 252: $timer->Start(); 253: $r=WebTest($param,$timeout); 254: $elapsedTime=$timer->Stop(); 255: $elapsedTime=round($elapsedTime,4); 256: if ($r<0) return -1; // open failed 257: if ($r==0) return -2; // no chars shown as returned 258: if ($elapsedTime<=0) return 0.0001; 259: return $elapsedTime; 260: break; 261: 262: case "host": 263: $timer=new TFNTimer(); 264: if (preg_match("/[a-zA-Z]/",$param)>0) $is_ip=false; 265: else $is_ip=true; 266: 267: $timer->Start(); 268: if ($is_ip) $result=gethostbyaddr($param); 269: else $result=gethostbyname($param); 270: $elapsedTime=$timer->Stop(); 271: 272: if ($result==$param) // lookup failed 273: return -1; 274: 275: if ($result=="") // lookup failed 276: return -1; 277: 278: $elapsedTime=round($elapsedTime,4); 279: if ($elapsedTime<=0) return 0.0001; 280: return $elapsedTime; 281: break; 282: 283: case "testloop": 284: return $param; 285: break; 286: 287: case "testrand": 288: mt_srand(microtime()*1000000); 289: if ( ($param=="") || ($param==0) ) $param=100; 290: return mt_rand(0,$param); 291: break; 292: 293: case "ping": 294: return PingTest($param,$timeout); 295: break; 296: 297: default: 298: if (isset($NATS)) // try and see if a test is registered 299: { 300: if (isset($NATS->Tests->QuickList[$test])) // exists 301: { 302: $NATS->Tests->Tests[$test]->Create(); 303: return $NATS->Tests->Tests[$test]->instance->DoTest($test,$param,$hostname,$timeout,$params); 304: } 305: } 306: 307: } 308: return -1; // did not run any test so untested 309: } 310: 311: function SimpleEval($test,$result) 312: { 313: global $NATS; 314: switch($test) 315: { 316: case "ping": // handles both types of simple evaluation (inbuilt ICMP and remote ping) 317: if ($result<=0) return 2; 318: return 0; 319: case "web": case "wsize": 320: if ($result<=0) return 2; 321: return 0; 322: /* 323: case "tcp": 324: if ($result==1) return 0; 325: return 2; 326: */ 327: case "wtime": 328: if ($result<0) return 2; 329: return 0; 330: /* 331: case "mysql": 332: if ($result<=0) return 2; 333: return 0; 334: 335: case "mysqlrows": 336: if ($result<=0) return 2; // no rows returned or error 337: return 0; 338: */ 339: case "host": case "dns": 340: if ($result<=0) return 2; // no records returned or error 341: 342: case "testloop": 343: return 0; 344: case "testrand": 345: return 0; 346: 347: default: 348: if (isset($NATS)) 349: { 350: if (isset($NATS->Tests->QuickList[$test])) 351: { 352: $NATS->Tests->Tests[$test]->Create(); 353: return $NATS->Tests->Tests[$test]->instance->Evaluate($result); 354: } 355: } 356: } 357: return -1; // untested if we don't know WTF the result was 358: } 359: 360: function aText($al) 361: { 362: return oText($al); // uses function in tests.inc.php with site config support 363: /* -- depreciated 364: switch($al) 365: { 366: case -1: return "Untested"; 367: case 0: return "Passed"; 368: case 1: return "Warning"; 369: case 2: return "Failed"; 370: default: return "Unknown"; 371: } 372: */ 373: } 374: ?>