File: 0.04.01a/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\" AND testenabled=1"; 187: $res=$NATS->DB->Query($q); 188: while ($lrow=$NATS->DB->Fetch_Array($res)) 189: { 190: db(" Test: ".$lrow['testtype']." (".$lrow['testparam'].")"); 191: 192: // Build parameter array 193: $params=array(); 194: for ($a=1; $a<10; $a++) 195: { 196: $parstr="testparam".$a; 197: $params[$a]=$lrow[$parstr]; 198: } 199: 200: $NATS->Event("Tester ".$trid." Node ".$row['nodeid']." Doing ".$lrow['testtype']."(".$lrow['testparam'].")",10,"Tester","Test"); 201: if ($dotests) $result=DoTest($lrow['testtype'],$lrow['testparam'],$row['hostname'],$lrow['timeout'],$params); 202: else $result=0; 203: $NATS->Event("Tester ".$trid." Node ".$row['nodeid']." Result ".$result." from ".$lrow['testtype']."(".$lrow['testparam'].")",10,"Tester","Test"); 204: db(" Result: ".$result); 205: 206: if ($dotests) 207: { 208: // evaluation 209: if ($lrow['simpleeval']==1) $lvl=SimpleEval($lrow['testtype'],$result); 210: else $lvl=nats_eval("L".$lrow['localtestid'],$result); 211: db(" Eval: ".$lvl); 212: 213: // put in the custom retries based on attempts here - we KNOW dotests is on so don't need to worry about untested status 214: $att=$lrow['attempts']; 215: if ( ($lvl!=0) && (is_numeric($att)) && ($att>1) ) 216: { 217: for ($a=2; $a<=$att; $a++) 218: { 219: db(" Test: ".$lrow['testtype']." (".$lrow['testparam'].") X".$a); 220: $NATS->Event("Tester ".$trid." Node ".$row['nodeid']." X".$a." Doing ".$lrow['testtype']."(".$lrow['testparam'].")",10,"Tester","Test"); 221: $result=DoTest($lrow['testtype'],$lrow['testparam'],$row['hostname'],$lrow['timeout']); 222: db(" Result: ".$result); 223: if ($lrow['simpleeval']==1) $lvl=SimpleEval($lrow['testtype'],$result); 224: else $lvl=nats_eval("L".$lrow['localtestid'],$result); 225: db(" Eval: ".$lvl); 226: if ($lvl==0) $a=$att+1; // test passed 227: } 228: } 229: 230: // $lvl is now the last lvl regardless of where it came from 231: 232: if ($lvl>$alertlevel) $alertlevel=$lvl; 233: if ($lvl>0) 234: { 235: if ($lrow['testname']=="") $s=$lrow['testtype']."/".substr($lrow['testparam'],0,5)." "; 236: else $s=$lrow['testname']." "; 237: if ($lvl>1) $s.="failed"; 238: else $s.="warning"; 239: $alerts[$alertc++]=$s; 240: } 241: } else $lvl=-1; 242: 243: // record it 244: if ($lrow['testrecord']==1) 245: { 246: $tid="L".$lrow['localtestid']; 247: $iq="INSERT INTO fnrecord(testid,nodeid,alertlevel,testvalue,recordx) VALUES("; 248: $iq.="\"".$tid."\",\"".$row['nodeid']."\",".$lvl.",".$result.",".time().")"; 249: $NATS->DB->Query($iq); 250: db(" Recording Test"); 251: } 252: 253: // update localtest record 254: $uq="UPDATE fnlocaltest SET lastrunx=".time().",alertlevel=".$lvl." WHERE localtestid=".$lrow['localtestid']; 255: $NATS->DB->Query($uq); 256: 257: 258: } 259: 260: 261: 262: $NATS->Event("Tester ".$trid." Finished Node ".$row['nodeid'],10,"Tester","Node"); 263: 264: 265: db("Highest Alert Level: ".$alertlevel); 266: db("Alert Count : ".$alertc); 267: $als=""; 268: foreach($alerts as $al) $als.=$al.", "; 269: db("Alerts: ".$als); 270: 271: $NATS->SetAlerts($row['nodeid'],$alertlevel,$alerts); 272: 273: db(" "); 274: 275: if ($alertlevel>$highalertlevel) $highalertlevel=$alertlevel; 276: $talertc+=$alertc; 277: 278: } 279: 280: 281: 282: db("Finished Tests... Finishing Off"); 283: db("Summary: Tester ".$trid." Highest Level ".$highalertlevel.", Alerts ".$talertc); 284: $uq="UPDATE fntestrun SET finishx=".time().",routput=\"".ss($dbt)."\" WHERE trid=".$trid; 285: $NATS->DB->Query($uq); 286: 287: 288: $NATS->Event("Tester ".$trid." Highest Level ".$highalertlevel.", Alerts ".$talertc,7,"Tester","Stat"); 289: $NATS->Event("Tester ".$trid." Finished",5,"Tester","Stop"); 290: 291: // in here for now... 292: $NATS->ActionFlush(); 293: 294: $NATS->Stop(); 295: db("NATS Stopped... Finished"); 296: ?> 297: 298: