tazpanel view hardware.cgi @ rev 428

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