File:
0.02.24a/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 $Version="0.02.24";
29: var $Release="a";
30:
31: function Start()
32: {
33: $this->DB=new TNATS_DB();
34: $this->Cfg=new TNATS_Cfg();
35: $this->DB->Connect();
36: $this->Cfg->Load($this->DB);
37: $this->init=true;
38: }
39:
40: function Stop()
41: {
42: $this->DB->Disconnect();
43: $this->init=false;
44: }
45:
46: function Event($logevent,$loglevel=1,$modid="NONE",$catid="NONE")
47: {
48: global $NATS_Session;
49: if ((isset($NATS_Session))&&($NATS_Session->auth)) $username=$NATS_Session->username;
50: else $username="";
51: $l=$this->Cfg->Get("log.level");
52: //echo "** $l **\n";
53: if ( $l=="" ) $l=10; // debug logging if no variable
54: if ( $l < $loglevel ) return false;
55: if (strlen($logevent)>249) $logevent=substr($logevent,0,245)."...";
56: $q="INSERT INTO fnlog(postedx,modid,catid,loglevel,logevent,username) VALUES(".time().",";
57: $q.="\"".ss($modid)."\",\"".ss($catid)."\",".ss($loglevel).",\"".ss($logevent)."\",\"".ss($username)."\")";
58: //echo $q;
59: $this->DB->Query($q);
60: }
61:
62: function AlertAction($nodeid,$alertlevel,$change,$alerttext)
63: {
64: echo "Called for node: ".$nodeid."\n";
65: if ($change==0) return false;
66: //echo $nodeid.":".$alertlevel.":".$change.":".$alerttext."\n";
67: // get all the alertactions for this node id
68: $q="SELECT aaid FROM fnnalink WHERE nodeid=\"".ss($nodeid)."\"";
69: $r=$this->DB->Query($q);
70: while ($arow=$this->DB->Fetch_Array($r))
71: {
72: // get details for this alert action
73: $aq="SELECT * FROM fnalertaction WHERE aaid=".$arow['aaid']." LIMIT 0,1";
74: //echo $aq."\n";
75: $ar=$this->DB->Query($aq);
76: $aa=$this->DB->Fetch_Array($ar);
77: $this->DB->Free($ar);
78: //echo $aa['atype']."FISH\n";
79:
80: // UGGGGGGGG continue!!
81: if ( ($aa['atype']=="") || ($aa['atype']=="Disabled") ) continue;
82: if ( ($aa['awarnings']==0) && ($alertlevel<2) ) continue;
83: if ( ($aa['adecrease']==0) && ($change<1) ) continue;
84:
85: // made it this far
86: $ndata=$aa['mdata']."\n".$nodeid.": ".$alerttext;
87: $uq="UPDATE fnalertaction SET mdata=\"".ss($ndata)."\" WHERE aaid=".$arow['aaid'];
88: //echo $uq."\n";
89: $this->DB->Query($uq);
90: }
91: }
92:
93: function ActionFlush()
94: {
95: global $allowed; // allowed chars from screen in YA BODGE
96: $q="SELECT * FROM fnalertaction WHERE mdata!=\"\"";
97: $r=$this->DB->Query($q);
98: while ($row=$this->DB->Fetch_Array($r))
99: {
100:
101: // clear mdata right at the start to get around duplicate emails whilst processing
102: $q="UPDATE fnalertaction SET mdata=\"\" WHERE aaid=".$row['aaid'];
103: $this->DB->Query($q);
104:
105: if ($row['atype']=="email")
106: {
107: if ($row['esubject']==0) $sub="";
108: else if ($row['esubject']==1) $sub="FreeNATS Alert";
109: else $sub="** FreeNATS Alert **";
110: $body="";
111: if ($row['etype']==0) $body=$row['mdata'];
112: else $body="FreeNATS Alert,\r\n".$row['mdata']."\r\n--FreeNATS @ ".nicedt(time());
113: //$tolist=preg_split("[\n\r]",$row['etolist']);
114: $tolist=array();
115: $f=0;
116: $tolist[0]="";
117: for ($a=0; $a118: {
119: $chr=$row['etolist'][$a];
120: //echo $chr;
121: if (strpos($allowed,$chr)===false) // special char
122: {
123: $f++;
124: $tolist[$f]="";
125: }
126: else
127: {
128: $tolist[$f].=$chr;
129: }
130: }
131:
132: foreach($tolist as $toaddr)
133: {
134: $toaddr=nices($toaddr);
135: if ($toaddr!="")
136: {
137: //echo $row['efrom'].":".$toaddr.":".$sub.":".$body."\n\n";
138: //$body="fish";
139: //mail($toaddr,$sub,$body,"From: ".$row['efrom']."\r\n","-f".$row['efrom']);
140: $header="From: ".$row['efrom']."\r\n";
141: //$header="";
142: //$exhead="-f".$row['efrom'];
143: //db("Sending Email: ".$toaddr);
144: mail($toaddr,$sub,$body,$header);
145: $this->Event("Sent alert email to ".$toaddr,4,"Flush","Email");
146: }
147: }
148:
149: }
150: }
151: }
152:
153: function GetAlerts()
154: {
155: $q="SELECT nodeid,alertlevel FROM fnalert WHERE closedx=0";
156: $r=$this->DB->Query($q);
157: $c=0;
158: $al=array();
159: while ($row=$this->DB->Fetch_Array($r))
160: {
161: $al[$c]['nodeid']=$row['nodeid'];
162: $al[$c]['alertlevel']=$row['alertlevel'];
163: $c++;
164: }
165: if ($c>0) return $al;
166: else return false;
167: }
168:
169: function SetAlerts($nodeid,$alertlevel,$alerts="")
170: {
171: // get current alert level
172: $q="SELECT alertlevel FROM fnnode WHERE nodeid=\"".ss($nodeid)."\"";
173: $r=$this->DB->Query($q);
174: $row=$this->DB->Fetch_Array($r);
175: $this->DB->Free($r);
176: $cal=$row['alertlevel'];
177:
178: if ($alertlevel>$cal)
179: {
180: // trigger alert process
181: }
182:
183: if ($alertlevel!=$cal)
184: {
185: // update table
186: $q="UPDATE fnnode SET alertlevel=".ss($alertlevel)." WHERE nodeid=\"".ss($nodeid)."\"";
187: $this->DB->Query($q);
188: }
189:
190:
191: // ALERTS
192: // is there an existing alert for this node
193: $q="SELECT alertid,alertlevel FROM fnalert WHERE nodeid=\"".ss($nodeid)."\" AND closedx=0";
194: $r=$this->DB->Query($q);
195: if ($row=$this->DB->Fetch_Array($r))
196: { // yes there is
197: // if new alert level is 0 let's close it
198: if ($alertlevel==0)
199: {
200: $q="UPDATE fnalert SET closedx=".time()." WHERE alertid=".$row['alertid'];
201: $this->DB->Query($q);
202: if (is_array($alerts)) $alerts[]="Alert Closed";
203: else
204: {
205: $alerts=array();
206: $alerts[]="Alert Closed";
207: }
208: }
209: $alertid=$row['alertid'];
210: // otherwise update the alert to the new value regardless
211: $q="UPDATE fnalert SET alertlevel=".ss($alertlevel)." WHERE alertid=".$alertid;
212: $this->DB->Query($q);
213: }
214: else
215: { // no there's not
216: if ($alertlevel>0) // only if an actual alert
217: {
218: $q="INSERT INTO fnalert(nodeid,alertlevel,openedx) VALUES(";
219: $q.="\"".ss($nodeid)."\",".ss($alertlevel).",".time().")";
220: $this->DB->Query($q);
221: $alertid=$this->DB->Insert_Id();
222: }
223: }
224: // ALERT LOG with $alertid
225: $t=time();
226: $at="";
227: if (is_array($alerts))
228: {
229: foreach($alerts as $alert)
230: {
231: if ($at!="") $at.=", ";
232: $at.=$alert;
233: //echo $at."\n";
234: $iq="INSERT INTO fnalertlog(alertid,postedx,logentry) VALUES(";
235: $iq.=$alertid.",".$t.",\"".ss($alert)."\")";
236: //echo $iq;
237: $this->DB->Query($iq);
238: }
239: }
240:
241: $this->AlertAction($nodeid,$alertlevel,$alertlevel-$cal,$at);
242:
243:
244:
245: }
246:
247: function NodeAlertLevel($nodeid)
248: {
249: $q="SELECT alertlevel FROM fnnode WHERE nodeid=\"".ss($nodeid)."\"";
250: $r=$this->DB->Query($q);
251: if ($row=$this->DB->Fetch_Array($r)) return $row['alertlevel'];
252: else return -1;
253: }
254:
255: function GroupAlertLevel($groupid)
256: {
257: $lvl=-1;
258: $q="SELECT nodeid FROM fngrouplink WHERE groupid=\"".ss($groupid)."\"";
259: $r=$this->DB->Query($q);
260: while ($row=$this->DB->Fetch_Array($r))
261: {
262: $nl=$this->NodeAlertLevel($row['nodeid']);
263: if ($nl>$lvl) $lvl=$nl;
264: }
265: $this->DB->Free($r);
266: return $lvl;
267: }
268:
269: }
270: ?>