tazpanel view hardware.cgi @ rev 456

boot.cgi: ISO mine: "table"izing input form.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Mon Apr 13 01:44:08 2015 +0300 (2015-04-13)
parents b0146d791379
children aa42490e9679
line source
1 #!/bin/sh
2 #
3 # Hardware part of TazPanel - Devices, drivers, printing
4 #
5 # Copyright (C) 2011-2015 SliTaz GNU/Linux - BSD License
6 #
8 # Common functions from libtazpanel
9 . lib/libtazpanel
10 get_config
11 header
13 TITLE=$(_ 'TazPanel - Hardware')
15 # Call an optional module
16 lib() {
17 module=lib/$1
18 shift
19 [ -s $module ] && . $module "$@"
20 }
22 disk_info() {
23 fdisk -l | fgrep Disk | while read a b c; do
24 d=${b#/dev/}
25 d="/sys/block/${d%:}/device"
26 [ -d $d ] && echo "$a $b $c, $(cat $d/vendor) $(cat $d/model)"
27 done 2> /dev/null | sed 's/ */ /g'
28 }
30 lsusb_table() {
31 cat <<EOT
32 <table class="wide zebra">
33 <thead>
34 <tr>
35 <td>$(_ 'Bus')</td>
36 <td>$(_ 'Device')</td>
37 <td>$(_ 'ID')</td>
38 <td>$(_ 'Name')</td>
39 </tr>
40 </thead>
41 <tbody>
42 EOT
43 lsusb | sed 's|^Bus \([0-9]*\)|<tr><td>\1</td>|;
44 s|</td> Device \([0-9]*\):|</td><td>\1</td>|;
45 s|</td> ID \([^:]*:[^ ]*\)|</td><td><a href="?lsusb=\1">\1</a></td>|;
46 s| |<td>|2;
47 s|.*$|\0</td></tr>|'
48 echo "</tbody></table>"
49 }
52 lspci_table() {
53 cat <<EOT
54 <table class="wide zebra">
55 <thead><tr><td>Slot</td><td>Device</td><td>Name</td></thead>
56 <tbody>
57 EOT
58 lspci | sed 's| |</td><td>|;
59 s|: |</td><td>|;
60 s|^\([^<]*\)|<a href="?lspci=\1">\1</a>|;
61 s|^.*$|<tr><td>\0</td></tr>|'
62 echo "</tbody></table>"
63 }
66 #
67 # Commands
68 #
70 case " $(GET) " in
71 *\ print\ *)
72 xhtml_header
73 echo "<h2>TODO</h2>"
74 ;;
77 *\ tazx\ *)
78 xhtml_header
79 cat <<EOT
80 <pre>$(tazx auto)</pre>
81 EOT
82 ;;
85 *\ detect\ *)
86 # Front end for Tazhw
87 # TODO: Add button to detect webcam, etc. Like in tazhw box.
88 xhtml_header
89 cat <<EOT
90 <h2>$(_ 'Detect hardware')</h2>
91 <p>$(_ 'Detect PCI and USB hardware')</p>
93 <section>
94 <pre>$(tazhw detect-pci | sed 's|^>|\&gt;|g')</pre>
95 <pre>$(tazhw detect-usb | sed 's|^>|\&gt;|g')</pre>
96 </section>
97 EOT
98 ;;
101 *\ modules\ *|*\ modinfo\ *)
102 xhtml_header
103 cat <<EOT
104 <h2>$(_ 'Kernel modules')</h2>
105 <p>$(_ 'Manage, search or get information about the Linux kernel modules')</p>
107 <form>
108 <input type="hidden" name="modules"/>
109 <input type="search" name="search" class="float-right" placeholder="$(_ 'Modules search')" results="5" autosave="modsearch" autocomplete="on"/>
110 </form>
111 EOT
112 # Request may be modinfo output that we want in the page itself
113 get_modinfo="$(GET modinfo)"
114 if [ -n "$get_modinfo" ]; then
115 cat <<EOT
116 <section>
117 <header>$(_ 'Detailed information for module: %s' $get_modinfo)</header>
119 EOT
120 modinfo $get_modinfo | awk 'BEGIN{print "<table class=\"wide zebra\">"}
121 {
122 printf("<tr><td><b>%s</b></td>", $1)
123 $1=""; printf("<td>%s</td></tr>", $0)
124 }
125 END{print "</table></section>"}'
126 fi
128 if [ -n "$(GET modprobe)" ]; then
129 echo "<pre>$(modprobe -v $(GET modprobe))</pre>"
130 fi
131 if [ -n "$(GET rmmod)" ]; then
132 echo "Removing"
133 rmmod -w $(GET rmmod)
134 fi
135 get_search="$(GET search)"
136 if [ -n "$get_search" ]; then
137 _ 'Matching result(s) for: %s' $get_search
138 echo '<pre>'
139 modprobe -l | grep "$(GET search)" | while read line
140 do
141 name=$(basename $line)
142 mod=${name%.ko.gz}
143 echo "$(_ 'Module:') <a href='?modinfo=$mod'>$mod</a>"
144 done
145 echo '</pre>'
146 fi
147 cat <<EOT
148 <section>
149 <table class="zebra">
150 <thead>
151 <tr>
152 <td>$(_ 'Module')</td>
153 <td>$(_ 'Description')</td>
154 <td>$(_ 'Size')</td>
155 <td>$(_ 'Used')</td>
156 <td>$(_ 'by')</td>
157 </tr>
158 <thead>
159 <tbody>
160 EOT
161 # Get the list of modules and link to modinfo
162 lsmod | tail -n+2 | awk '{
163 gsub(",", " ", $4);
164 printf("<tr><td><a href=\"?modinfo=%s\">%s</a></td><td>", $1, $1);
165 system("modinfo -d " $1);
166 printf("</td><td>%s</td><td>%s</td><td>%s</td></tr>", $2, $3, $4);
167 }'
168 cat <<EOT
169 </thead>
170 </table>
171 </section>
172 EOT
173 ;;
176 *\ lsusb\ *)
177 xhtml_header
178 vidpid="$(GET lsusb)"
179 cat <<EOT
180 <h2>$(_ 'Information for USB Device %s' $vidpid)</h2>
182 <p>$(_ 'Detailed information about specified device.')</p>
184 <section>$(lsusb_table)</section>
185 EOT
186 [ "$vidpid" != 'lsusb' ] && cat <<EOT
187 <section>
188 <pre style="white-space: pre-wrap">$(lsusb -vd $vidpid | syntax_highlighter lsusb)</pre>
189 </section>
190 EOT
191 ;;
194 *\ lspci\ *)
195 xhtml_header
196 slot="$(GET lspci)"
197 cat <<EOT
198 <h2>$(_ 'Information for PCI Device %s' $slot)</h2>
200 <p>$(_ 'Detailed information about specified device.')</p>
202 <section>$(lspci_table)</section>
203 EOT
204 [ "$slot" != 'lspci' ] && cat <<EOT
205 <section>
206 <pre style="white-space: pre-wrap">$(lspci -vs $slot | syntax_highlighter lspci)</pre>
207 </section>
208 EOT
209 ;;
212 *)
213 [ -n "$(GET brightness)" ] &&
214 echo -n $(GET brightness) > /sys/devices/virtual/backlight/$(GET dev)/brightness
216 #
217 # Default to summary with mounted filesystem, loaded modules
218 #
219 xhtml_header
220 cat <<EOT
221 <h2>$(_ 'Drivers &amp; Devices')</h2>
222 <p>$(_ 'Manage your computer hardware')</p>
224 <form><!--
225 --><button name="modules" data-icon="modules">$(_ 'Kernel modules')</button><!--
226 --><button name="detect" data-icon="detect" data-root>$(_ 'Detect PCI/USB')</button><!--
227 --><button name="tazx" data-icon="tazx" data-root>$(_ 'Auto-install Xorg video driver')</button><!--
228 --></form>
230 EOT
233 # Battery state
234 if [ -n "$(ls /proc/acpi/battery/*/info 2>/dev/null)" ]; then
235 cat <<EOT
236 <section>
237 <header>$(_ 'Battery')</header>
238 <div>
239 <table class="wide">
240 EOT
241 for dev in /proc/acpi/battery/*; do
242 grep ^present $dev/info | grep -q yes || continue
243 design=$(sed '/design capacity:/!d; s/[^0-9]*\([0-9]*\).*/\1/' < $dev/info)
244 remain=$(sed '/remaining capacity/!d; s/[^0-9]*\([0-9]*\).*/\1/' < $dev/state)
245 rate=$(sed '/present rate/!d; s/[^0-9]*\([0-9]*\).*/\1/' < $dev/state)
246 full=$(sed '/last full capacity/!d; s/[^0-9]*\([0-9]*\).*/\1/' < $dev/info)
247 warning=$(sed '/design capacity warning/!d;s/[^0-9]*\([0-9]*\).*/\1/' < $dev/info)
248 low=$(sed '/design capacity low/!d; s/[^0-9]*\([0-9]*\).*/\1/' < $dev/info)
249 state=$(sed '/charging state/!d; s/\([^:]*:[ ]\+\)\([a-z]\+\)/\2/' < $dev/state)
251 rempct=$(( $remain * 100 / $full ))
252 cat <<EOT
253 <tr>
254 <td><span data-icon="battery">$(_ 'Battery')</span>
255 $(grep "^battery type" $dev/info | sed 's/.*: *//')
256 $(grep "^design capacity:" $dev/info | sed 's/.*: *//') </td>
257 <td>$(_ 'health') $(( (100*$full)/$design))%</td>
258 <td class="meter"><meter min="0" max="$full" value="$remain" low="$low"
259 high="$warning" optimum="$full"></meter>
260 <span>
261 EOT
262 case "$state" in
263 "discharging")
264 remtime=$(( $remain * 60 / $rate ))
265 remtimef=$(printf "%d:%02d" $(($remtime/60)) $(($remtime%60)))
266 _ 'Discharging %d%% - %s' $rempct $remtimef ;;
267 "charging")
268 remtime=$(( ($full - $remain) * 60 / $rate ))
269 remtimef=$(printf "%d:%02d" $(($remtime/60)) $(($remtime%60)))
270 eval_gettext 'Charging %d%% - %s' $rempct $remtimef ;;
271 "charged")
272 _ 'Charged 100%' ;;
273 esac
274 cat <<EOT
276 </span>
277 </td>
278 </tr>
279 EOT
280 done
281 cat <<EOT
282 </table>
283 </div>
284 </section>
285 EOT
286 fi
289 # Thermal sensors
290 if [ -n "$(ls /sys/devices/virtual/thermal/*/temp 2>/dev/null)" ]; then
291 echo "<p><span data-icon=\"temperature\">$(_ 'Temperature:')</span>"
292 for temp in /sys/devices/virtual/thermal/*/temp; do
293 awk '{ print $1/1000 "℃" }' < $temp
294 done
295 echo '</p>'
296 fi
299 # Brightness
300 if [ -n "$(ls /sys/devices/virtual/backlight/*/brightness 2>/dev/null)" ]; then
301 echo '<form>'
302 for dev in /sys/devices/virtual/backlight/*/brightness; do
303 name=$(echo $dev | sed 's|.*/backlight/\([^/]*\).*|\1|')
304 cat <<EOT
305 <input type="hidden" name="dev" value="$name"/>
306 <span data-icon="brightness">$(_ 'Brightness')</span> \
307 $(sed 's/.*\.//;s/_*$//' < /sys/devices/virtual/backlight/$name/device/path):
308 <select name="brightness" onchange="submit();">
309 EOT
310 max=$(cat /sys/devices/virtual/backlight/$name/max_brightness)
311 for i in $(seq 0 $max); do
312 echo -n "<option value=\"$i\""
313 [ $i -eq $(cat /sys/devices/virtual/backlight/$name/actual_brightness) ] &&
314 echo -n " selected=\"selected\""
315 echo "> $(( (($i + 1) * 100) / ($max + 1) ))% </option>"
316 done
317 echo '</select>'
318 done
319 echo '</form>'
320 fi
323 cat <<EOT
324 <section>
325 <form action="#mount" class="wide">
326 <header id="disk">$(_ 'Filesystem usage statistics')</header>
327 <div>
328 <pre>$(disk_info)</pre>
329 </div>
330 EOT
333 #
334 # Loop device management actions
335 #
336 device=$(GET loopdev)
337 lib crypto $device
338 case "$device" in
339 /dev/*loop*)
340 set -- $(losetup | grep ^$device:)
341 [ -n "$3" ] && losetup -d $device
342 ro=""
343 [ -n "$(GET readonly)" ] && ro="-r"
344 file="$(GET backingfile)"
345 [ -n "$file" ] && losetup -o $(GET offset) $ro $device $file
346 esac
349 #
350 # Disk stats and management (mount, umount, check)
351 #
352 device=$(GET device)
353 lib crypto $device
354 case "$device" in
355 *[\;\`\&\|\$]*) ;;
356 mount\ *)
357 ro=""
358 [ -n "$(GET readonly)" ] && ro="-r"
359 mntdir="$(GET mountpoint)"
360 [ -d "$mntdir" ] || mkdir -p "$mntdir"
361 $device $ro "$mntdir";;
362 umount\ *|swapon\ *|swapoff\ *)
363 $device ;;
364 esac
365 cat <<EOT
366 <table id="mount" class="zebra wide center">
367 EOT
368 df_thead
369 echo '<tbody>'
370 for fs in $(blkid | sort | sed 's/:.*//'); do
371 set -- $(df -h | grep "^$fs ")
372 size=$2
373 used=$3
374 av=$4
375 grep "^$fs " /proc/mounts | grep -q "[, ]ro[, ]" &&
376 av="<del>$av</del>"
377 pct=$5
378 mp=$6
380 # action
381 action="mount"
382 [ -n "$mp" ] && action="umount"
383 type=$(blkid $fs | sed '/TYPE=/!d;s/.* TYPE="\([^"]*\).*/\1/')
384 [ "$type" == "swap" ] && action="swapon"
385 if grep -q "^$fs " /proc/swaps; then
386 action="swapoff"
387 set -- $(grep "^$fs " /proc/swaps)
388 size=$(blk2h $(($3*2)))
389 used=$(blk2h $(($4*2)))
390 av=$(blk2h $((2*($3-$4))))
391 pct=$(((100*$4)/$3))%
392 mp=swap
393 fi
395 # size
396 [ -z "$size" ] &&
397 size="$(blk2h $(cat /sys/block/${fs#/dev/}/size /sys/block/*/${fs#/dev/}/size))"
399 # image
400 disktype="hdd"
401 case "$(cat /sys/block/${fs#/dev/}/removable 2>/dev/null ||
402 cat /sys/block/${fs:5:3}/removable 2>/dev/null)" in
403 1) disktype="removable" ;;
404 esac
405 case "$(cat /sys/block/${fs#/dev/}/ro 2>/dev/null ||
406 cat /sys/block/${fs:5:3}/ro 2>/dev/null)" in
407 1) disktype="cd" ;;
408 esac
410 cat <<EOT
411 <tr>
412 <td><input type="radio" name="device" value="$action $fs" id="${fs#/dev/}"/><!--
413 --><label for="${fs#/dev/}" data-icon="$disktype">&thinsp;${fs#/dev/}</label></td>
414 <td>$(blkid $fs | sed '/LABEL=/!d;s/.*LABEL="\([^"]*\).*/\1/')</td>
415 <td>$type</td>
416 <td>$size</td>
417 <td>$av</td>
418 EOT
419 if [ -n "$pct" ]; then
420 cat <<EOT
421 <td class="meter"><meter min="0" max="100" value="${pct%%%}" low="70"
422 high="90" optimum="10"></meter>
423 <span>$used - $pct</span>
424 </td>
425 EOT
426 else
427 cat <<EOT
428 <td> </td>
429 EOT
430 fi
431 cat <<EOT
432 <td>$mp</td>
433 <td>$(blkid $fs | sed '/UUID=/!d;s/.*UUID="\([^"]*\).*/\1/')</td>
434 </tr>
435 EOT
436 done
437 cat <<EOT
438 </tbody>
439 </table>
440 EOT
441 [ "$REMOTE_USER" == "root" ] && cat <<EOT
442 $(lib crypto input)
444 <footer>
445 <button type="submit">mount / umount</button> -
446 $(_ 'new mount point:') <input type="text" name="mountpoint" value="/media/usbdisk"/> -
447 <input type="checkbox" name="readonly" id="ro"><label for="ro">&thinsp;$(_ 'read-only')</label>
448 </footer>
449 EOT
450 cat <<EOT
451 </form>
452 </section>
453 EOT
456 #
457 # /etc/fstab
458 #
459 cat <<EOT
460 <section>
461 <header>
462 $(_ 'Filesystems table')
463 EOT
464 [ -w /etc/fstab ] && cat <<EOT
465 <form action="index.cgi">
466 <input type="hidden" name="file" value="/etc/fstab"/>
467 <button name="action" value="edit" data-icon="edit">$(_ 'Edit')</button>
468 </form>
469 EOT
470 cat <<EOT
471 </header>
472 <table class="wide zebra center">
473 <thead>
474 <tr>
475 <td>$(_ 'Disk')</td>
476 <td>$(_ 'Mount point')</td>
477 <td>$(_ 'Type')</td>
478 <td>$(_ 'Options')</td>
479 <td>$(_ 'Freq')</td>
480 <td>$(_ 'Pass')</td>
481 </tr>
482 </thead>
483 <tbody>
484 EOT
486 grep -v '^#' /etc/fstab | awk '{
487 print "<tr><td>" $1 "</td><td>" $2 "</td><td>" $3 "</td><td>" $4
488 print "</td><td>" $5 "</td><td>" $6 "</td></tr>"
489 }
490 END{print "</tbody></table>"}'
492 cat <<EOT
493 </section>
494 EOT
497 #
498 # Loop device management GUI
499 #
500 cat <<EOT
501 <section>
502 <header>$(_ 'Loop devices')</header>
504 <form action="#loop" class="wide">
505 <div class="scroll">
506 <table id="loop" class="wide zebra scroll">
507 <thead>
508 <tr>
509 <td>$(_ 'Device')</td>
510 <td>$(_ 'Backing file')</td>
511 <td>$(_ 'Access')</td>
512 <td>$(_ 'Offset')</td>
513 </tr>
514 </thead>
515 <tbody>
516 EOT
517 for devloop in $(ls /dev/*loop[0-9]*); do
518 loop="${devloop#/dev/}"
519 case "$(cat /sys/block/$loop/ro 2>/dev/null)" in
520 0) ro="$(_ "read/write")" ;;
521 1) ro="$(_ "read only")" ;;
522 *) ro="" ;;
523 esac
524 set -- $(losetup | grep ^$devloop:) ${ro// /&nbsp;}
525 cat <<EOT
526 <tr><td><input type="radio" name="loopdev" value="$devloop" id="$loop"/><!--
527 --><label for="$loop" data-icon="loopback">$loop</label></td>
528 <td>$3</td><td align="center">$4</td><td align="right">$2</td>
529 </tr>
530 EOT
531 done
532 cat <<EOT
533 </tbody>
534 </table>
535 </div>
537 $(lib crypto input)
539 <footer>
540 <button type="submit" data-icon="ok">$(_ 'Setup')</button> -
541 $(_ 'new backing file:') <input type="text" name="backingfile"/> -
542 $(_ 'offset in bytes:') <input type="text" name="offset" value="0" size="8"/> -
543 <input type="checkbox" name="readonly" id="ro"/><label for="ro">$(_ 'read only')</label>
544 </footer>
545 </form>
546 </section>
547 EOT
550 #
551 # System memory
552 #
553 mem_total=$(free -m | awk '$1 ~ "M" {print $2}')
554 mem_used=$((100 * $(free -m | awk '$1 ~ "+" {print $3}') / mem_total))
555 mem_buff=$((100 * $(free -m | awk '$1 ~ "M" {print $6}') / mem_total))
556 mem_free=$((100 - mem_used - mem_buff))
558 cat <<EOT
559 <section>
560 <header>$(_ 'System memory')</header>
562 <div class="sysmem"><!--
563 --><span class="sysmem_used" style="width: ${mem_used}%" title="$(_ 'Used')" ><span>${mem_used}%</span></span><!--
564 EOT
565 [ $mem_buff != 0 ] && cat <<EOT
566 --><span class="sysmem_buff" style="width: ${mem_buff}%" title="$(_ 'Buffers')"><span>${mem_buff}%</span></span><!--
567 EOT
568 cat <<EOT
569 --><span class="sysmem_free" style="width: ${mem_free}%" title="$(_ 'Free')" ><span>${mem_free}%</span></span><!--
570 --></div>
572 <table class="wide zebra center">
573 <thead>
574 <tr>
575 <td>&nbsp;</td>
576 <td>total</td>
577 <td>used</td>
578 <td>free</td>
579 <td>shared</td>
580 <td>buffers</td>
581 </tr>
582 </thead>
583 <tbody>
584 EOT
586 free -m | awk '
587 $1 ~ "M" {print "<tr><td>"$1"</td><td>"$2"</td><td>"$3"</td><td>"$4"</td><td>"$5"</td><td>"$6"</td></tr>"}
588 $1 ~ "+" {print "<tr><td>"$1 $2"</td><td></td><td>"$3"</td><td>"$4"</td><td></td><td></td></tr>"}
589 $1 ~ "S" {print "<tr><td>"$1"</td><td>"$2"</td><td>"$3"</td><td>"$4"</td><td></td><td></td></tr>"}'
591 cat <<EOT
592 </tbody>
593 </table>
594 </section>
595 EOT
598 #
599 # lspci and lsusb summary tables
600 #
601 cat <<EOT
602 <section>
603 <header>lspci</header>
604 $(lspci_table)
605 </section>
608 <section>
609 <header>lsusb</header>
610 $(lsusb_table)
611 </section>
612 EOT
613 ;;
614 esac
616 xhtml_footer
617 exit 0