File: 0.02.22a/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 Foobar. 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: require("include.php"); 33: $dbt=""; 34: function db($txt,$nl=true) 35: { 36: global $dbt; 37: echo $txt; 38: $dbt.=$txt; 39: if ($nl) 40: { 41: echo "\n"; 42: $dbt.="
\n";
43: } 44: } 45: $NATS->Start(); 46: if ($nfilter!="") $st=": Node ".$nfilter; 47: else $st=""; 48: db("NATS Tester Script Starting".$st); 49: 50: $highalertlevel=-1; 51: $talertc=0; 52: 53: // check if already running 54: $cq="SELECT startx FROM fntestrun WHERE fnode=\"".ss($nfilter)."\" AND finishx=0"; 55: $cr=$NATS->DB->Query($cq); 56: if ($NATS->DB->Num_Rows($cr)>0) 57: { 58: $NATS->Event("Tester Already Running: Aborted",1,"Tester","Error"); 59: db("Tester Already Running: Aborted"); 60: $NATS->Stop(); 61: exit(); 62: } 63: 64: $gq="INSERT INTO fntestrun(startx,fnode) VALUES(".time().",\"".ss($nfilter)."\")"; 65: $NATS->DB->Query($gq); 66: $trid=$NATS->DB->Insert_Id(); 67: db("Test ID: ".$trid." (Started at ".nicedt(time()).")"); 68: $NATS->Event("Tester ".$trid." Started",5,"Tester","Start"); 69: 70: db(" "); 71: 72: if ($nfilter=="") $q="SELECT * FROM fnnode WHERE nodeenabled=1"; 73: else 74: { 75: $q="SELECT * FROM fnnode WHERE nodeid=\"".ss($nfilter)."\" AND nodeenabled=1"; 76: } 77: $r=$NATS->DB->Query($q); 78: 79: 80: while ($row=$NATS->DB->Fetch_Array($r)) 81: { 82: $alertlevel=0; 83: $alerts=array(); 84: $alertc=0; 85: db("NodeID: ".$row['nodeid']); 86: $NATS->Event("Tester ".$trid." Node ".$row['nodeid'],10,"Tester","Node"); 87: 88: $ptr=0; 89: $pal=0; 90: if ($row['pingtest']) 91: { 92: db(" Ping Test: Yes"); 93: $NATS->Event("Tester ".$trid." Pinging Node ".$row['nodeid'],10,"Tester","Ping"); 94: $ptr=PingTest($row['hostname']); 95: $NATS->Event("Tester ".$trid." Ping Node ".$row['nodeid']." Returned ".$ptr,10,"Tester","Ping"); 96: db(" Ping Returned: ".$ptr); 97: if ( ($ptr<=0) && ($NATS->Cfg->Get("test.icmp.trytwice","1")=="1") ) 98: { 99: // try again... 100: db(" Trying Ping Again"); 101: $NATS->Event("Tester ".$trid." Ping X2 Node ".$row['nodeid'],10,"Tester","Ping"); 102: $ptr=PingTest($row['hostname']); 103: $NATS->Event("Tester ".$trid." Ping Node ".$row['nodeid']." Returned ".$ptr,10,"Tester","Ping"); 104: db(" Ping Returned: ".$ptr); 105: } 106: 107: if ($ptr<=0) 108: { 109: $alertlevel=2; 110: db(" Ping Test: Failed"); 111: $alerts[$alertc++]="ping failed"; 112: $pal=2; 113: } 114: else db(" Ping Test: Passed"); 115: 116: // pingtest output bodge 117: // is there a test entry for ICMP 118: $fq="SELECT localtestid FROM fnlocaltest WHERE nodeid=\"".$row['nodeid']."\" AND testtype=\"ICMP\""; 119: $fr=$NATS->DB->Query($fq); 120: $ltid_icmp=""; 121: if ($irow=$NATS->DB->Fetch_Array($fr)) 122: { // exists 123: $uq="UPDATE fnlocaltest SET alertlevel=".$pal.",lastrunx=".time()." WHERE localtestid=".$irow['localtestid']; 124: $ltid_icmp=$irow['localtestid']; 125: //echo $uq; 126: $NATS->DB->Query($uq); 127: } 128: else 129: { // doesn't exist 130: $uq="INSERT INTO fnlocaltest(nodeid,testtype,alertlevel,lastrunx) VALUES(\"".$row['nodeid']."\",\"ICMP\",".$pal.",".time().")"; 131: //echo $uq; 132: $NATS->DB->Query($uq); 133: $ltid_icmp=$NATS->DB->Insert_Id(); 134: } 135: $NATS->DB->Free($fr); 136: 137: // record the ICMP bodge-test here 138: $rq="INSERT INTO fnrecord(testid,recordx,testvalue,alertlevel,nodeid) VALUES(\"L".$ltid_icmp."\",".time().",".$ptr.",".$pal.",\"".$row['nodeid']."\")"; 139: $NATS->DB->Query($rq); 140: //echo $rq." ".$NATS->DB->Affected_Rows()."\n"; 141: 142: } 143: else 144: { // further ICMP bodge - update to -1 or do nothing if the test doesn't exist 145: $uq="UPDATE fnlocaltest SET alertlevel=-1,lastrunx=".time()." WHERE nodeid=\"".$row['nodeid']."\" AND testtype=\"ICMP\""; 146: $NATS->DB->Query($uq); 147: } 148: 149: if (($row['pingfatal'])&&($ptr<=0)) 150: { 151: db(" Ping Fatal: Yes - Not Continuing"); 152: $NATS->Event("Tester ".$trid." Ping Fatal for Node ".$row['nodeid'],10,"Tester","Ping"); 153: } 154: else 155: { // do the tests 156: 157: db("Doing Local Tests"); 158: $NATS->Event("Tester ".$trid." Testing Node ".$row['nodeid'],10,"Tester","Test"); 159: $q="SELECT * FROM fnlocaltest WHERE nodeid=\"".$row['nodeid']."\" AND testtype!=\"ICMP\""; 160: $res=$NATS->DB->Query($q); 161: while ($lrow=$NATS->DB->Fetch_Array($res)) 162: { 163: db(" Test: ".$lrow['testtype']." (".$lrow['testparam'].")"); 164: $NATS->Event("Tester ".$trid." Node ".$row['nodeid']." Doing ".$lrow['testtype']."(".$lrow['testparam'].")",10,"Tester","Test"); 165: $result=DoTest($lrow['testtype'],$lrow['testparam'],$row['hostname']); 166: $NATS->Event("Tester ".$trid." Node ".$row['nodeid']." Result ".$result." from ".$lrow['testtype']."(".$lrow['testparam'].")",10,"Tester","Test"); 167: db(" Result: ".$result); 168: 169: // evaluation 170: if ($lrow['simpleeval']==1) $lvl=SimpleEval($lrow['testtype'],$result); 171: else $lvl=nats_eval("L".$lrow['localtestid'],$result); 172: 173: db(" Eval: ".$lvl); 174: if ($lvl>$alertlevel) $alertlevel=$lvl; 175: if ($lvl>0) 176: { 177: $s=$lrow['testtype']."/".substr($lrow['testparam'],0,5)." "; 178: if ($lvl>1) $s.="failed"; 179: else $s.="warning"; 180: $alerts[$alertc++]=$s; 181: } 182: 183: // record it 184: if ($lrow['testrecord']==1) 185: { 186: $tid="L".$lrow['localtestid']; 187: $iq="INSERT INTO fnrecord(testid,nodeid,alertlevel,testvalue,recordx) VALUES("; 188: $iq.="\"".$tid."\",\"".$row['nodeid']."\",".$lvl.",".$result.",".time().")"; 189: $NATS->DB->Query($iq); 190: db(" Recording Test"); 191: } 192: 193: // update localtest record 194: $uq="UPDATE fnlocaltest SET lastrunx=".time().",alertlevel=".$lvl." WHERE localtestid=".$lrow['localtestid']; 195: $NATS->DB->Query($uq); 196: } 197: $NATS->Event("Tester ".$trid." Finished Node ".$row['nodeid'],10,"Tester","Node"); 198: } 199: 200: db("Highest Alert Level: ".$alertlevel); 201: db("Alert Count : ".$alertc); 202: $als=""; 203: foreach($alerts as $al) $als.=$al.", "; 204: db("Alerts: ".$als); 205: 206: $NATS->SetAlerts($row['nodeid'],$alertlevel,$alerts); 207: 208: db(" "); 209: 210: if ($alertlevel>$highalertlevel) $highalertlevel=$alertlevel; 211: $talertc+=$alertc; 212: 213: } 214: 215: 216: 217: db("Finished Tests... Finishing Off"); 218: db("Summary: Tester ".$trid." Highest Level ".$highalertlevel.", Alerts ".$talertc); 219: $uq="UPDATE fntestrun SET finishx=".time().",routput=\"".ss($dbt)."\" WHERE trid=".$trid; 220: $NATS->DB->Query($uq); 221: 222: 223: $NATS->Event("Tester ".$trid." Highest Level ".$highalertlevel.", Alerts ".$talertc,7,"Tester","Stat"); 224: $NATS->Event("Tester ".$trid." Finished",5,"Tester","Stop"); 225: 226: // in here for now... 227: $NATS->ActionFlush(); 228: 229: $NATS->Stop(); 230: db("NATS Stopped... Finished"); 231: ?> 232: 233: