File: 1.00.0a/server/base/freenats.inc.php (View as HTML)

  1: <?php // freenats.inc.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: class TFreeNATS
 24: {
 25: var $init=false;
 26: var $DB;
 27: var $Cfg;
 28: var $Tests;
 29: var $Version="1.00.0";
 30: var $Release="a";
 31: 
 32: function TFreeNATS()
 33: 	{
 34: 	$this->Tests=new TNATS_Tests(); // need this available during the include phase pre-start
 35: 	}
 36: 
 37: function Start()
 38: 	{
 39: 	$this->DB=new TNATS_DB();
 40: 	$this->Cfg=new TNATS_Cfg();
 41: 	$this->DB->Connect();
 42: 	$this->Cfg->Load($this->DB);
 43: 	$this->init=true;
 44: 	}
 45: 	
 46: function Stop()
 47: 	{
 48: 	$this->DB->Disconnect();
 49: 	$this->init=false;
 50: 	}	
 51: 	
 52: function Event($logevent,$loglevel=1,$modid="NONE",$catid="NONE")
 53: 	{
 54: 	global $NATS_Session;
 55: 	if ((isset($NATS_Session))&&($NATS_Session->auth)) $username=$NATS_Session->username;
 56: 	else $username="";
 57: 	$l=$this->Cfg->Get("log.level");
 58: 	//echo "** $l **\n";
 59: 	if ( $l=="" ) $l=10; // debug logging if no variable
 60: 	if ( $l < $loglevel ) return false;
 61: 	if (strlen($logevent)>249) $logevent=substr($logevent,0,245)."...";
 62: 	$q="INSERT INTO fnlog(postedx,modid,catid,loglevel,logevent,username) VALUES(".time().",";
 63: 	$q.="\"".ss($modid)."\",\"".ss($catid)."\",".ss($loglevel).",\"".ss($logevent)."\",\"".ss($username)."\")";
 64: 	//echo $q;
 65: 	$this->DB->Query($q);
 66: 	}
 67: 
 68: function AlertAction($nodeid,$alertlevel,$change,$alerttext)
 69: 	{
 70: 	echo "Called for node: ".$nodeid."\n";
 71: 	if ($change==0) return false;
 72: 	//echo $nodeid.":".$alertlevel.":".$change.":".$alerttext."\n";
 73: 	// get all the alertactions for this node id
 74: 	$q="SELECT aaid FROM fnnalink WHERE nodeid=\"".ss($nodeid)."\"";
 75: 	$r=$this->DB->Query($q);
 76: 	while ($arow=$this->DB->Fetch_Array($r))
 77: 		{
 78: 		// get details for this alert action
 79: 		$aq="SELECT * FROM fnalertaction WHERE aaid=".$arow['aaid']." LIMIT 0,1";
 80: 		//echo $aq."\n";
 81: 		$ar=$this->DB->Query($aq);
 82: 		$aa=$this->DB->Fetch_Array($ar);
 83: 		$this->DB->Free($ar);
 84: 		//echo $aa['atype']."FISH\n";
 85: 		
 86: 		// UGGGGGGGG continue!!
 87: 		if ( ($aa['atype']=="") || ($aa['atype']=="Disabled") ) continue;
 88: 		if ( ($aa['awarnings']==0) && ($alertlevel<2) ) continue;
 89: 		if ( ($aa['adecrease']==0) && ($change<1) ) continue;
 90: 		
 91: 		// made it this far
 92: 		if ($aa['mdata']!="") $ndata=$aa['mdata']."\n".$nodeid.": ".$alerttext;
 93: 		else $ndata=$nodeid.": ".$alerttext;
 94: 		$uq="UPDATE fnalertaction SET mdata=\"".ss($ndata)."\" WHERE aaid=".$arow['aaid'];
 95: 		//echo $uq."\n";
 96: 		$this->DB->Query($uq);
 97: 		}
 98: 	}
 99: 	
100: function ActionFlush()
101: 	{
102: 	global $allowed; // allowed chars from screen in YA BODGE
103: 	$q="SELECT * FROM fnalertaction WHERE mdata!=\"\"";
104: 	$r=$this->DB->Query($q);
105: 	while ($row=$this->DB->Fetch_Array($r))
106: 		{
107: 			
108: 			$doalert=true;
109: 			
110: 		// clear mdata right at the start to get around duplicate emails whilst processing
111: 			$q="UPDATE fnalertaction SET mdata=\"\" WHERE aaid=".$row['aaid'];
112: 			$this->DB->Query($q);
113: 			
114: 			if ($this->DB->Affected_Rows()<=0) // already flushed or failed to flush
115: 				{
116: 				$doalert=false;
117: 				$this->Event("Alert Action Already Flushed - Skipping",8,"Flush","Action");
118: 				}
119: 			
120: 		// alert counter
121: 		$td=date("Ymd");
122: 		if ($td!=$row['ctrdate']) // new day or no flush record
123: 			{
124: 			$q="UPDATE fnalertaction SET ctrdate=\"".$td."\",ctrtoday=1 WHERE aaid=".$row['aaid'];
125: 			$this->DB->Query($q);
126: 			}
127: 		else
128: 			{
129: 			
130: 				if ( ($row['ctrlimit']==0) || ($row['ctrlimit']>$row['ctrtoday']) ) // no limit or below
131: 					{
132: 					$q="UPDATE fnalertaction SET ctrtoday=ctrtoday+1 WHERE aaid=".$row['aaid'];
133: 					$this->DB->Query($q);
134: 					}
135: 				else // at or over limit
136: 					{
137: 					$this->Event("Alert Action Limit Reached - Skipping",2,"Flush","Action");
138: 					$doalert=false;
139: 					}
140: 			
141: 			}
142: 			
143: 			
144: 		if ($row['atype']=="email")
145: 			{
146: 			if ($row['esubject']==0) $sub="";
147: 			else if ($row['esubject']==1) $sub="FreeNATS Alert";
148: 			else $sub="** FreeNATS Alert **";
149: 			$body="";
150: 			if ($row['etype']==0) $body=$row['mdata'];
151: 			else $body="FreeNATS Alert,\r\n".$row['mdata']."\r\n--FreeNATS @ ".nicedt(time());
152: 			//$tolist=preg_split("[\n\r]",$row['etolist']);
153: 			$tolist=array();
154: 			$f=0;
155: 			$tolist[0]="";
156: 			for ($a=0; $a<strlen($row['etolist']); $a++)
157: 				{
158: 				$chr=$row['etolist'][$a];
159: 				//echo $chr;
160: 				if (strpos($allowed,$chr)===false) // special char
161: 					{
162: 					$f++;
163: 					$tolist[$f]="";
164: 					}
165: 				else
166: 					{
167: 					$tolist[$f].=$chr;
168: 					}
169: 				}
170: 			
171: 			foreach($tolist as $toaddr)
172: 				{
173: 				$toaddr=nices($toaddr);
174: 				if ($toaddr!="")
175: 					{
176: 					//echo $row['efrom'].":".$toaddr.":".$sub.":".$body."\n\n";
177: 					//$body="fish";
178: 					//mail($toaddr,$sub,$body,"From: ".$row['efrom']."\r\n","-f".$row['efrom']);
179: 					$header="From: ".$row['efrom']."\r\n";
180: 					//$header="";
181: 					//$exhead="-f".$row['efrom'];
182: 					//db("Sending Email: ".$toaddr);
183: 					if ($doalert)
184: 						{
185: 						mail($toaddr,$sub,$body,$header);
186: 						$this->Event("Sent alert email to ".$toaddr,4,"Flush","Email");
187: 						}		
188: 					}
189: 				}
190: 				
191: 				
192: 				
193: 			}
194: 		else if ($row['atype']=="url")
195: 			{
196: 			// url send
197: 			if ($row['etype']==0) $body=$row['mdata'];
198: 			else $body="FreeNATS Alert,\r\n".$row['mdata']."\r\n--FreeNATS @ ".nicedt(time());
199: 			
200: 			$body=urlencode($body);
201: 			$tolist=array();
202: 			$f=0;
203: 			$tolist[0]="";
204: 			for ($a=0; $a<strlen($row['etolist']); $a++)
205: 				{
206: 				$chr=$row['etolist'][$a];
207: 				//echo $chr;
208: 				if (strpos($allowed,$chr)===false) // special char
209: 					{
210: 					$f++;
211: 					$tolist[$f]="";
212: 					}
213: 				else
214: 					{
215: 					$tolist[$f].=$chr;
216: 					}
217: 				}
218: 			
219: 			foreach($tolist as $tourl)
220: 				{
221: 				if ($doalert)
222: 					{
223: 					$url=$tourl.$body;
224: 					$fp=@fopen($url,"r");
225: 					if ($fp>0) fclose($fp);
226: 					else $this->Event("URL Alert Failed ".$url,1,"Flush","URL");
227: 					$this->Event("URL Alert ".$url,4,"Flush","URL");
228: 					}
229: 				}
230: 			
231: 			
232: 			}
233: 			
234: 		}
235: 	}	
236: 
237: function GetAlerts()
238: 	{
239: 	$q="SELECT nodeid,alertlevel FROM fnalert WHERE closedx=0";
240: 	$r=$this->DB->Query($q);
241: 	$c=0;
242: 	$al=array();
243: 	while ($row=$this->DB->Fetch_Array($r))
244: 		{
245: 		$al[$c]['nodeid']=$row['nodeid'];
246: 		$al[$c]['alertlevel']=$row['alertlevel'];
247: 		$c++;
248: 		}
249: 	if ($c>0) return $al;
250: 	else return false;
251: 	}
252: 	
253: function SetAlerts($nodeid,$alertlevel,$alerts="")
254: 	{
255: 	if ($alerts=="") $alerts=array();
256: 	// get current alert level
257: 	$q="SELECT alertlevel,nodealert FROM fnnode WHERE nodeid=\"".ss($nodeid)."\"";
258: 	$r=$this->DB->Query($q);
259: 	$row=$this->DB->Fetch_Array($r);
260: 	$this->DB->Free($r);
261: 	$cal=$row['alertlevel'];
262: 	
263: 	if ($alertlevel>$cal)
264: 		{
265: 		// trigger alert process
266: 		}
267: 		
268: 	if ($alertlevel!=$cal)
269: 		{
270: 		// update table
271: 		$q="UPDATE fnnode SET alertlevel=".ss($alertlevel)." WHERE nodeid=\"".ss($nodeid)."\"";
272: 		$this->DB->Query($q);
273: 		}
274: 		
275: 	// do not continue if node alert isn't set
276: 	if ($row['nodealert']!=1) return 0;
277: 		
278: 	// ALERTS
279: 	// is there an existing alert for this node
280: 	$q="SELECT alertid,alertlevel FROM fnalert WHERE nodeid=\"".ss($nodeid)."\" AND closedx=0";
281: 	$r=$this->DB->Query($q);
282: 	if ($row=$this->DB->Fetch_Array($r))
283: 		{ // yes there is
284: 		// if new alert level is 0 let's close it
285: 		if ($alertlevel==0)
286: 			{
287: 			$alertid=$row['alertid'];
288: 			$q="UPDATE fnalert SET closedx=".time()." WHERE alertid=".$row['alertid'];
289: 			$this->DB->Query($q);
290: 			if (is_array($alerts)) $alerts[]="Alert Closed";
291: 			else
292: 				{
293: 				//$alerts=array();
294: 				$alerts[]="Alert Closed";
295: 				}
296: 			}
297: 		else
298: 			{
299: 			$alertid=$row['alertid'];
300: 			// otherwise update the alert to the new value (was: regardless, now just if not a 0)
301: 			$q="UPDATE fnalert SET alertlevel=".ss($alertlevel)." WHERE alertid=".$alertid;
302: 			$this->DB->Query($q);
303: 			}
304: 		}
305: 	else
306: 		{ // no there's not
307: 		if ($alertlevel>0) // only if an actual alert
308: 			{
309: 			$q="INSERT INTO fnalert(nodeid,alertlevel,openedx) VALUES(";
310: 			$q.="\"".ss($nodeid)."\",".ss($alertlevel).",".time().")";
311: 			$this->DB->Query($q);
312: 			$alertid=$this->DB->Insert_Id();
313: 			}
314: 		}
315: 	// ALERT LOG with $alertid
316: 	$t=time();
317: 	$at="";
318: 	if (is_array($alerts))
319: 		{
320: 		foreach($alerts as $alert)
321: 			{
322: 			if (isset($alertid)) // misses on manual runs methinx
323: 				{
324: 				if ($at!="") $at.=", ";
325: 				$at.=$alert;
326: 				//echo $at."\n";
327: 				$iq="INSERT INTO fnalertlog(alertid,postedx,logentry) VALUES(";
328: 				$iq.=$alertid.",".$t.",\"".ss($alert)."\")";
329: 				//echo $iq;
330: 				$this->DB->Query($iq);
331: 				}
332: 			}
333: 		}
334: 		
335: 	$this->AlertAction($nodeid,$alertlevel,$alertlevel-$cal,$at);
336: 	
337: 		
338: 		
339: 	}
340: 
341: function NodeAlertLevel($nodeid)
342: 	{
343: 	$q="SELECT alertlevel FROM fnnode WHERE nodeid=\"".ss($nodeid)."\"";
344: 	$r=$this->DB->Query($q);
345: 	if ($row=$this->DB->Fetch_Array($r)) return $row['alertlevel'];
346: 	else return -1;
347: 	}	
348: 
349: function GroupAlertLevel($groupid)
350: 	{
351: 	$lvl=-1;
352: 	$q="SELECT nodeid FROM fngrouplink WHERE groupid=\"".ss($groupid)."\"";
353: 	$r=$this->DB->Query($q);
354: 	while ($row=$this->DB->Fetch_Array($r))
355: 		{
356: 		$nl=$this->NodeAlertLevel($row['nodeid']);
357: 		if ($nl>$lvl) $lvl=$nl;
358: 		}
359: 	$this->DB->Free($r);
360: 	return $lvl;
361: 	}
362: 	
363: function PhoneHome($mode=0,$type="ping") // 0 - php, 1 - html, 2 - data
364: {
365: if ($mode<2)
366: 	{
367: 	$qs="?type=".$type."&data=version=".$this->Version;
368: 	if (isset($_SERVER['REMOTE_ADDR']))
369: 		$qs.=",ip=".$_SERVER['REMOTE_ADDR'];
370: 	$ploc="http://www.purplepixie.org/freenats/report/";
371: 	if ($mode==1) $ploc.="ping.html";
372: 	else $ploc.="ping.php";
373: 	
374: 	$ploc.=$qs;
375: 	
376: 	$lp=@fopen($ploc,"r");
377: 	if ($lp>0) @fclose($lp);
378: 	}
379: else
380: 	{
381: 	// data post -- !!
382: 	}
383: }
384: 
385: function GetNode($nodeid)
386: 	{
387: 	$return_row=false;
388: 	$q="SELECT * FROM fnnode WHERE nodeid=\"".ss($nodeid)."\" LIMIT 0,1";
389: 	$r=$this->DB->Query($q);
390: 	if ($row=$this->DB->Fetch_Array($r))
391: 		$return_row=true;
392: 		
393: 	$this->DB->Free($r);
394: 	if ($return_row) // found a valid
395: 		{
396: 		if ($row['nodename']!="") $row['name']=$row['nodename']; // make a "nice" name for it
397: 		else $row['name']=$row['nodeid'];
398: 		
399: 		$row['alerttext']=oText($row['alertlevel']); // textual alert status
400: 		
401: 		$row['lastrundt']=nicedt($row['lastrunx']); // text date-time last run
402: 		$row['lastrunago']=dtago($row['lastrunx'],false); // last run ago
403: 		
404: 		// protection
405: 		$row['nskey']="";
406: 		
407: 		return $row;
408: 		}
409: 	else
410: 		return false; // or failed
411: 	}
412: 	
413: function GetGroup($groupid)
414: 	{
415: 	$q="SELECT * FROM fngroup WHERE groupid=".ss($groupid)." LIMIT 0,1";
416: 	$r=$this->DB->Query($q);
417: 	if (!$row=$this->DB->Fetch_Array($r)) return false;
418: 	
419: 	$this->DB->Free($r);
420: 	$row['alertlevel']=$this->GroupAlertLevel($groupid);
421: 	$row['alerttext']=oText($row['alertlevel']);
422: 	return $row;
423: 	}
424: 	
425: function GetTest($testid)
426: 	{
427: 	if ($testid=="") return false;
428: 	$class=$testid[0];
429: 	if (is_numeric($class))
430: 		{
431: 		// test ID will stay the same
432: 		$class="L";
433: 		$anytestid=$testid;
434: 		}
435: 	else
436: 		{
437: 		//$testid=substr($testid,1); // as it will here also so direct use to graphs can be made
438: 		$anytestid=substr($testid,1); // the classless version
439: 		}
440: 		
441: 	$q="";
442: 	switch($class)
443: 		{
444: 		case "L": // local tests 
445: 			$q="SELECT * FROM fnlocaltest WHERE localtestid=".ss($anytestid)." LIMIT 0,1";
446: 			break;
447: 		case "N": // node-side test
448: 			$q="SELECT * FROM fnnstest WHERE nstestid=".ss($anytestid)." LIMIT 0,1";
449: 			break;
450: 		default:
451: 			return false; // can't lookup this class
452: 		}
453: 		
454: 	if ($q=="") return false;
455: 	
456: 	$r=$this->DB->Query($q);
457: 	
458: 	if (!$row=$this->DB->Fetch_Array($r)) return false;
459: 	
460: 	$row['class']=$class;
461: 	$row['testid']=$testid;
462: 	$row['anytestid']=$anytestid;
463: 	$row['alerttext']=oText($row['alertlevel']);
464: 	$row['lastrundt']=nicedt($row['lastrunx']);
465: 	$row['lastrunago']=dtago($row['lastrunx'],false);
466: 	
467: 	if  ($row['testname']!="")  $row['name']=$row['testname'];
468: 	else
469: 			{
470: 			if ($class=="L")
471: 				{
472: 				$row['name']=lText($row['testtype']); // TODO OTHER TESTS
473: 				if ($row['testparam']!="") $row['name'].=" (".$row['testparam'].")";
474: 				}
475: 			else if ($class=="N")
476: 				{
477: 				if ($row['testdesc']!="") $row['name']=$row['testdesc'];
478: 				else $row['name']=$row['testtype'];
479: 				}
480: 			}
481: 	
482: 	$this->DB->Free($r);
483: 	
484: 	return $row;
485: 	}
486: 
487: 	
488: function DeleteTest($testid)
489: 	{
490: 	if ($testid=="") return false;
491: 	$class=$testid[0];
492: 	if (is_numeric($class))
493: 		{
494: 		// test ID will stay the same
495: 		$class="L";
496: 		$anytestid=$testid;
497: 		}
498: 	else
499: 		{
500: 		$anytestid=substr($testid,1); // the classless version
501: 		}
502: 		
503: 	$q="";
504: 	switch($class)
505: 		{
506: 		case "L": // local tests 
507: 			$q="DELETE FROM fnlocaltest WHERE localtestid=".ss($anytestid);
508: 			break;
509: 		case "N": // node-side test
510: 			$q="DELETE FROM fnnstest WHERE nstestid=".ss($anytestid);
511: 			break;
512: 		default:
513: 			return false; // can't lookup this class
514: 		}
515: 		
516: 	if ($q=="") return false;
517: 	
518: 	$this->DB->Query($q);
519: 	$tests=$this->DB->Affected_Rows();
520: 	
521: 	$rq="DELETE FROM fnrecord WHERE testid=\"".ss($testid)."\"";
522: 	$this->DB->Query($rq);
523: 	$records=$this->DB->Affected_Rows();
524: 	
525: 	$eq="DELETE FROM fneval WHERE testid=\"".ss($testid)."\"";
526: 	$this->DB->Query($eq);
527: 	$eval=$this->DB->Affected_Rows();
528: 	
529: 	$s="Deleted test ".$testid." (".$tests." tests, ".$records." records, ".$eval." evaluators)";
530: 	$this->Event($s,6,"Test","Delete");
531: 	}
532: 	
533: 	
534: 	
535: function InvalidateTest($testid,$rightnow=false)
536: 	{
537: 	$class=$testid[0];
538: 	if (is_numeric($class)) $class="L";
539: 	else $testid=substr($testid,1);
540: 	if ($rightnow)
541: 		{
542: 		$nextx=time();
543: 		$q="UPDATE ";
544: 		if ($class=="L") $q.="fnlocaltest";
545: 		// other ones here
546: 		
547: 		$q.=" SET nextrunx=".$nextx." WHERE ";
548: 		
549: 		if ($class=="L") $q.="localtestid=".$testid;
550: 		// other ones here
551: 		
552: 		$this->DB->Query($q);
553: 		return true;
554: 		}
555: 	// otherwise use it's interval
556: 	$q="SELECT testinterval FROM ";
557: 	
558: 	if ($class=="L") $q.="fnlocaltest WHERE localtestid=";
559: 	// other ones here
560: 	
561: 	$q.=$testid;
562: 	$r=$this->DB->Query($q);
563: 	if ($row=$this->DB->Fetch_Array($r))
564: 		{
565: 		$this->DB->Free($r);
566: 		$nextx=next_run_x($row['testinterval']);
567: 		$q="UPDATE ";
568: 		if ($class=="L") $q.="fnlocaltest";
569: 		// other ones here
570: 		
571: 		$q.=" SET nextrunx=".$nextx." WHERE ";
572: 		
573: 		if ($class=="L") $q.="localtestid=".$testid;
574: 		// other ones here
575: 		
576: 		$this->DB->Query($q);
577: 		return true;
578: 		}
579: 	return false;
580: 	}		
581: 	
582: function InvalidateNode($nodeid,$rightnow=false,$testsaswell=false)
583: 	{
584: 	if ($rightnow)
585: 		{
586: 		$nextx=time();
587: 		$q="UPDATE fnnode SET nextrunx=".$nextx." WHERE nodeid=\"".ss($nodeid)."\"";
588: 		$this->DB->Query($q);
589: 		if ($testsaswell)
590: 			{
591: 			$q="UPDATE fnlocaltest SET nextrunx=".$nextx." WHERE nodeid=\"".ss($nodeid)."\"";
592: 			$this->DB->Query($q);
593: 			}
594: 		return true;
595: 		}
596: 	// otherwise set to it's interval
597: 	$q="SELECT testinterval FROM fnnode WHERE nodeid=\"".ss($nodeid)."\"";
598: 	$r=$this->DB->Query($q);
599: 	if ($row=$this->DB->Fetch_Array($r))
600: 		{
601: 		$nextx=next_run_x($row['testinterval']);
602: 		$uq="UPDATE fnnode SET nextrunx=".$nextx." WHERE nodeid=\"".ss($nodeid)."\"";
603: 		$this->DB->Query($uq);
604: 		$this->DB->Free($r);
605: 		if ($testsaswell)
606: 			{
607: 			$uq="UPDATE fnlocaltest SET nextrunx=".$nextx." WHERE nodeid=\"".ss($nodeid)."\"";
608: 			$this->DB->Query($uq);
609: 			}
610: 		return true;
611: 		}
612: 	return false;
613: 	}
614: 	
615: 	
616: function NodeSide_Pull($nodeid)
617: 	{
618: 	$q="SELECT nsenabled,nspullenabled,nsurl,nskey,nsinterval FROM fnnode WHERE nodeid=\"".ss($nodeid)."\" LIMIT 0,1";
619: 	$r=$this->DB->Query($q);
620: 	if (!$row=$this->DB->Fetch_Array($r)) return false;
621: 	
622: 	$this->DB->Free($r);
623: 	
624: 	$url=$row['nsurl'];
625: 	if ($row['nskey']!="") $url.="?nodekey=".$row['nskey'];
626: 	$this->Event("NodeSide_Pull Started for ".$nodeid,10,"Node","Pull");
627: 	
628: 	$xmlobj=new TNodeXML();
629: 	
630: 	$fp=@fopen($url,"r");
631: 	if ($fp<=0)
632: 		{
633: 		$this->Event("Pull Failed URL ".$url,1,"Node","Pull");
634: 		return false;
635: 		}
636: 	$xml="";
637: 	while (!feof($fp))
638: 		{
639: 		$xml.=fgets($fp,4096);
640: 		}
641: 	if ($xml=="") return false;
642: 	
643: 	//echo $xml;
644: 	
645: 	$result=$xmlobj->Parse($xml);
646: 	
647: 	if ($xmlobj->Error()!="")
648: 		{
649: 		$this->Event("NodeXML Error: ".$xmlobj->Error(),1,"Node","Pull");
650: 		return false;
651: 		}
652: 	$this->Event("NodeSide_Pull Fetched ".$xmlobj->Tests." tests for ".$nodeid,10,"Node","Pull");
653: 	// Now just to actually process it...
654: 	$this->NodeSide_Process($nodeid,$xmlobj);
655: 	return true;
656: 	}	
657: 	
658: function NodeSide_Process($nodeid,&$xmlobj)
659: 	{ // nodeid + takes a TNodeXML Object
660: 	$this->Event("NodeSide_Process for ".$nodeid,10,"Node","Pull");
661: 	$q="SELECT * FROM fnnstest WHERE nodeid=\"".ss($nodeid)."\"";
662: 	$r=$this->DB->Query($q);
663: 	$tests=array();
664: 	while ($row=$this->DB->Fetch_Array($r))
665: 		{
666: 		$tests[$row['testtype']]=$row;
667: 		if (isset($xmlobj->Catalogue[$row['testtype']]))
668: 			{ // this test is in the DB and catalogue
669: 			$tests[$row['testtype']]['incat']=true;
670: 			if ($row['testenabled']==1) // it is enabled - so we test it
671: 				{
672: 				if ($row['simpleeval']==1) $level=$xmlobj->Catalogue[$row['testtype']]['ALERTLEVEL']; // use provided level
673: 				else $level=nats_eval("N".$row['nstestid'],$xmlobj->Catalogue[$row['testtype']]['VALUE']);
674: 				$dbs="Nodeside ".$row['testtype']." on ".$row['nodeid']." = ".$level;
675: 				if ($level==0) $debuglev=8;
676: 				else if ($level>0) $debuglev=5;
677: 				else $debuglev=2;
678: 				$this->Event($dbs,$debuglev,"Node","Process");
679: 				
680: 				if ($row['testrecord']==1) // record it
681: 					{
682: 					$testvalue=$xmlobj->Catalogue[$row['testtype']]['VALUE'];
683: 					if (!is_numeric($testvalue)) $testvalue=0;
684: 					$iq="INSERT INTO fnrecord(testid,alertlevel,recordx,nodeid,testvalue) VALUES(";
685: 					$iq.="\"N".$row['nstestid']."\",".$level.",".time().",\"".$row['nodeid']."\",".$testvalue.")";
686: 					$this->DB->Query($iq);
687: 					if ($this->DB->Affected_Rows()<=0)
688: 						$this->Event("Nodeside ".$row['testtype']." Failed to Record",1,"Node","Process");
689: 					}
690: 					
691: 				// We don't do any alerting here - the tester will do that for us!
692: 				$uq="UPDATE fnnstest SET lastrunx=".time().",lastvalue=\"".ss($xmlobj->Catalogue[$row['testtype']]['VALUE'])."\",alertlevel=".$level." ";
693: 				$uq.="WHERE nstestid=".$row['nstestid'];
694: 				$this->DB->Query($uq);
695: 				if ($this->DB->Affected_Rows()<=0)
696: 						$this->Event("Nodeside ".$row['testtype']." Failed to Update or Same Values",5,"Node","Process");
697: 				
698: 				}
699: 					
700: 				
701: 				
702: 			}
703: 		else
704: 			{
705: 			// test in the DB but NOT in the catalogue
706: 			//$xmlobj->Catalogue[$row['testtype']]['incat']=false;
707: 			if ($row['testenabled']==1)
708: 				{ // enabled so shown in lists etc
709: 				// Update it to show failed status
710: 				$this->Event("No nodeside data for test N".$row['nstestid'],3,"Node","Process");
711: 				$uq="UPDATE fnnstest SET alertlevel=2,lastvalue=-1 WHERE nstestid=".$row['nstestid'];
712: 				$this->DB->Query($uq);
713: 				}
714: 			else // not enabled so simply delete
715: 				{
716: 				$this->DeleteTest("N".$row['nstestid']);
717: 				}
718: 			}
719: 		}
720: 	$this->DB->Free($r);
721: 	
722: 	
723: 	// and finally we look for new tests i.e. in the cat but not in the DB
724: 	foreach($xmlobj->Catalogue as $val)
725: 		{
726: 		$key=$val['NAME'];
727: 		if (!isset($tests[$key])) // not in the DB
728: 			{
729: 			$q="INSERT INTO fnnstest(nodeid,testtype,testdesc,lastvalue,lastrunx,alertlevel) ";
730: 			$q.="VALUES(\"".ss($nodeid)."\",\"".$key."\",\"".ss($val['DESC'])."\",\"".ss($val['VALUE'])."\",".time().",".ss($val['ALERTLEVEL']).")";
731: 			//echo $q."<br>";
732: 			$this->DB->Query($q);
733: 			}
734: 		}
735: 	
736: 	
737: 	}
738: 	
739: 	
740: }
741: ?>