tazpanel view hardware.cgi @ rev 618

hardware.cgi: add SSD detection
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Nov 02 21:57:05 2017 +0100 (2017-11-02)
parents 26f60e49e3d5
children 9ecd97ecafa7
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=$(_ '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 t="HD"
27 [ "$(cat /sys/block/${d%:}/queue/rotational)" -eq "0" ] && t="SSD"
28 [ -d $d ] && echo "$a $b $c, $t $(cat $d/vendor) $(cat $d/model)"
29 smartctl -a ${b%:} | sed '/^Model/,/^Firmware/!d'
30 done 2> /dev/null | sed 's/ */ /g'
31 }
33 lsusb_vd() {
34 if lsusb --help 2>&1 | grep -qi busybox ; then
35 for i in /sys/class/usb_device/*/device ; do
36 grep -qs ${1%:*} $i/idVendor || continue
37 grep -qs ${1#*:} $i/idProduct || continue
38 ( cd $i ; for j in * ; do
39 [ -f $j -a -r $j ] || continue
40 case "$j" in
41 descriptors|remove) continue
42 esac
43 awk -vn=$j '{printf "%-20s %s\n",n,$0;n=""}' <$j
44 done )
45 break
46 done
47 else
48 lsusb -vd $1 | syntax_highlighter lsusb
49 fi
50 }
52 lsusb_table() {
53 cat <<EOT
54 <table class="wide zebra">
55 <thead>
56 <tr>
57 <td>$(_ 'Bus')</td>
58 <td>$(_ 'Device')</td>
59 <td>$(_ 'ID')</td>
60 <td>$(_ 'Name')</td>
61 </tr>
62 </thead>
63 <tbody>
64 EOT
65 lsusb | while read x b y d z id name ; do
66 echo "<tr><td>$b</td><td>${d%:}</td><td><a href=\"?lsusb=$id"
67 p=$(printf "class/usb_device/usbdev%d.%d" $b ${d%:})
68 grep -qs 0 /sys/$p/device/authorized && id="<del>$id</del>"
69 echo "\">$id</td><td>${name:-$(sed 's/ .*//' /sys/$p/device/manufacturer) $(cat /sys/$p/device/product)}</td></tr>"
70 done
71 echo "</tbody></table>"
72 }
75 lspci_table() {
76 cat <<EOT
77 <table class="wide zebra">
78 <thead><tr>
79 <td>$(_ 'Slot')</td>
80 <td>$(_ 'Device')</td>
81 <td>$(_ 'Name')</td>
82 </tr></thead>
83 <tbody>
84 EOT
85 lspci | while read a b c id ; do
86 if [ $b != "Class" ] || [ ! -s /usr/share/misc/pci.ids.gz ]; then
87 echo "X$a $b $c $id"
88 continue
89 fi
90 echo -n "$a "
91 zcat /usr/share/misc/pci.ids.gz | \
92 awk -va=${c:0:2} -vb=${c:2:2} -vh=${id:0:4} -vl=${id:5:4} '{
93 if ($1 == "C" && $2 == a) class=substr($0,5)
94 if (class != "" && $1 == b) { class=substr($0,5); exit }
95 if (substr($0,1,4) == h) m=substr($0,7)
96 else if (m == "") next
97 else if (substr($0,2,4) == l) { name=m substr($0,7); m="" }
98 else if ($1 == h && $2 == l) { name=m substr($0,14); m="" }
99 } END { print "[" a b "] " class ": [" h "/" l "] " name }'
100 done | sed 's| |</td><td>|;
101 s|: |</td><td>|;
102 s|^X\([^<]*\)|<a href="?lspci=\1">\1</a>|;
103 s|^.*$|<tr><td>\0</td></tr>|'
104 echo "</tbody></table>"
105 }
108 #
109 # Commands
110 #
112 case " $(GET) " in
113 *\ print\ *)
114 xhtml_header
115 echo "<h2>TODO</h2>"
116 ;;
119 *\ tazx\ *)
120 xhtml_header
121 cat <<EOT
122 <pre>$(tazx auto)</pre>
123 EOT
124 ;;
127 *\ detect\ *)
128 # Front end for Tazhw
129 # TODO: Add button to detect webcam, etc. Like in tazhw box.
130 xhtml_header "$(_ 'Detect hardware')"
131 cat <<EOT
132 <p>$(_ 'Detect PCI and USB hardware')</p>
133 EOT
134 tazhw detect-pci | htmlize
135 tazhw detect-usb | htmlize | filter_taztools_msgs
136 ;;
139 *\ modules\ *|*\ modinfo\ *)
140 xhtml_header "$(_ 'Kernel modules')"
142 search="$(GET search)"
143 cat <<EOT
144 <p>$(_ 'Manage, search or get information about the Linux kernel modules')</p>
146 <form class="search">
147 <input type="hidden" name="modules"/>
148 <input type="search" name="search" value="$search" placeholder="$(_ 'Modules search')" results="5" autosave="modsearch" autocomplete="on"/>
149 <button type="submit">$(_n 'Search')</button>
150 </form>
151 EOT
152 # Request may be modinfo output that we want in the page itself
153 get_modinfo="$(GET modinfo)"
154 if [ -n "$get_modinfo" ]; then
155 cat <<EOT
156 <section>
157 <header>$(_ 'Detailed information for module: %s' $get_modinfo)</header>
158 <div class="scroll">
160 EOT
161 modinfo $get_modinfo | awk 'BEGIN{print "<table class=\"wide zebra\">"}
162 {
163 printf("<tr><td><b>%s</b></td>", $1)
164 $1=""; printf("<td>%s</td></tr>", $0)
165 }
166 END{print "</table></div></section>"}'
167 fi
169 if [ -n "$(GET modprobe)" ]; then
170 echo "<pre>$(modprobe -v $(GET modprobe))</pre>"
171 fi
172 if [ -n "$(GET rmmod)" ]; then
173 echo "Removing"
174 rmmod -w $(GET rmmod)
175 fi
177 # Module search
178 if [ -n "$search" ]; then
179 cat <<EOT
180 <section>
181 <header>$(_ 'Matching result(s) for: %s' $search)</header>
182 <pre class="scroll">
183 EOT
184 busybox modprobe -l | grep "$search" | while read line
185 do
186 name=$(basename $line)
187 mod=${name%.ko.xz}
188 echo "<span data-icon=\"@modules@\">$(_ 'Module:')</span> <a href='?modinfo=$mod'>$mod</a>"
189 done
190 echo '</pre></section>'
191 fi
193 cat <<EOT
194 <section>
195 <table class="zebra">
196 <thead>
197 <tr>
198 <td>$(_ 'Module')</td>
199 <td>$(_ 'Description')</td>
200 <td>$(_ 'Size')</td>
201 <td>$(_ 'Used')</td>
202 <td>$(_ 'by')</td>
203 </tr>
204 <thead>
205 <tbody>
206 EOT
207 # Get the list of modules and link to modinfo
208 lsmod | tail -n+2 | awk '{
209 gsub(",", " ", $4);
210 printf("<tr><td><a href=\"?modinfo=%s\">%s</a></td><td>", $1, $1);
211 system("modinfo -d " $1);
212 printf("</td><td>%s</td><td>%s</td><td>%s</td></tr>", $2, $3, $4);
213 }'
214 cat <<EOT
215 </thead>
216 </table>
217 </section>
218 EOT
219 ;;
222 *\ lsusb\ *)
223 xhtml_header
224 vidpid="$(GET lsusb)"
225 cat <<EOT
226 <h2>$(_ 'Information for USB Device %s' $vidpid)</h2>
228 <p>$(_ 'Detailed information about specified device.')</p>
230 <section>$(lsusb_table)</section>
231 EOT
232 [ "$vidpid" != 'lsusb' ] && cat <<EOT
233 <section>
234 <pre style="white-space: pre-wrap">$(lsusb_vd $vidpid)</pre>
235 </section>
236 EOT
237 ;;
240 *\ lspci\ *)
241 xhtml_header
242 slot="$(GET lspci)"
243 cat <<EOT
244 <h2>$(_ 'Information for PCI Device %s' $slot)</h2>
246 <p>$(_ 'Detailed information about specified device.')</p>
248 <section>$(lspci_table)</section>
249 EOT
250 [ "$slot" != 'lspci' ] && cat <<EOT
251 <section>
252 <pre style="white-space: pre-wrap">$(lspci -vs $slot | syntax_highlighter lspci)</pre>
253 </section>
254 EOT
255 ;;
258 *)
259 [ -n "$(GET brightness)" ] &&
260 echo -n $(GET brightness) > /sys/devices/virtual/backlight/$(GET dev)/brightness
262 #
263 # Default to summary with mounted filesystem, loaded modules
264 #
265 xhtml_header "$(_ 'Drivers &amp; Devices')"
266 cat <<EOT
267 <p>$(_ 'Manage your computer hardware')</p>
269 <form><!--
270 --><button name="modules" data-icon="@modules@">$(_ 'Kernel modules')</button><!--
271 --><button name="detect" data-icon="@detect@" data-root>$(_ 'Detect PCI/USB')</button><!--
272 --><button name="tazx" data-icon="@tazx@" data-root>$(_ 'Auto-install Xorg video driver')</button><!--
273 --></form>
275 EOT
278 # Battery state
279 if [ -n "$(ls /proc/acpi/battery/*/info 2>/dev/null)" ]; then
280 cat <<EOT
281 <section>
282 <header>$(_ 'Battery')</header>
283 <div>
284 <table class="wide">
285 EOT
286 for dev in /proc/acpi/battery/*; do
287 grep ^present $dev/info | grep -q yes || continue
288 design=$(sed '/design capacity:/!d; s/[^0-9]*\([0-9]*\).*/\1/' < $dev/info)
289 remain=$(sed '/remaining capacity/!d; s/[^0-9]*\([0-9]*\).*/\1/' < $dev/state)
290 rate=$(sed '/present rate/!d; s/[^0-9]*\([0-9]*\).*/\1/' < $dev/state)
291 full=$(sed '/last full capacity/!d; s/[^0-9]*\([0-9]*\).*/\1/' < $dev/info)
292 warning=$(sed '/design capacity warning/!d;s/[^0-9]*\([0-9]*\).*/\1/' < $dev/info)
293 low=$(sed '/design capacity low/!d; s/[^0-9]*\([0-9]*\).*/\1/' < $dev/info)
294 state=$(sed '/charging state/!d; s/\([^:]*:[ ]\+\)\([a-z]\+\)/\2/' < $dev/state)
296 rempct=$(( $remain * 100 / $full ))
297 cat <<EOT
298 <tr>
299 <td><span data-icon="@battery@">$(_ 'Battery')</span>
300 $(grep "^battery type" $dev/info | sed 's/.*: *//')
301 $(grep "^design capacity:" $dev/info | sed 's/.*: *//') </td>
302 <td>$(_ 'health') $(( (100*$full)/$design))%</td>
303 <td class="meter"><meter min="0" max="$full" value="$remain" low="$low"
304 high="$warning" optimum="$full"></meter>
305 <span>
306 EOT
307 case "$state" in
308 "discharging")
309 remtime=$(( $remain * 60 / $rate ))
310 remtimef=$(printf "%d:%02d" $(($remtime/60)) $(($remtime%60)))
311 _ 'Discharging %d%% - %s' $rempct $remtimef ;;
312 "charging")
313 remtime=$(( ($full - $remain) * 60 / $rate ))
314 remtimef=$(printf "%d:%02d" $(($remtime/60)) $(($remtime%60)))
315 _ 'Charging %d%% - %s' $rempct $remtimef ;;
316 "charged")
317 _ 'Charged 100%%' ;;
318 esac
319 cat <<EOT
321 </span>
322 </td>
323 </tr>
324 EOT
325 done
326 cat <<EOT
327 </table>
328 </div>
329 </section>
330 EOT
331 fi
334 # Thermal sensors
335 if [ -n "$(ls /sys/devices/virtual/thermal/*/temp 2>/dev/null)" ]; then
336 echo "<p><span data-icon=\"@temperature@\">$(_ 'Temperature:')</span>"
337 for temp in /sys/devices/virtual/thermal/*/temp; do
338 awk '{ print $1/1000 "℃" }' < $temp
339 done
340 echo '</p>'
341 fi
344 # Brightness
345 if [ -n "$(ls /sys/devices/virtual/backlight/*/brightness 2>/dev/null)" ]; then
346 echo '<form>'
347 for dev in /sys/devices/virtual/backlight/*/brightness; do
348 name=$(echo $dev | sed 's|.*/backlight/\([^/]*\).*|\1|')
349 cat <<EOT
350 <input type="hidden" name="dev" value="$name"/>
351 <span data-icon="@brightness@">$(_ 'Brightness')</span> \
352 $(sed 's/.*\.//;s/_*$//' < /sys/devices/virtual/backlight/$name/device/path):
353 <select name="brightness" onchange="submit();">
354 EOT
355 max=$(cat /sys/devices/virtual/backlight/$name/max_brightness)
356 for i in $(seq 0 $max); do
357 echo -n "<option value=\"$i\""
358 [ $i -eq $(cat /sys/devices/virtual/backlight/$name/actual_brightness) ] &&
359 echo -n " selected=\"selected\""
360 echo "> $(( (($i + 1) * 100) / ($max + 1) ))% </option>"
361 done
362 echo '</select>'
363 done
364 echo '</form>'
365 fi
368 cat <<EOT
369 <section>
370 <form action="#mount" class="wide">
371 <header id="disk">$(_ 'Filesystem usage statistics')</header>
372 <div>
373 <pre>$(disk_info)</pre>
374 </div>
375 EOT
378 #
379 # Loop device management actions
380 #
381 device=$(GET loopdev)
382 lib crypto $device
383 case "$device" in
384 /dev/*loop*)
385 set -- $(losetup | grep ^$device:)
386 [ -n "$3" ] && losetup -d $device
387 ro=""
388 [ -n "$(GET readonly)" ] && ro="-r"
389 file="$(GET backingfile)"
390 [ -n "$file" ] && losetup -o $(GET offset) $ro $device $file
391 esac
394 #
395 # Disk stats and management (mount, umount, check)
396 #
397 device=$(GET device)
398 lib crypto $device
399 case "$device" in
400 *[\;\`\&\|\$]*) ;;
401 mount\ *)
402 ro=""
403 [ -n "$(GET readonly)" ] && ro="-r"
404 mntdir="$(GET mountpoint)"
405 [ -d "$mntdir" ] || mkdir -p "$mntdir"
406 $device $ro "$mntdir";;
407 umount\ *|swapon\ *|swapoff\ *)
408 $device ;;
409 esac
410 cat <<EOT
411 <table id="mount" class="zebra wide center">
412 EOT
413 df_thead
414 echo '<tbody>'
415 for fs in $(blkid | sort | sed 's/:.*//'); do
416 set -- $(df -h | grep "^$fs ")
417 size=$2
418 used=$3
419 av=$4
420 grep "^$fs " /proc/mounts | grep -q "[, ]ro[, ]" &&
421 av="<del>$av</del>"
422 pct=$5
423 mp=$6
425 # action
426 action="mount"
427 [ -n "$mp" ] && action="umount"
428 type=$(blkid $fs | sed '/ TYPE=/!d;s/.* TYPE="\([^"]*\).*/\1/')
429 [ -n "$type" ] || continue
430 [ "$type" == "swap" ] && action="swapon"
431 if grep -q "^$fs " /proc/swaps; then
432 action="swapoff"
433 set -- $(grep "^$fs " /proc/swaps)
434 size=$(blk2h $(($3*2)))
435 used=$(blk2h $(($4*2)))
436 av=$(blk2h $((2*($3-$4))))
437 pct=$(((100*$4)/$3))%
438 mp=swap
439 fi
441 # size
442 [ -z "$size" ] &&
443 size="$(blk2h $(cat /sys/block/${fs#/dev/}/size /sys/block/*/${fs#/dev/}/size))"
445 # image
446 disktype="@hdd@"
447 case "$(cat /sys/block/${fs#/dev/}/removable 2>/dev/null ||
448 cat /sys/block/${fs:5:3}/removable 2>/dev/null)" in
449 1) disktype="@removable@" ;;
450 esac
451 case "$(cat /sys/block/${fs#/dev/}/ro 2>/dev/null ||
452 cat /sys/block/${fs:5:3}/ro 2>/dev/null)" in
453 1) disktype="@cd@" ;;
454 esac
456 radio="<input type=\"radio\" name=\"device\" value=\"$action $fs\" id=\"${fs#/dev/}\"/>"
457 [ "$REMOTE_USER" == "root" ] || radio=""
458 cat <<EOT
459 <tr>
460 <td>$radio<!--
461 --><label for="${fs#/dev/}" data-icon="$disktype">&thinsp;${fs#/dev/}</label></td>
462 <td>$(blkid $fs | sed '/LABEL=/!d;s/.*LABEL="\([^"]*\).*/\1/')</td>
463 <td>$type</td>
464 <td>$size</td>
465 <td>$av</td>
466 EOT
467 if [ -n "$pct" ]; then
468 cat <<EOT
469 <td class="meter"><meter min="0" max="100" value="${pct%%%}" low="70"
470 high="90" optimum="10"></meter>
471 <span>$used - $pct</span>
472 </td>
473 EOT
474 else
475 cat <<EOT
476 <td> </td>
477 EOT
478 fi
479 cat <<EOT
480 <td>$mp</td>
481 <td>$(blkid $fs | sed '/UUID=/!d;s/.*UUID="\([^"]*\).*/\1/')</td>
482 </tr>
483 EOT
484 done
485 cat <<EOT
486 </tbody>
487 </table>
488 EOT
489 [ "$REMOTE_USER" == "root" ] && cat <<EOT
490 $(lib crypto input)
492 <footer>
493 <button type="submit">mount / umount</button> -
494 $(_ 'new mount point:') <input type="text" name="mountpoint" value="/media/usbdisk"/> -
495 <input type="checkbox" name="readonly" id="ro"><label for="ro">&thinsp;$(_ 'read-only')</label>
496 </footer>
497 EOT
498 cat <<EOT
499 </form>
500 </section>
501 EOT
504 #
505 # /etc/fstab
506 #
507 cat <<EOT
508 <section>
509 <header>
510 $(_ 'Filesystems table')
511 $(edit_button /etc/fstab)
512 </header>
513 <table class="wide zebra center">
514 <thead>
515 <tr>
516 <td>$(_ 'Disk')</td>
517 <td>$(_ 'Mount point')</td>
518 <td>$(_ 'Type')</td>
519 <td>$(_ 'Options')</td>
520 <td>$(_ 'Freq')</td>
521 <td>$(_ 'Pass')</td>
522 </tr>
523 </thead>
524 <tbody>
525 EOT
527 grep -v '^#' /etc/fstab | awk '{
528 print "<tr><td>" $1 "</td><td>" $2 "</td><td>" $3 "</td><td>" $4
529 print "</td><td>" $5 "</td><td>" $6 "</td></tr>"
530 }
531 END{print "</tbody></table>"}'
533 cat <<EOT
534 </section>
535 EOT
538 #
539 # Loop device management GUI
540 #
541 cat <<EOT
542 <section>
543 <header>$(_ 'Loop devices')</header>
545 <form action="#loop" class="wide">
546 <div class="scroll">
547 <table id="loop" class="wide zebra scroll">
548 <thead>
549 <tr>
550 <td>$(_ 'Device')</td>
551 <td>$(_ 'Backing file')</td>
552 <td>$(_ 'Size')</td>
553 <td>$(_ 'Access')</td>
554 <td>$(_ 'Offset')</td>
555 </tr>
556 </thead>
557 <tbody>
558 EOT
559 for devloop in $(ls /dev/*loop[0-9]*); do
560 loop="${devloop#/dev/}"
561 dir=/sys/block/$loop
562 case "$(cat $dir/ro 2>/dev/null)" in
563 0) ro="$(_ "read/write")" ;;
564 1) ro="$(_ "read only")" ;;
565 *) ro="" ;;
566 esac
567 size=$(blk2h $(cat $dir/size))
568 [ "$size" == "0.0K" ] && size="" && ro=""
569 set -- $(losetup $devloop)
570 set -- "${3:-$(cat $dir/loop/backing_file)}" "${2:-$(cat $dir/loop/offset)}" ${ro// /&nbsp;}
571 cat <<EOT
572 <tr><td><input type="radio" name="loopdev" value="$devloop" id="$loop"/><!--
573 --><label for="$loop" data-icon="@loopback@">$loop</label></td>
574 <td>$1</td><td>$size</td><td align="center">$3</td><td align="right">$2</td>
575 </tr>
576 EOT
577 done
578 cat <<EOT
579 </tbody>
580 </table>
581 </div>
583 $(lib crypto input)
585 <footer>
586 <button type="submit" data-icon="@ok@">$(_ 'Setup')</button> -
587 $(_ 'new backing file:') <input type="text" name="backingfile"/> -
588 $(_ 'offset in bytes:') <input type="text" name="offset" value="0" size="8"/> -
589 <input type="checkbox" name="readonly" id="ro"/><label for="ro">$(_ 'read only')</label>
590 </footer>
591 </form>
592 </section>
593 EOT
596 #
597 # System memory
598 #
599 mem_total=$(free -m | awk '$1 ~ "M" {print $2}')
600 mem_used=$((100 * $(free -m | awk '$1 ~ "+" {print $3}') / mem_total))
601 mem_buff=$((100 * $(free -m | awk '$1 ~ "M" {print $6}') / mem_total))
602 mem_free=$((100 - mem_used - mem_buff))
604 cat <<EOT
605 <section>
606 <header>$(_ 'System memory')</header>
608 <div class="sysmem"><!--
609 --><span class="sysmem_used" style="width: ${mem_used}%" title="$(_ 'Used')" ><span>${mem_used}%</span></span><!--
610 EOT
611 [ $mem_buff != 0 ] && cat <<EOT
612 --><span class="sysmem_buff" style="width: ${mem_buff}%" title="$(_ 'Buffers')"><span>${mem_buff}%</span></span><!--
613 EOT
614 cat <<EOT
615 --><span class="sysmem_free" style="width: ${mem_free}%" title="$(_ 'Free')" ><span>${mem_free}%</span></span><!--
616 --></div>
618 <table class="wide zebra center">
619 <thead>
620 <tr>
621 <td>&nbsp;</td>
622 <td>total</td>
623 <td>used</td>
624 <td>free</td>
625 <td>shared</td>
626 <td>buffers</td>
627 </tr>
628 </thead>
629 <tbody>
630 EOT
632 free -m | awk '
633 $1 ~ "M" {print "<tr><td>"$1"</td><td>"$2"</td><td>"$3"</td><td>"$4"</td><td>"$5"</td><td>"$6"</td></tr>"}
634 $1 ~ "+" {print "<tr><td>"$1 $2"</td><td></td><td>"$3"</td><td>"$4"</td><td></td><td></td></tr>"}
635 $1 ~ "S" {print "<tr><td>"$1"</td><td>"$2"</td><td>"$3"</td><td>"$4"</td><td></td><td></td></tr>"}'
637 cat <<EOT
638 </tbody>
639 </table>
640 </section>
641 EOT
644 #
645 # lspci and lsusb summary tables
646 #
647 cat <<EOT
648 <section>
649 <header>lspci</header>
650 $(lspci_table)
651 </section>
654 <section>
655 <header>lsusb</header>
656 $(lsusb_table)
657 </section>
658 EOT
659 ;;
660 esac
662 xhtml_footer
663 exit 0