source: trunk/push-web @ 411

Last change on this file since 411 was 411, checked in by g7moreau, 4 years ago
  • error in digit glob
  • Property svn:executable set to *
File size: 9.9 KB
RevLine 
[85]1#!/bin/bash
2#
[408]3# Copyright (C) 2005-2020 Gabriel Moreau <Gabriel.Moreau(A)univ-grenoble-alpes.fr>
[372]4# License GNU GPL version 2 or later
5#
[355]6# 2007/10/23 Gabriel Moreau
7# 2010/11/03 Gabriel Moreau
8# 2011/03/30 Gabriel Moreau - make it generic
9# 2017/11/04 Gabriel Moreau - all reworks - use tab
[85]10
[344]11export PATH=/bin:/sbin:/usr/bin:/usr/sbin
12export LANG=C
13
[85]14SITE_NAME=''
15REMOTE_SERVER=localhost
16REMOTE_USER=''
17REMOTE_FOLDER=/var/www/klask/
18
[292]19GRAPH_MODULO=9
20GRAPH_SHIFT=1
21
[87]22if [ -f /etc/klask/push-web.conf ]
[85]23then
24   . /etc/klask/push-web.conf
25fi
26
[87]27TMP='/tmp/klask-push-web'
[85]28
[87]29
30# remote command
31DOT=/usr/bin/dot
[85]32
[87]33
[344]34mkdir -p ${TMP}
[87]35if [ ! -d ${TMP} ]
[85]36then
37   echo "Error: Folder ${TMP} do not exist !"
38   exit 1
39fi
40cd ${TMP}
41
42
[344]43LOCALTIME=$(date "+%Y-%m-%d %H:%M")
[408]44TIMEFILE=$(date "+%Y-%m-%d.%H-%M")
[85]45
[408]46CACHE_TIMEOUT=50
47find ${TMP}/ -type f -a -mtime +${CACHE_TIMEOUT} -delete
[85]48
[361]49klask exportsw --format dot --modulo ${GRAPH_MODULO} --shift ${GRAPH_SHIFT} > ${TMP}/switch-map.txt.new
[410]50klask exportsw                                                              > ${TMP}/switch-db.txt.new
[85]51
[344]52klask exportdb    > ${TMP}/hostname-db.txt
53klask ip-free     > ${TMP}/ipfree-db.txt
54klask bad-vlan-id > ${TMP}/badvlan-db.txt
[85]55
[410]56cmp --silent ${TMP}/switch-db.txt.new ${TMP}/switch-db.txt || {
57   mv -f ${TMP}/switch-db.txt.new ${TMP}/switch-db.${TIMEFILE}.txt;
58   ln -sf ${TMP}/switch-db.${TIMEFILE}.txt ${TMP}/switch-db.txt
59   }
[85]60
[410]61cmp --silent ${TMP}/switch-map.txt.new ${TMP}/switch-map.txt || {
62   mv -f ${TMP}/switch-map.txt.new ${TMP}/switch-map.${TIMEFILE}.txt;
63   ln -sf ${TMP}/switch-map.${TIMEFILE}.txt ${TMP}/switch-map.txt
64   }
65
66old_switch_db=$((echo 'command <tt>klask exportsw --format txt</tt>';
[411]67      (cd ${TMP}/; ls -1 switch-db.20[0-9][0-9]-[0-9][0-9]-[0-9][0-9].[0-9][0-9]-[0-9][0-9].txt;) \
[410]68      | sed -e 's/^switch-db\.//; s/\.txt$//;' \
69      | xargs -r -I {} printf "<a href=\"switch-db.%s.txt\">%s</a>\n" '{}' '{}' \
70      ) \
71   | paste -sd ',' -)
72
73old_switch_map=$((echo 'graphviz';\
[411]74      (cd ${TMP}/; ls -1 switch-map.20[0-9][0-9]-[0-9][0-9]-[0-9][0-9].[0-9][0-9]-[0-9][0-9].txt;) \
[410]75      | sed -e 's/^switch-map\.//; s/\.txt$//;' \
76      | xargs -r -I {} printf "<a href=\"switch-map.%s.txt\">%s</a>\n" '{}' '{}' \
77      )\
78   | paste -sd ',' -)
79
[344]80cat <<END > ${TMP}/index.html
[349]81<!DOCTYPE html>
[350]82<html lang="en">
[85]83<head>
84 <link rel="stylesheet" type="text/css" href="style-klask.css" />
[359]85 <!-- <meta http-equiv="refresh" content="1800"> -->
[349]86 <title>Klask mapping net-tools</title>
[85]87 <noscript>
88 </noscript>
89 <script src="sorttable-klask.js"></script>
[408]90 <script src="svg-pan-zoom-klask.js"></script>
[85]91 <script>
92function delayedRefresh() {
93 setTimeout( "refresh()", 1800*1000 );
94 }
95
96function refresh() {
97 history.go(0);
98 }
[355]99 
100function selectTab(event, tabName) {
101 var i;
102 var x = document.getElementsByClassName("selectableTabContent");
103 var tablinks = document.getElementsByClassName("selectableTabButton");
104 for (i = 0; i < x.length; i++) {
105  x[i].style.display = "none";
106  }
107 for (i = 0; i < x.length; i++) {
108  tablinks[i].id = "";
109  }
110 document.getElementById(tabName).style.display = "block";
111 event.currentTarget.id = "selected";
112 }
[365]113
114// https://stackoverflow.com/questions/17591447/how-to-reload-current-page-without-losing-any-form-data
115// http://www.uitrick.com/javascript/how-to-keep-a-global-variable-alive-even-after-page-refresh-javascript/
116// https://blog.udemy.com/javascript-page-refresh/
117
118// localStorage.setItem('bgcolor', document.getElementById('bgcolor').value);
119// localStorage.getItem('bgcolor');
120
121// https://stackoverflow.com/questions/5683087/chaining-getelementbyid
122function getById(id, context) {
123 var el = document.getElementById(id);
124 return context.contains(el) ? el : null;
125 }
126function getByName(name, context) {
127 var i;
128 var x = document.getElementsByName(name);
129 for (i = 0; i < x.length; i++) {
130  if ((x[i].getAttribute('name') == name) && (context.contains(x[i]))) {
131   return x[i];
132   }
133  }
134 return null;
135 }
136
137// Run on page load
138window.onload = function() {
139 if (typeof(Storage) !== "undefined") {
140  // Code for localStorage/sessionStorage.
141  var tabName = localStorage.getItem('navbar-selected');
142  if (tabName == "undefined") { return; }
143  var navbar = document.getElementById('navbar');
144  var tab = getByName(tabName, navbar);
145  var i;
146  var x = document.getElementsByClassName("selectableTabContent");
147  var tablinks = document.getElementsByClassName("selectableTabButton");
148
149  for (i = 0; i < x.length; i++) {
150   x[i].style.display = "none";
151   }
152  for (i = 0; i < x.length; i++) {
153   tablinks[i].id = "";
154   }
155
156  document.getElementById(tabName).style.display = "block";
157  tab.id = "selected";
158  }
159 else {
160  // Sorry! No Web Storage support..
161  }
162 }
163
164// Before refreshing the page, save the form data to sessionStorage
165window.onbeforeunload = function() {
166 if (typeof(Storage) !== "undefined") {
167  // Code for localStorage/sessionStorage.
168  // var navbar = document.getElementById('navbar');
169  var selected = document.getElementById('selected'); // getById('selected', navbar);
170
171  var tabName = selected.getAttribute('name');
172  localStorage.setItem('navbar-selected', tabName);
173  }
174 else {
175  // Sorry! No Web Storage support..
176  }
177 }
[85]178 </script>
179</head>
[359]180<body> <!-- onload="delayedRefresh" -->
[85]181<h1>
[338]182<b><a href="http://servforge.legi.grenoble-inp.fr/projects/klask" class="circle">Klask</a></b>
[359]183Tool for <a href="switch-map.png">mapping</a> the local network - site ${SITE_NAME}
184<a class="refresh" href="./">${LOCALTIME}</a>
[85]185</h1>
186
[331]187<div id="navbar">
188 <ul>
[365]189  <li class="selectableTabButton" name="IP_detected"   onclick="selectTab(event, 'IP_detected')" id="selected">IP detected</li>
190  <li class="selectableTabButton" name="IP_available"  onclick="selectTab(event, 'IP_available')">IP available</li>
191  <li class="selectableTabButton" name="VLAN_mismatch" onclick="selectTab(event, 'VLAN_mismatch')">VLAN mismatch</li>
192  <li class="selectableTabButton" name="MAP"           onclick="selectTab(event, 'MAP')">MAP (png)</li>
193  <li class="selectableTabButton" name="RAW_data"      onclick="selectTab(event, 'RAW_data')">RAW data</li>
[331]194 </ul>
195</div>
[85]196
[331]197<div id="content">
[355]198
199 <div id="IP_detected" class="selectableTabContent">
200  <p>
[356]201  List of all IP addresses detected on the local network (on all VLANs)
202  since the origin of the database
203  (except those that have been deleted manually and voluntarily).
[355]204  </p>
[85]205END
206
[344]207klask exportdb --format html >> ${TMP}/index.html
[85]208
[344]209cat <<END >> ${TMP}/index.html
[355]210 </div>
211
212 <div id="IP_available" class="selectableTabContent" style="display:none">
213  <p>
[356]214  List all free IP addresses (which have never been used or not used for two years).
215  These IP addresses are available to assign to all new computers.
[355]216  </p>
[85]217END
218
[355]219klask ip-free --format html >> ${TMP}/index.html
[85]220
[355]221cat <<END >> ${TMP}/index.html
222 </div>
[85]223
[355]224 <div id="VLAN_mismatch" class="selectableTabContent" style="display:none">
225  <p>
[356]226  List all computers connected to switches where the VLAN is misconfigured.
227  Returns the port and VLAN to configure on these switches.
[355]228  </p>
[85]229END
230
[355]231klask bad-vlan-id --format html >> ${TMP}/index.html
[85]232
[355]233cat <<END >> ${TMP}/index.html
234 </div>
[85]235
[355]236 <div id="MAP" class="selectableTabContent" style="display:none">
237   <p>
[356]238   Direct link to local network map in the formats <a href="switch-map.png">png</a> and <a href="switch-map.svg">svg</a>.
[363]239   Because the map could be big,
240   you will have a better web experience with
241   <a href="https://addons.mozilla.org/en-US/firefox/addon/image-zoom/">Image Zoom<a> plugin (or equivalent) installed.
[355]242   </p>
[408]243   
244   <embed type="image/svg+xml" src="switch-map.svg" id="my-embed-switch-map"/>
245   <script>
246      document.getElementById('my-embed-switch-map').addEventListener('load', function(){
247         // Will get called after embed element was loaded
248         svgPanZoom(document.getElementById('my-embed-switch-map'));
249         })
250   </script>
[355]251   <img class="map" src="switch-map.png">
252 </div>
[329]253
[355]254 <div id="RAW_data" class="selectableTabContent" style="display:none">
255  <p>
[356]256   The main raw results of Klask are accessible directly,
257   via <tt>wget</tt> or <tt>curl</tt> tools for example,
258   for any specific treatment:
[355]259   <ul>
260    <li><a href="hostname-db.txt">Computers DB</a> (command <tt>klask exportdb --format txt</tt>),</li>
[410]261    <li><a href="switch-db.txt">Switches DB</a> (${old_switch_db}),</li>
[355]262    <li><a href="ipfree-db.txt">IP available</a> (command <tt>klask ip-free --format txt</tt>),</li>
263    <li><a href="badvlan-db.txt">VLAN mismatch</a> (command <tt>klask bad-vlan-id --format txt</tt>),</li>
[410]264    <li>Map format: <a href="switch-map.txt">dot source</a> (${old_switch_map}), <a href="switch-map.png">png</a>, <a href="switch-map.svg">svg</a>.</li>
[355]265   </ul>
266  </p>
267 </div>
[331]268</div>
[329]269
[343]270<p class="copyright">
[341]271Klask (version $(klask version | grep ^Version | cut -f 2 -d ' ')) - $(klask version | grep ^Copyright | cut -f 1 -d '<')
272</p>
[363]273<!-- Try to prefect switch map image -->
274<link rel="prefetch" href="switch-map.png">
[329]275</body>
276</html>
277END
278
[95]279if [ "${REMOTE_SERVER}" = "localhost" ]
[85]280then
[362]281   rsync --times \
[85]282      /usr/share/klask/sorttable-klask.js \
[408]283      /usr/share/klask/svg-pan-zoom-klask.js \
[85]284      /usr/share/klask/style-klask.css \
285      ${TMP}/index.html \
286      ${TMP}/*-db.txt \
287      ${TMP}/switch-map.txt \
[411]288      ${TMP}/*.20[0-9][0-9]-[0-9][0-9]-[0-9][0-9].[0-9][0-9]-[0-9][0-9].txt \
[85]289      ${REMOTE_FOLDER}/
290
[361]291   [ -x ${DOT} ] && (cd ${REMOTE_FOLDER};
292      [ switch-map.txt -nt switch-map.svg ] && ${DOT} -T svg switch-map.txt > switch-map.svg;
293      [ switch-map.txt -nt switch-map.png ] && ${DOT} -T png switch-map.txt > switch-map.png)
[85]294
295else
[362]296   rsync --times \
[85]297      /usr/share/klask/sorttable-klask.js \
[408]298      /usr/share/klask/svg-pan-zoom-klask.js \
[85]299      /usr/share/klask/style-klask.css \
300      ${TMP}/index.html \
301      ${TMP}/*-db.txt \
302      ${TMP}/switch-map.txt \
[411]303      ${TMP}/*.20[0-9][0-9]-[0-9][0-9]-[0-9][0-9].[0-9][0-9]-[0-9][0-9].txt \
[85]304      ${REMOTE_USER}@${REMOTE_SERVER}:${REMOTE_FOLDER}/
305
306
[361]307   ssh ${REMOTE_USER}@${REMOTE_SERVER} "(cd ${REMOTE_FOLDER};
308      [ switch-map.txt -nt switch-map.svg ] && ${DOT} -T svg switch-map.txt > switch-map.svg;
309      [ switch-map.txt -nt switch-map.png ] && ${DOT} -T png switch-map.txt > switch-map.png)"
[85]310fi
Note: See TracBrowser for help on using the repository browser.