source: client/engine/Inventory.lib @ cca8f29

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 cca8f29 was 8a45568, checked in by ramon <ramongomez@…>, 12 years ago

Versión 1.0.5, #501 #568: Evitar que el cliente para Linux se ejecute más de una vez; crear correctamente enlaces en menú por defecto.

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

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