summaryrefslogtreecommitdiff
path: root/api/gen_etat_reseau.php
blob: b8246498f9d1e3877b7c8793534ba88e6b616a94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?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
AND e.equipement_mode_id != 'power'");
$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 ) {
?>
    <h2 id="<?=$ville?>"><?=$ville?></h2>
<?php
  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>