File: 1.02.3a/server/bin/tester.php (View as HTML)

  1: <?php
  2: /* -------------------------------------------------------------
  3: This file is part of FreeNATS
  4: 
  5: FreeNATS is (C) Copyright 2008 PurplePixie Systems
  6: 
  7: FreeNATS is free software: you can redistribute it and/or modify
  8: it under the terms of the GNU General Public License as published by
  9: the Free Software Foundation, either version 3 of the License, or
 10: (at your option) any later version.
 11: 
 12: FreeNATS is distributed in the hope that it will be useful,
 13: but WITHOUT ANY WARRANTY; without even the implied warranty of
 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15: GNU General Public License for more details.
 16: 
 17: You should have received a copy of the GNU General Public License
 18: along with FreeNATS.  If not, see www.gnu.org/licenses
 19: 
 20: For more information see www.purplepixie.org/freenats
 21: -------------------------------------------------------------- */
 22: 
 23: if ((isset($argc))&&(isset($argv))) // specific node or all nodes
 24: 	{
 25: 	if ($argc>1)
 26: 		{
 27: 		$nfilter=$argv[1];
 28: 		}
 29: 	else $nfilter="";
 30: 	}
 31: 
 32: require("include.php");
 33: 
 34: $dbt="";
 35: 
 36: function db($txt,$nl=true) // debug text
 37: {
 38: global $dbt;
 39: echo $txt;
 40: $dbt.=$txt;
 41: if ($nl) 
 42: 	{
 43: 	echo "\n";
 44: 	$dbt.=" <br>\n";
 45: 	}
 46: }
 47: 
 48: $NATS->Start();
 49: if ($nfilter!="") $st=": Node ".$nfilter;
 50: else $st="";
 51: db("NATS Tester Script Starting".$st);
 52: 
 53: $highalertlevel=-1;
 54: $talertc=0;
 55: 
 56: // check if already running
 57: $still_running=false;
 58: $cq="SELECT trid,startx FROM fntestrun WHERE fnode=\"".ss($nfilter)."\" AND finishx=0 LIMIT 0,1";
 59: $cr=$NATS->DB->Query($cq);
 60: 
 61: if ($runrow=$NATS->DB->Fetch_Array($cr))
 62: 	{ // yes there is a testrun session for this node(s)
 63: 	$timelimit=$NATS->Cfg->Get("test.session.limit",60*60);
 64: 	if ( (!is_numeric($timelimit)) || ($timelimit<0) ) $timelimit=60*60; // bogus config value
 65: 	
 66: 	// n.b. a timelimit of 0 means the session will never expire so...
 67: 	if ( ($timelimit>0) && ((time()-$runrow['startx'])>$timelimit) )
 68: 		{
 69: 		// valid time limit and the difference is more than the limit so close it
 70: 		$uq="UPDATE fntestrun SET finishx=1 WHERE trid=".$runrow['trid'];
 71: 		$NATS->DB->Query($uq);
 72: 		if ($NATS->DB->Affected_Rows()>0) 
 73: 			{
 74: 			$NATS->Event("Tester Already Running: Cleared",3,"Tester","Stale");
 75: 			db("Tester Already Running: Cleared");
 76: 			}
 77: 		else $NATS->Event("Tester Already Running: Failed to Clear",1,"Tester","Stale"); // weirdness... run anyway
 78: 		}
 79: 	else $still_running=true; // either never timesout or newer than timelimit ago
 80: 	}
 81: $NATS->DB->Free($cr);
 82: 
 83: // and if it is then don't continue
 84: if ($still_running)
 85: 	{
 86: 	$NATS->Event("Tester Already Running: Aborted",1,"Tester","Error");
 87: 	db("Tester Already Running: Aborted");
 88: 	$NATS->Stop();
 89: 	exit();
 90: 	}
 91: 
 92: 
 93: $gq="INSERT INTO fntestrun(startx,fnode) VALUES(".time().",\"".ss($nfilter)."\")";
 94: $NATS->DB->Query($gq);
 95: $trid=$NATS->DB->Insert_Id();
 96: db("Test ID: ".$trid." (Started at ".nicedt(time()).")");
 97: $NATS->Event("Tester ".$trid." Started",5,"Tester","Start");
 98: 
 99: db(" ");
100: 
101: // Find node to test - must be enabled, have id if set, and be due to be tested (nextrunx)
102: 
103: $q="SELECT * FROM fnnode WHERE nodeenabled=1";
104: if ($nfilter!="") $q.=" AND nodeid=\"".ss($nfilter)."\"";
105: $q.=" AND nextrunx<=".time();
106: 
107: $r=$NATS->DB->Query($q);
108: 
109: 
110: while ($row=$NATS->DB->Fetch_Array($r))
111: 	{
112: 	$dotests=true;
113: 	$alertlevel=0;
114: 	$alerts=array();
115: 	$alertc=0;
116: 	db("NodeID: ".$row['nodeid']);
117: 	$NATS->Event("Tester ".$trid." Node ".$row['nodeid'],10,"Tester","Node");
118: 
119: 	// Scheduling Test In Here - sets dotests to false and alertlevel to -1 untested
120: 	if ($row['scheduleid']!=0) // has a schedule
121: 		{
122: 		db(" Has Schedule: Yes - Checking");
123: 		$run=run_x_in_schedule(time(),$row['scheduleid']);
124: 		if (!$run)
125: 			{
126: 			db(" In Schedule: No - Skipping Tests");
127: 			$NATS->Event("Tester ".$trid." Skipped by Schedule",5,"Tester","Node");
128: 			$dotests=false;
129: 			$alertlevel=-1;
130: 			}
131: 		else db(" In Schedule: Yes");
132: 		}
133: 	
134: 	$eventdata=array( "nodeid" => $row['nodeid'], "in_schedule" => $dotests );
135: 	$NATS->EventHandler("node_test_start",$eventdata);
136: 	
137: 	
138: 	$ptr=0;
139: 	$pal=0;
140: 	
141: 
142: 	// Update lastrun and nextrun regardless of dotests
143: 	$q="UPDATE fnnode SET lastrunx=".time().",nextrunx=".next_run_x($row['testinterval'])." WHERE nodeid=\"".ss($row['nodeid'])."\"";
144: 	$NATS->DB->Query($q);
145: 
146: 	
147: 	if ($row['pingtest']&&$dotests)
148: 		{
149: 		db(" Ping Test: Yes");
150: 		$NATS->Event("Tester ".$trid." Pinging Node ".$row['nodeid'],10,"Tester","Ping");
151: 		$ptr=PingTest($row['hostname']);
152: 		$NATS->Event("Tester ".$trid." Ping Node ".$row['nodeid']." Returned ".$ptr,10,"Tester","Ping");
153: 		db(" Ping Returned: ".$ptr);
154: 		if ( ($ptr<=0) && ($NATS->Cfg->Get("test.icmp.attempts","2")>1) )
155: 			{
156: 			$att=$NATS->Cfg->Get("test.icmp.attempts","2");
157: 			for ($a=2; $a<=$att; $a++) // starting on second attempt
158: 				{
159: 				// try again...
160: 				test_sleep();
161: 				db(" Trying Ping Again - X".$a);
162: 				$NATS->Event("Tester ".$trid." Ping X".$a." Node ".$row['nodeid'],10,"Tester","Ping");
163: 				$ptr=PingTest($row['hostname']);
164: 				$NATS->Event("Tester ".$trid." Ping Node ".$row['nodeid']." Returned ".$ptr,10,"Tester","Ping");
165: 				db(" Ping Returned: ".$ptr);
166: 				if ($ptr>0) $a=$att+1; // break out of the loop
167: 				}
168: 			}
169: 			
170: 		if ($ptr<=0) 
171: 			{
172: 			$alertlevel=2;
173: 			db(" Ping Test: Failed");
174: 			$alerts[$alertc++]="ping failed";
175: 			$pal=2;
176: 			}
177: 		else db(" Ping Test: Passed");
178: 		
179: 		// pingtest output bodge
180: 		// is there a test entry for ICMP
181: 		$fq="SELECT localtestid FROM fnlocaltest WHERE nodeid=\"".$row['nodeid']."\" AND testtype=\"ICMP\"";
182: 		$fr=$NATS->DB->Query($fq);
183: 		$ltid_icmp="";
184: 		if ($irow=$NATS->DB->Fetch_Array($fr))
185: 			{ // exists
186: 			$uq="UPDATE fnlocaltest SET alertlevel=".$pal.",lastrunx=".time().",lastvalue=".$ptr.",testrecord=1,testinterval=0 WHERE localtestid=".$irow['localtestid'];
187: 			$ltid_icmp=$irow['localtestid'];
188: 			//echo $uq;
189: 			$NATS->DB->Query($uq);
190: 			}
191: 		else
192: 			{ // doesn't exist
193: 			$uq="INSERT INTO fnlocaltest(nodeid,testrecord,testinterval,testtype,alertlevel,lastrunx,lastvalue) VALUES(\"".$row['nodeid']."\",1,0,\"ICMP\",".$pal.",".time().",".$ptr.")";
194: 			//echo $uq;
195: 			$NATS->DB->Query($uq);
196: 			$ltid_icmp=$NATS->DB->Insert_Id();
197: 			}
198: 		$NATS->DB->Free($fr);
199: 		
200: 		// record the ICMP bodge-test here
201: 		$rq="INSERT INTO fnrecord(testid,recordx,testvalue,alertlevel,nodeid) VALUES(\"L".$ltid_icmp."\",".time().",".$ptr.",".$pal.",\"".$row['nodeid']."\")";
202: 		$NATS->DB->Query($rq);
203: 		//echo $rq." ".$NATS->DB->Affected_Rows()."\n";
204: 		
205: 		}
206: 	else
207: 		{ // further ICMP bodge - update to -1 or do nothing if the test doesn't exist
208: 		$uq="UPDATE fnlocaltest SET alertlevel=-1,lastrunx=".time()." WHERE nodeid=\"".$row['nodeid']."\" AND testtype=\"ICMP\"";
209: 		$NATS->DB->Query($uq);
210: 		}
211: 
212: 	if ($dotests&&($row['pingfatal'])&&($ptr<=0))
213: 		{
214: 		db(" Ping Fatal: Yes - Not Continuing");
215: 		$NATS->Event("Tester ".$trid." Ping Fatal for Node ".$row['nodeid'],10,"Tester","Ping");
216: 		$dotests=false;
217: 		}
218: 
219: 	 	// do the tests - only actually exec if dotests true
220: 
221: 	 	$first_test=true;
222: 	 	
223: 		db("Doing Local Tests");
224: 		$NATS->Event("Tester ".$trid." Testing Node ".$row['nodeid'],10,"Tester","Test");
225: 		$q="SELECT * FROM fnlocaltest WHERE nodeid=\"".$row['nodeid']."\" AND testtype!=\"ICMP\" AND testenabled=1 ORDER BY localtestid ASC";
226: 		$res=$NATS->DB->Query($q);
227: 		while ($lrow=$NATS->DB->Fetch_Array($res))
228: 			{
229: 			if ($lrow['nextrunx']<=time()) $testdue=true;
230: 			else $testdue=false;
231: 				
232: 			if ($first_test)
233: 				{
234: 				$first_test=false;
235: 				if ($row['pingtest']==1) test_sleep(); // sleep if has done a ping
236: 				}
237: 			else test_sleep();
238: 			
239: 			if ($testdue)
240: 				{
241: 			
242: 				$eventdata=array("nodeid"=>$row['nodeid'],"testid"=>"L".$lrow['testparam'],"testtype"=>$lrow['testtype']);
243: 				$NATS->EventHandler("localtest_start",$eventdata);
244: 					
245: 				db(" Test: ".$lrow['testtype']." (".$lrow['testparam'].")");
246: 				
247: 				// Build parameter array
248: 				$params=array();
249: 				$params[0]=$lrow['testparam']; // pass standard param in as 0
250: 				for ($a=1; $a<10; $a++)
251: 					{
252: 					$parstr="testparam".$a;
253: 					$params[$a]=$lrow[$parstr];
254: 					}
255: 				
256: 				if ($dotests)
257: 					{
258: 					$NATS->Event("Tester ".$trid." Node ".$row['nodeid']." Doing ".$lrow['testtype']."(".$lrow['testparam'].")",10,"Tester","Test");
259: 					$result=DoTest($lrow['testtype'],$lrow['testparam'],$row['hostname'],$lrow['timeout'],$params,$row['nodeid']);
260: 					$NATS->Event("Tester ".$trid." Node ".$row['nodeid']." Result ".$result." from ".$lrow['testtype']."(".$lrow['testparam'].")",10,"Tester","Test");
261: 					db(" Result: ".$result);
262: 					}
263: 				else $result=0;
264: 				
265: 				if ($dotests)
266: 				{
267: 				// evaluation
268: 				if ($lrow['simpleeval']==1) $lvl=SimpleEval($lrow['testtype'],$result);
269: 				else $lvl=nats_eval("L".$lrow['localtestid'],$result);
270: 				db(" Eval: ".$lvl);
271: 				
272: 				// put in the custom retries based on attempts here - we KNOW dotests is on so don't need to worry about untested status
273: 				$att=$lrow['attempts'];
274: 				if ( ($lvl!=0) && (is_numeric($att)) && ($att>1) )
275: 					{
276: 					for ($a=2; $a<=$att; $a++)
277: 						{
278: 						test_sleep();
279: 						db(" Test: ".$lrow['testtype']." (".$lrow['testparam'].") X".$a);
280: 						$NATS->Event("Tester ".$trid." Node ".$row['nodeid']." X".$a." Doing ".$lrow['testtype']."(".$lrow['testparam'].")",10,"Tester","Test");
281: 						$result=DoTest($lrow['testtype'],$lrow['testparam'],$row['hostname'],$lrow['timeout'],$params,$row['nodeid']);
282: 						db(" Result: ".$result);
283: 						if ($lrow['simpleeval']==1) $lvl=SimpleEval($lrow['testtype'],$result);
284: 						else $lvl=nats_eval("L".$lrow['localtestid'],$result);
285: 						db(" Eval: ".$lvl);
286: 						if ($lvl==0) $a=$att+1; // test passed
287: 						}
288: 					}
289: 				
290: 				// $lvl is now the last lvl regardless of where it came from
291: 						
292: 				if ($lvl>$alertlevel) $alertlevel=$lvl;
293: 				if ($lvl>0)
294: 					{
295: 					if ($lrow['testname']=="") $s=$lrow['testtype']."/".substr($lrow['testparam'],0,5)." ";
296: 					else $s=$lrow['testname']." ";
297: 					if ($lvl>1) $s.="failed";
298: 					else $s.="warning";
299: 					$alerts[$alertc++]=$s;
300: 					}
301: 				} else $lvl=-1;
302: 					
303: 				// record it
304: 				if ($lrow['testrecord']==1)
305: 					{
306: 					$tid="L".$lrow['localtestid'];
307: 					$iq="INSERT INTO fnrecord(testid,nodeid,alertlevel,testvalue,recordx) VALUES(";
308: 					$iq.="\"".$tid."\",\"".$row['nodeid']."\",".$lvl.",".$result.",".time().")";
309: 					$NATS->DB->Query($iq);
310: 					db(" Recording Test");
311: 					}
312: 				if ((!isset($result))||(!is_numeric($result))) $result=0; // safety net
313: 					
314: 				// update localtest record
315: 				$uq="UPDATE fnlocaltest SET lastrunx=".time().",nextrunx=".next_run_x($lrow['testinterval']).",alertlevel=".$lvl.",lastvalue=".$result." WHERE localtestid=".$lrow['localtestid'];
316: 				$NATS->DB->Query($uq);
317: 				
318: 				$eventdata=array("nodeid"=>$row['nodeid'],"testid"=>"L".$lrow['testparam'],"testtype"=>$lrow['testtype'],"alertlevel"=>$lvl);
319: 				$NATS->EventHandler("localtest_finish",$eventdata);
320: 				}
321: 				
322: 			else // test not due so take pre-existing level for it
323: 				{
324: 				$lvl=$lrow['alertlevel'];
325: 				if (($lvl>0)&&($lvl>$alertlevel)) $alertlevel=$lvl;
326: 				}
327: 			
328: 			
329: 			}
330: 			
331: 	// Node-side testy magic
332: 	db("Nodeside Testing");
333: 	$freshdata=false;
334: 	if ( $dotests && ($row['nsenabled']==1) && ($row['nspullenabled']==1) ) // should be doing a pull
335: 		{
336: 		$pullalert=$row['nspullalert']; // what happened the last time we tried
337: 		
338: 		if ($row['nsnextx']<=time()) // the time is right
339: 			{
340: 			db(" Pulling Data");
341: 			$pull_result=$NATS->Nodeside_Pull($row['nodeid']);
342: 			
343: 			if ($pull_result===false) // Pull Failed
344: 				{
345: 				db(" Pull Failed");
346: 				$pullalert=1; // alert
347: 				$alerts[$alertc++]="pull failed";
348: 				$alertlevel=2;
349: 				}
350: 			else // Pull Worked
351: 				{
352: 				$freshdata=true;
353: 				$pullalert=0; // ok
354: 				db(" Pull Succeeded");
355: 				}
356: 				
357: 				
358: 			db(" Updating Pull nslast/nextx and nspullalert");
359: 			$uq="UPDATE fnnode SET nsnextx=".next_run_x($row['nsinterval']).",nspullalert=".$pullalert.",nslastx=".time()." WHERE nodeid=\"".$row['nodeid']."\"";
360: 			$NATS->DB->Query($uq);
361: 			if ($NATS->DB->Affected_Rows()<=0) db(" - Failed");
362: 			}
363: 		/*
364: 		// Process for alerts in here - whether pulled or not!
365: 		$tq="SELECT testtype,testname,alertlevel FROM fnnstest WHERE nodeid=\"".$row['nodeid']."\" AND testenabled=1 AND testalerts=1 AND alertlevel>0";
366: 		$tr=$NATS->DB->Query($tq);
367: 		while ($trow=$NATS->DB->Fetch_Array($tr))
368: 			{
369: 			if ($trow['testname']=="") $tname=$trow['testtype'];
370: 			else $tname=$trow['testname'];
371: 			if ($freshdata) $alerts[$alertc++]=$tname." ".oText($trow['alertlevel']); // only record text to log if fresh
372: 			if ($trow['alertlevel']>$alertlevel) $alertlevel=$trow['alertlevel'];
373: 			}
374: 		*/
375: 			
376: 		// and finally again use pullalert - this is either the new value if a pull was attempted or just remains the same as the old one
377: 		// if pull not scheduled yet
378: 		if ($pullalert>0) $alertlevel=2; // so mark a failure
379: 
380: 		}
381: 		
382: 	if ( ($dotests && ($row['nsenabled']==1) && ($row['nspullenabled']==1)) ||		// pull and tests are on
383: 		(($row['nsenabled']==1)&&($row['nspushenabled']==1)) )	// or pushed
384: 		{
385: 		if ($row['nsfreshpush']==1)
386: 			{
387: 			$freshdata=true;
388: 			$uq="UPDATE fnnode SET nsfreshpush=0 WHERE nodeid=\"".$row['nodeid']."\"";
389: 			$NATS->DB->Query($uq);
390: 			}
391: 		// Process for alerts in here - whether pulled or not!
392: 		$tq="SELECT testtype,testname,alertlevel FROM fnnstest WHERE nodeid=\"".$row['nodeid']."\" AND testenabled=1 AND testalerts=1 AND alertlevel>0";
393: 		$tr=$NATS->DB->Query($tq);
394: 		while ($trow=$NATS->DB->Fetch_Array($tr))
395: 			{
396: 			if ($trow['testname']=="") $tname=$trow['testtype'];
397: 			else $tname=$trow['testname'];
398: 			if ($freshdata) $alerts[$alertc++]=$tname." ".oText($trow['alertlevel']); // only record text to log if fresh
399: 			if ($trow['alertlevel']>$alertlevel) $alertlevel=$trow['alertlevel'];
400: 			}
401: 		}
402: 			
403: 	$NATS->Event("Tester ".$trid." Finished Node ".$row['nodeid'],10,"Tester","Node");
404: 
405: 	$eventdata=array( "nodeid" => $row['nodeid'], "alertlevel" => $alertlevel );
406: 	$NATS->EventHandler("node_test_finish",$eventdata);
407: 
408: 	db("Highest Alert Level: ".$alertlevel);
409: 	db("Alert Count        : ".$alertc);
410: 	$als="";
411: 	foreach($alerts as $al) $als.=$al.", ";
412: 	db("Alerts: ".$als);
413: 
414: 	$NATS->SetAlerts($row['nodeid'],$alertlevel,$alerts);
415: 	
416: 	// This is where child/slave nodes would spawn
417: 	
418: 	
419: 	// End of the node... carry forward highest level
420: 
421: 	db(" ");
422: 	
423: 	if ($alertlevel>$highalertlevel) $highalertlevel=$alertlevel;
424: 	$talertc+=$alertc;
425: 	
426: 	}
427: 
428: 
429: 
430: db("Finished Tests... Finishing Off");
431: db("Summary: Tester ".$trid." Highest Level ".$highalertlevel.", Alerts ".$talertc);
432: if ($highalertlevel>-1)
433: 	{
434: 	$uq="UPDATE fntestrun SET finishx=".time().",routput=\"".ss($dbt)."\" WHERE trid=".$trid;
435: 	$NATS->DB->Query($uq);
436: 	}
437: else
438: 	{
439: 	$uq="DELETE FROM fntestrun WHERE trid=".$trid;
440: 	$NATS->DB->Query($uq);
441: 	}
442: 
443: 
444: $NATS->Event("Tester ".$trid." Highest Level ".$highalertlevel.", Alerts ".$talertc,7,"Tester","Stat");
445: $NATS->Event("Tester ".$trid." Finished",5,"Tester","Stop");
446: 
447: // in here for now...
448: $NATS->ActionFlush();
449: 
450: $NATS->Stop();
451: db("NATS Stopped... Finished");
452: ?>
453: 
454: