File: 0.02.77a/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: if ($row['pingtest']&&$dotests) 112: { 113: db(" Ping Test: Yes"); 114: $NATS->Event("Tester ".$trid." Pinging Node ".$row['nodeid'],10,"Tester","Ping"); 115: $ptr=PingTest($row['hostname']); 116: $NATS->Event("Tester ".$trid." Ping Node ".$row['nodeid']." Returned ".$ptr,10,"Tester","Ping"); 117: db(" Ping Returned: ".$ptr); 118: if ( ($ptr<=0) && ($NATS->Cfg->Get("test.icmp.attempts","2")>1) ) 119: { 120: $att=$NATS->Cfg->Get("test.icmp.attempts","2"); 121: for ($a=2; $a<=$att; $a++) // starting on second attempt 122: { 123: // try again... 124: db(" Trying Ping Again - X".$a); 125: $NATS->Event("Tester ".$trid." Ping X".$a." Node ".$row['nodeid'],10,"Tester","Ping"); 126: $ptr=PingTest($row['hostname']); 127: $NATS->Event("Tester ".$trid." Ping Node ".$row['nodeid']." Returned ".$ptr,10,"Tester","Ping"); 128: db(" Ping Returned: ".$ptr); 129: if ($ptr>0) $a=$att+1; // break out of the loop 130: } 131: } 132: 133: if ($ptr<=0) 134: { 135: $alertlevel=2; 136: db(" Ping Test: Failed"); 137: $alerts[$alertc++]="ping failed"; 138: $pal=2; 139: } 140: else db(" Ping Test: Passed"); 141: 142: // pingtest output bodge 143: // is there a test entry for ICMP 144: $fq="SELECT localtestid FROM fnlocaltest WHERE nodeid=\"".$row['nodeid']."\" AND testtype=\"ICMP\""; 145: $fr=$NATS->DB->Query($fq); 146: $ltid_icmp=""; 147: if ($irow=$NATS->DB->Fetch_Array($fr)) 148: { // exists 149: $uq="UPDATE fnlocaltest SET alertlevel=".$pal.",lastrunx=".time()." WHERE localtestid=".$irow['localtestid']; 150: $ltid_icmp=$irow['localtestid']; 151: //echo $uq; 152: $NATS->DB->Query($uq); 153: } 154: else 155: { // doesn't exist 156: $uq="INSERT INTO fnlocaltest(nodeid,testtype,alertlevel,lastrunx) VALUES(\"".$row['nodeid']."\",\"ICMP\",".$pal.",".time().")"; 157: //echo $uq; 158: $NATS->DB->Query($uq); 159: $ltid_icmp=$NATS->DB->Insert_Id(); 160: } 161: $NATS->DB->Free($fr); 162: 163: // record the ICMP bodge-test here 164: $rq="INSERT INTO fnrecord(testid,recordx,testvalue,alertlevel,nodeid) VALUES(\"L".$ltid_icmp."\",".time().",".$ptr.",".$pal.",\"".$row['nodeid']."\")"; 165: $NATS->DB->Query($rq); 166: //echo $rq." ".$NATS->DB->Affected_Rows()."\n"; 167: 168: } 169: else 170: { // further ICMP bodge - update to -1 or do nothing if the test doesn't exist 171: $uq="UPDATE fnlocaltest SET alertlevel=-1,lastrunx=".time()." WHERE nodeid=\"".$row['nodeid']."\" AND testtype=\"ICMP\""; 172: $NATS->DB->Query($uq); 173: } 174: 175: if ($dotests&&($row['pingfatal'])&&($ptr<=0)) 176: { 177: db(" Ping Fatal: Yes - Not Continuing"); 178: $NATS->Event("Tester ".$trid." Ping Fatal for Node ".$row['nodeid'],10,"Tester","Ping"); 179: $dotests=false; 180: } 181: 182: // do the tests - only actually exec if dotests true 183: 184: db("Doing Local Tests"); 185: $NATS->Event("Tester ".$trid." Testing Node ".$row['nodeid'],10,"Tester","Test"); 186: $q="SELECT * FROM fnlocaltest WHERE nodeid=\"".$row['nodeid']."\" AND testtype!=\"ICMP\""; 187: $res=$NATS->DB->Query($q); 188: while ($lrow=$NATS->DB->Fetch_Array($res)) 189: { 190: db(" Test: ".$lrow['testtype']." (".$lrow['testparam'].")"); 191: $NATS->Event("Tester ".$trid." Node ".$row['nodeid']." Doing ".$lrow['testtype']."(".$lrow['testparam'].")",10,"Tester","Test"); 192: if ($dotests) $result=DoTest($lrow['testtype'],$lrow['testparam'],$row['hostname'],$lrow['timeout']); 193: else $result=0; 194: $NATS->Event("Tester ".$trid." Node ".$row['nodeid']." Result ".$result." from ".$lrow['testtype']."(".$lrow['testparam'].")",10,"Tester","Test"); 195: db(" Result: ".$result); 196: 197: if ($dotests) 198: { 199: // evaluation 200: if ($lrow['simpleeval']==1) $lvl=SimpleEval($lrow['testtype'],$result); 201: else $lvl=nats_eval("L".$lrow['localtestid'],$result); 202: db(" Eval: ".$lvl); 203: 204: // put in the custom retries based on attempts here - we KNOW dotests is on so don't need to worry about untested status 205: $att=$lrow['attempts']; 206: if ( ($lvl!=0) && (is_numeric($att)) && ($att>1) ) 207: { 208: for ($a=2; $a<=$att; $a++) 209: { 210: db(" Test: ".$lrow['testtype']." (".$lrow['testparam'].") X".$a); 211: $NATS->Event("Tester ".$trid." Node ".$row['nodeid']." X".$a." Doing ".$lrow['testtype']."(".$lrow['testparam'].")",10,"Tester","Test"); 212: $result=DoTest($lrow['testtype'],$lrow['testparam'],$row['hostname'],$lrow['timeout']); 213: db(" Result: ".$result); 214: if ($lrow['simpleeval']==1) $lvl=SimpleEval($lrow['testtype'],$result); 215: else $lvl=nats_eval("L".$lrow['localtestid'],$result); 216: db(" Eval: ".$lvl); 217: if ($lvl==0) $a=$att+1; // test passed 218: } 219: } 220: 221: // $lvl is now the last lvl regardless of where it came from 222: 223: if ($lvl>$alertlevel) $alertlevel=$lvl; 224: if ($lvl>0) 225: { 226: if ($lrow['testname']=="") $s=$lrow['testtype']."/".substr($lrow['testparam'],0,5)." "; 227: else $s=$lrow['testname']." "; 228: if ($lvl>1) $s.="failed"; 229: else $s.="warning"; 230: $alerts[$alertc++]=$s; 231: } 232: } else $lvl=-1; 233: 234: // record it 235: if ($lrow['testrecord']==1) 236: { 237: $tid="L".$lrow['localtestid']; 238: $iq="INSERT INTO fnrecord(testid,nodeid,alertlevel,testvalue,recordx) VALUES("; 239: $iq.="\"".$tid."\",\"".$row['nodeid']."\",".$lvl.",".$result.",".time().")"; 240: $NATS->DB->Query($iq); 241: db(" Recording Test"); 242: } 243: 244: // update localtest record 245: $uq="UPDATE fnlocaltest SET lastrunx=".time().",alertlevel=".$lvl." WHERE localtestid=".$lrow['localtestid']; 246: $NATS->DB->Query($uq); 247: 248: 249: } 250: 251: 252: 253: $NATS->Event("Tester ".$trid." Finished Node ".$row['nodeid'],10,"Tester","Node"); 254: 255: 256: db("Highest Alert Level: ".$alertlevel); 257: db("Alert Count : ".$alertc); 258: $als=""; 259: foreach($alerts as $al) $als.=$al.", "; 260: db("Alerts: ".$als); 261: 262: $NATS->SetAlerts($row['nodeid'],$alertlevel,$alerts); 263: 264: db(" "); 265: 266: if ($alertlevel>$highalertlevel) $highalertlevel=$alertlevel; 267: $talertc+=$alertc; 268: 269: } 270: 271: 272: 273: db("Finished Tests... Finishing Off"); 274: db("Summary: Tester ".$trid." Highest Level ".$highalertlevel.", Alerts ".$talertc); 275: $uq="UPDATE fntestrun SET finishx=".time().",routput=\"".ss($dbt)."\" WHERE trid=".$trid; 276: $NATS->DB->Query($uq); 277: 278: 279: $NATS->Event("Tester ".$trid." Highest Level ".$highalertlevel.", Alerts ".$talertc,7,"Tester","Stat"); 280: $NATS->Event("Tester ".$trid." Finished",5,"Tester","Stop"); 281: 282: // in here for now... 283: $NATS->ActionFlush(); 284: 285: $NATS->Stop(); 286: db("NATS Stopped... Finished"); 287: ?> 288: 289: