File: 0.02.56a/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.56"; 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: if ($aa['mdata']!="") $ndata=$aa['mdata']."\n".$nodeid.": ".$alerttext; 87: else $ndata=$nodeid.": ".$alerttext; 88: $uq="UPDATE fnalertaction SET mdata=\"".ss($ndata)."\" WHERE aaid=".$arow['aaid']; 89: //echo $uq."\n"; 90: $this->DB->Query($uq); 91: } 92: } 93: 94: function ActionFlush() 95: { 96: global $allowed; // allowed chars from screen in YA BODGE 97: $q="SELECT * FROM fnalertaction WHERE mdata!=\"\""; 98: $r=$this->DB->Query($q); 99: while ($row=$this->DB->Fetch_Array($r)) 100: { 101: 102: $doalert=true; 103: 104: // clear mdata right at the start to get around duplicate emails whilst processing 105: $q="UPDATE fnalertaction SET mdata=\"\" WHERE aaid=".$row['aaid']; 106: $this->DB->Query($q); 107: 108: if ($this->DB->Affected_Rows()<=0) // already flushed or failed to flush 109: { 110: $doalert=false; 111: $this->Event("Alert Action Already Flushed - Skipping",8,"Flush","Action"); 112: } 113: 114: // alert counter 115: $td=date("Ymd"); 116: if ($td!=$row['ctrdate']) // new day or no flush record 117: { 118: $q="UPDATE fnalertaction SET ctrdate=\"".$td."\",ctrtoday=1 WHERE aaid=".$row['aaid']; 119: $this->DB->Query($q); 120: } 121: else 122: { 123: 124: if ( ($row['ctrlimit']==0) || ($row['ctrlimit']>$row['ctrtoday']) ) // no limit or below 125: { 126: $q="UPDATE fnalertaction SET ctrtoday=ctrtoday+1 WHERE aaid=".$row['aaid']; 127: $this->DB->Query($q); 128: } 129: else // at or over limit 130: { 131: $this->Event("Alert Action Limit Reached - Skipping",2,"Flush","Action"); 132: $doalert=false; 133: } 134: 135: } 136: 137: 138: if ($row['atype']=="email") 139: { 140: if ($row['esubject']==0) $sub=""; 141: else if ($row['esubject']==1) $sub="FreeNATS Alert"; 142: else $sub="** FreeNATS Alert **"; 143: $body=""; 144: if ($row['etype']==0) $body=$row['mdata']; 145: else $body="FreeNATS Alert,\r\n".$row['mdata']."\r\n--FreeNATS @ ".nicedt(time()); 146: //$tolist=preg_split("[\n\r]",$row['etolist']); 147: $tolist=array(); 148: $f=0; 149: $tolist[0]=""; 150: for ($a=0; $a151: { 152: $chr=$row['etolist'][$a]; 153: //echo $chr; 154: if (strpos($allowed,$chr)===false) // special char 155: { 156: $f++; 157: $tolist[$f]=""; 158: } 159: else 160: { 161: $tolist[$f].=$chr; 162: } 163: } 164: 165: foreach($tolist as $toaddr) 166: { 167: $toaddr=nices($toaddr); 168: if ($toaddr!="") 169: { 170: //echo $row['efrom'].":".$toaddr.":".$sub.":".$body."\n\n"; 171: //$body="fish"; 172: //mail($toaddr,$sub,$body,"From: ".$row['efrom']."\r\n","-f".$row['efrom']); 173: $header="From: ".$row['efrom']."\r\n"; 174: //$header=""; 175: //$exhead="-f".$row['efrom']; 176: //db("Sending Email: ".$toaddr); 177: if ($doalert) 178: { 179: mail($toaddr,$sub,$body,$header); 180: $this->Event("Sent alert email to ".$toaddr,4,"Flush","Email"); 181: } 182: } 183: } 184: 185: 186: 187: } 188: else if ($row['atype']=="url") 189: { 190: // url send 191: if ($row['etype']==0) $body=$row['mdata']; 192: else $body="FreeNATS Alert,\r\n".$row['mdata']."\r\n--FreeNATS @ ".nicedt(time()); 193: 194: $body=urlencode($body); 195: $tolist=array(); 196: $f=0; 197: $tolist[0]=""; 198: for ($a=0; $a199: { 200: $chr=$row['etolist'][$a]; 201: //echo $chr; 202: if (strpos($allowed,$chr)===false) // special char 203: { 204: $f++; 205: $tolist[$f]=""; 206: } 207: else 208: { 209: $tolist[$f].=$chr; 210: } 211: } 212: 213: foreach($tolist as $tourl) 214: { 215: if ($doalert) 216: { 217: $url=$tourl.$body; 218: $fp=@fopen($url,"r"); 219: if ($fp>0) fclose($fp); 220: else $this->Event("URL Alert Failed ".$url,1,"Flush","URL"); 221: $this->Event("URL Alert ".$url,4,"Flush","URL"); 222: } 223: } 224: 225: 226: } 227: 228: } 229: } 230: 231: function GetAlerts() 232: { 233: $q="SELECT nodeid,alertlevel FROM fnalert WHERE closedx=0"; 234: $r=$this->DB->Query($q); 235: $c=0; 236: $al=array(); 237: while ($row=$this->DB->Fetch_Array($r)) 238: { 239: $al[$c]['nodeid']=$row['nodeid']; 240: $al[$c]['alertlevel']=$row['alertlevel']; 241: $c++; 242: } 243: if ($c>0) return $al; 244: else return false; 245: } 246: 247: function SetAlerts($nodeid,$alertlevel,$alerts="") 248: { 249: // get current alert level 250: $q="SELECT alertlevel,nodealert FROM fnnode WHERE nodeid=\"".ss($nodeid)."\""; 251: $r=$this->DB->Query($q); 252: $row=$this->DB->Fetch_Array($r); 253: $this->DB->Free($r); 254: $cal=$row['alertlevel']; 255: 256: if ($alertlevel>$cal) 257: { 258: // trigger alert process 259: } 260: 261: if ($alertlevel!=$cal) 262: { 263: // update table 264: $q="UPDATE fnnode SET alertlevel=".ss($alertlevel)." WHERE nodeid=\"".ss($nodeid)."\""; 265: $this->DB->Query($q); 266: } 267: 268: // do not continue if node alert isn't set 269: if ($row['nodealert']!=1) return 0; 270: 271: // ALERTS 272: // is there an existing alert for this node 273: $q="SELECT alertid,alertlevel FROM fnalert WHERE nodeid=\"".ss($nodeid)."\" AND closedx=0"; 274: $r=$this->DB->Query($q); 275: if ($row=$this->DB->Fetch_Array($r)) 276: { // yes there is 277: // if new alert level is 0 let's close it 278: if ($alertlevel==0) 279: { 280: $q="UPDATE fnalert SET closedx=".time()." WHERE alertid=".$row['alertid']; 281: $this->DB->Query($q); 282: if (is_array($alerts)) $alerts[]="Alert Closed"; 283: else 284: { 285: $alerts=array(); 286: $alerts[]="Alert Closed"; 287: } 288: } 289: else 290: { 291: $alertid=$row['alertid']; 292: // otherwise update the alert to the new value (was: regardless, now just if not a 0) 293: $q="UPDATE fnalert SET alertlevel=".ss($alertlevel)." WHERE alertid=".$alertid; 294: $this->DB->Query($q); 295: } 296: } 297: else 298: { // no there's not 299: if ($alertlevel>0) // only if an actual alert 300: { 301: $q="INSERT INTO fnalert(nodeid,alertlevel,openedx) VALUES("; 302: $q.="\"".ss($nodeid)."\",".ss($alertlevel).",".time().")"; 303: $this->DB->Query($q); 304: $alertid=$this->DB->Insert_Id(); 305: } 306: } 307: // ALERT LOG with $alertid 308: $t=time(); 309: $at=""; 310: if (is_array($alerts)) 311: { 312: foreach($alerts as $alert) 313: { 314: if (isset($alertid)) // misses on manual runs methinx 315: { 316: if ($at!="") $at.=", "; 317: $at.=$alert; 318: //echo $at."\n"; 319: $iq="INSERT INTO fnalertlog(alertid,postedx,logentry) VALUES("; 320: $iq.=$alertid.",".$t.",\"".ss($alert)."\")"; 321: //echo $iq; 322: $this->DB->Query($iq); 323: } 324: } 325: } 326: 327: $this->AlertAction($nodeid,$alertlevel,$alertlevel-$cal,$at); 328: 329: 330: 331: } 332: 333: function NodeAlertLevel($nodeid) 334: { 335: $q="SELECT alertlevel FROM fnnode WHERE nodeid=\"".ss($nodeid)."\""; 336: $r=$this->DB->Query($q); 337: if ($row=$this->DB->Fetch_Array($r)) return $row['alertlevel']; 338: else return -1; 339: } 340: 341: function GroupAlertLevel($groupid) 342: { 343: $lvl=-1; 344: $q="SELECT nodeid FROM fngrouplink WHERE groupid=\"".ss($groupid)."\""; 345: $r=$this->DB->Query($q); 346: while ($row=$this->DB->Fetch_Array($r)) 347: { 348: $nl=$this->NodeAlertLevel($row['nodeid']); 349: if ($nl>$lvl) $lvl=$nl; 350: } 351: $this->DB->Free($r); 352: return $lvl; 353: } 354: 355: function PhoneHome($mode=0,$type="ping") // 0 - php, 1 - html, 2 - data 356: { 357: if ($mode<2) 358: { 359: $qs="?type=".$type."&data=version=".$this->Version; 360: if (isset($_SERVER['REMOTE_ADDR'])) 361: $qs.=",ip=".$_SERVER['REMOTE_ADDR']; 362: $ploc="http://www.purplepixie.org/freenats/report/"; 363: if ($mode==1) $ploc.="ping.html"; 364: else $ploc.="ping.php"; 365: 366: $ploc.=$qs; 367: 368: $lp=@fopen($ploc,"r"); 369: if ($lp>0) @fclose($lp); 370: } 371: else 372: { 373: // data post -- !! 374: } 375: } 376: 377: } 378: ?>