File: 0.02.66a/server/bin/tester.sh (View as HTML)

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