diff options
-rw-r--r-- | api/gen_etat_reseau.php | 262 |
1 files changed, 262 insertions, 0 deletions
diff --git a/api/gen_etat_reseau.php b/api/gen_etat_reseau.php new file mode 100644 index 0000000..20323da --- /dev/null +++ b/api/gen_etat_reseau.php @@ -0,0 +1,262 @@ +<?php +/** + * Copyright 2017 Ludovic Pouzenc <ludovic@pouzenc.fr> + * + * This file is part of CHD Gestion. + * + * CHD Gestion is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * CHD Gestion is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with CHD Gestion. If not, see <http://www.gnu.org/licenses/>. + **/ + +// topological sorting from https://gist.github.com/gowon/1744369137826450b185 +function topoNodeLevel($k, $ar,$n,$ref=array()) +{ + if(!isset($ar[$n][$k])){return 0;} + if(in_array($n,$ref)){return -1;} + $ref[]=$n; + $r=topoNodeLevel($k, $ar,$ar[$n][$k],$ref); + return ($r==-1?-1:$r+1); +} + +function toposort($k, &$array) { + $ar=array(); + foreach($array as $i=>$tmp) + { + $ar[]=topoNodeLevel($k, $array,$i); + } + if(!in_array(-1,$ar)) + { + array_multisort($ar,SORT_ASC,$array); + }else{ + error_log("Circular reference detected : " . print_r($array, true)); + } +} + +include_once('inc/config.php'); +$mysqli = new mysqli($db_config['host'], $db_config['username'], $db_config['password'], $db_config['database']); +if (mysqli_connect_errno()) { + die(mysqli_connect_error()); +} +unset($db_config); +$mysqli->set_charset("utf8") or die($mysqli->error); + +// Détail des équipements (nom, ip, uplink) +$res = $mysqli->query("SELECT e.id, + IFNULL(NULLIF(e.hostname,''), CONCAT('e', e.id)) as 'name', e.uplink_id, e.ipmgmt_id, r.title as relais +FROM equipements e +LEFT JOIN relais r ON ( r.id = e.relais_id ) +WHERE (e.relais_id IS NOT NULL OR e.hostname='STG1') +AND e.date_hs IS NULL"); +$equipements = array(); +while ( $e_row = $res->fetch_assoc() ) { + $id = $e_row['id']; + $equipements[$id] = $e_row; +} + +// Liste des relais par ville, avec les équipements associés +$res = $mysqli->query("SELECT v.title as ville, r.title as relais, GROUP_CONCAT(e.id) as equipement_ids +FROM villes v +JOIN relais r ON (v.id = r.ville_id) +JOIN equipements e ON (r.id = e.relais_id) +WHERE e.date_hs IS NULL +GROUP BY v.title, r.title +"); + +// Pour chaque relais +$world = array(); +while ( $r_row = $res->fetch_assoc() ) { + // La ville sera en clé du tableau associatif + $ville = $r_row['ville']; + unset($r_row['ville']); + + // On veut un tableau d'id d'équipements à partir de la chaine séparée par des virgules récupérée + $eids = explode(',', $r_row['equipement_ids']); + unset($r_row['equipement_ids']); + + // Construction d'un tableau d'équipements impliqué dans l'accès à internet du relais considéré + $r_row['equipements'] = array(); + + // Pour chaque id d'équipement (initialement : les équipements sur le relai, après : les équipements sur le chemin) + while ( $eid = array_pop($eids) ) { + if ( array_key_exists($eid, $equipements) ) { + // On référence l'équipement depuis le tableau en mémoire de tous les équipements + $r_row['equipements'][$eid] = &$equipements[$eid]; + + // On ajoute les équipements en amont s'il ne sont pas déjà là + $uplink_id = $equipements[$eid]['uplink_id']; + if ( ! array_key_exists($uplink_id, $r_row['equipements'])) { + array_push($eids, $uplink_id); + } + } + } + // On trie topologiquement les équipements + toposort('uplink_id', $r_row['equipements']); + + // On ajoute la ligne relais enrichie au tableau des relais, indexé par ville + $world[$ville][] = $r_row; +} +?> +<!DOCTYPE html> +<html lang="fr_FR"> + <head> + <meta charset="UTF-8"> + <title>Comminges Haut Débit : État du réseau</title> + <script src="jquery-1.11.3.min.js"></script> + <style> +h1 { + font-size:30px; + color: #076bdb; + text-align:left; +} + +h2 { + font-size:20px; + color: #464646; + text-align:left; + border-bottom:1px solid; + color:#ff921e; +} + +caption { + font-size:17px; + font-weight:bold; + margin:35px 0 10px 0; + padding:0; +} +table { + border-collapse:collapse; + display: inline-block; + vertical-align: top; + padding: 0 2em 0 0; +} +table td { + border: 1px solid #b8bcbe; /*b8bcbe*/ + /*background:#FAFAFA;*/ + padding:3px; +} +table th { + border: 1px solid #b8bcbe; /* b8bcbe */ + background-color:#5d9de5; + padding:5px; + color:#ffffff; +} +table tr { + background-color:#f5f8ff; +} +table tr:hover { + background-color:#ffe4a3; +} + +nav { + float: right; + /*width: 40%;*/ + background: #eee; + font-size: 0.8em; + padding: 1em 2em; + margin: 0 0 0.5em 0.5em; +} +nav ul { + padding: 0; +} +nav li { + margin: 0 0 0.25em 0; +} +nav a { + text-decoration: none; +} +nav a:hover, nav a:active { + text-decoration: underline; +} + +div.debug { + display: none; +} + </style> + <script> + function ping(ip, element){ + $.get('poke.php?ip='+ip, function( state ) { + if(state == 'green') + content = "<a href=\"#\" onclick=\"p=prompt('Redémarrage : Merci d\\'entrer votre mot de passe', ''); resp = $.ajax({url: 'reboot.php?pass='+p+'&ip="+ip+"', async: false}).responseText; alert(resp); return false;\">Redémarrer</a> / <a href=\"http://"+ip+"\" target=\"_blank\">Accéder</a>"; + else + content = "<a href=\"http://"+ip+"\" target=\"_blank\">Injoignable depuis St-Go</a>"; + + $('.'+element).each(function() { + $(this).html(content); + $(this).attr('style', 'background: ' + state); + }); + + }); + } + + function toc() { + var ToC = "<h3>Accès rapide :</h3><ul>"; + var el, title, link; + $("h2").each(function(index) { + /* console.log( index + ": " + $( this ).text() ); */ + el = $(this); + title = el.text(); + link = "#" + el.attr("id"); + ToC += "<li><a href='" + link + "'>" + title + "</a></li>"; + }); + ToC += "</ul>"; + return ToC; + } + + $(document).ready(function() { + $("nav").append(toc()); + }); + </script> + </head> + <body> + <h1>Comminges Haut Débit : État du réseau</h1> + <div>Généré le <?=date(DATE_ATOM)?></div> + <nav role='navigation'></nav> +<?php +$already_done_script = array(); + +foreach ( $world as $ville => $relais ) { + echo "<h2>$ville</h2>\n"; + foreach ( $relais as $r ) { +?> + <table> + <caption><?=$r['relais']?></caption> + <tr><th>Site</th><th>Équipement</th><th>État</th></tr> +<?php + foreach ( $r['equipements'] as $e ) { + $eid = $e['id']; +?> + <tr> + <td><?=$e['relais']?></td> + <td><?=$e['name']?></td> + <td class="e<?=$eid?>"> +<?php + if ( ! array_key_exists($eid, $already_done_script) ) { +?> + <script>ping('<?=$e['ipmgmt_id']?>', 'e<?=$eid?>')</script> +<?php + $already_done_script[$eid] = 1; + } +?> + </td> + </tr> +<?php + } +?> + </table> + <div class="debug"><pre><?=print_r($r, true);?></pre></div> +<?php + } +} +?> + </body> +</html> |