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