<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html 
xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Supervision Webservices OPAC 3D</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" href="../admin/css/styles.css" />
    <link rel="stylesheet" type="text/css" href="css/styles.css" />
    <link rel="shortcut icon" type="image/x-icon" href="css/images/favicon.ico" />
    <script type="text/javascript" src="js/index.js"></script>
  </head>
  <body>
      <h1 style="margin-bottom:10px;">Supervision Webservices OPAC 3D</h1>
	  <?php
            require_once("class/Client.class.php");
            require_once("class/Supervision.class.php");
            date_default_timezone_set('UTC');
            $today = date("d/m/Y");
            $today_tmp = date("YmdHis");
            $searchClient = false;

            if ($_POST['tri']) $tri = $_POST['tri'];
            if ($_GET['tri']) $tri = $_GET['tri'];
            if (isset($_GET['searchClient']) && $_GET['searchClient'] != '') $searchClient = $_GET['searchClient'];  
            if ($tri=="client_desc") $req_tri = " ORDER BY client DESC";
            elseif ($tri=="client_asc") $req_tri = " ORDER BY client ASC";
            elseif ($tri=="url_desc") $req_tri = " ORDER BY url_opac3d DESC";
            elseif ($tri=="url_asc") $req_tri = " ORDER BY url_opac3d ASC";
            elseif ($tri=="last_complet_desc") $req_tri = " ORDER BY last_complet DESC";
            elseif ($tri=="last_complet_asc") $req_tri = " ORDER BY last_complet ASC";
            elseif ($tri=="last_partiel_desc") $req_tri = " ORDER BY last_partiel DESC";
            elseif ($tri=="last_partiel_asc") $req_tri = " ORDER BY last_partiel ASC";
            elseif ($tri=="erreur_agent_desc") $req_tri = " ORDER BY erreur_agent DESC";
            elseif ($tri=="erreur_agent_asc") $req_tri = " ORDER BY erreur_agent ASC";
            elseif ($tri=="erreur_config_desc") $req_tri = " ORDER BY erreur_config DESC";
            elseif ($tri=="erreur_config_asc") $req_tri = " ORDER BY erreur_config ASC";
            elseif ($tri=="infos_monitoring_desc") $req_tri = " ORDER BY infos_monitoring DESC";
            elseif ($tri=="infos_monitoring_asc") $req_tri = " ORDER BY infos_monitoring ASC";
            else $req_tri = " ORDER BY erreur_agent DESC";

            $supervision = new Supervision();
            if ($supervision->configFile) {
                if ($_GET['action'] == "check") {
                    $client = $_POST['client'];
                    $url_testconfig = $_POST['url_testconfig'];
                    $url_monitoring = $_POST['url_monitoring'];
                    $url_infosclass = $_POST['url_infosclass'];

                    echo "<p class=\"infos\">client : $client<br />";
                    echo "url_testconfig : $url_testconfig<br />";
                    if ($supervision->connectBddOpac()) {
                        $sql = "SELECT * FROM institution LEFT JOIN institution_status ON institution.code=institution_status.institution WHERE code = '$client'";
                        echo "sql : $sql<br />";
                        $result = mysql_query($sql);
                        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                                $last_complet = $row['last_update_complete'];
                                $last_partiel = $row['last_update_partial'];
                        }

                        $erreur_agent = $supervision->check_url($url_testconfig);
                        $erreur_config = $supervision->check_config($url_testconfig);
                        $infos_monitoring = $supervision->check_monitoring($url_monitoring);
                        $resVersion = $supervision->check_infosclass($url_infosclass);
                        if (isset($resVersion) && $resVersion != "0"){
                            $version_tmp = explode("|", $resVersion);
                            $class_check_version = $version_tmp[1];
                        }
                        echo "erreur_agent : $erreur_agent<br />";
                        echo "erreur_config : $erreur_config<br />";
                        echo "last_complet : $last_complet<br />";
                        echo "last_partiel : $last_partiel<br />";

                        if ($supervision->connectBddSupervision()) {
                            $clientBdd = $supervision->getClientByName($client);
                            if ($clientBdd) {
                                $clientBdd->setErreur_agent($erreur_agent);
                                $clientBdd->setErreur_Config($erreur_config);
                                $clientBdd->setInfos_monitoring($infos_monitoring);
                                $clientBdd->setLast_complet($last_complet);
                                $clientBdd->setLast_partiel($last_partiel);
                                $clientBdd->setClass_version($class_check_version);
                                $clientBdd->save();
                                echo "<br />Client mis à jour<br />";
                            }
                            else {
                                echo '<p class="errorBig">Client introuvable</p>';
                            }
                        }
                        else {
                            echo '<p class="errorBig">Erreur de connexion à la base de supervision</p>';
                        }
                    }
                    else {
                        echo '<p class="errorBig">Erreur de connexion à la base opac</p>';
                    }
                }
                else if ($_GET['action'] == "reinstallAgent" && !empty($_GET['client']) && !empty($_GET['class'])) {
                    $supervision->reinstallAgent($_GET['client'], $_GET['class']);
                    echo '<p class="infos">Agent réinstallé pour '.$_GET['client'].'<br />Cliquez sur Check pour vérifier</p>';
                }
                if ($supervision->connectBddSupervision()) {
                    if ($searchClient) {
                        $tabLineClient = $supervision->searchClients($searchClient, $req_tri);
                    }
                    else {
                        $tabLineClient = $supervision->getAllClients($req_tri);
                    }
                    echo "<center><h1>Etat des clients ".($searchClient ? "'%$searchClient%' " : "")."au ".date("d/m/Y H:i:s")."</h1></center>";
                    echo '<p style="text-align:center;">Rechercher un client : <input type="text" id="searchClient" value="" size="15" /> <input type="button" value="OK" onclick="searchClient();" /></p>';
                    echo "<table border=0 cellspacing=0 cellpadding=0 width=100% align=center>"; echo "<tr BGCOLOR=#CDECE7>

                    <th>";
                    if ($tri=="client_desc") echo "<a href=index.php?tri=client_asc>Client</a>";
                    elseif ($tri=="client_asc") echo "<a href=index.php?tri=client_desc>Client</a>";
                    else echo "<a href=index.php?tri=client_asc>Client</a>";
                    echo "</th>

                    <th>Url config</th>
                    <th>Url monitoring</th>
                    <th>Logs</th>
                    <th>Export</th>

                    <th>";
                    if ($tri=="last_complet_desc") echo "<a href=index.php?tri=last_complet_asc>Dernier complet</a>";
                    elseif ($tri=="last_complet_asc") echo "<a href=index.php?tri=last_complet_desc>Dernier complet</a>";
                    else echo "<a href=index.php?tri=last_complet_asc>Dernier complet</a>";
                    echo "</th>

                    <th>";
                    if ($tri=="last_partiel_desc") echo "<a href=index.php?tri=last_partiel_asc>Dernier partiel</a>";
                    elseif ($tri=="last_partiel_asc") echo "<a href=index.php?tri=last_partiel_desc>Dernier partiel</a>";
                    else echo "<a href=index.php?tri=last_partiel_asc>Dernier partiel</a>";
                    echo "</th>

                    <th>";
                    if ($tri=="erreur_agent_desc") echo "<a href=index.php?tri=erreur_agent_asc>Erreur agent</a>";
                    elseif ($tri=="erreur_agent_asc") echo "<a href=index.php?tri=erreur_agent_desc>Erreur agent</a>";
                    else echo "<a href=index.php?tri=erreur_agent_asc>Erreur agent</a>";
                    echo "</th>

                    <th>";
                    if ($tri=="erreur_config_desc") echo "<a href=index.php?tri=erreur_config_asc>Erreur config</a>";
                    elseif ($tri=="erreur_config_asc") echo "<a href=index.php?tri=erreur_config_desc>Erreur config</a>";
                    else echo "<a href=index.php?tri=erreur_config_asc>Erreur config</a>";
                    echo "</th>

                    <th style=\"width:15%;\">";
                    if ($tri=="infos_monitoring_desc") echo "<a href=index.php?tri=infos_monitoring_asc>Infos monitoring</a>";
                    elseif ($tri=="infos_monitoring_asc") echo "<a href=index.php?tri=infos_monitoring_desc>Infos monitoring</a>";
                    else echo "<a href=index.php?tri=infos_monitoring_asc>Infos monitoring</a>";
                    echo "</th> 

                    <th>Class name</th>		
                    <th>Class version</th>		
                    <th>Check</th>		
                    </tr>";

                    $tabVersions = array();
                    if ($tabLineClient) {
                        foreach ($tabLineClient as $key => $clientSupervise) {
                            if ($key%2 == 1) $color = "";
                            else $color="#CDECE7";
                            $client = $clientSupervise->getClient();
                            $url_opac3d = $clientSupervise->getUrl_opac3d();
                            $ip_agent = $clientSupervise->getIp_agent();
                            $url_testconfig = $clientSupervise->getUrl_testconfig();
                            $url_monitoring = $clientSupervise->getUrl_monitoring();
                            $url_infosclass = $clientSupervise->getUrl_infosclass();
                            $erreur_agent = $clientSupervise->getErreur_agent();
                            $erreur_config = $clientSupervise->getErreur_config();
                            $last_complet = $clientSupervise->getLast_complet();
                            $last_partiel = $clientSupervise->getLast_partiel();
                            $class_name = $clientSupervise->getClass_name();
                            $class_version = $clientSupervise->getClass_version();
                            $date_check = $clientSupervise->getDate_check();
                            $infos_monitoring = $clientSupervise->getInfos_monitoring();
                            
                            if (!isset($tabVersions[$class_name])) {
                                $tabVersions[$class_name] = $supervision->getVersionClassServer($class_name);
                            }

                            if ($erreur_agent==1) $color_erreur_agent = "red";
                            else $color_erreur_agent = "";

                            if (!empty($erreur_config)) $color_erreur_config = "red";
                            else $color_erreur_config = "";
                            
                            $dateTimeLastComplet = new DateTime($last_complet);
                            $last_complet_render = $dateTimeLastComplet->format('d/m/Y H:i:s');
                            if ($last_complet == '0000-00-00 00:00:00') {
                                $color_erreur_partiel = "red";
                                $last_complet_render = 'Aucun complet';
                            }
                            else  $color_erreur_partiel = "";
                            $dateTimeLastPartiel = new DateTime($last_partiel);
                            $last_partiel_render = $dateTimeLastPartiel->format('d/m/Y H:i:s');
                            $laps = $dateTimeLastPartiel->diff(new DateTime());
                            if ($last_partiel == '0000-00-00 00:00:00' || $laps->days > 30) {
                                $color_erreur_partiel = "red";
                                if ($last_partiel == '0000-00-00 00:00:00') {
                                    $last_partiel_render = 'Aucun partiel';
                                }
                            }
                            else  $color_erreur_partiel = "";
                            if ($infos_monitoring != 'OK') {
                                $color_erreur_monitoring = "red";
                            }
                            else  $color_erreur_monitoring = "";
                            
                            if ($class_version != $tabVersions[$class_name]) {
                                $td_class_version = 'style="background-color:red;cursor:help;" title="Version du serveur : '.$tabVersions[$class_name].'"';
                            }
                            else {
                                $td_class_version = "";
                            }

                            echo "<form method=post action=index.php?action=check>";
                            echo "<input type=hidden name=date_check value=$date_check>";
                            echo "<input type=hidden name=client value=$client>";	
                            echo "<input type=hidden name=url_testconfig value=$url_testconfig>";
                            echo "<input type=hidden name=url_monitoring value=$url_monitoring>";
                            echo "<input type=hidden name=url_infosclass value=$url_infosclass>";
                            echo "<input type=hidden name=tri value=$tri>";

                            echo "<tr BGCOLOR=$color>
                                    <td align=center><b><a href=\"http://$url_opac3d\" target=_blank>".ucfirst($client)."</b></a></td>
                                    <td><a href=$url_testconfig target=_blank>Url config</a><br /><a href=http://tools.esezhame.fr/test_config.php?url=$url_testconfig target=_blank>Proxy decalog</a></td>
                                    <td><a href=$url_monitoring target=_blank>Url monitoring</a><br /><a href=http://tools.esezhame.fr/test_config.php?url=$url_monitoring target=_blank>Proxy decalog</a></td>
                                    <td align=center>
                                            <a href=http://$ip_agent/webservices/client/logs/access.log target=_blank>Access log</a><br />
                                            <a href=http://$ip_agent/webservices/client/logs/errors.log target=_blank>Error log</a><br />
                                    </td>
                                    <td align=center>
                                            <a href=http://$ip_agent/webservices/client/exportiso/$client/exports_diff.log target=_blank>Export_diff</a><br />
                                             <a href=http://$ip_agent/webservices/client/exportiso/$client/exports_full.log target=_blank>Export_full</a><br />
                                    </td>

                                    <td align=center>$last_complet_render</td>
                                    <td align=center BGCOLOR=$color_erreur_partiel>$last_partiel_render</td>
                                    <td align=center BGCOLOR=$color_erreur_agent>$erreur_agent</td>
                                    <td style=\"background-color:$color_erreur_config\">$erreur_config</td>
                                    <td style=\"background-color:$color_erreur_monitoring;cursor:help;\" title=\"".htmlentities($infos_monitoring)."\">".htmlentities(substr($infos_monitoring, 0, 80).(strlen($infos_monitoring) > 80 ? " [...]" : ""))."</td>
                                    <td align=center>$class_name</td>
                                    <td align=center $td_class_version>$class_version <img id=\"reinstallBtn\" src=\"css/download.png\" title=\"Réinstaller les classes SIGB\" onclick=\"reinstallAgent('$client', '$class_name');\" /></td>
                                    <td style=\"text-align:center;\"><input id=\"checkBtn\" type=submit name=check value=\"\" title=\"Vérifier\"></td>
                                </tr>";
                            echo "</form>";
                        }
                    }
                    else {
                        echo '<tr><td colspan="13" style="text-align:center;">Aucun client supervisé</td></tr>';
                    }
                }
                else {
                    echo '<p class="errorBig">Erreur de connexion à la base de supervision</p>';
                }
            }
            else {
                echo '<p class="errorBig">Erreur de lecture du fichier de configuration</p>';
            }
	  
            echo "</table>";
	  
	  ?>
  </body> </html>
