source: trunk/push-web @ 408

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