File: 1.01.2a/server/web/api.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: ob_start(); 24: require("include.php"); 25: $NATS->Start(); 26: $session=true; 27: if (!$NATS_Session->Check($NATS->DB)) 28: { 29: $session=false; 30: } 31: 32: $abs=GetAbsolute(); 33: 34: if (isset($_REQUEST['mode'])) $mode=$_REQUEST['mode']; 35: else $mode="xml"; 36: 37: // api.public - is available without session auth 38: // api.key - usage key used if public and no session (if set) 39: 40: if ($NATS->Cfg->Get("api.public",0)!=1) // NOT public 41: { 42: if (!$session) 43: { 44: echo "Error: Public API Access Disabled"; 45: exit(); 46: } 47: } 48: else if (!$session) // IS PUBLIC and not logged in 49: { 50: $key=$NATS->Cfg->Get("api.key",""); 51: if ($key!="") // require a key 52: { 53: if ( (!isset($_REQUEST['apikey'])) || ($_REQUEST['apikey'] != $key) ) 54: { 55: // No key or doesn't match 56: echo "Error: Public API Key Mismatch"; 57: exit(); 58: } 59: } 60: } 61: 62: // Got this far so it must be a winner (either public or no key or correct key) 63: 64: 65: 66: function lo($text) // line out 67: { 68: echo $text."\n"; 69: } 70: 71: // Header 72: ob_clean(); 73: switch($mode) 74: { 75: case "xml": 76: header("Content-type: text/xml"); 77: lo(""); 78: lo(""); 79: break; 80: 81: case "js": 82: if (isset($_REQUEST['dataid'])) $dataid=$_REQUEST['dataid']; 83: else 84: { 85: $allow="abcdef0123456789"; 86: $allow_len=strlen($allow); 87: mt_srand(microtime()*1000000); 88: $id_len=10; 89: $dataid="fnd_"; 90: for ($a=0; $a<$id_len; $a++) 91: { 92: $dataid.=$allow[mt_rand(0,$allow_len-1)]; 93: } 94: } 95: lo("var ".$dataid."=new Array();"); 96: break; 97: 98: 99: } 100: ob_end_flush(); 101: 102: // Queries 103: $query_count=count($_REQUEST['query']); 104: for ($a=0; $a<$query_count; $a++) 105: { 106: switch($_REQUEST['query'][$a]) 107: { 108: case "nodelist": 109: $q="SELECT nodeid FROM fnnode"; 110: if ((!isset($_REQUEST['param'][$a])) || ($_REQUEST['param'][$a]!=1)) $q.=" WHERE nodeenabled=1"; 111: $q.=" ORDER BY weight ASC"; 112: $r=$NATS->DB->Query($q); 113: if ($mode=="js") 114: { 115: lo($dataid."[".$a."]=new Array();"); 116: } 117: else if ($mode=="xml") lo("DB->Num_Rows($r)."\" query=\"".$a."\">"); 118: $ctr=0; 119: while ($row=$NATS->DB->Fetch_Array($r)) 120: { 121: $nodealert=$NATS->NodeAlertLevel($row['nodeid']); 122: if ($mode=="xml") lo("".$row['nodeid'].""); 123: else if ($mode=="js") 124: { 125: lo($dataid."[".$a."][".$ctr."]=new Array();"); 126: lo($dataid."[".$a."][".$ctr."][0]='".$row['nodeid']."';"); 127: lo($dataid."[".$a."][".$ctr."][1]='".$nodealert."';"); 128: } 129: $ctr++; 130: } 131: if ($mode=="xml") lo(""); 132: $NATS->DB->Free($r); 133: break; 134: 135: 136: case "node": 137: $nodedata=$NATS->GetNode($_REQUEST['param'][$a]); 138: if ($nodedata) // got a valid response 139: { 140: if ($mode=="js") 141: { 142: lo($dataid."[".$a."]=new Array();"); 143: } 144: else if ($mode=="xml") lo(""); 145: $ctr=0; 146: foreach($nodedata as $key => $val) 147: { 148: if (!is_numeric($key)) // pesky double-arrays avoided 149: { 150: if ($mode=="xml") lo(" <".$key.">".$val.""); 151: else if ($mode=="js") 152: { 153: lo($dataid."[".$a."][".$ctr."]=new Array;"); 154: lo($dataid."[".$a."][".$ctr."][0]='".$key."';"); 155: lo($dataid."[".$a."][".$ctr."][1]='".$val."';"); 156: } 157: $ctr++; 158: } 159: } 160: if ($mode=="xml") lo(""); 161: } 162: break; 163: 164: case "group": 165: $groupdata=$NATS->GetGroup($_REQUEST['param'][$a]); 166: if ($groupdata) // got a valid response 167: { 168: if ($mode=="js") 169: { 170: lo($dataid."[".$a."]=new Array();"); 171: } 172: else if ($mode=="xml") lo(""); 173: $ctr=0; 174: foreach($groupdata as $key => $val) 175: { 176: if (!is_numeric($key)) // pesky double-arrays avoided 177: { 178: if ($mode=="xml") lo(" <".$key.">".$val.""); 179: else if ($mode=="js") 180: { 181: lo($dataid."[".$a."][".$ctr."]=new Array;"); 182: lo($dataid."[".$a."][".$ctr."][0]='".$key."';"); 183: lo($dataid."[".$a."][".$ctr."][1]='".$val."';"); 184: } 185: $ctr++; 186: } 187: } 188: if ($mode=="xml") lo(""); 189: } 190: break; 191: 192: 193: case "test": 194: $testdata=$NATS->GetTest($_REQUEST['param'][$a],true); 195: if ($testdata) // got a valid response 196: { 197: 198: if ( (isset($_REQUEST['param1'][$a])) && (isset($_REQUEST['param2'][$a])) ) 199: { // get data 200: $testdata['period.startx']=0; 201: $testdata['period.finishx']=0; 202: $testdata['period.tested']=0; 203: $testdata['period.passed']=0; 204: $testdata['period.warning']=0; 205: $testdata['period.failed']=0; 206: $testdata['period.untested']=0; 207: $testdata['period.average']=0; 208: 209: if (($testdata['testrecord']==1)||($testdata['testtype']=="ICMP")) 210: { 211: $sx=smartx($_REQUEST['param1'][$a]); 212: $fx=smartx($_REQUEST['param2'][$a]); 213: $testdata['period.startx']=$sx; 214: $testdata['period.finishx']=$fx; 215: 216: $q="SELECT alertlevel,COUNT(recordid) AS counter FROM fnrecord WHERE testid=\"".ss($testdata['testid'])."\" AND "; 217: $q.="recordx>=".ss($sx)." AND recordx<=".ss($fx)." GROUP BY alertlevel"; 218: //echo $q; 219: $r=$NATS->DB->Query($q); 220: while ($row=$NATS->DB->Fetch_Array($r)) 221: { 222: switch ($row['alertlevel']) 223: { 224: case -1: $testdata['period.untested']+=$row['counter']; 225: break; 226: case 0: $testdata['period.passed']+=$row['counter']; 227: break; 228: case 1: $testdata['period.warning']+=$row['counter']; 229: break; 230: case 2: $testdata['period.failed']+=$row['counter']; 231: break; 232: } 233: $testdata['period.tested']+=$row['counter']; 234: } 235: $NATS->DB->Free($r); 236: 237: $q="SELECT AVG(testvalue) FROM fnrecord WHERE testid=\"".ss($testdata['testid'])."\" AND "; 238: $q.="recordx>=".ss($sx)." AND recordx<=".ss($fx); //." AND alertlevel IN (0,1)"; // warnings and passes only 239: 240: $r=$NATS->DB->Query($q); 241: 242: if ($row=$NATS->DB->Fetch_Array($r)) 243: { 244: $testdata['period.average']=round($row['AVG(testvalue)'],4); 245: } 246: 247: $NATS->DB->Free($r); 248: 249: } 250: } 251: 252: 253: // header 254: 255: if ($mode=="js") 256: { 257: lo($dataid."[".$a."]=new Array();"); 258: lo($dataid."[".$a."][0]=new Array();"); // Keys 259: lo($dataid."[".$a."][1]=new Array();"); // Values 260: } 261: else if ($mode=="xml") lo(""); 262: $ctr=0; 263: foreach($testdata as $key => $val) 264: { 265: if (!is_numeric($key)) // pesky double-arrays avoided 266: { 267: if ($mode=="xml") lo(" <".$key.">".$val.""); 268: else if ($mode=="js") 269: { 270: lo($dataid."[".$a."][0][".$ctr."]='".$key."';"); 271: lo($dataid."[".$a."][1][".$ctr."]='".$val."';"); 272: } 273: $ctr++; 274: } 275: } 276: if ($mode=="xml") lo(""); 277: } 278: break; 279: 280: case "alerts": 281: $alerts=$NATS->GetAlerts(); 282: 283: $count=count($alerts); 284: if ($alerts===false) $count=0; // as showing a 1 in count otherwise 285: if ($mode=="xml") lo(""); 286: else if ($mode=="js") lo($dataid."[".$a."]=new Array();"); 287: 288: if ($alerts) // some were returned 289: { 290: // nodeid alertlevel 291: for ($alctr=0; $alctr<$count; $alctr++) 292: { 293: if ($mode=="xml") lo(" ".$alerts[$alctr]['nodeid'].""); 294: else if ($mode=="js") lo($dataid."[".$a."][".$alctr."]='".$alerts[$alctr]['nodeid']."';"); 295: } 296: } 297: 298: if ($mode=="xml") lo(""); 299: 300: break; 301: 302: 303: case "testdata": 304: // param = testid 305: // param1 = startx 306: // param2 = finishx 307: 308: $q="SELECT recordx,testvalue,alertlevel FROM fnrecord WHERE testid=\"".ss($_REQUEST['param'][$a])."\" AND "; 309: $sx=smartx($_REQUEST['param1'][$a]); 310: $fx=smartx($_REQUEST['param2'][$a]); 311: $q.="recordx>=".ss($sx)." AND recordx<=".ss($fx)." ORDER BY recordx ASC"; 312: 313: $r=$NATS->DB->Query($q); 314: $count=$NATS->DB->Num_Rows($r); 315: 316: if ($mode=="xml") lo(""); 317: else if ($mode=="js") 318: { 319: lo($dataid."[".$a."]=new Array();"); 320: } 321: 322: $ctr=0; 323: while ($row=$NATS->DB->Fetch_Array($r)) 324: { 325: if ($mode=="xml") 326: { 327: lo(" ".$row['testvalue'].""); 328: } 329: else 330: { 331: lo($dataid."[".$a."][".$ctr."]=new Array();"); 332: lo($dataid."[".$a."][".$ctr."][0]=".$row['recordx'].";"); 333: lo($dataid."[".$a."][".$ctr."][1]=".$row['testvalue'].";"); 334: lo($dataid."[".$a."][".$ctr."][2]=".$row['alertlevel'].";"); 335: } 336: $ctr++; 337: } 338: $NATS->DB->Free($r); 339: 340: if ($mode=="xml") lo(""); 341: 342: break; 343: 344: } 345: } 346: 347: // Footer and Finish 348: 349: if ($mode=="xml") lo(""); 350: else if ($mode=="js") 351: { 352: if(isset($_REQUEST['callback'])) 353: { 354: lo($_REQUEST['callback']."(".$dataid.");"); 355: } 356: } 357: 358: ?>