. ************************************************************************ */ //************************** //***** 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 .= "
".$label.":

".$info; if (isset($_SESSION["cachedqueries"][$GETVars['server']][$query]) && $_SESSION["cachedqueries"][$GETVars['server']][$query]["timestamp"] != '' ) { $ret .= "


Results shown on this page have been read from cache (Query time: ".strftime("%H:%M:%S", $_SESSION["cachedqueries"][$GETVars['server']][$query]["timestamp"]).")"; } $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 .= "
"; $ret .= "".$searchfieldxml.": "; $ret .= " "; $ret .= ""; $ret .= "

"; return $ret; } //******************************* //*** generate server list *** //******************************* function getServerlist() { global $configarray; global $from; global $GETVars; $ret = ""; $serverlist = $configarray["serverlist"]; $i = 0; $ret = ""; $ret .= ""; 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 .= ""; } return $ret."
ServernameDescriptionIP-AddressPort
".$servername."".$listdescription."".$listip."".$listport."
"; } //******************************* //*** 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 .= "
"; $out .= ""; $out .= ""; while(list($key, $val) = each($configarray["queryarray"][$GETVars['qq']])) { $rowi++; $out .= "\n"; } $out .= "
KeyValue
".ucfirst($key).""; if (!is_array($val)) { if ($key == "query") { $out .= ""; } else if ($key == "cache") { $selyes = ""; $selno = ""; if ($val == "yes") { $selyes = "SELECTED"; } else { $selno = "SELECTED"; } $out .= ""; } else { $out .= ""; } } else { if ($key == "header"){ $headerlabelstring = ''; $headernamestring = ''; if (is_array($val["column"])){ while(list($keyheader, $valheader) = each($val["column"])) { if ($headerlabelstring == '') { $headerlabelstring .= $valheader["label"]; $headernamestring .= $valheader["name"]; } else { $headerlabelstring .= ",".$valheader["label"]; $headernamestring .= ",".$valheader["name"]; } } } else { $headerlabelstring = $val["column"]; } $out .= ""; //$out .= "
"; //$out .= ""; } } if ($rowi == 1) { $rowi = -1; } $out .= "
"; $out .= ""; $out .= ""; $out .= ""; return $out; } //************************* //*** save edited query *** //************************* function saveQuery(){ global $configarray; global $GETVars; global $menukey; global $query; $queryarray = $configarray["queryarray"]; while(list($key, $val) = each($queryarray[$GETVars['qq']])) { if (!is_array($val)) { $queryarray[$GETVars['qq']][$key] = stripslashes($_POST[$key]); } else { $header = array(); if ($key == "header"){ $queryarray[$GETVars['qq']][$key]["column"] = split(",",$_POST[$key]); } } } $_SESSION["configarray"]["queryarray"][$GETVars['qq']] = $queryarray[$GETVars['qq']]; $_SESSION["configarray"]["menuarray"][$GETVars["menu"]][$menukey] = $queryarray[$GETVars['qq']]["label"]; $_SESSION["configarray"]["menuarray"][$GETVars["menu"]][$menukey] = $queryarray[$GETVars['qq']]["label"]; $configarray["menuarray"][$GETVars['menu']][$menukey] = $queryarray[$GETVars['qq']]["label"]; $query = $queryarray[$GETVars['qq']]["query"]; // TODO: Delete cached query in session } //******************************* //*** header for timetable *** //******************************* function generate_timetable_header($startpunkt = '', $FirstCol = '') { $header = $FirstCol["label"]; $out= "".$header.""; for ($count = 0; $count <= 24; $count++) { $imagename = strftime("%H", $startpunkt+($count*3600)); $out .= ""; } $out .= ""; return $out; } //******************************* //*** generate timetable *** //******************************* function generate_timetable($tablearray = '', $FirstCol = '') { global $searchfield; $now = time(); $out = ''; $height = 8; $faktor = 120; $oneday = 86400; $onehour = 3600; $tolerance = 1200; $startpunkt = ((ceil(time()/$onehour)*$onehour)-$onehour-$oneday)-(($searchfield-24)*$onehour); $endpunkt = $startpunkt + $oneday + $onehour; $lastpoint = ($endpunkt - $startpunkt)/$faktor; $out .= ""; $out .= generate_timetable_header($startpunkt, $FirstCol); $out .= ""; $lasttimepoint=$now-($searchfield*$onehour)-$tolerance; $repeatingcol = ""; $ii=1; while(list($keyrow, $valrow) = each($tablearray)) { if ($valrow[1] <= $endpunkt && $valrow[2] > $lasttimepoint){ $name = $valrow[0]; $status = $valrow[3]; $statusmsg = ""; $dur = strftime("%H:%M", ($valrow[2]-$valrow[1])-$onehour); $shade=""; if ($valrow[1] < $lasttimepoint) { // cut the bar at the left side to fit into table $start = 0; } else { $start = ($valrow[1]-$startpunkt)/$faktor; } $end = ($valrow[2]-$startpunkt)/$faktor; $duration = $end - $start; // fake a longer time for better visibility if ($duration < 2){$duration=2;} // cut the bar at the right side to fit into table if (($start+$duration)>$lastpoint) { $duration = $lastpoint-$start; $shade="light"; } if ($valrow[1] < $lasttimepoint) { $shade="light"; } if (isset($status)){ if ($status == "YES" || $status == "Completed") { $barcol = $shade."green"; $statusmsg = ", Status was OK"; }else{ $barcol = $shade."red"; $statusmsg = ", Status was UNSUCCESSFUL"; } } else { $barcol = $shade."grey"; $statusmsg = ""; } if($ii == 1) { $out .= ""; } else { $out .= ""; $ii = 0; } if ($repeatingcol != $valrow[0]){ $out .= ""; $repeatingcol = $valrow[0]; } else { $out .= ""; } if ($valrow[3] != 'Missed') { $out .= "\n"; $ii++; } } $out .= generate_timetable_header($startpunkt); $out .= "
".$valrow[0]."".$valrow[0].""; $out .= ""; $out .= ""; } else { $out .= ""; } $out .= "
"; return $out; } /** * Convert SimpleXMLElement object to array * Copyright Daniel FAIVRE 2005 - www.geomaticien.com * Copyleft GPL license */ function simplexml2array($xml) { if (get_class($xml) == 'SimpleXMLElement') { $attributes = $xml->attributes(); foreach($attributes as $k=>$v) { if ($v) $a[$k] = (string) $v; } $x = $xml; $xml = get_object_vars($xml); } if (is_array($xml)) { if (count($xml) == 0) return (string) $x; // for CDATA foreach($xml as $key=>$value) { $r[$key] = simplexml2array($value); } if (isset($a)) $r['@'] = $a; // Attributes return $r; } return (string) $xml; } function getConfigArray() { $configxml = simplexml_load_file("includes/config.xml"); $queryxml = simplexml_load_file("includes/queries.xml"); $serverxml = simplexml_load_file("includes/server.xml"); $configarray = simplexml2array($configxml); $queryconfigarray = simplexml2array($queryxml); $serverarray = simplexml2array($serverxml); $retArray = array(); //Navigation $menuarrayxml = $queryconfigarray["navigation"]["mainmenuitem"]; $mainmenuarrayxml = $menuarrayxml; $menuarray = array(); $mainmenuarray = array(); while(list($keymain, $submenu) = each($mainmenuarrayxml)) { $menuname = $submenu["name"]; $menulabel = $submenu["label"]; $url = "q=overview&m=".$menuname; $mainmenuarray[$url] = $menulabel; } $mainmenuarray["trennlinie"] = "trennlinie"; //$mainmenuarray["q=admin&m=main"] = "Admin"; $mainmenuarray["q=serverlist&m=main"] = "Change Server"; $mainmenuarray["q=logout"] = "Logout"; $menuarray["main"] = $mainmenuarray; while(list($key, $submenu) = each($menuarrayxml)) { $menuname = $submenu["name"]; $menulabel = $submenu["label"]; $submenuitems = $submenu["submenuitem"]; $submenuarray = array(); $submenuarray[""] = "<---"; while(list($subkey, $submenuitem) = each($submenuitems)) { $submenuitem_name = $submenuitem["name"]; $submenuitem_label = $submenuitem["label"]; if ($submenuitem["notforlibclient"] == "yes") { $submenuitem_label .= "_notforlibclient"; } $url = "q=".$submenuitem_name."&m=".$menuname; $submenuarray[$url] = $submenuitem_label; } $submenuarray["trennlinie"] = "trennlinie"; //$submenuarray["q=admin&m=".$submenu["name"]] = "Admin"; $submenuarray["q=serverlist&m=".$submenu["name"]] = "Change Server"; $submenuarray["q=logout"] = "Logout"; $menuarray[$menuname] = $submenuarray; } $retArray["menuarray"] = $menuarray; //Infoboxes $ret = array(); while(list($key, $submenu) = each($queryconfigarray["indexquery"]["infobox"])) { $ret[$submenu["name"]] = $submenu["infoboxitem"]; } $retArray["infoboxarray"] = $ret; //Queries $ret = array(); while(list($key, $submenu) = each($queryconfigarray["navigation"]["mainmenuitem"])) { $submenuitems = $submenu["submenuitem"]; while(list($subkey, $submenuitem) = each($submenuitems)) { $ret[$submenuitem["name"]] = $submenuitem; } } $retArray["queryarray"] = $ret; //Colors $ret = array(); while(list($key, $color) = each($configarray["cellcolors"]["color"])) { $ret[$color["name"]] = $color["shade"]; } $retArray["colorsarray"] = $ret; $retArray["timeout"] = $configarray["timeout"]; // Servers $serverarrayxml = $serverarray["serverarray"]["server"]; $retArray["defaultserver"] = $serverarray["serverarray"]["defaultserver"]; $ret = array(); if (isset($serverarrayxml[1]) && is_array($serverarrayxml[1])) { while(list($key, $submenu) = each($serverarrayxml)) { $ret[$submenu["servername"]] = $submenu; } } else { $ret[$serverarrayxml["servername"]] = $serverarrayxml; } $retArray["serverlist"] = $ret; return $retArray; } ?>