source: client/engine/Inventory.lib @ 288b716

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 288b716 was 4d2b852, checked in by ramon <ramongomez@…>, 12 years ago

Versión 1.0.5: Función ogGetOsVersion incluye el nuevo tipo GrubLoader para sistema de archivos independiente con cargador GRUB.

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

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