tazpanel view hardware.cgi @ rev 439

index.cgi: restore header in the file viewer; network.cgi: show both disabled start/stop/restart buttons for user.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Tue Apr 07 04:12:24 2015 +0300 (2015-04-07)
parents fa8dd339b418
children 169f1ccfb613
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 }
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>$(gettext 'Bus')</td>
36 <td>$(gettext 'Device')</td>
37 <td>$(gettext 'ID')</td>
38 <td>$(gettext '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>$(gettext 'Detect hardware')</h2>
91 <p>$(gettext '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>$(gettext 'Kernel modules')</h2>
106 <form id="actions">
107 <input type="hidden" name="modules"/>
108 $(gettext 'Manage, search or get information about the Linux kernel modules')
110 <input type="search" name="search" class="float-right" placeholder="$(gettext 'Modules search')" results="5" autosave="modsearch" autocomplete="on"/>
111 </form>
112 EOT
113 # Request may be modinfo output that we want in the page itself
114 get_modinfo="$(GET modinfo)"
115 if [ -n "$get_modinfo" ]; then
116 cat <<EOT
117 <section>
118 <header>$(eval_gettext 'Detailed information for module: $get_modinfo')</header>
120 EOT
121 modinfo $get_modinfo | awk 'BEGIN{print "<table class=\"wide zebra\">"}
122 {
123 printf("<tr><td><b>%s</b></td>", $1)
124 $1=""; printf("<td>%s</td></tr>", $0)
125 }
126 END{print "</table></section>"}'
127 fi
129 if [ -n "$(GET modprobe)" ]; then
130 echo "<pre>$(modprobe -v $(GET modprobe))</pre>"
131 fi
132 if [ -n "$(GET rmmod)" ]; then
133 echo "Removing"
134 rmmod -w $(GET rmmod)
135 fi
136 get_search="$(GET search)"
137 if [ -n "$get_search" ]; then
138 eval_gettext 'Matching result(s) for: $get_search'
139 echo '<pre>'
140 modprobe -l | grep "$(GET search)" | while read line
141 do
142 name=$(basename $line)
143 mod=${name%.ko.gz}
144 echo "$(gettext 'Module:') <a href='?modinfo=$mod'>$mod</a>"
145 done
146 echo '</pre>'
147 fi
148 cat <<EOT
149 <section>
150 <table class="zebra">
151 <thead>
152 <tr>
153 <td>$(gettext 'Module')</td>
154 <td>$(gettext 'Description')</td>
155 <td>$(gettext 'Size')</td>
156 <td>$(gettext 'Used')</td>
157 <td>$(gettext 'by')</td>
158 </tr>
159 <thead>
160 <tbody>
161 EOT
162 # Get the list of modules and link to modinfo
163 lsmod | tail -n+2 | awk '{
164 gsub(",", " ", $4);
165 printf("<tr><td><a href=\"?modinfo=%s\">%s</a></td><td>", $1, $1);
166 system("modinfo -d " $1);
167 printf("</td><td>%s</td><td>%s</td><td>%s</td></tr>", $2, $3, $4);
168 }'
169 cat <<EOT
170 </thead>
171 </table>
172 </section>
173 EOT
174 ;;
177 *\ lsusb\ *)
178 xhtml_header
179 vidpid="$(GET lsusb)"
180 cat <<EOT
181 <h2>$(eval_gettext 'Information for USB Device $vidpid')</h2>
183 <p>$(gettext 'Detailed information about specified device.')</p>
185 <section>$(lsusb_table)</section>
186 EOT
187 [ "$vidpid" != 'lsusb' ] && cat <<EOT
188 <section>
189 <pre style="white-space: pre-wrap">$(lsusb -vd $vidpid | syntax_highlighter lsusb)</pre>
190 </section>
191 EOT
192 ;;
195 *\ lspci\ *)
196 xhtml_header
197 slot="$(GET lspci)"
198 cat <<EOT
199 <h2>$(eval_gettext 'Information for PCI Device $slot')</h2>
201 <p>$(gettext 'Detailed information about specified device.')</p>
203 <section>$(lspci_table)</section>
204 EOT
205 [ "$slot" != 'lspci' ] && cat <<EOT
206 <section>
207 <pre style="white-space: pre-wrap">$(lspci -vs $slot | syntax_highlighter lspci)</pre>
208 </section>
209 EOT
210 ;;
213 *)
214 [ -n "$(GET brightness)" ] &&
215 echo -n $(GET brightness) > /sys/devices/virtual/backlight/$(GET dev)/brightness
217 #
218 # Default to summary with mounted filesystem, loaded modules
219 #
220 xhtml_header
221 cat <<EOT
222 <h2>$(gettext 'Drivers &amp; Devices')</h2>
223 <p>$(gettext 'Manage your computer hardware')</p>
225 <form><!--
226 --><button name="modules" data-icon="modules">$(gettext 'Kernel modules')</button><!--
227 --><button name="detect" data-icon="detect" data-root>$(gettext 'Detect PCI/USB')</button><!--
228 --><button name="tazx" data-icon="tazx" data-root>$(gettext 'Auto-install Xorg video driver')</button><!--
229 --></form>
231 EOT
234 # Battery state
235 if [ -n "$(ls /proc/acpi/battery/*/info 2>/dev/null)" ]; then
236 cat <<EOT
237 <section>
238 <header>$(gettext 'Battery')</header>
239 <div>
240 <table class="wide">
241 EOT
242 for dev in /proc/acpi/battery/*; do
243 grep ^present $dev/info | grep -q yes || continue
244 design=$(sed '/design capacity:/!d; s/[^0-9]*\([0-9]*\).*/\1/' < $dev/info)
245 remain=$(sed '/remaining capacity/!d; s/[^0-9]*\([0-9]*\).*/\1/' < $dev/state)
246 rate=$(sed '/present rate/!d; s/[^0-9]*\([0-9]*\).*/\1/' < $dev/state)
247 full=$(sed '/last full capacity/!d; s/[^0-9]*\([0-9]*\).*/\1/' < $dev/info)
248 warning=$(sed '/design capacity warning/!d;s/[^0-9]*\([0-9]*\).*/\1/' < $dev/info)
249 low=$(sed '/design capacity low/!d; s/[^0-9]*\([0-9]*\).*/\1/' < $dev/info)
250 state=$(sed '/charging state/!d; s/\([^:]*:[ ]\+\)\([a-z]\+\)/\2/' < $dev/state)
252 rempct=$(( $remain * 100 / $full ))
253 cat <<EOT
254 <tr>
255 <td><span data-icon="battery">$(gettext 'Battery')</span>
256 $(grep "^battery type" $dev/info | sed 's/.*: *//')
257 $(grep "^design capacity:" $dev/info | sed 's/.*: *//') </td>
258 <td>$(gettext 'health') $(( (100*$full)/$design))%</td>
259 <td class="meter"><meter min="0" max="$full" value="$remain" low="$low"
260 high="$warning" optimum="$full"></meter>
261 <span>
262 EOT
263 case "$state" in
264 "discharging")
265 remtime=$(( $remain * 60 / $rate ))
266 remtimef=$(printf "%d:%02d" $(($remtime/60)) $(($remtime%60)))
267 eval_gettext 'Discharging $rempct% - $remtimef' ;;
268 "charging")
269 remtime=$(( ($full - $remain) * 60 / $rate ))
270 remtimef=$(printf "%d:%02d" $(($remtime/60)) $(($remtime%60)))
271 eval_gettext 'Charging $rempct% - $remtimef' ;;
272 "charged")
273 gettext 'Charged 100%' ;;
274 esac
275 cat <<EOT
277 </span>
278 </td>
279 </tr>
280 EOT
281 done
282 cat <<EOT
283 </table>
284 </div>
285 </section>
286 EOT
287 fi
290 # Thermal sensors
291 if [ -n "$(ls /sys/devices/virtual/thermal/*/temp 2>/dev/null)" ]; then
292 echo "<p><span data-icon=\"temperature\">$(gettext 'Temperature:')</span>"
293 for temp in /sys/devices/virtual/thermal/*/temp; do
294 awk '{ print $1/1000 "℃" }' < $temp
295 done
296 echo '</p>'
297 fi
300 # Brightness
301 if [ -n "$(ls /sys/devices/virtual/backlight/*/brightness 2>/dev/null)" ]; then
302 echo '<form>'
303 for dev in /sys/devices/virtual/backlight/*/brightness; do
304 name=$(echo $dev | sed 's|.*/backlight/\([^/]*\).*|\1|')
305 cat <<EOT
306 <input type="hidden" name="dev" value="$name"/>
307 <span data-icon="brightness">$(gettext 'Brightness')</span> \
308 $(sed 's/.*\.//;s/_*$//' < /sys/devices/virtual/backlight/$name/device/path):
309 <select name="brightness" onchange="submit();">
310 EOT
311 max=$(cat /sys/devices/virtual/backlight/$name/max_brightness)
312 for i in $(seq 0 $max); do
313 echo -n "<option value=\"$i\""
314 [ $i -eq $(cat /sys/devices/virtual/backlight/$name/actual_brightness) ] &&
315 echo -n " selected=\"selected\""
316 echo "> $(( (($i + 1) * 100) / ($max + 1) ))% </option>"
317 done
318 echo '</select>'
319 done
320 echo '</form>'
321 fi
324 cat <<EOT
325 <section>
326 <form action="#mount" class="wide">
327 <header id="disk">$(gettext 'Filesystem usage statistics')</header>
328 <div>
329 <pre>$(disk_info)</pre>
330 </div>
331 EOT
334 #
335 # Loop device management actions
336 #
337 device=$(GET loopdev)
338 lib crypto $device
339 case "$device" in
340 /dev/*loop*)
341 set -- $(losetup | grep ^$device:)
342 [ -n "$3" ] && losetup -d $device
343 ro=""
344 [ -n "$(GET readonly)" ] && ro="-r"
345 file="$(GET backingfile)"
346 [ -n "$file" ] && losetup -o $(GET offset) $ro $device $file
347 esac
350 #
351 # Disk stats and management (mount, umount, check)
352 #
353 device=$(GET device)
354 lib crypto $device
355 case "$device" in
356 *[\;\`\&\|\$]*) ;;
357 mount\ *)
358 ro=""
359 [ -n "$(GET readonly)" ] && ro="-r"
360 mntdir="$(GET mountpoint)"
361 [ -d "$mntdir" ] || mkdir -p "$mntdir"
362 $device $ro "$mntdir";;
363 umount\ *|swapon\ *|swapoff\ *)
364 $device ;;
365 esac
366 cat <<EOT
367 <table id="mount" class="zebra wide center">
368 EOT
369 df_thead
370 echo '<tbody>'
371 for fs in $(blkid | sort | sed 's/:.*//'); do
372 set -- $(df -h | grep "^$fs ")
373 size=$2
374 used=$3
375 av=$4
376 grep "^$fs " /proc/mounts | grep -q "[, ]ro[, ]" &&
377 av="<del>$av</del>"
378 pct=$5
379 mp=$6
381 # action
382 action="mount"
383 [ -n "$mp" ] && action="umount"
384 type=$(blkid $fs | sed '/TYPE=/!d;s/.* TYPE="\([^"]*\).*/\1/')
385 [ "$type" == "swap" ] && action="swapon"
386 if grep -q "^$fs " /proc/swaps; then
387 action="swapoff"
388 set -- $(grep "^$fs " /proc/swaps)
389 size=$(blk2h $(($3*2)))
390 used=$(blk2h $(($4*2)))
391 av=$(blk2h $((2*($3-$4))))
392 pct=$(((100*$4)/$3))%
393 mp=swap
394 fi
396 # size
397 [ -z "$size" ] &&
398 size="$(blk2h $(cat /sys/block/${fs#/dev/}/size /sys/block/*/${fs#/dev/}/size))"
400 # image
401 disktype="hdd"
402 case "$(cat /sys/block/${fs#/dev/}/removable 2>/dev/null ||
403 cat /sys/block/${fs:5:3}/removable 2>/dev/null)" in
404 1) disktype="removable" ;;
405 esac
406 case "$(cat /sys/block/${fs#/dev/}/ro 2>/dev/null ||
407 cat /sys/block/${fs:5:3}/ro 2>/dev/null)" in
408 1) disktype="cd" ;;
409 esac
411 cat <<EOT
412 <tr>
413 <td><input type="radio" name="device" value="$action $fs" id="${fs#/dev/}"/><!--
414 --><label for="${fs#/dev/}" data-icon="$disktype">&thinsp;${fs#/dev/}</label></td>
415 <td>$(blkid $fs | sed '/LABEL=/!d;s/.*LABEL="\([^"]*\).*/\1/')</td>
416 <td>$type</td>
417 <td>$size</td>
418 <td>$av</td>
419 EOT
420 if [ -n "$pct" ]; then
421 cat <<EOT
422 <td class="meter"><meter min="0" max="100" value="${pct%%%}" low="70"
423 high="90" optimum="10"></meter>
424 <span>$used - $pct</span>
425 </td>
426 EOT
427 else
428 cat <<EOT
429 <td> </td>
430 EOT
431 fi
432 cat <<EOT
433 <td>$mp</td>
434 <td>$(blkid $fs | sed '/UUID=/!d;s/.*UUID="\([^"]*\).*/\1/')</td>
435 </tr>
436 EOT
437 done
438 cat <<EOT
439 </tbody>
440 </table>
441 EOT
442 [ "$REMOTE_USER" == "root" ] && cat <<EOT
443 $(lib crypto input)
445 <footer>
446 <button type="submit">mount / umount</button> -
447 $(gettext 'new mount point:') <input type="text" name="mountpoint" value="/media/usbdisk"/> -
448 <input type="checkbox" name="readonly" id="ro"><label for="ro">&thinsp;$(gettext 'read-only')</label>
449 </footer>
450 EOT
451 cat <<EOT
452 </form>
453 </section>
454 EOT
457 #
458 # /etc/fstab
459 #
460 cat <<EOT
461 <section>
462 <header>
463 $(gettext 'Filesystems table')
464 EOT
465 [ -w /etc/fstab ] && cat <<EOT
466 <form action="index.cgi">
467 <input type="hidden" name="file" value="/etc/fstab"/>
468 <button name="action" value="edit" data-icon="edit">$(gettext 'Edit')</button>
469 </form>
470 EOT
471 cat <<EOT
472 </header>
473 <table class="wide zebra center">
474 <thead>
475 <tr>
476 <td>$(gettext 'Disk')</td>
477 <td>$(gettext 'Mount point')</td>
478 <td>$(gettext 'Type')</td>
479 <td>$(gettext 'Options')</td>
480 <td>$(gettext 'Freq')</td>
481 <td>$(gettext 'Pass')</td>
482 </tr>
483 </thead>
484 <tbody>
485 EOT
487 grep -v '^#' /etc/fstab | awk '{
488 print "<tr><td>" $1 "</td><td>" $2 "</td><td>" $3 "</td><td>" $4
489 print "</td><td>" $5 "</td><td>" $6 "</td></tr>"
490 }
491 END{print "</tbody></table>"}'
493 cat <<EOT
494 </section>
495 EOT
498 #
499 # Loop device management GUI
500 #
501 cat <<EOT
502 <section>
503 <header>$(gettext 'Loop devices')</header>
505 <form action="#loop" class="wide">
506 <div class="scroll">
507 <table id="loop" class="wide zebra scroll">
508 <thead>
509 <tr>
510 <td>$(gettext 'Device')</td>
511 <td>$(gettext 'Backing file')</td>
512 <td>$(gettext 'Access')</td>
513 <td>$(gettext 'Offset')</td>
514 </tr>
515 </thead>
516 <tbody>
517 EOT
518 for devloop in $(ls /dev/*loop[0-9]*); do
519 loop="${devloop#/dev/}"
520 case "$(cat /sys/block/$loop/ro 2>/dev/null)" in
521 0) ro="$(gettext "read/write")" ;;
522 1) ro="$(gettext "read only")" ;;
523 *) ro="" ;;
524 esac
525 set -- $(losetup | grep ^$devloop:) ${ro// /&nbsp;}
526 cat <<EOT
527 <tr><td><input type="radio" name="loopdev" value="$devloop" id="$loop"/><!--
528 --><label for="$loop" data-icon="loopback">$loop</label></td>
529 <td>$3</td><td align="center">$4</td><td align="right">$2</td>
530 </tr>
531 EOT
532 done
533 cat <<EOT
534 </tbody>
535 </table>
536 </div>
538 $(lib crypto input)
540 <footer>
541 <button type="submit" data-icon="ok">$(gettext 'Setup')</button> -
542 $(gettext 'new backing file:') <input type="text" name="backingfile"/> -
543 $(gettext 'offset in bytes:') <input type="text" name="offset" value="0" size="8"/> -
544 <input type="checkbox" name="readonly" id="ro"/><label for="ro">$(gettext 'read only')</label>
545 </footer>
546 </form>
547 </section>
548 EOT
551 #
552 # System memory
553 #
554 mem_total=$(free -m | awk '$1 ~ "M" {print $2}')
555 mem_used=$((100 * $(free -m | awk '$1 ~ "+" {print $3}') / mem_total))
556 mem_buff=$((100 * $(free -m | awk '$1 ~ "M" {print $6}') / mem_total))
557 mem_free=$((100 - mem_used - mem_buff))
559 cat <<EOT
560 <section>
561 <header>$(gettext 'System memory')</header>
563 <div class="sysmem"><!--
564 --><span class="sysmem_used" style="width: ${mem_used}%" title="$(gettext 'Used')" ><span>${mem_used}%</span></span><!--
565 EOT
566 [ $mem_buff != 0 ] && cat <<EOT
567 --><span class="sysmem_buff" style="width: ${mem_buff}%" title="$(gettext 'Buffers')"><span>${mem_buff}%</span></span><!--
568 EOT
569 cat <<EOT
570 --><span class="sysmem_free" style="width: ${mem_free}%" title="$(gettext 'Free')" ><span>${mem_free}%</span></span><!--
571 --></div>
573 <table class="wide zebra center">
574 <thead>
575 <tr>
576 <td>&nbsp;</td>
577 <td>total</td>
578 <td>used</td>
579 <td>free</td>
580 <td>shared</td>
581 <td>buffers</td>
582 </tr>
583 </thead>
584 <tbody>
585 EOT
587 free -m | awk '
588 $1 ~ "M" {print "<tr><td>"$1"</td><td>"$2"</td><td>"$3"</td><td>"$4"</td><td>"$5"</td><td>"$6"</td></tr>"}
589 $1 ~ "+" {print "<tr><td>"$1 $2"</td><td></td><td>"$3"</td><td>"$4"</td><td></td><td></td></tr>"}
590 $1 ~ "S" {print "<tr><td>"$1"</td><td>"$2"</td><td>"$3"</td><td>"$4"</td><td></td><td></td></tr>"}'
592 cat <<EOT
593 </tbody>
594 </table>
595 </section>
596 EOT
599 #
600 # lspci and lsusb summary tables
601 #
602 cat <<EOT
603 <section>
604 <header>lspci</header>
605 $(lspci_table)
606 </section>
609 <section>
610 <header>lsusb</header>
611 $(lsusb_table)
612 </section>
613 EOT
614 ;;
615 esac
617 xhtml_footer
618 exit 0