File: 0.04.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.04.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: 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: if ($alerts=="") $alerts=array(); 250: // get current alert level 251: $q="SELECT alertlevel,nodealert FROM fnnode WHERE nodeid=\"".ss($nodeid)."\""; 252: $r=$this->DB->Query($q); 253: $row=$this->DB->Fetch_Array($r); 254: $this->DB->Free($r); 255: $cal=$row['alertlevel']; 256: 257: if ($alertlevel>$cal) 258: { 259: // trigger alert process 260: } 261: 262: if ($alertlevel!=$cal) 263: { 264: // update table 265: $q="UPDATE fnnode SET alertlevel=".ss($alertlevel)." WHERE nodeid=\"".ss($nodeid)."\""; 266: $this->DB->Query($q); 267: } 268: 269: // do not continue if node alert isn't set 270: if ($row['nodealert']!=1) return 0; 271: 272: // ALERTS 273: // is there an existing alert for this node 274: $q="SELECT alertid,alertlevel FROM fnalert WHERE nodeid=\"".ss($nodeid)."\" AND closedx=0"; 275: $r=$this->DB->Query($q); 276: if ($row=$this->DB->Fetch_Array($r)) 277: { // yes there is 278: // if new alert level is 0 let's close it 279: if ($alertlevel==0) 280: { 281: $alertid=$row['alertid']; 282: $q="UPDATE fnalert SET closedx=".time()." WHERE alertid=".$row['alertid']; 283: $this->DB->Query($q); 284: if (is_array($alerts)) $alerts[]="Alert Closed"; 285: else 286: { 287: //$alerts=array(); 288: $alerts[]="Alert Closed"; 289: } 290: } 291: else 292: { 293: $alertid=$row['alertid']; 294: // otherwise update the alert to the new value (was: regardless, now just if not a 0) 295: $q="UPDATE fnalert SET alertlevel=".ss($alertlevel)." WHERE alertid=".$alertid; 296: $this->DB->Query($q); 297: } 298: } 299: else 300: { // no there's not 301: if ($alertlevel>0) // only if an actual alert 302: { 303: $q="INSERT INTO fnalert(nodeid,alertlevel,openedx) VALUES("; 304: $q.="\"".ss($nodeid)."\",".ss($alertlevel).",".time().")"; 305: $this->DB->Query($q); 306: $alertid=$this->DB->Insert_Id(); 307: } 308: } 309: // ALERT LOG with $alertid 310: $t=time(); 311: $at=""; 312: if (is_array($alerts)) 313: { 314: foreach($alerts as $alert) 315: { 316: if (isset($alertid)) // misses on manual runs methinx 317: { 318: if ($at!="") $at.=", "; 319: $at.=$alert; 320: //echo $at."\n"; 321: $iq="INSERT INTO fnalertlog(alertid,postedx,logentry) VALUES("; 322: $iq.=$alertid.",".$t.",\"".ss($alert)."\")"; 323: //echo $iq; 324: $this->DB->Query($iq); 325: } 326: } 327: } 328: 329: $this->AlertAction($nodeid,$alertlevel,$alertlevel-$cal,$at); 330: 331: 332: 333: } 334: 335: function NodeAlertLevel($nodeid) 336: { 337: $q="SELECT alertlevel FROM fnnode WHERE nodeid=\"".ss($nodeid)."\""; 338: $r=$this->DB->Query($q); 339: if ($row=$this->DB->Fetch_Array($r)) return $row['alertlevel']; 340: else return -1; 341: } 342: 343: function GroupAlertLevel($groupid) 344: { 345: $lvl=-1; 346: $q="SELECT nodeid FROM fngrouplink WHERE groupid=\"".ss($groupid)."\""; 347: $r=$this->DB->Query($q); 348: while ($row=$this->DB->Fetch_Array($r)) 349: { 350: $nl=$this->NodeAlertLevel($row['nodeid']); 351: if ($nl>$lvl) $lvl=$nl; 352: } 353: $this->DB->Free($r); 354: return $lvl; 355: } 356: 357: function PhoneHome($mode=0,$type="ping") // 0 - php, 1 - html, 2 - data 358: { 359: if ($mode<2) 360: { 361: $qs="?type=".$type."&data=version=".$this->Version; 362: if (isset($_SERVER['REMOTE_ADDR'])) 363: $qs.=",ip=".$_SERVER['REMOTE_ADDR']; 364: $ploc="http://www.purplepixie.org/freenats/report/"; 365: if ($mode==1) $ploc.="ping.html"; 366: else $ploc.="ping.php"; 367: 368: $ploc.=$qs; 369: 370: $lp=@fopen($ploc,"r"); 371: if ($lp>0) @fclose($lp); 372: } 373: else 374: { 375: // data post -- !! 376: } 377: } 378: 379: function GetNode($nodeid) 380: { 381: $return_row=false; 382: $q="SELECT * FROM fnnode WHERE nodeid=\"".ss($nodeid)."\" LIMIT 0,1"; 383: $r=$this->DB->Query($q); 384: if ($row=$this->DB->Fetch_Array($r)) 385: $return_row=true; 386: 387: $this->DB->Free($r); 388: if ($return_row) // found a valid 389: { 390: if ($row['nodename']!="") $row['name']=$row['nodename']; // make a "nice" name for it 391: else $row['name']=$row['nodeid']; 392: 393: $row['alerttext']=oText($row['alertlevel']); // textual alert status 394: 395: $row['lastrundt']=nicedt($row['lastrunx']); // text date-time last run 396: $row['lastrunago']=dtago($row['lastrunx'],false); // last run ago 397: 398: return $row; 399: } 400: else 401: return false; // or failed 402: } 403: 404: function GetGroup($groupid) 405: { 406: $q="SELECT * FROM fngroup WHERE groupid=".ss($groupid)." LIMIT 0,1"; 407: $r=$this->DB->Query($q); 408: if (!$row=$this->DB->Fetch_Array($r)) return false; 409: 410: $this->DB->Free($r); 411: $row['alertlevel']=$this->GroupAlertLevel($groupid); 412: $row['alerttext']=oText($row['alertlevel']); 413: return $row; 414: } 415: 416: function GetTest($testid) 417: { 418: if ($testid=="") return false; 419: $class=$testid[0]; 420: if (is_numeric($class)) 421: { 422: // test ID will stay the same 423: $class="L"; 424: $anytestid=$testid; 425: } 426: else 427: { 428: //$testid=substr($testid,1); // as it will here also so direct use to graphs can be made 429: $anytestid=substr($testid,1); // the classless version 430: } 431: 432: $q=""; 433: switch($class) 434: { 435: case "L": // local tests only thus-far 436: $q="SELECT * FROM fnlocaltest WHERE localtestid=".ss($anytestid)." LIMIT 0,1"; 437: break; 438: default: 439: return false; // can't lookup this class 440: } 441: 442: if ($q=="") return false; 443: 444: $r=$this->DB->Query($q); 445: 446: if (!$row=$this->DB->Fetch_Array($r)) return false; 447: 448: $row['class']=$class; 449: $row['testid']=$testid; 450: $row['anytestid']=$anytestid; 451: $row['alerttext']=oText($row['alertlevel']); 452: $row['lastrundt']=nicedt($row['lastrunx']); 453: $row['lastrunago']=dtago($row['lastrunx'],false); 454: 455: if ($row['testname']!="") $row['name']=$row['testname']; 456: else 457: { 458: $row['name']=lText($row['testtype']); // TODO OTHER TESTS 459: if ($row['testparam']!="") $row['name'].=" (".$row['testparam'].")"; 460: } 461: 462: $this->DB->Free($r); 463: 464: return $row; 465: } 466: 467: 468: } 469: ?>