source: trunk/push-web

Last change on this file was 412, checked in by g7moreau, 5 years ago
  • rename TMP to CACHE_FOLDER. User can change it inside the configuration file (update sample)
  • Property svn:executable set to *
File size: 10.3 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
22CACHE_TIMEOUT=50
23CACHE_FOLDER='/tmp/klask-push-web'
24
25if [ -f /etc/klask/push-web.conf ]
26then
27   . /etc/klask/push-web.conf
28fi
29
30
31# remote command
32DOT=/usr/bin/dot
33
34mkdir -p ${CACHE_FOLDER}
35if [ ! -d ${CACHE_FOLDER} ]
36then
37   echo "Error: Folder ${CACHE_FOLDER} do not exist !"
38   exit 1
39fi
40cd ${CACHE_FOLDER}
41
42LOCALTIME=$(date "+%Y-%m-%d %H:%M")
43TIMEFILE=$(date "+%Y-%m-%d.%H-%M")
44
45find ${CACHE_FOLDER}/ -type f -a -mtime +${CACHE_TIMEOUT} -delete
46
47klask exportsw --format dot --modulo ${GRAPH_MODULO} --shift ${GRAPH_SHIFT} > ${CACHE_FOLDER}/switch-map.txt.new
48klask exportsw                                                              > ${CACHE_FOLDER}/switch-db.txt.new
49
50klask exportdb    > ${CACHE_FOLDER}/hostname-db.txt
51klask ip-free     > ${CACHE_FOLDER}/ipfree-db.txt
52klask bad-vlan-id > ${CACHE_FOLDER}/badvlan-db.txt
53
54cmp --silent ${CACHE_FOLDER}/switch-db.txt.new ${CACHE_FOLDER}/switch-db.txt || {
55   mv -f ${CACHE_FOLDER}/switch-db.txt.new ${CACHE_FOLDER}/switch-db.${TIMEFILE}.txt;
56   ln -sf ${CACHE_FOLDER}/switch-db.${TIMEFILE}.txt ${CACHE_FOLDER}/switch-db.txt
57   }
58
59cmp --silent ${CACHE_FOLDER}/switch-map.txt.new ${CACHE_FOLDER}/switch-map.txt || {
60   mv -f ${CACHE_FOLDER}/switch-map.txt.new ${CACHE_FOLDER}/switch-map.${TIMEFILE}.txt;
61   ln -sf ${CACHE_FOLDER}/switch-map.${TIMEFILE}.txt ${CACHE_FOLDER}/switch-map.txt
62   }
63
64# keep trace of old switch map
65old_switch_db=$((echo 'command <tt>klask exportsw --format txt</tt>';
66      (cd ${CACHE_FOLDER}/; 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;) \
67      | sed -e 's/^switch-db\.//; s/\.txt$//;' \
68      | xargs -r -I {} printf " <a href=\"switch-db.%s.txt\">%s</a>\n" '{}' '{}' \
69      ) \
70   | paste -sd ',' -)
71
72old_switch_map=$((echo 'graphviz';\
73      (cd ${CACHE_FOLDER}/; 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;) \
74      | sed -e 's/^switch-map\.//; s/\.txt$//;' \
75      | xargs -r -I {} printf " <a href=\"switch-map.%s.txt\">%s</a>\n" '{}' '{}' \
76      )\
77   | paste -sd ',' -)
78
79
80cat <<END > ${CACHE_FOLDER}/index.html
81<!DOCTYPE html>
82<html lang="en">
83<head>
84 <link rel="stylesheet" type="text/css" href="style-klask.css" />
85 <!-- <meta http-equiv="refresh" content="1800"> -->
86 <title>Klask mapping net-tools</title>
87 <noscript>
88 </noscript>
89 <script src="sorttable-klask.js"></script>
90 <script src="svg-pan-zoom-klask.js"></script>
91 <script>
92function delayedRefresh() {
93 setTimeout( "refresh()", 1800*1000 );
94 }
95
96function refresh() {
97 history.go(0);
98 }
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 }
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 }
178 </script>
179</head>
180<body> <!-- onload="delayedRefresh" -->
181<h1>
182<b><a href="http://servforge.legi.grenoble-inp.fr/projects/klask" class="circle">Klask</a></b>
183Tool for <a href="switch-map.png">mapping</a> the local network - site ${SITE_NAME}
184<a class="refresh" href="./">${LOCALTIME}</a>
185</h1>
186
187<div id="navbar">
188 <ul>
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>
194 </ul>
195</div>
196
197<div id="content">
198
199 <div id="IP_detected" class="selectableTabContent">
200  <p>
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).
204  </p>
205END
206
207klask exportdb --format html >> ${CACHE_FOLDER}/index.html
208
209cat <<END >> ${CACHE_FOLDER}/index.html
210 </div>
211
212 <div id="IP_available" class="selectableTabContent" style="display:none">
213  <p>
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.
216  </p>
217END
218
219klask ip-free --format html >> ${CACHE_FOLDER}/index.html
220
221cat <<END >> ${CACHE_FOLDER}/index.html
222 </div>
223
224 <div id="VLAN_mismatch" class="selectableTabContent" style="display:none">
225  <p>
226  List all computers connected to switches where the VLAN is misconfigured.
227  Returns the port and VLAN to configure on these switches.
228  </p>
229END
230
231klask bad-vlan-id --format html >> ${CACHE_FOLDER}/index.html
232
233cat <<END >> ${CACHE_FOLDER}/index.html
234 </div>
235
236 <div id="MAP" class="selectableTabContent" style="display:none">
237   <p>
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>.
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.
242   </p>
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>
251   <img class="map" src="switch-map.png">
252 </div>
253
254 <div id="RAW_data" class="selectableTabContent" style="display:none">
255  <p>
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:
259   <ul>
260    <li><a href="hostname-db.txt">Computers DB</a> (command <tt>klask exportdb --format txt</tt>),</li>
261    <li><a href="switch-db.txt">Switches DB</a> (${old_switch_db}),</li>
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>
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>
265   </ul>
266  </p>
267 </div>
268</div>
269
270<p class="copyright">
271Klask (version $(klask version | grep ^Version | cut -f 2 -d ' ')) - $(klask version | grep ^Copyright | cut -f 1 -d '<')
272</p>
273<!-- Try to prefect switch map image -->
274<link rel="prefetch" href="switch-map.png">
275</body>
276</html>
277END
278
279if [ "${REMOTE_SERVER}" = "localhost" ]
280then
281   rsync --times \
282      /usr/share/klask/sorttable-klask.js \
283      /usr/share/klask/svg-pan-zoom-klask.js \
284      /usr/share/klask/style-klask.css \
285      ${CACHE_FOLDER}/index.html \
286      ${CACHE_FOLDER}/*-db.txt \
287      ${CACHE_FOLDER}/switch-map.txt \
288      ${CACHE_FOLDER}/*.20[0-9][0-9]-[0-9][0-9]-[0-9][0-9].[0-9][0-9]-[0-9][0-9].txt \
289      ${REMOTE_FOLDER}/
290
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)
294
295else
296   rsync --times \
297      /usr/share/klask/sorttable-klask.js \
298      /usr/share/klask/svg-pan-zoom-klask.js \
299      /usr/share/klask/style-klask.css \
300      ${CACHE_FOLDER}/index.html \
301      ${CACHE_FOLDER}/*-db.txt \
302      ${CACHE_FOLDER}/switch-map.txt \
303      ${CACHE_FOLDER}/*.20[0-9][0-9]-[0-9][0-9]-[0-9][0-9].[0-9][0-9]-[0-9][0-9].txt \
304      ${REMOTE_USER}@${REMOTE_SERVER}:${REMOTE_FOLDER}/
305
306
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)"
310fi
Note: See TracBrowser for help on using the repository browser.