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