.
************************************************************************
*/
//**************************
//***** initialize *********
//**************************
function initialize() {
global $configarray;
global $queryarray;
global $GETVars;
global $bLoggedIn;
global $bLoginOK;
global $from;
global $adminmenu;
global $submenu;
global $menukey;
global $printURL;
global $query;
$bCheckLogin;
session_name("tsmmonitor");
session_start();
if (!isset($_SESSION) || !isset($_SESSION['configarray'])) {
$_SESSION['configarray']= getConfigArray();
}
// Login if not logged in and credentials are ok
if ((isset($_POST) && isset($_POST["loginname"]) && isset($_POST["loginpasswort"]) && (!isset($_SESSION["logindata"])))){
$_SESSION["logindata"]["user"] = $_POST["loginname"];
$_SESSION["logindata"]["pass"] = $_POST["loginpasswort"];
$_SESSION["logindata"]["loggedin"] = TRUE;
$bCheckLogin = TRUE;
}
// GET-variables
$GETVars["menu"] = $_GET['m'];
$GETVars["qq"] = $_GET['q'];
$GETVars['ob'] = $_GET['sort'];
$GETVars['print'] = $_GET['print'];
$GETVars['orderdir'] = $_GET['so'];
if ($_POST['s'] != ''){
$GETVars['server'] = $_POST['s'];
} else {
$GETVars['server'] = $_GET['s'];
}
// Session-variables
$from = $_SESSION['from'];
$configarray = $_SESSION['configarray'];
// get configarray items
$adminmenu = $configarray["adminmenu"];
$timeout = $configarray["timeout"];
// timeout
if( !ini_get('safe_mode') && ini_get('max_execution_time')!=$timeout){
ini_set('max_execution_time', $timeout);
}
// set defaults if vars are empty
if ($GETVars["menu"] == "") { $GETVars["menu"]="main"; }
if ($GETVars["qq"] == ""){ $GETVars["qq"]="index"; }
if ($GETVars['server'] == "") { $GETVars['server']=$configarray["defaultserver"]; }
if ($GETVars['orderdir'] == "") { $GETVars['orderdir'] = "asc"; }
if ($GETVars['print'] == "") { $GETVars['print'] = FALSE; } else if ($GETVars['print'] == "true") { $GETVars['print'] = TRUE; }
// Check if logged in
if ($GETVars["qq"] != "logout" && isset($_SESSION["logindata"]["loggedin"])) {
$bLoggedIn = $_SESSION["logindata"]["loggedin"];
}
if ($_SESSION['timeshift'] == '' || !strstr($GETVars["qq"], 'dynamictimetable')){
$_SESSION['timeshift'] = 0 ;
}
$submenu = $configarray["menuarray"][$GETVars['menu']];
$menukey = "q=".$GETVars['qq']."&m=".$GETVars["menu"];
$query = $configarray["queryarray"][$GETVars['qq']]["query"];
$orderby = $configarray["queryarray"][$GETVars['qq']]["orderby"];
if (isset($GETVars['ob']) && $GETVars['ob']!=""){
$query = $query." order by ".$GETVars['ob']." ".$GETVars['orderdir'];
} else if (isset($orderby) && $orderby!="") {
$query = $query." order by ".$orderby." ".$GETVars['orderdir'];
}
$bLoginOK = TRUE;
if ($bCheckLogin){
$bLoginOK = checkLogin();
$bLoggedIn = $bLoginOK;
}
$printURL = "index.php?q=".$GETVars['qq']."&m=".$GETVars['menu']."&s=".$GETVars['server']."&print=TRUE";
$queryarray = $configarray["queryarray"];
}
//********************
//***** menu *********
//********************
function get_menu($menu = '', $activelink = '') {
global $GETVars;
global $configarray;
if (!isset($menu)) { return ""; };
while(list($key, $val) = each($menu)) {
$bCont = TRUE;
if (strstr($val, '_notforlibclient')){
if ($configarray["serverlist"][$GETVars['server']]["libraryclient"] == "yes"){
$bCont = FALSE;
}
//$val=preg_replace('/[_notforlibclient]/', '', $val);
$val=str_replace("_notforlibclient","",$val);
}
$key = "index.php?".$key."&s=".$GETVars['server'];
if ($val == "trennlinie") {
$links .= "
\n";
} else if ($bCont){
if ($key != $activelink && $key != $activelink) {
$links .= "$val\n";
} else {
$links .= "
$val
\n";
}
}
}
return $links;
}
//********************
//***** info *********
//********************
function get_info() {
global $configarray;
global $GETVars;
global $query;
$ret = "";
$label = $configarray["queryarray"][$GETVars['qq']]["label"];
$info = $configarray["queryarray"][$GETVars['qq']]["info"];
if ($info != ""){
$ret .= "";
return $ret;
}
}
//********************
//*** table header ***
//********************
function get_tableheader($headerarray = '') {
$tableheader="";
global $GETVars;
global $orderby;
global $searchfield;
$orderdir = $GETVars['orderdir'];
if ($orderdir == "asc"){
$sonew="desc";
} else if ($orderdir == "desc") {
$sonew="asc";
}
// If table has more than one column
if (is_array($headerarray) && is_array($headerarray[0])) {
foreach ($headerarray as $col) {
$label = $col["label"];
$name = $col["name"];
$arrow = "";
if (($GETVars['ob'] == $name && $GETVars['ob']!="") || ($GETVars['ob']=="" && $orderby!="" && $orderby == $name)){
$link = "href='index.php?q=".$GETVars['qq']."&m=".$GETVars['menu']."&sort=".$name."&sf=".$searchfield."&so=".$sonew."&s=".$GETVars['server']."'";
if ($orderdir == "asc") {
$arrow = "⇑";
} else if ($orderdir == "desc") {
$arrow = "⇓";
}
} else {
$arrow = "";
$link = "href='index.php?q=".$GETVars['qq']."&m=".$GETVars['menu']."&sort=".$name."&sf=".$searchfield."&s=".$GETVars['server']."'";
}
$tableheader = $tableheader."".$label." ".$arrow." | ";
}
} else {
if ($orderdir == "asc") {
$arrow = "⇑";
} else if ($orderdir == "desc") {
$arrow = "⇓";
}
$link = "href='index.php?q=".$GETVars['qq']."&m=".$GETVars['menu']."&sort=".$name."&sf=".$searchfield."&so=".$sonew."'";
$label = $headerarray["label"];
$tableheader = $tableheader."".$label." ".$arrow." | ";
}
$tableheader=$tableheader."
";
return $tableheader;
}
//***************************
//***** check login *******
//***************************
function checkLogin() {
global $configarray;
global $GETVars;
$user = $_SESSION["logindata"]["user"];
$pass = $_SESSION["logindata"]["pass"];
$server = $GETVars['server'];
$ip = $configarray["serverlist"][$server]["ip"];
$port = $configarray["serverlist"][$server]["port"];
$ret = TRUE;
$mustnotbenull="";
$handle = popen("dsmadmc -se=$server -id=$user -password=$pass -TCPServeraddress=$ip -COMMMethod=TCPIP -TCPPort=$port", 'r');
if ($handle) {
while (!feof($handle)) {
$read = fgets($handle, 4096);
$mustnotbenull .= $read;
if (strstr($read, 'ANS8034E') || strstr($read, 'ANS8023E')) {
$ret = FALSE;
}
}
} else {
echo "no handle!";
}
pclose($handle);
if ($mustnotbenull == "") {
echo "Cannot execute/find dsmadmc. Check if it's in PATH and/or that permissions have been set correctly (Linux/Unix: chmod ugo+x dsmadmc)!";
$ret = FALSE;
}
return $ret;
}
//***************************
//***** check for alert ****
//***************************
function checkAlert($comperator = '', $alertval = '', $val = '') {
$error = false;
if (substr($val, -1) == "*") {
$val = substr($val,0,-1);
}
if ($comperator == "equal"){
if ($val == $alertval ){
$error=true;
}
} else if ($comperator == "notequal"){
if ($val != $alertval ){
$error=true;
}
} else if ($comperator == "less") {
if ($val < $alertval ){
$error=true;
}
} else if ($comperator == "more") {
if ($val > $alertval ){
$error=true;
}
}
return $error;
}
//***************************
//***** execute ***********
//***************************
function execute($query = '', $overview = 'no', $type = 'table', $cache = 'no') {
global $configarray;
global $GETVars;
$server = $GETVars['server'];
$user = $_SESSION["logindata"]["user"];
$pass = $_SESSION["logindata"]["pass"];
$ip = $configarray["serverlist"][$server]["ip"];
$port = $configarray["serverlist"][$server]["port"];
global $searchfield;
//global $queryarray;
$colorsarray = $configarray["colorsarray"];
global $orderdir;
$alerting = $configarray["queryarray"][$GETVars['qq']]["alerting"];
$outp = '';
$outp_cache = '';
$stop=FALSE;
$tablearray = array();
$originalquery = $query;
$query = ereg_replace("NOTEQUAL","<>",$query);
$query = ereg_replace("LESS","<",$query);
if (isset($searchfield) && $searchfield != ""){
$query = ereg_replace("SEARCHFIELD","$searchfield",$query);
}
if ($cache=='yes' && isset($_SESSION["cachedqueries"]) && isset($_SESSION["cachedqueries"][$server][$originalquery]) && $_SESSION["cachedqueries"][$server][$originalquery]["timestamp"] != ''){
$outp = $_SESSION["cachedqueries"][$server][$originalquery]["query"];
} else {
$handle = popen("dsmadmc -se=$server -id=$user -password=$pass -TCPServeraddress=$ip -COMMMethod=TCPIP -TCPPort=$port -dataonly=yes -TAB \"$query\" ", 'r');
if ($handle) {
if ($type == "table") {
$i=1;
while (!feof($handle) && !$stop) {
$read = fgets($handle, 4096);
$stop = strstr($read, 'ANR2034E');
if ($read != ' ' && $read != '' && !$stop) {
$read=preg_replace('/[\n]+/', '', $read);
if ($overview == 'no') {
$color = "";
$cols = split("\t", $read);
$col = $cols[$alerting["alert_field"]];
$error = checkAlert($alerting["alert_comp"], $alerting["alert_val"], $col);
if($error) {
$color = $alerting["alert_col"];
} else {
$color = $alerting["ok_col"];
}
$colorzebra = $colorsarray[$color][$i];
if ($i % 2 == 0) {
$outp = $outp."";
}else{
$outp = $outp."
";
}
$i++;
for ($co = 0; $co < count($cols); $co++) {
if($color!="" && $alerting["alert_field"]==$co) {
if ($i % 2 == 0) {
$cellcol = $colorsarray[$color][1];
} else {
$cellcol = $colorsarray[$color][0];
}
$outp = $outp."".$cols[$co]." | ";
} else {
$outp = $outp."".$cols[$co]." | ";
}
}
$outp = $outp."
\n";
$outp_cache = $outp;
} else if ($overview == 'yes'){
$read=ereg_replace("\t"," - ",$read);
$outp = $outp.$read;
if ($cache == 'yes') {
$outp_cache = $outp."*";
}
}
}
}
}
else if ($type == "verticaltable") {
while (!feof($handle) && !$stop) {
$read = fgets($handle, 4096);
$stop = strstr($read, 'ANR2034E');
if ($read != ' ' && $read != '' && !$stop) {
$read = preg_replace('/[\n]+/', '', $read);
$outp = split("\t",$read);
$stop = true;
}
}
}
else if ($type == "timetable") {
$outp = array();;
while (!feof($handle) && !$stop) {
$read = fgets($handle, 4096);
$stop = strstr($read, 'ANR2034E');
if ($read != ' ' && $read != '' && !$stop) {
$read=preg_replace('/[\n]+/', '', $read);
$rowarray = explode("\t",$read);
$rowarray2 = array();
while(list($keycell, $valcell) = each($rowarray)) {
if ($keycell == 0 || $keycell == 3) {
$rowarray2[$keycell] = $valcell;
} else {
$date = $rowarray[$keycell];
$rowarray2[$keycell] = mktime(substr($date,11,2),substr($date,14,2),substr($date,17,2),substr($date,5,2),substr($date,8,2),substr($date,0,4));
}
}
array_push($outp, $rowarray2);
}
}
}
if ($cache == 'yes') {
$_SESSION["cachedqueries"][$server][$originalquery]["query"] = $outp_cache;
$_SESSION["cachedqueries"][$server][$originalquery]["timestamp"] = time();
}
}
pclose($handle);
}
return $outp;
}
//*******************************
//*** generate server list ***
//*******************************
function getSearchfield() {
global $GETVars;
global $searchfieldxml;
$ret = "";
$link = $_SERVER['PHP_SELF']."?q=".$GETVars['qq']."&m=".$GETVars['menu']."&s=".$GETVars['server'];
$ret .= "
";
return $ret;
}
//*******************************
//*** generate server list ***
//*******************************
function getServerlist() {
global $configarray;
global $from;
global $GETVars;
$ret = "";
$serverlist = $configarray["serverlist"];
$i = 0;
$ret = "";
$ret .= "Servername | Description | IP-Address | Port |
";
while(list($servername,$serveritems) = each($serverlist)) {
$listip = $serveritems["ip"];
$listdescription = $serveritems["description"];
$listport = $serveritems["port"];
if ($i == 0) {
$ret .= "";
$i = 1;
} else {
$ret .= "
";
$i = 0;
}
$listlink = $_SERVER['PHP_SELF']."?q=".$from."&m=".$GETVars['menu']."&s=".$servername;
$ret .= "".$servername." | ".$listdescription." | ".$listip." | ".$listport." |
";
}
return $ret."
";
}
//*******************************
//*** generate overview table ***
//*******************************
function get_overview_rows($subindexqueryarray = '') {
global $GETVars;
global $configarray;
$out="";
$i=0;
while(list($key, $val) = each($subindexqueryarray)) {
$bgcol="";
$comperator = "";
$alertval = "";
$alertcol = "";
$cellcolors = $configarray["colorsarray"];
$cache = $subindexqueryarray[$key]["cache"];
if ($configarray["serverlist"][$GETVars['server']]["libraryclient"] == "yes" && $subindexqueryarray[$key]["notforlibclient"] == "yes") {
$res = "-";
} else {
$res = execute($subindexqueryarray[$key]["query"], 'yes', 'table', $cache);
}
if ($i == 1){
$out = $out."";
$i=0;
}else{
$out = $out." |
";
$i=1;
}
$out .= $subindexqueryarray[$key]["header"];
$comperator = $subindexqueryarray[$key]["alert_comp"];
$alertval = $subindexqueryarray[$key]["alert_val"];
$alertcol = $subindexqueryarray[$key]["alert_col"];
$unit = $subindexqueryarray[$key]["unit"];
$error = checkAlert($comperator, $alertval, $res);
if ($error && $res != "-"){
$bgcol="bgcolor='".$cellcolors[$alertcol][$i]."'";
} else {
$bgcol="bgcolor='".$cellcolors["green"][$i]."'";
}
$out .= " | ".$res." ".$unit." |
\n";
}
return $out;
}
//**********************************
//*** show edit page for queries ***
//**********************************
function showEdit(){
global $configarray;
global $GETVars;
$out = "";
$rowi=-1;
$out .= "