source: client/engine/Inventory.lib @ 831830f

918-git-images-111dconfigfileconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacionwebconsole3
Last change on this file since 831830f was 825dd06, checked in by ramon <ramongomez@…>, 12 years ago

Versión 1.0.5, #548: Detectar los nuevos sistemas operativos GNU/Linux usando por defecto el nuevo fichero estándar /etc/os-release.

git-svn-id: https://opengnsys.es/svn/branches/version1.0@3536 a21b9725-9963-47de-94b9-378ad31fedc9

  • Property mode set to 100755
File size: 12.9 KB
Line 
1#!/bin/bash
2#/**
3#@file    Inventory.lib
4#@brief   Librería o clase Inventory
5#@class   Inventory
6#@brief   Funciones para recogida de datos de inventario de hardware y software de los clientes.
7#@version 1.0.5
8#@warning License: GNU GPLv3+
9#*/
10
11
12#/**
13#         ogGetArch
14#@brief   Devuelve el tipo de arquitectura del cliente.
15#@return  str_arch - Arquitectura (i386 para 32 bits, x86_64 para 64 bits).
16#@version 0.9.2 - Primera versión para OpenGnSys.
17#@author  Ramon Gomez, ETSII Universidad de Sevilla
18#@date    2010-07-17
19#*/
20function ogGetArch ()
21{
22if [ "$*" == "help" ]; then
23    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  x86_64"
24    return
25fi
26
27[ -d /lib64 ] && echo "x86_64" || echo "i386"
28}
29
30
31#/**
32#         ogGetOsVersion int_ndisk int_npartition
33#@brief   Devuelve la versión del sistema operativo instalado en un sistema de archivos.
34#@param   int_ndisk      nº de orden del disco
35#@param   int_npartition nº de orden de la partición
36#@return  OSType:OSVersion - tipo y versión del sistema operativo.
37#@note    OSType = { Android, BSD, Hurd, Linux, MacOS, Solaris, Windows, WinLoader }
38#@note    Requisitos: awk, head, chroot
39#@exception OG_ERR_FORMAT    Formato incorrecto.
40#@exception OG_ERR_NOTFOUND  Disco o partición no corresponden con un dispositiv
41#@exception OG_ERR_PARTITION Fallo al montar el sistema de archivos.
42#@version 0.9 - Primera versión para OpenGnSys
43#@author  Ramon Gomez, ETSII Universidad de Sevilla
44#@date    2009-09-15
45#@version 1.0.4 - Incluir tipos BSD, MacOS y Solaris.
46#@author  Ramon Gomez, ETSII Universidad de Sevilla
47#@date    2012-06-29
48#@version 1.0.5 - Incluir tipos Hurd y WinLoader, leer por defecto fichero /etc/os-release.
49#@author  Ramon Gomez, ETSII Universidad de Sevilla
50#@date    2013-02-04
51#*/ ##
52function ogGetOsVersion ()
53{
54# Variables locales.
55local MNTDIR TYPE DISTRIB VERSION IS64BIT FILE
56# Si se solicita, mostrar ayuda.
57if [ "$*" == "help" ]; then
58    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
59           "$FUNCNAME 1 2  =>  Linux:Ubuntu 11.04 64 bits"
60    return
61fi
62# Error si no se reciben 2 parametros.
63[ $# = 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
64
65# Montar la particion, si no lo estaba previamente.
66MNTDIR=$(ogMount $1 $2) || return $?
67
68# Buscar tipo de sistema operativo.
69# Para GNU/Linux: leer descripción.
70TYPE="Linux"
71FILE="$MNTDIR/etc/os-release"
72[ -r $FILE ] && VERSION="$(awk -F= '$1~/PRETTY_NAME/ {gsub(/\"/,"",$2); print $2}' $FILE)"
73# Si no se puede obtener, buscar en ficheros del sistema.
74if [ -z "$VERSION" ]; then
75    FILE="$MNTDIR/etc/lsb-release"
76    [ -r $FILE ] && VERSION="$(awk -F= '$1~/DESCRIPTION/ {gsub(/\"/,"",$2); print $2}' $FILE)"
77    for DISTRIB in redhat SuSE mandrake gentoo; do
78        FILE="$MNTDIR/etc/${DISTRIB}-release"
79        [ -r $FILE ] && VERSION="$(head -1 $FILE)"
80    done
81    FILE="$MNTDIR/etc/arch-release"
82    [ -r $FILE ] && VERSION="Arch Linux"
83    FILE="$MNTDIR/etc/slackware-version"
84    [ -r $FILE ] && VERSION="Slackware $(cat $FILE)"
85fi
86# Si no se encuentra, intentar ejecutar "lsb_release".
87[ -z "$VERSION" ] && VERSION=$(chroot $MNTDIR lsb_release -d 2>/dev/null | awk -F":\t" '{print $2}')
88# Comprobar Linux de 64 bits.
89[ -n "$VERSION" ] && [ -e $MNTDIR/lib64 ] && IS64BIT="$MSG_64BIT"
90# Para Android, leer fichero de propiedades.
91if [ -z "$VERSION" ]; then
92    TYPE="Android"
93    FILE="$MNTDIR/android*/system/build.prop"
94    [ -r $FILE ] && VERSION="Android $(awk -F= '$1~/(product.brand|build.version.release)/ {print $2}' $FILE | tr '\n' ' ')"
95    [ -e $MNTDIR/lib64 ] && IS64BIT="$MSG_64BIT"
96fi
97# Para GNU/Hurd, comprobar fichero de inicio (basado en os-prober).
98if [ -z "$VERSION" ]; then
99    TYPE="Hurd"
100    FILE="$MNTDIR/hurd/init"
101    [ -r $FILE ] && VERSION="GNU/Hurd"
102fi
103# Para Windows: leer la version del registro.
104if [ -z "$VERSION" ]; then
105    TYPE="Windows"
106    VERSION=$(ogGetRegistryValue $MNTDIR software '\Microsoft\Windows NT\CurrentVersion\ProductName')
107    [ -n "$(ogGetRegistryValue $MNTDIR software '\Microsoft\Windows\CurrentVersion\ProgramW6432Dir' 2>/dev/null)" ] && IS64BIT="$MSG_64BIT"
108fi
109# Para cargador Windows: buscar versión en fichero BCD (basado en os-prober).
110if [ -z "$VERSION" ]; then
111    TYPE="WinLoader"
112    FILE="$(ogGetPath $MNTDIR/boot/bcd)"
113    if [ -n "$FILE" ]; then
114        for DISTRIB in "Windows 8" "Windows 7" "Windows Vista" \
115                       "Windwos Server 2008" "Windwos Server 2008 R2" \
116                       "Windwos Recovery Environment"; do
117            if grep -qs "$(echo "$DISTRIB" | sed 's/./&./g')" $FILE; then
118               VERSION="$DISTRIB loader"
119            fi
120        done
121    fi
122fi
123# Para MacOS: ejecutar "uname" mediante el programa "ld-mac".
124if [ -z "$VERSION" ]; then
125    TYPE="MacOS"
126    FILE="$MNTDIR/usr/bin/uname"
127    [ -n "$(file -b $FILE | grep 'Mach-O')" ] && VERSION="Mac OS $(ld-mac $FILE -sr 2>/dev/null)"
128fi
129# Para FreeBSD: obtener datos del Kernel.
130### TODO Revisar solución.
131if [ -z "$VERSION" ]; then
132    TYPE="BSD"
133    FILE="$MNTDIR/boot/kernel/kernel"
134    if [ -r $FILE ]; then
135        VERSION="$(strings $FILE|awk '/@.*RELEASE/ {print $1,$2}')"
136        [ -n "$(file -b $FILE | grep 'x86-64')" ] && IS64BIT="$MSG_64BIT"
137    fi
138fi
139# Para Solaris: leer el fichero de versión.
140### TODO Revisar solución.
141if [ -z "$VERSION" ]; then
142    TYPE="Solaris"
143    FILE="$MNTDIR/etc/release"
144    [ -r $FILE ] && VERSION="$(head -1 $FILE)"
145fi
146
147# Mostrar resultado y salir sin errores.
148[ -n "$VERSION" ] && echo "$TYPE:$VERSION $IS64BIT"
149return 0
150}
151
152
153#/**
154#         ogGetOsType int_ndisk int_npartition
155#@brief   Devuelve el tipo del sistema operativo instalado.
156#@param   int_ndisk      nº de orden del disco
157#@param   int_npartition nº de orden de la partición
158#@return  OSType - Tipo de sistema operativo.
159#@note    OSType = { Android, BSD, Linux, MacOS, Windows }
160#@see     ogGetOsVersion
161#*/ ##
162function ogGetOsType ()
163{
164# Si se solicita, mostrar ayuda.
165if [ "$*" == "help" ]; then
166    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
167           "$FUNCNAME 1 2  =>  Linux"
168    return
169fi
170ogGetOsVersion "$@" | cut -sf1 -d:
171}
172
173
174#/**
175#         ogListHardwareInfo
176#@brief   Lista el inventario de hardware de la máquina cliente.
177#@return  TipoDispositivo:Modelo    (por determinar)
178#@warning Se ignoran los parámetros de entrada.
179#@note    TipoDispositivo = { ata, bio, boa, cdr, cpu, dis, fir, mem, mod, mul, net, ser, vga }
180#@note    Requisitos: lshw, awk
181#@version 0.1 - Primeras pruebas con OpenGnSys
182#@author  Ramon Gomez, ETSII Universidad de Sevilla
183#@date    2009-07-28
184#*/ ##
185function ogListHardwareInfo ()
186{
187# Si se solicita, mostrar ayuda.
188if [ "$*" == "help" ]; then
189    ogHelp "$FUNCNAME" "$FUNCNAME"
190    return
191fi
192
193# Recopilación de disposibivos procesando la salida de \c lshw
194ogEcho info "$MSG_HARDWAREINVENTORY}"
195lshw | awk 'BEGIN {type="mod";}
196        /product:/ {sub(/ *product: */,"");  prod=$0;}
197        /vendor:/  {sub(/ *vendor: */,"");   vend=$0;}
198        /version:/ {sub(/ *version: */,"v.");vers=$0;}
199        /size:/    {sub(/ *size: */,"");     size=$0;}
200        /\*-/      {if (type=="mem")
201                    print type"="size;
202                else
203                    if (type!="" && prod!="")
204                        print type"="vend,prod,size,vers;
205                type=prod=vend=vers=size="";}
206        /-core/    {type="boa";}
207        /-firmware/ {type="bio";}
208        /-cpu/     {type="cpu";}
209        /-memory/  {type="mem";}
210        /-ide/     {type="ide";}
211        /-disk/    {type="dis";}
212        /-cdrom/   {type="cdr";}
213        /-display/ {type="vga";}
214        /-network/ {type="net";}
215        /-multimedia/ {type="mul";}
216        /-usb/     {type="usb";}
217        /-firewire/ {type="fir";}
218        /-serial/  {type="bus";}
219        END        {if (type!="" && prod!="")
220                    print type"="vend,prod,size,vers;}
221      '
222# */ (comentario para Doxygen)
223}
224
225
226#/**
227#         ogListSoftware int_ndisk int_npartition
228#@brief   Lista el inventario de software instalado en un sistema operativo.
229#@param   int_ndisk      nº de orden del disco
230#@param   int_npartition nº de orden de la partición
231#@return  programa versión ...
232#@warning Se ignoran los parámetros de entrada.
233#@note    Requisitos: ...
234#@todo    Detectar software en Linux
235#@version 0.1 - Primeras pruebas con OpenGnSys
236#@author  Ramon Gomez, ETSII Universidad de Sevilla
237#@date    2009-09-23
238#*/ ##
239function ogListSoftware ()
240{
241# Variables locales.
242local MNTDIR TYPE DPKGDIR RPMDIR PACMANDIR KEYS KEYS32 k PROG VERS
243
244# Si se solicita, mostrar ayuda.
245if [ "$*" == "help" ]; then
246    ogHelp "$FUNCNAME" "$FUNCNAME 1 1"
247    return
248fi
249# Error si no se reciben 2 parametros.
250[ $# = 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
251
252# Obtener tipo de sistema de archivos y montarlo.
253TYPE=$(ogGetFsType $1 $2) || return $?
254MNTDIR=$(ogMount $1 $2) || return $?
255
256case "$TYPE" in
257    EXT[234]|REISERFS|REISER4)
258        # Procesar paquetes dpkg.
259        DPKGDIR="${MNTDIR}/var/lib/dpkg"
260        if [ -r $DPKGDIR ]; then
261        #    dpkg --admindir=$DPKGDIR -l | \
262            # Proceso de fichero en sistemas de 64 bits.
263            if [ -e $MNTDIR/lib64 ]; then
264                awk '/Package:/ {if (pack!="") print pack,vers;
265                                 sub(/-dev$/,"",$2);
266                                 pack=$2}
267                     /Version:/ {sub(/^.*:/,"",$2); sub(/-.*$/,"",$2);
268                                 vers=$2}
269                     /Status:/  {if ($2!="install") pack=vers=""}
270                     END        {if (pack!="") print pack,vers}
271                    ' $MNTDIR/var/lib/dpkg/status | sort | uniq
272            else
273                # FIXME Sólo 32 bits
274                chroot "$MNTDIR" /usr/bin/dpkg -l | \
275                    awk '$1~/ii/ {sub(/-dev$/,"",$2); sub(/^.*:/,"",$3);
276                                  sub(/-.*$/,"",$3); print $2,$3}
277                        ' | sort | uniq
278            fi
279        fi
280        # Procesar paquetes RPM.
281        RPMDIR="${MNTDIR}/var/lib/rpm"
282        if [ -r $RPMDIR ]; then
283            # Correccion inconsistencia de version de base de datos.
284            #    rm -f ${RPMDIR}/__db.*
285            #    rpm --dbpath $RPMDIR -qa --qf "%{NAME} %{VERSION}\n" | \
286            # FIXME Sólo 32 bits
287            chroot $MNTDIR /bin/rpm -qa --qf "%{NAME} %{VERSION}\n" | \
288                   awk '$1!~/-devel$/ {sub(/-.*$/,"",$2); print $0}' | \
289                   sort | uniq
290                #    rm -f ${RPMDIR}/__db.*
291        fi
292        # Procesar paquetes pacman.
293        PACMANDIR="${MNTDIR}/var/lib/pacman/local"
294        if [ -r $PACMANDIR ]; then
295            # FIXME Separar nombre y versión de los paquetes
296            ls -A $PACMANDIR
297        fi
298        ;;
299    NTFS|HNTFS|FAT32|HFAT32)
300        # Claves de registro para programas instalados: formato "{clave}".
301        KEYS=$(ogListRegistryKeys $MNTDIR software '\Microsoft\Windows\CurrentVersion\Uninstall')
302        KEYS32=$(ogListRegistryKeys $MNTDIR software '\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall')
303        # Mostrar los valores "DisplayName" y "DisplayVersion" para cada clave.
304        (for k in $KEYS; do
305            PROG=$(ogGetRegistryValue $MNTDIR software "\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\$k\\DisplayName")
306            if [ -n "$PROG" ]; then
307                VERS=$(ogGetRegistryValue $MNTDIR software "\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\$k\\DisplayVersion")
308                echo "$PROG $VERS"
309            fi
310        done
311        for k in $KEYS32; do
312            PROG=$(ogGetRegistryValue $MNTDIR software "\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\$k\\DisplayName")
313            if [ -n "$PROG" ]; then
314                VERS=$(ogGetRegistryValue $MNTDIR software "\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\$k\\DisplayVersion")
315                echo "$PROG $VERS"
316            fi
317        done) | sort | uniq
318        ;;
319    *)  ogRaiseError $OG_ERR_PARTITION "$1, $2"
320        return $? ;;
321esac
322}
323
324#/**  @function ogInfoCache: @brief muestra la informacion de la CACHE.
325#@param  sin parametros
326#@return  texto que se almacena en $IP.-InfoCache.  punto_montaje, tama?oTotal, TamanioOcupado, TaminioLibre, imagenes dentro de la cahce
327#@warning  Salidas de errores no determinada
328#@warning   printf no soportado por busybox
329#@attention
330#@version 0.1   Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga
331#*/
332function ogInfoCache ()
333{
334local info infoFilesystem infoSize infoUsed infoUsedPorcet infoMountedOn content
335if ogMountCache
336then
337        info=`df -h | grep $OGCAC`
338        infoFilesystem=`echo $info | cut -f1 -d" "`
339        infoSize=`echo $info | cut -f2 -d" "`
340        infoUsed=`echo $info | cut -f3 -d" "`
341        infoAvail=`echo $info | cut -f4 -d" "`
342        infoUsedPorcet=`echo $info | cut -f5 -d" "`
343        infoMountedOn=`echo $info | cut -f2 -d" "`
344        if `ls  ${OGCAC}$OGIMG > /dev/null 2>&1`
345        then
346               cd ${OGCAC}${OPENGNSYS}
347                #content=`find images/ -type f -printf "%h/  %f  %s \n"`   busybox no soporta printf
348                content=`find images/ -type f`
349                cd /
350                echo $info
351                echo -ne $content
352                echo " "
353                #echo "$info" > ${OGLOG}/${IP}-InfoCache
354                #echo "$content" >> {$OGLOG}/${IP}-InfoCache
355        else
356                echo $info
357                #echo "$info" > {$OGLOG}/${IP}-InfoCache
358        fi
359        ogUnmountCache
360else
361        echo " "
362        #echo " " > {$OGLOG}/${IP}-InfoCache
363
364fi
365}
366
Note: See TracBrowser for help on using the repository browser.