File:
0.04.19a/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 // IS PUBLIC
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:
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 "node":
109: $nodedata=$NATS->GetNode($_REQUEST['param'][$a]);
110: if ($nodedata) // got a valid response
111: {
112: if ($mode=="js")
113: {
114: lo($dataid."[".$a."]=new Array();");
115: //lo($dataid."[".$a."][0]=new Array();"); // Keys
116: //lo($dataid."[".$a."][1]=new Array();"); // Values
117: }
118: else if ($mode=="xml") lo("");
119: $ctr=0;
120: foreach($nodedata as $key => $val)
121: {
122: if (!is_numeric($key)) // pesky double-arrays avoided
123: {
124: if ($mode=="xml") lo(" <".$key.">".$val."".$key.">");
125: else if ($mode=="js")
126: {
127: lo($dataid."[".$a."][".$ctr."]=new Array;");
128: lo($dataid."[".$a."][".$ctr."][0]='".$key."';");
129: lo($dataid."[".$a."][".$ctr."][1]='".$val."';");
130: }
131: $ctr++;
132: }
133: }
134: if ($mode=="xml") lo("");
135: }
136: break;
137:
138:
139: case "test":
140: $testdata=$NATS->GetTest($_REQUEST['param'][$a]);
141: if ($testdata) // got a valid response
142: {
143:
144: if ( (isset($_REQUEST['param1'][$a])) && (isset($_REQUEST['param2'][$a])) )
145: { // get data
146: $testdata['period.startx']=0;
147: $testdata['period.finishx']=0;
148: $testdata['period.tested']=0;
149: $testdata['period.passed']=0;
150: $testdata['period.warning']=0;
151: $testdata['period.failed']=0;
152: $testdata['period.untested']=0;
153: $testdata['period.average']=0;
154:
155: if (($testdata['testrecord']==1)||($testdata['testtype']=="ICMP"))
156: {
157: $sx=smartx($_REQUEST['param1'][$a]);
158: $fx=smartx($_REQUEST['param2'][$a]);
159: $testdata['period.startx']=$sx;
160: $testdata['period.finishx']=$fx;
161:
162: $q="SELECT alertlevel,COUNT(recordid) AS counter FROM fnrecord WHERE testid=\"".ss($testdata['testid'])."\" AND ";
163: $q.="recordx>=".ss($sx)." AND recordx<=".ss($fx)." GROUP BY alertlevel";
164: //echo $q;
165: $r=$NATS->DB->Query($q);
166: while ($row=$NATS->DB->Fetch_Array($r))
167: {
168: switch ($row['alertlevel'])
169: {
170: case -1: $testdata['period.untested']+=$row['counter'];
171: break;
172: case 0: $testdata['period.passed']+=$row['counter'];
173: break;
174: case 1: $testdata['period.warning']+=$row['counter'];
175: break;
176: case 2: $testdata['period.failed']+=$row['counter'];
177: break;
178: }
179: $testdata['period.tested']+=$row['counter'];
180: }
181: $NATS->DB->Free($r);
182:
183: $q="SELECT AVG(testvalue) FROM fnrecord WHERE testid=\"".ss($testdata['testid'])."\" AND ";
184: $q.="recordx>=".ss($sx)." AND recordx<=".ss($fx); //." AND alertlevel IN (0,1)"; // warnings and passes only
185:
186: $r=$NATS->DB->Query($q);
187:
188: if ($row=$NATS->DB->Fetch_Array($r))
189: {
190: $testdata['period.average']=round($row['AVG(testvalue)'],4);
191: }
192:
193: $NATS->DB->Free($r);
194:
195: }
196: }
197:
198:
199: // header
200:
201: if ($mode=="js")
202: {
203: lo($dataid."[".$a."]=new Array();");
204: lo($dataid."[".$a."][0]=new Array();"); // Keys
205: lo($dataid."[".$a."][1]=new Array();"); // Values
206: }
207: else if ($mode=="xml") lo("");
208: $ctr=0;
209: foreach($testdata as $key => $val)
210: {
211: if (!is_numeric($key)) // pesky double-arrays avoided
212: {
213: if ($mode=="xml") lo(" <".$key.">".$val."".$key.">");
214: else if ($mode=="js")
215: {
216: lo($dataid."[".$a."][0][".$ctr."]='".$key."';");
217: lo($dataid."[".$a."][1][".$ctr."]='".$val."';");
218: }
219: $ctr++;
220: }
221: }
222: if ($mode=="xml") lo("");
223: }
224: break;
225:
226: case "alerts":
227: $alerts=$NATS->GetAlerts();
228:
229: $count=count($alerts);
230: if ($alerts===false) $count=0; // as showing a 1 in count otherwise
231: if ($mode=="xml") lo("");
232: else if ($mode=="js") lo($dataid."[".$a."]=new Array();");
233:
234: if ($alerts) // some were returned
235: {
236: // nodeid alertlevel
237: for ($alctr=0; $alctr<$count; $alctr++)
238: {
239: if ($mode=="xml") lo(" ".$alerts[$alctr]['nodeid']."");
240: else if ($mode=="js") lo($dataid."[".$a."][".$alctr."]='".$alerts[$alctr]['nodeid']."';");
241: }
242: }
243:
244: if ($mode=="xml") lo("");
245:
246: break;
247:
248: }
249: }
250:
251: // Footer and Finish
252:
253: if ($mode=="xml") lo("");
254: else if ($mode=="js")
255: {
256: if(isset($_REQUEST['callback']))
257: {
258: lo($_REQUEST['callback']."(".$dataid.");");
259: }
260: }
261:
262: ?>