File: 0.04.19a/server/bin/tester.sh (View as Code)

1: #!/usr/bin/php -q 2: 3: /* ------------------------------------------------------------- 4: This file is part of FreeNATS 5: 6: FreeNATS is (C) Copyright 2008 PurplePixie Systems 7: 8: FreeNATS is free software: you can redistribute it and/or modify 9: it under the terms of the GNU General Public License as published by 10: the Free Software Foundation, either version 3 of the License, or 11: (at your option) any later version. 12: 13: FreeNATS is distributed in the hope that it will be useful, 14: but WITHOUT ANY WARRANTY; without even the implied warranty of 15: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16: GNU General Public License for more details. 17: 18: You should have received a copy of the GNU General Public License 19: along with FreeNATS. If not, see www.gnu.org/licenses 20: 21: For more information see www.purplepixie.org/freenats 22: -------------------------------------------------------------- */ 23: 24: if ((isset($argc))&&(isset($argv))) 25: { 26: if ($argc>1) 27: { 28: $nfilter=$argv[1]; 29: } 30: else $nfilter=""; 31: } 32: 33: require("include.php"); 34: 35: $dbt=""; 36: 37: function db($txt,$nl=true) 38: { 39: global $dbt; 40: echo $txt; 41: $dbt.=$txt; 42: if ($nl) 43: { 44: echo "\n"; 45: $dbt.="
\n";
46: } 47: } 48: 49: $NATS->Start(); 50: if ($nfilter!="") $st=": Node ".$nfilter; 51: else $st=""; 52: db("NATS Tester Script Starting".$st); 53: 54: $highalertlevel=-1; 55: $talertc=0; 56: 57: // check if already running 58: $cq="SELECT startx FROM fntestrun WHERE fnode=\"".ss($nfilter)."\" AND finishx=0"; 59: $cr=$NATS->DB->Query($cq); 60: if ($NATS->DB->Num_Rows($cr)>0) 61: { 62: $NATS->Event("Tester Already Running: Aborted",1,"Tester","Error"); 63: db("Tester Already Running: Aborted"); 64: $NATS->Stop(); 65: exit(); 66: } 67: 68: $gq="INSERT INTO fntestrun(startx,fnode) VALUES(".time().",\"".ss($nfilter)."\")"; 69: $NATS->DB->Query($gq); 70: $trid=$NATS->DB->Insert_Id(); 71: db("Test ID: ".$trid." (Started at ".nicedt(time()).")"); 72: $NATS->Event("Tester ".$trid." Started",5,"Tester","Start"); 73: 74: db(" "); 75: 76: if ($nfilter=="") $q="SELECT * FROM fnnode WHERE nodeenabled=1"; 77: else 78: { 79: $q="SELECT * FROM fnnode WHERE nodeid=\"".ss($nfilter)."\" AND nodeenabled=1"; 80: } 81: $r=$NATS->DB->Query($q); 82: 83: 84: while ($row=$NATS->DB->Fetch_Array($r)) 85: { 86: $dotests=true; 87: $alertlevel=0; 88: $alerts=array(); 89: $alertc=0; 90: db("NodeID: ".$row['nodeid']); 91: $NATS->Event("Tester ".$trid." Node ".$row['nodeid'],10,"Tester","Node"); 92: 93: // Scheduling Test In Here - sets dotests to false and alertlevel to -1 untested 94: if ($row['scheduleid']!=0) // has a schedule 95: { 96: db(" Has Schedule: Yes - Checking"); 97: $run=run_x_in_schedule(time(),$row['scheduleid']); 98: if (!$run) 99: { 100: db(" In Schedule: No - Skipping Tests"); 101: $NATS->Event("Tester ".$trid." Skipped by Schedule",5,"Tester","Node"); 102: $dotests=false; 103: $alertlevel=-1; 104: } 105: else db(" In Schedule: Yes"); 106: } 107: 108: 109: $ptr=0; 110: $pal=0; 111: 112: if ($dotests) // record running now 113: { 114: $q="UPDATE fnnode SET lastrunx=".time()." WHERE nodeid=\"".ss($row['nodeid'])."\""; 115: $NATS->DB->Query($q); 116: } 117: 118: if ($row['pingtest']&&$dotests) 119: { 120: db(" Ping Test: Yes"); 121: $NATS->Event("Tester ".$trid." Pinging Node ".$row['nodeid'],10,"Tester","Ping"); 122: $ptr=PingTest($row['hostname']); 123: $NATS->Event("Tester ".$trid." Ping Node ".$row['nodeid']." Returned ".$ptr,10,"Tester","Ping"); 124: db(" Ping Returned: ".$ptr); 125: if ( ($ptr<=0) && ($NATS->Cfg->Get("test.icmp.attempts","2")>1) ) 126: { 127: $att=$NATS->Cfg->Get("test.icmp.attempts","2"); 128: for ($a=2; $a<=$att; $a++) // starting on second attempt 129: { 130: // try again... 131: db(" Trying Ping Again - X".$a); 132: $NATS->Event("Tester ".$trid." Ping X".$a." Node ".$row['nodeid'],10,"Tester","Ping"); 133: $ptr=PingTest($row['hostname']); 134: $NATS->Event("Tester ".$trid." Ping Node ".$row['nodeid']." Returned ".$ptr,10,"Tester","Ping"); 135: db(" Ping Returned: ".$ptr); 136: if ($ptr>0) $a=$att+1; // break out of the loop 137: } 138: } 139: 140: if ($ptr<=0) 141: { 142: $alertlevel=2; 143: db(" Ping Test: Failed"); 144: $alerts[$alertc++]="ping failed"; 145: $pal=2; 146: } 147: else db(" Ping Test: Passed"); 148: 149: // pingtest output bodge 150: // is there a test entry for ICMP 151: $fq="SELECT localtestid FROM fnlocaltest WHERE nodeid=\"".$row['nodeid']."\" AND testtype=\"ICMP\""; 152: $fr=$NATS->DB->Query($fq); 153: $ltid_icmp=""; 154: if ($irow=$NATS->DB->Fetch_Array($fr)) 155: { // exists 156: $uq="UPDATE fnlocaltest SET alertlevel=".$pal.",lastrunx=".time().",lastvalue=".$ptr." WHERE localtestid=".$irow['localtestid']; 157: $ltid_icmp=$irow['localtestid']; 158: //echo $uq; 159: $NATS->DB->Query($uq); 160: } 161: else 162: { // doesn't exist 163: $uq="INSERT INTO fnlocaltest(nodeid,testtype,alertlevel,lastrunx,lastvalue) VALUES(\"".$row['nodeid']."\",\"ICMP\",".$pal.",".time().",".$ptr.")"; 164: //echo $uq; 165: $NATS->DB->Query($uq); 166: $ltid_icmp=$NATS->DB->Insert_Id(); 167: } 168: $NATS->DB->Free($fr); 169: 170: // record the ICMP bodge-test here 171: $rq="INSERT INTO fnrecord(testid,recordx,testvalue,alertlevel,nodeid) VALUES(\"L".$ltid_icmp."\",".time().",".$ptr.",".$pal.",\"".$row['nodeid']."\")"; 172: $NATS->DB->Query($rq); 173: //echo $rq." ".$NATS->DB->Affected_Rows()."\n"; 174: 175: } 176: else 177: { // further ICMP bodge - update to -1 or do nothing if the test doesn't exist 178: $uq="UPDATE fnlocaltest SET alertlevel=-1,lastrunx=".time()." WHERE nodeid=\"".$row['nodeid']."\" AND testtype=\"ICMP\""; 179: $NATS->DB->Query($uq); 180: } 181: 182: if ($dotests&&($row['pingfatal'])&&($ptr<=0)) 183: { 184: db(" Ping Fatal: Yes - Not Continuing"); 185: $NATS->Event("Tester ".$trid." Ping Fatal for Node ".$row['nodeid'],10,"Tester","Ping"); 186: $dotests=false; 187: } 188: 189: // do the tests - only actually exec if dotests true 190: 191: db("Doing Local Tests"); 192: $NATS->Event("Tester ".$trid." Testing Node ".$row['nodeid'],10,"Tester","Test"); 193: $q="SELECT * FROM fnlocaltest WHERE nodeid=\"".$row['nodeid']."\" AND testtype!=\"ICMP\" AND testenabled=1 ORDER BY localtestid ASC"; 194: $res=$NATS->DB->Query($q); 195: while ($lrow=$NATS->DB->Fetch_Array($res)) 196: { 197: db(" Test: ".$lrow['testtype']." (".$lrow['testparam'].")"); 198: 199: // Build parameter array 200: $params=array(); 201: for ($a=1; $a<10; $a++) 202: { 203: $parstr="testparam".$a; 204: $params[$a]=$lrow[$parstr]; 205: } 206: 207: $NATS->Event("Tester ".$trid." Node ".$row['nodeid']." Doing ".$lrow['testtype']."(".$lrow['testparam'].")",10,"Tester","Test"); 208: if ($dotests) $result=DoTest($lrow['testtype'],$lrow['testparam'],$row['hostname'],$lrow['timeout'],$params); 209: else $result=0; 210: $NATS->Event("Tester ".$trid." Node ".$row['nodeid']." Result ".$result." from ".$lrow['testtype']."(".$lrow['testparam'].")",10,"Tester","Test"); 211: db(" Result: ".$result); 212: 213: if ($dotests) 214: { 215: // evaluation 216: if ($lrow['simpleeval']==1) $lvl=SimpleEval($lrow['testtype'],$result); 217: else $lvl=nats_eval("L".$lrow['localtestid'],$result); 218: db(" Eval: ".$lvl); 219: 220: // put in the custom retries based on attempts here - we KNOW dotests is on so don't need to worry about untested status 221: $att=$lrow['attempts']; 222: if ( ($lvl!=0) && (is_numeric($att)) && ($att>1) ) 223: { 224: for ($a=2; $a<=$att; $a++) 225: { 226: db(" Test: ".$lrow['testtype']." (".$lrow['testparam'].") X".$a); 227: $NATS->Event("Tester ".$trid." Node ".$row['nodeid']." X".$a." Doing ".$lrow['testtype']."(".$lrow['testparam'].")",10,"Tester","Test"); 228: $result=DoTest($lrow['testtype'],$lrow['testparam'],$row['hostname'],$lrow['timeout'],$params); 229: db(" Result: ".$result); 230: if ($lrow['simpleeval']==1) $lvl=SimpleEval($lrow['testtype'],$result); 231: else $lvl=nats_eval("L".$lrow['localtestid'],$result); 232: db(" Eval: ".$lvl); 233: if ($lvl==0) $a=$att+1; // test passed 234: } 235: } 236: 237: // $lvl is now the last lvl regardless of where it came from 238: 239: if ($lvl>$alertlevel) $alertlevel=$lvl; 240: if ($lvl>0) 241: { 242: if ($lrow['testname']=="") $s=$lrow['testtype']."/".substr($lrow['testparam'],0,5)." "; 243: else $s=$lrow['testname']." "; 244: if ($lvl>1) $s.="failed"; 245: else $s.="warning"; 246: $alerts[$alertc++]=$s; 247: } 248: } else $lvl=-1; 249: 250: // record it 251: if ($lrow['testrecord']==1) 252: { 253: $tid="L".$lrow['localtestid']; 254: $iq="INSERT INTO fnrecord(testid,nodeid,alertlevel,testvalue,recordx) VALUES("; 255: $iq.="\"".$tid."\",\"".$row['nodeid']."\",".$lvl.",".$result.",".time().")"; 256: $NATS->DB->Query($iq); 257: db(" Recording Test"); 258: } 259: if ((!isset($result))||(!is_numeric($result))) $result=0; // safety net 260: 261: // update localtest record 262: $uq="UPDATE fnlocaltest SET lastrunx=".time().",alertlevel=".$lvl.",lastvalue=".$result." WHERE localtestid=".$lrow['localtestid']; 263: $NATS->DB->Query($uq); 264: 265: 266: } 267: 268: 269: 270: $NATS->Event("Tester ".$trid." Finished Node ".$row['nodeid'],10,"Tester","Node"); 271: 272: 273: db("Highest Alert Level: ".$alertlevel); 274: db("Alert Count : ".$alertc); 275: $als=""; 276: foreach($alerts as $al) $als.=$al.", "; 277: db("Alerts: ".$als); 278: 279: $NATS->SetAlerts($row['nodeid'],$alertlevel,$alerts); 280: 281: db(" "); 282: 283: if ($alertlevel>$highalertlevel) $highalertlevel=$alertlevel; 284: $talertc+=$alertc; 285: 286: } 287: 288: 289: 290: db("Finished Tests... Finishing Off"); 291: db("Summary: Tester ".$trid." Highest Level ".$highalertlevel.", Alerts ".$talertc); 292: $uq="UPDATE fntestrun SET finishx=".time().",routput=\"".ss($dbt)."\" WHERE trid=".$trid; 293: $NATS->DB->Query($uq); 294: 295: 296: $NATS->Event("Tester ".$trid." Highest Level ".$highalertlevel.", Alerts ".$talertc,7,"Tester","Stat"); 297: $NATS->Event("Tester ".$trid." Finished",5,"Tester","Stop"); 298: 299: // in here for now... 300: $NATS->ActionFlush(); 301: 302: $NATS->Stop(); 303: db("NATS Stopped... Finished"); 304: ?> 305: 306: