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