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
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
50klask exportsw                                                              > ${TMP}/switch-db.txt.new
51
52klask exportdb    > ${TMP}/hostname-db.txt
53klask ip-free     > ${TMP}/ipfree-db.txt
54klask bad-vlan-id > ${TMP}/badvlan-db.txt
55
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   }
60
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>';
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;) \
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';\
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;) \
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
80cat <<END > ${TMP}/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 >> ${TMP}/index.html
208
209cat <<END >> ${TMP}/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 >> ${TMP}/index.html
220
221cat <<END >> ${TMP}/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 >> ${TMP}/index.html
232
233cat <<END >> ${TMP}/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      ${TMP}/index.html \
286      ${TMP}/*-db.txt \
287      ${TMP}/switch-map.txt \
288      ${TMP}/*.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      ${TMP}/index.html \
301      ${TMP}/*-db.txt \
302      ${TMP}/switch-map.txt \
303      ${TMP}/*.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.