4cb2c17f98c777f94fa0aee5d8ef4b4f93398573
[grml-hwinfo.git] / grml-hwinfo
1 #!/bin/sh
2 # Filename:      grml-hwinfo
3 # Purpose:       get hardware information
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 ################################################################################
8 # Notice: Some ideas have been taken from
9 # http://club.black.co.at/david/hwdb/infodump
10 # by David Schmitt <david@schmitt.edv-bus.at>
11 ################################################################################
12
13 # variables
14 UNAME="$(uname -r)"
15 PN="$(basename $0)"
16 [ -n "$WORKING_DIR" -a -d "$WORKING_DIR" ] || WORKING_DIR=$(pwd)
17 VERSION='0.4.1'
18
19 TIMESTAMP='+%F--%H-%M-%S-%Z'
20 DATE="$(date $TIMESTAMP)"
21
22 GENERATE_FILE='1'
23 GENERATE_DIRECTORY=''
24
25 while getopts "hdf" args; do
26   case "$args" in
27   h) echo "$PN - collect hardware information
28
29 This tool collects information of the hardware this tool is being executed
30 on.  It can be executed as normal user to collect some basic information or
31 with root permissions to collect as much information as possible.  By
32 default, a file named grml-hwinfo-TIMESTAMP.tar.bz2 storing all collected
33 information will be created in the current working directory. Alternatively,
34 you can have it create a directory with all information.
35
36 Options:
37
38 -h  :  Display this help
39 -f  :  Create grml-hwinfo-TIMESTAMP.tar.bz2
40 -d  :  Create grml-hwinfo-TIMESTAMP/
41
42 -df :  Create both, the directory and the file
43 "
44
45   exit 0;;
46
47   d)
48   GENERATE_FILE=''
49   GENERATE_DIRECTORY='1'
50   ;;
51
52   f) # generating file; default behaviour
53   GENERATE_FILE='1'
54   ;;
55
56   esac
57   done
58
59 # Generate output/temporary directory name & path, and output file path
60 OUTFILE=""
61 OUTDIRNAME="grml-hwinfo-${DATE}"
62 OUTDIR="${WORKING_DIR}/${OUTDIRNAME}"
63 mkdir "${OUTDIR}" || { echo 'Directory "'${OUTDIR}'" already exists, aborting.'>&2 ; exit 1; }
64
65 if [ -n "$GENERATE_FILE" ] ; then
66    OUTFILE_="${OUTDIR}.tar.bz2"
67    [ -e "${OUTFILE_}" ] && { echo 'File "'${OUTFILE_}'" already exists, aborting.'>&2 ; rm -r "${OUTDIR}"; exit 1; }
68    OUTFILE=${OUTFILE_}
69    touch "${OUTFILE}"
70 fi
71
72 if [ "$(id -u)" != "0" ] ; then
73    NOTROOT=1
74    echo "Notice: you are running $PN without root permissions. Not all information will be collected."
75    echo
76 fi
77
78 # check whether a binary is available and executable
79 exectest() {
80 if [ -z "$1" ] ; then
81    echo 'Usage: exectest <binary>'>&2
82    return 1
83 else
84    test -e "$(which $1)" && return 0 || return 1
85 fi
86 }
87
88 # echo a list of all disks and their size
89 # taken from http://cvs.debian.org/fai/lib/disk-info
90 diskandsize() {
91     local isdisk major minor blocks device suffix
92     while read major minor blocks device suffix; do
93         isdisk=1
94         # skip ide cdrom
95         [ -f /proc/ide/$device/media ] && grep -q cdrom /proc/ide/$device/media && isdisk=0
96         [ "$isdisk" -eq 1 ] && echo "$device $blocks"
97     done
98 }
99
100 list_disks() {
101 # print only every second entry; used by disk_info
102 i=0
103 for ent in $@; do
104     if [ "$i" -eq 0 ]; then
105         echo $ent
106         i=1
107     else
108         i=0
109     fi
110 done
111 }
112
113 disk_info() {
114     # the variable holds a space separated list of devices and their block size
115     device_size=`grep -E ' cciss/c.d.$| ida/c.d.$| rd/c.d.$| hd.$| sd.$|/disc$' /proc/partitions | diskandsize`
116     # a list of all local disks, without size
117     disklist=`list_disks $device_size`
118 }
119
120 echo "Running grml-hwinfo ${VERSION} - collecting hardware information."
121
122 cd "${OUTDIR}" || exit 1
123 (
124
125   [ -n "$GENERATE_FILE" ]      && echo "Output file:      $OUTFILE"
126   [ -n "$GENERATE_DIRECTORY" ] && echo "Output directory: $OUTDIR"
127   echo "This might take a few seconds/minutes. Please be patient..."
128
129 # some sysinfo
130   date > date
131   if [ -r /etc/grml_version ] ; then
132      cat /etc/grml_version > grml_version
133   fi
134   if [ -r /etc/debian_version ] ; then
135      cat /etc/debian_version > debian_version
136   fi
137   uname -a > uname
138
139 # disks / devices
140   [ -f /proc/scsi/scsi ] && cat /proc/scsi/scsi > scsi
141   (lspci; lspci -n) | sort > lspci
142   [ -r /proc/bus/pnp ] && lspnp > lspnp
143   [ -r /proc/bus/usb ] && lsusb > lsusb
144   cat /proc/partitions > partitions
145   find /proc/ide/ -name geometry -exec grep . {} \; > proc_ide 2>/dev/null
146   df -h > df 2>/dev/null
147   for i in free lsmod mount lsdev ; do
148       exectest $i  && $i > $i
149   done
150
151 # proc stuff
152   for i in cpuinfo interrupts cmdline devices dma fb iomem ioports \
153     mdstat meminfo modules mtrr pci version ; do
154     [ -r /proc/$i ] && cat /proc/$i > proc_$i
155   done
156   exectest sysdump  && sysdump > sysdump 2>sysdump.error
157 # for sysnet in `find /proc/sys/net/*/ -type f | grep -v flush`; do cat $sysnet >> sysnet ; done
158
159 # log
160   dmesg > dmesg.cur
161
162 # hwinfo
163   exectest discover && discover -v --type-summary --enable-bus all > discover 2> discover.2
164   exectest hwinfo   && hwinfo log=hwinfo
165   exectest x86info  && x86info > x86info 2>x86info.2
166
167 # net stuff
168   exectest ifconfig  && ifconfig -v -a  > ifconfig
169   exectest ip        && ip route show   > ip_route
170   exectest ip        && ip link show    > ip_link
171   exectest route     && route -n        > route
172
173 # software
174   if exectest dpkg ; then
175      dpkg --get-selections   > dpkg_get_selections
176      COLUMNS=300 dpkg --list > dpkg_list
177      COLUMNS=1000 dpkg -l linux-image-$UNAME | grep linux-image-$UNAME | tr -s ' ' > running_kernel
178   fi
179
180 # power management
181   exectest laptop-detect  && laptop-detect >/dev/null 2>/dev/null && echo "0" > laptop_detected
182   exectest acpi_available && acpi_available && cat /proc/acpi/info > acpi_info
183   exectest acpi && acpi > acpi 2> acpi.error && acpi -v > acpi.version
184   [ -r /proc/apm/ ] && apm > apm
185
186 # kernel stuff
187   if [ -r /proc/config.gz ] ; then
188      zcat /proc/config.gz > kernelconfig
189   else
190      [ -r /boot/config-$UNAME ] && cat /boot/config-$UNAME > kernelconfig
191   fi
192
193 # X stuff
194   if [ -n "${DISPLAY}" ] ; then
195      exectest xviddetect  && xviddetect         > xviddetect
196      exectest xvidtune    && xvidtune -show     > xdivtune
197      exectest xrandr      && xrandr             > xrandr
198      exectest xdpyinfo    && xdpyinfo           > xdpyinfo
199      X -version > x_version 2>&1
200   fi
201
202   for i in Xorg.0.log Xorg.7.log Xorg.8.log XFree86.0.log XFree86.7.log XFree86.8.log dmesg ; do
203       cp /var/log/$i log_$i 2>/dev/null
204   done
205
206   cp /etc/X11/xorg.conf    xorg.conf    2>/dev/null
207   cp /etc/modules          modules      2>/dev/null
208   cp /etc/X11/XF86Config-4 XF86Config-4 2>/dev/null
209
210 # not available:
211 # sysinfo -class device -level all
212
213 # as root:
214 if [ -n "$NOTROOT" ] ; then
215    echo "not running as root" > root
216 else
217    echo "running as root" > root
218    disk_info
219    exectest sfdisk     && sfdisk -d > sfdisk 2>sfdisk.error
220    exectest ddcprobe   && ddcprobe  > ddcprobe
221    exectest dmidecode  && dmidecode > dmidecode
222
223    exectest dconf && dconf -o dconf
224
225    if [ -x /usr/share/doc/lm-sensors/examples/eeprom/decode-dimms.pl ] ; then
226       /usr/share/doc/lm-sensors/examples/eeprom/decode-dimms.pl > decode-dimms 2>decode-dimms.error
227    fi
228
229    for disk in $disklist; do
230       exectest smartctl  && echo "smartctl -a /dev/$disk :\n" >> smartctl && smartctl -a /dev/$disk       >> smartctl             && echo "\n\n" >> smartctl
231       exectest hdparm    && echo "hdparm -iv  /dev/$disk :\n" >> hdparm   && hdparm -iv  /dev/$disk       >> hdparm               && echo "\n\n" >> hdparm
232       exectest fdisk     && echo "fdisk -lu   /dev/$disk :\n" >> fdisk    && fdisk -lu   /dev/$disk       >> fdisk 2>>fdisk.error && echo "\n\n" >> fdisk
233       exectest parted    && echo "parted -s   /dev/$disk :\n" >> parted   && parted -s   /dev/$disk print >> parted               && echo "\n\n" >> parted
234       LC_ALL=C file -s /dev/$disk?* | grep -v ": empty" >> file_disk
235    done
236 fi
237 )
238
239 echo
240
241 cd "${WORKING_DIR}"
242
243 # create tarball
244 if [ -n "$GENERATE_FILE" ] ; then
245   tar jcf "${OUTFILE}" "${OUTDIRNAME}"
246   [ -r "$OUTFILE" ] && echo "$OUTFILE ("$(ls -ahl "$OUTFILE" | awk '{print $5}')") has been generated."
247 fi
248
249 # remove (temporary) output directory if needed, else keep it, as it doubles
250 # as the real output directory.
251 if [ -z "$GENERATE_DIRECTORY" ] ; then
252   rm -r "${OUTDIR}"
253 else
254   [ -r "${OUTDIR}" ] && echo "${OUTDIR} has been generated."
255 fi
256
257 exit 0
258
259 ## END OF FILE##################################################################