2 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
3 // Copyright (C) 2010 Winch Gate Property Limited
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Affero General Public License as
7 // published by the Free Software Foundation, either version 3 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Affero General Public License for more details.
15 // You should have received a copy of the GNU Affero General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 global $HTTP_POST_VARS, $HTTP_GET_VARS;
20 include('authenticate.php');
23 include('display_view.php');
26 if (isset($reset_filters))
34 $HTTP_VARS = array_merge($HTTP_POST_VARS, $HTTP_GET_VARS);
42 if (isset($active_player) && $active_player != "")
44 $arr = explode(',', $active_player);
45 foreach($arr as $player)
47 $pl = explode(' ', $player);
48 nel_query("*.*.EGS.loadPlayer ".$pl[0], $dummyResult);
49 nel_query("*.*.EGS.activePlayer ".$pl[0]." ".$pl[1], $dummyResult);
53 if (isset($current_tid) && $current_tid != "")
56 $updatedShardAnnot = false;
58 // select table and display name
59 foreach ($HTTP_VARS as $var => $value)
61 if (strncmp($var, "selectTid_", 10) == 0)
62 $tid = (int)substr($var, 10);
63 else if (strncmp($var, "upd_annot_button_", 17) == 0)
65 $shard = substr($var, 17);
67 if ($shardLockState[$shard]['lock_state'] == 1)
69 $annot = $GLOBALS["upd_annot_text_".$shard];
71 $result = sqlquery("SELECT shard FROM shard_annotation WHERE shard='$shard'");
72 if (!$result ||
sqlnumrows($result) == 0)
73 sqlquery("INSERT INTO shard_annotation SET shard='$shard'");
75 sqlquery("UPDATE shard_annotation SET annotation='$annot', user='$uid', post_date=NOW() WHERE shard='$shard'");
77 $updatedShardAnnot = true;
80 else if (strncmp($var, "upd_lock_button_", 16) == 0)
82 $shard = substr($var, 16);
84 if ($shardLockState[$shard]['lock_state'] != 1)
86 $result = sqlquery("SELECT shard FROM shard_annotation WHERE shard='$shard'");
88 if (!$result ||
sqlnumrows($result) == 0)
89 sqlquery("INSERT INTO shard_annotation SET shard='$shard', annotation='locked by $admlogin', user='$uid', post_date=NOW()");
91 sqlquery("UPDATE shard_annotation SET lock_user='$uid', lock_ip='$REMOTE_ADDR', lock_date=NOW() WHERE shard='$shard'");
93 $updatedShardAnnot = true;
96 else if (strncmp($var, "upd_unlock_button_", 18) == 0)
98 $shard = substr($var, 18);
100 if ($shardLockState[$shard]['lock_state'] == 1)
102 $result = sqlquery("SELECT shard FROM shard_annotation WHERE shard='$shard'");
103 if (!$result ||
sqlnumrows($result) == 0)
104 sqlquery("INSERT INTO shard_annotation SET shard='$shard', annotation='locked by $admlogin', user='$uid', post_date=NOW()");
106 sqlquery("UPDATE shard_annotation SET lock_user='0' WHERE shard='$shard'");
108 $updatedShardAnnot = true;
113 if ($updatedShardAnnot)
119 if (isset($select_view) && $select_view != "")
120 $tname = explode(',', $select_view);
122 if (isset($tname) && count($tname)>0 && !isset($tid))
125 foreach($tname as $tbname)
127 $result = sqlquery("SELECT tid, name FROM view_table WHERE uid='$uid' AND name='$tbname'");
128 if ($result && ($arr=sqlfetch($result)))
136 $result = sqlquery("SELECT tid, name FROM view_table WHERE uid='$gid' AND name='$tbname'");
137 if ($result && ($arr=sqlfetch($result)))
154 $result = sqlquery("SELECT default_view FROM user, view_table WHERE user.uid='$uid' AND (view_table.uid='$uid' OR view_table.uid='$gid') AND view_table.tid=user.default_view");
155 if ($result && ($arr=sqlfetch($result)))
157 $tid=$arr["default_view"];
164 $result = sqlquery("SELECT tid, name FROM view_table WHERE uid='$uid' ORDER BY ordering LIMIT 1");
165 if ($result && ($arr=sqlfetch($result)))
168 $tname = $arr["name"];
172 $result = sqlquery("SELECT tid, name FROM view_table WHERE uid='$gid' ORDER BY ordering LIMIT 1");
173 if ($result && ($arr=sqlfetch($result)))
176 $tname = $arr["name"];
187 $result = sqlquery("SELECT name, refresh_rate FROM view_table WHERE tid='$tid'");
188 if ($result && ($result=sqlfetch($result)))
190 $tname = $result["name"];
191 $refreshRate = $result["refresh_rate"];
195 foreach ($HTTP_VARS as $var => $value)
197 if (strncmp($var, "select_", 7) == 0)
199 $sel_arr = explode(":", substr($var, 7));
201 if (!$keep_shard) $sel_arr[0] = '*';
202 if (!$keep_server) $sel_arr[1] = '*';
203 if (!$keep_service) $sel_arr[2] = '*';
205 $sel[] = $sel_arr[0].".".$sel_arr[1].".".$sel_arr[2].".*";
207 else if (strncmp($var, "execServCommand_", 16) == 0)
209 $execServCommand = substr($var, 16);
213 if (!isset($sel) ||
!is_array($sel))
214 $sel = array("*.*.*.*");
217 $selShards = array();
219 // add shard access to selection
220 if ($admlogin != "root" && !$IsNevrax)
222 if (count($shardAccess)>0)
226 foreach ($shardAccess as $sshard)
228 $shards[] = $sshard["shard"];
229 $filtershard = $sshard["shard"];
230 foreach ($unfiltered as $filter)
232 list($shard, $server, $service, $variable) = explode(".", $filter);
233 if ($shard == "*" ||
$shard == $filtershard)
235 $sel[] = "$filtershard.$server.$service.$variable";
248 $result = sqlquery("SELECT DISTINCT shard FROM service ORDER BY shard");
249 while ($result && ($arr=sqlfetch($result)))
251 $shards[] = $arr["shard"];
257 if (isset($execServCommand) && isset($factPaths) && isset($execServParams) && isset($variableData[$execServCommand]))
260 $cmd = $variableData[$execServCommand];
261 $path = $cmd["path"];
263 $paths = expandQuery($factPaths);
265 for ($i=0; $i<count($paths); ++
$i)
267 $fpath = filterPathUsingAliases($paths[$i], $path);
269 $newPaths[] = $fpath;
272 $fullPath = factorizeQuery("[".join($newPaths, ",")."]");
274 // filter selection with command
275 $fullCmd = $fullPath." ".$execServParams;
276 logUser($uid, "COMMAND=".$fullCmd);
277 $qstate = nel_query($fullPath." ".$execServParams, $cmdResult);
282 // display available user and group views
283 $result = sqlquery("SELECT view.name AS name, view.tid AS tid, view.uid AS gid, user.login AS owner FROM view_table AS view, user WHERE view.uid=user.uid AND (view.uid='$uid' OR view.uid='$gid') ORDER BY gid, ordering");
287 while ($arr = sqlfetch($result))
288 $ownerTables[$arr["owner"]][] = $arr;
291 htmlProlog($_SERVER['PHP_SELF'], "View Selection '$tname'", true);
294 $current_tname = $tname;
296 $use_refreshRate = (isset($form_refreshRate) && $form_refreshRate != 0 ?
$form_refreshRate : $refreshRate);
298 if ($use_refreshRate > 0)
300 echo "<script><!--\n";
301 echo " var sURL = unescape(window.location.pathname);\n";
302 echo " var pos = sURL.indexOf('.php');\n";
303 echo " function refresh() { window.location.replace( sURL ); }\n";
304 echo " if (pos >= 0) {\n";
305 echo " sURL = sURL.substr(0, pos+4)+'?current_tid=$tid&form_refreshRate=$form_refreshRate';\n";
306 echo " setTimeout(\"refresh()\", ".($use_refreshRate*1000).");\n";
308 echo "//--></script>\n";
312 echo "<table width=100%><form method=post action='".$_SERVER['PHP_SELF']."' name='viewForm'><tr valign=top>\n";
314 if (count($ownerTables)>0)
316 $clr = array("#E8E8E8", "#E0E0E0");
318 foreach ($ownerTables as $owner => $ownerViews)
323 foreach ($ownerViews as $arr)
328 echo "</table></td><td width=150 bgcolor=$bgcolor><table><tr><th> </th></tr>\n";
330 echo "<td width=150 bgcolor=$bgcolor><table><tr><th align=left>$owner views</th></tr>\n";
332 if ($tname == $arr["name"])
333 echo "<tr><td nowrap><input type=submit name='selectTid_".$arr["tid"]."' value='View'> <b>".$arr["name"]."</b></td></tr>";
335 echo "<tr><td nowrap><input type=submit name='selectTid_".$arr["tid"]."' value='View'> ".$arr["name"]."</td></tr>";
339 echo "</table></td>\n";
343 echo "<td width=40> </td>\n";
346 $use_filter_shard = $filter_shard;
347 $use_filter_server = $filter_server;
348 $use_filter_service = $filter_service;
349 $use_filter_entity = $filter_entity;
351 if (!isset($use_filter_shard) ||
$use_filter_shard == "") $use_filter_shard = "*";
352 if (!isset($use_filter_server) ||
$use_filter_server == "") $use_filter_server = "*";
353 if (!isset($use_filter_service) ||
$use_filter_service == "") $use_filter_service = "*";
354 if (!isset($use_filter_entity) ||
$use_filter_entity == "") $use_filter_entity = "*";
356 $view_filter = $use_filter_shard.".".$use_filter_server.".".$use_filter_service.".".$use_filter_entity;
358 $selAllShards = false;
359 for ($i=0; $i<count($sel); ++
$i)
361 $sel[$i] = partialFilterPath($sel[$i], $view_filter);
363 $expSel = explode('.', $sel[$i]);
364 if ($expSel[0] == '*')
365 $selAllShards = true;
366 $selShards[$expSel[0]] = true;
369 echo "<td nowrap bgcolor=#E0E0E0>\n";
370 if (count($shards) > 0)
372 echo "<b>Shard shortcuts</b><table><tr valign=top><td>\n";
375 echo "<tr><td><a href='".$_SERVER['PHP_SELF']."?current_tid=$tid&form_refreshRate=$form_refreshRate&filter_shard=$link_shard&filter_server=&filter_service='><i>All</i></a></td></tr>";
376 foreach ($shards as $link_shard)
378 if ($i%5
== 0 && $i>0)
379 echo "</table></td><td><table>\n";
381 echo ($selShards[$link_shard]) ?
'<td nowrap bgcolor=#C0C0C0><b>' : '<td nowrap>';
382 $alias = $shardLockState[$link_shard]['alias'];
384 $alias = $link_shard;
386 $alias .= "($link_shard)";
387 echo "<a href='".$_SERVER['PHP_SELF']."?current_tid=$tid&form_refreshRate=$form_refreshRate&filter_shard=$link_shard&filter_server=&filter_service='>$alias</a>";
388 if ($selShards[$link_shard])
394 echo "</td></tr></table>\n";
398 echo "<td width=100% align=right>\n";
399 echo "<table><tr><th>Refresh rate</th></tr>\n";
400 echo "<tr><td><select name=form_refreshRate onChange='submit()'>\n";
401 echo "<option value='0'".(!isset($form_refreshRate) ||
$form_refreshRate==0 ?
" selected" : "").">View setting (".($refreshRate>0 ?
"$refreshRate seconds" : "no refresh").")\n";
402 echo "<option value='-1'".($form_refreshRate==-1 ?
" selected" : "").">No refresh\n";
403 echo "<option value='30'".($form_refreshRate==30 ?
" selected" : "").">30 seconds\n";
404 echo "<option value='60'".($form_refreshRate==60 ?
" selected" : "").">60 seconds\n";
405 echo "<option value='120'".($form_refreshRate==120 ?
" selected" : "").">2 minutes\n";
406 echo "<option value='300'".($form_refreshRate==300 ?
" selected" : "").">5 minutes\n";
407 echo "</select></td></tr></table>\n";
410 echo "<td align=right><table><tr><th width=300>Selection</th></tr>\n";
413 echo "<tr><td>$s</td></tr>\n";
414 echo "</table></td>\n";
416 echo "</tr></table><br>Content of view <font size=3><b>$tname</b></font><br><br>\n";
418 echo "<table><tr><td>\n";
420 $queryErrors = array();
428 foreach ($HTTP_VARS as $var => $value)
429 if (strncmp($var, "override_", 9) == 0)
430 $overriden[substr($var, 9)]=$value;
432 $shardAccessForbidden = array();
434 foreach ($HTTP_VARS as $var => $value)
436 if (strncmp($var, "updvar_", 7) == 0)
438 $vv = substr($var, 7);
439 $prevValue = $GLOBALS["prevvar_$vv"];
441 $var_split = explode("|", $vv);
442 $shard = $var_split[0];
443 $var_split = $var_split[count($var_split)-1];
444 $override = $overriden[$var_split];
446 if ($shardLockState[$shard]['lock_state'] != 1)
448 if (!$shardAccessForbidden[$shard])
450 $shardAccessForbidden[$shard] = true;
451 $queryErrors[] = "You can't update values on shard '$shard', access is not locked for you";
456 if ($value != $prevValue)
458 $address = str_replace("|", ".", $vv);
459 $update[] = "$address=$value";
461 else if (isset($override) && $override != "" && $override != $prevValue)
463 $address = str_replace("|", ".", $vv);
464 $update[] = "$address=$override";
467 else if (strncmp($var, "current_select_", 15) == 0)
473 $sel = array_unique($sel);
475 if (isset($update) && count($update > 0))
477 if (count($update) > 1)
479 $query = "[".join(",", $update)."]";
480 $query = factorizeQuery($query);
485 $executeQuery = $query;
488 logUser($uid, "UPDATE=".$executeQuery);
489 $qstate = nel_query($executeQuery, $updateResult);
492 list($usec, $sec) = explode(" ", $bef);
493 $bef = ((float)$sec +
(float)$usec);
494 list($usec, $sec) = explode(" ", $aft);
495 $aft = ((float)$sec +
(float)$usec);
496 $tm = (int)(($aft-$bef)*1000.0);
498 $queryResult = "Executed $executeQuery<br>$tm milliseconds computation time<br>\n";
501 else if (isset($executeQuery))
504 $qstate = nel_query($executeQuery, $updateResult);
507 list($usec, $sec) = explode(" ", $bef);
508 $bef = ((float)$sec +
(float)$usec);
509 list($usec, $sec) = explode(" ", $aft);
510 $aft = ((float)$sec +
(float)$usec);
511 $tm = (int)(($aft-$bef)*1000.0);
513 $queryResult = "Executed $executeQuery<br>$tm milliseconds computation time<br>\n";
518 buildVariableEnv($uid, $gid, $tid, $vardisp, $bounds, $privilege, $tree, $condensed, $autoDisplay);
519 displayResult($updateResult, $vardisp, $bounds, $privilege);
523 // send select request
525 $sel = array_unique($sel);
527 if (count($sel) == 1 && $sel[0] == "")
530 $queryResult = displayViewTable($uid, $gid, $tid, $sel);
533 echo "<input type=hidden name='current_tid' value='$tid'>\n";
536 foreach ($sel as $selec)
537 echo "<input type=hidden name='current_select_".($i++
)."' value='$selec'>\n";
539 if (isset($tid) && $tid != "")
541 $result = sqlquery("SELECT view_row.name AS name, variable.vid AS vid FROM view_row, variable WHERE tid='$tid' AND variable.command='command' AND view_row.vid=variable.vid ORDER BY ordering");
542 if ($result && sqlnumrows($result) > 0)
544 echo "<br><br><b>Service commands</b> <font size=1>The commands are sent to all services seen in the view above</font><br>\n";
545 echo "Command parameters <input name=execServParams size=64 maxlength=128><br>\n";
547 echo "<tr valign=top>\n";
548 echo "<td><table>\n";
551 while ($result && ($arr=sqlfetch($result)))
553 if ($numInCol >= 5 ||
$arr["name"] == 'SEPARATOR')
555 echo "</table></td><td width=30></td><td><table>\n";
558 if ($arr["name"] != 'SEPARATOR')
560 echo "<tr><td><input type=submit name=execServCommand_".$arr["vid"]." value='".$arr["name"]."'></td></tr>\n";
565 if (isset($listPath) && count($listPath) > 0)
567 $address = "[".join(",", $listPath)."]";
568 $address = factorizeQuery($address);
570 echo "<input type=hidden name=factPaths value='$address'>\n";
573 echo "</table></td>\n";
574 echo "</tr></table>\n";
576 if (isset($cmdResult))
578 echo "<br>Result of command '$fullPath $execServParams':<br>\n";
579 echo "<table border=1><tr><td>\n";
580 echo "<pre>$cmdResult</pre>\n";
581 echo "</td></tr></table>\n";
586 if (count($queryErrors) > 0)
588 echo "<br><font size=3><b>Execution errors:</b></font>\n";
589 foreach ($queryErrors as $error)
590 echo "<br><font size=3 color=#FF0000><b>$error</b></font>\n";
592 echo "<br><br><font size=0>$queryResult</font>\n";
593 echo "</td></form></tr></table>\n";
595 function lvcmp($a, $b)
597 return ($a["path"] == $b["path"] ?
0 : $a["path"] < $b["path"] ?
-1 : 1);
600 if (isset($tid) && $tid != "" && count($listPath) > 0)
602 $result = sqlquery("SELECT view_row.name AS name, variable.vid, variable.path, warning_bound, error_bound, alarm_order FROM view_row, variable WHERE tid='$tid' AND variable.vid=view_row.vid AND graph!='0' ORDER BY name");
604 if ($result && sqlnumrows($result) > 0)
608 while ($result && ($arr=sqlfetch($result)))
610 $gname = $arr["name"];
612 $gfilter = $arr["path"];
614 $gwarn = $arr["warning_bound"];
615 $gerr = $arr["error_bound"];
616 $gord = $arr["alarm_order"];
618 for ($i=0; $i<count($listPath); ++
$i)
620 $path = $listPath[$i].".*";
621 $varpath = filterPathUsingAliases($path, $gfilter);
622 $rrdpath = $rrdrootpath."/".$varpath.".rrd";
623 if ($varpath != "" && file_exists($rrdpath))
624 $listVars[] = array("path" => $varpath, "name" => $gname, "warn" => $gwarn, "err" => $gerr, "order" => $gord);
628 if (count($listVars) > 0)
630 usort($listVars, "lvcmp");
632 echo "<br><br><b>View Graphs</b><br><br>\n";
633 echo "<table><tr valign=top>\n";
634 echo "<td><table>\n";
638 // remove too old picture files
639 // opendir for scanning gif files
640 if ($dir = @opendir
($gifoutputpath))
644 while($file = readdir($dir))
646 if (substr($file, 0, 7) == "tmppic_")
648 $fstats = stat("$gifoutputpath/$file");
649 if ($fstats[9] < $curtime - $gifpersistence)
650 $eraseFiles[] = "$gifoutputpath/$file";
656 if (count($eraseFiles) > 0)
657 foreach ($eraseFiles as $file)
662 mt_srand((float) microtime()*1000000);
663 $randFilename = "tmppic_".mt_rand(10000, 99999);
665 foreach ($listVars as $var)
667 $rrdvar = $var["path"];
668 $rrdpath = $rrdrootpath."/".$rrdvar.".rrd";
669 $rrdname = $var["name"];
670 $rrdwarn = $var["warn"];
671 $rrderr = $var["err"];
672 $rrdord = strtoupper($var["order"]);
674 echo "<tr><th>$rrdvar <font size=0>(using path $rrdpath)</font></th></tr>\n";
676 // generate a temp filename
678 $tempFilename_0 = "$gifhttplocation/".$randFilename."_".$counter."_0.gif";
679 $tempFilename_1 = "$gifhttplocation/".$randFilename."_".$counter."_1.gif";
680 $tempFilename_2 = "$gifhttplocation/".$randFilename."_".$counter."_2.gif";
682 $tempFilenameout_0 = "$gifoutputpath/".$randFilename."_".$counter."_0.gif";
683 $tempFilenameout_1 = "$gifoutputpath/".$randFilename."_".$counter."_1.gif";
684 $tempFilenameout_2 = "$gifoutputpath/".$randFilename."_".$counter."_2.gif";
688 // generate picture according to the temp filename
692 $rrdDEF = "DEF:val=$rrdpath:var:AVERAGE";
697 $rrdDEF .= " CDEF:warn=val,$rrdwarn,$rrdord,val,0,IF";
698 $rrdDraw .= "AREA:warn#FFCC88 ";
703 $rrdDEF .= " CDEF:err=val,$rrderr,$rrdord,val,0,IF";
704 $rrdDraw .= "AREA:err#FF4422 ";
707 $rrdDraw .= "LINE2:val#0000FF";
709 $execStr = "rrdtool graph $tempFilenameout_0 --start -1200 $rrdDEF $rrdDraw";
710 //echo "exec(\"$execStr\")<br>\n";
711 exec($execStr, $result, $retcode1);
712 // echo "<tr><td><img src='$tempFilename_0'></td></tr>";
714 $execStr = "rrdtool graph $tempFilenameout_1 --start -10800 $rrdDEF $rrdDraw";
715 //echo "exec(\"$execStr\")<br>\n";
716 exec($execStr, $result, $retcode2);
717 // echo "<tr><td><img src='$tempFilename_1'></td></tr>";
719 $execStr = "rrdtool graph $tempFilenameout_2 --start -86400 $rrdDEF $rrdDraw";
720 //echo "exec(\"$execStr\")<br>\n";
721 exec($execStr, $result, $retcode3);
722 // echo "<tr><td><img src='$tempFilename_2'></td></tr>";
723 echo "<tr><td><img src='$tempFilename_0'><img src='$tempFilename_1'><img src='$tempFilename_2'></td></tr>";
725 echo "<tr height=10><td colspan=1></td></tr>";
727 if ($retcode1 ||
$retcode2 ||
$retcode3)
729 echo "<b>RRDTool output:</b><br>\n";
735 echo "</table></td>\n";
736 echo "</tr></table>\n";
743 //print_r($shardLockState);