source: client/engine/Inventory.lib @ 46f7d6f

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 46f7d6f was 89a5166, checked in by ramon <ramongomez@…>, 11 years ago

541: Disponible inventario básico de software de Mac OS, listando solo aplicaciones pero no sus versiones.

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

  • Property mode set to 100755
File size: 14.3 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-10-07
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: detectar kernel y completar con fichero plist de información del sistema.
134if [ -z "$VERSION" ]; then
135    TYPE="MacOS"
136    # Kernel de Mac OS.
137    FILE="$MNTDIR/mach_kernel"
138    [ -n "$(file -b $FILE | grep 'Mach-O')" ] && VERSION="Mac OS"
139    [ -n "$(file -b $FILE | grep 'Mach-O 64-bit')" ] && IS64BIT="$MSG_64BIT"
140    # Datos de configuración de versión de Mac OS.
141    FILE="$MNTDIR/System/Library/CoreServices/SystemVersion.plist"
142    [ -r $FILE ] && VERSION=$(awk -F"[<>]" '
143                                  /ProductName/ {getline;s=$3}
144                                  /ProductVersion/ {getline;v=$3}
145                                  END {print s,v}' $FILE)
146    # Datos de recuperación de Mac OS.
147    FILE="$MNTDIR/com.apple.recovery.boot"
148    [ -r $FILE -a -n "$VERSION" ] && VERSION="$VERSION recovery"
149fi
150# Para FreeBSD: obtener datos del Kernel.
151### TODO Revisar solución.
152if [ -z "$VERSION" ]; then
153    TYPE="BSD"
154    FILE="$MNTDIR/boot/kernel/kernel"
155    if [ -r $FILE ]; then
156        VERSION="$(strings $FILE|awk '/@.*RELEASE/ {print $1,$2}')"
157        [ -n "$(file -b $FILE | grep 'x86-64')" ] && IS64BIT="$MSG_64BIT"
158    fi
159fi
160# Para Solaris: leer el fichero de versión.
161### TODO Revisar solución.
162if [ -z "$VERSION" ]; then
163    TYPE="Solaris"
164    FILE="$MNTDIR/etc/release"
165    [ -r $FILE ] && VERSION="$(head -1 $FILE)"
166fi
167
168# Mostrar resultado y salir sin errores.
169[ -n "$VERSION" ] && echo "$TYPE:$VERSION $IS64BIT"
170return 0
171}
172
173
174#/**
175#         ogGetOsType int_ndisk int_npartition
176#@brief   Devuelve el tipo del sistema operativo instalado.
177#@param   int_ndisk      nº de orden del disco
178#@param   int_npartition nº de orden de la partición
179#@return  OSType - Tipo de sistema operativo.
180#@note    OSType = { Android, BSD, Linux, MacOS, Windows }
181#@see     ogGetOsVersion
182#*/ ##
183function ogGetOsType ()
184{
185# Si se solicita, mostrar ayuda.
186if [ "$*" == "help" ]; then
187    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
188           "$FUNCNAME 1 2  =>  Linux"
189    return
190fi
191ogGetOsVersion "$@" | cut -sf1 -d:
192}
193
194
195#/**
196#         ogListHardwareInfo
197#@brief   Lista el inventario de hardware de la máquina cliente.
198#@return  TipoDispositivo:Modelo    (por determinar)
199#@warning Se ignoran los parámetros de entrada.
200#@note    TipoDispositivo = { ata, bio, boa, cdr, cpu, dis, fir, mem, mod, mul, net, ser, vga }
201#@note    Requisitos: lshw, awk
202#@version 0.1 - Primeras pruebas con OpenGnSys
203#@author  Ramon Gomez, ETSII Universidad de Sevilla
204#@date    2009-07-28
205#*/ ##
206function ogListHardwareInfo ()
207{
208# Si se solicita, mostrar ayuda.
209if [ "$*" == "help" ]; then
210    ogHelp "$FUNCNAME" "$FUNCNAME"
211    return
212fi
213
214# Recopilación de disposibivos procesando la salida de \c lshw
215ogEcho info "$MSG_HARDWAREINVENTORY}"
216lshw | awk 'BEGIN {type="mod";}
217        /product:/ {sub(/ *product: */,"");  prod=$0;}
218        /vendor:/  {sub(/ *vendor: */,"");   vend=$0;}
219        /version:/ {sub(/ *version: */,"v.");vers=$0;}
220        /size:/    {sub(/ *size: */,"");     size=$0;}
221        /\*-/      {if (type=="mem")
222                    print type"="size;
223                else
224                    if (type!="" && prod!="")
225                        print type"="vend,prod,size,vers;
226                type=prod=vend=vers=size="";}
227        /-core/    {type="boa";}
228        /-firmware/ {type="bio";}
229        /-cpu/     {type="cpu";}
230        /-memory/  {type="mem";}
231        /-ide/     {type="ide";}
232        /-disk/    {type="dis";}
233        /-cdrom/   {type="cdr";}
234        /-display/ {type="vga";}
235        /-network/ {type="net";}
236        /-multimedia/ {type="mul";}
237        /-usb/     {type="usb";}
238        /-firewire/ {type="fir";}
239        /-serial/  {type="bus";}
240        END        {if (type!="" && prod!="")
241                    print type"="vend,prod,size,vers;}
242      '
243# */ (comentario para Doxygen)
244}
245
246
247#/**
248#         ogListSoftware int_ndisk int_npartition
249#@brief   Lista el inventario de software instalado en un sistema operativo.
250#@param   int_ndisk      nº de orden del disco
251#@param   int_npartition nº de orden de la partición
252#@return  programa versión ...
253#@warning Se ignoran los parámetros de entrada.
254#@note    Requisitos: ...
255#@todo    Detectar software en Linux
256#@version 0.1 - Primeras pruebas con OpenGnSys
257#@author  Ramon Gomez, ETSII Universidad de Sevilla
258#@date    2009-09-23
259#@version 1.0.5 - Aproximación para inventario de software de Mac OS.
260#@author  Ramon Gomez, ETSII Universidad de Sevilla
261#@date    2013-10-08
262#*/ ##
263function ogListSoftware ()
264{
265# Variables locales.
266local MNTDIR TYPE DPKGDIR RPMDIR PACMANDIR KEYS KEYS32 k PROG VERS
267
268# Si se solicita, mostrar ayuda.
269if [ "$*" == "help" ]; then
270    ogHelp "$FUNCNAME" "$FUNCNAME 1 1"
271    return
272fi
273# Error si no se reciben 2 parametros.
274[ $# = 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
275
276# Obtener tipo de sistema de archivos y montarlo.
277TYPE=$(ogGetFsType $1 $2) || return $?
278MNTDIR=$(ogMount $1 $2) || return $?
279
280case "$TYPE" in
281    EXT[234]|REISERFS|REISER4)          # Software de GNU/Linux.
282        # Procesar paquetes dpkg.
283        DPKGDIR="${MNTDIR}/var/lib/dpkg"
284        if [ -r $DPKGDIR ]; then
285        #    dpkg --admindir=$DPKGDIR -l | \
286            # Proceso de fichero en sistemas de 64 bits.
287            if [ -e $MNTDIR/lib64 ]; then
288                awk '/Package:/ {if (pack!="") print pack,vers;
289                                 sub(/-dev$/,"",$2);
290                                 pack=$2}
291                     /Version:/ {sub(/^.*:/,"",$2); sub(/-.*$/,"",$2);
292                                 vers=$2}
293                     /Status:/  {if ($2!="install") pack=vers=""}
294                     END        {if (pack!="") print pack,vers}
295                    ' $MNTDIR/var/lib/dpkg/status | sort | uniq
296            else
297                # FIXME Sólo 32 bits
298                chroot "$MNTDIR" /usr/bin/dpkg -l | \
299                    awk '$1~/ii/ {sub(/-dev$/,"",$2); sub(/^.*:/,"",$3);
300                                  sub(/-.*$/,"",$3); print $2,$3}
301                        ' | sort | uniq
302            fi
303        fi
304        # Procesar paquetes RPM.
305        RPMDIR="${MNTDIR}/var/lib/rpm"
306        if [ -r $RPMDIR ]; then
307            # Correccion inconsistencia de version de base de datos.
308            #    rm -f ${RPMDIR}/__db.*
309            #    rpm --dbpath $RPMDIR -qa --qf "%{NAME} %{VERSION}\n" | \
310            # FIXME Sólo 32 bits
311            chroot $MNTDIR /bin/rpm -qa --qf "%{NAME} %{VERSION}\n" | \
312                   awk '$1!~/-devel$/ {sub(/-.*$/,"",$2); print $0}' | \
313                   sort | uniq
314                #    rm -f ${RPMDIR}/__db.*
315        fi
316        # Procesar paquetes pacman.
317        PACMANDIR="${MNTDIR}/var/lib/pacman/local"
318        if [ -r $PACMANDIR ]; then
319            # FIXME Separar nombre y versión de los paquetes
320            ls -A $PACMANDIR
321        fi
322        ;;
323    NTFS|HNTFS|FAT32|HFAT32)            # Software de Windows.
324        # Claves de registro para programas instalados: formato "{clave}".
325        KEYS=$(ogListRegistryKeys $MNTDIR software '\Microsoft\Windows\CurrentVersion\Uninstall')
326        KEYS32=$(ogListRegistryKeys $MNTDIR software '\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall')
327        # Mostrar los valores "DisplayName" y "DisplayVersion" para cada clave.
328        (for k in $KEYS; do
329            PROG=$(ogGetRegistryValue $MNTDIR software "\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\$k\\DisplayName")
330            if [ -n "$PROG" ]; then
331                VERS=$(ogGetRegistryValue $MNTDIR software "\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\$k\\DisplayVersion")
332                echo "$PROG $VERS"
333            fi
334        done
335        for k in $KEYS32; do
336            PROG=$(ogGetRegistryValue $MNTDIR software "\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\$k\\DisplayName")
337            if [ -n "$PROG" ]; then
338                VERS=$(ogGetRegistryValue $MNTDIR software "\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\$k\\DisplayVersion")
339                echo "$PROG $VERS"
340            fi
341        done) | sort | uniq
342        ;;
343    HFS|HFSPLUS)                        # Software de Mac OS.
344        # Listar directorios de aplicaciones (falta buscar versiones en ficheros .plist).
345        find "${MNTDIR}/Applications" -type d -name "*.app" -prune -print | \
346                while read k; do
347                    echo "$(basename "$k" .app)"
348                done | sort
349        ;;
350    *)  ogRaiseError $OG_ERR_PARTITION "$1, $2"
351        return $? ;;
352esac
353}
354
355#/**  @function ogInfoCache: @brief muestra la informacion de la CACHE.
356#@param  sin parametros
357#@return  texto que se almacena en $IP.-InfoCache.  punto_montaje, tama?oTotal, TamanioOcupado, TaminioLibre, imagenes dentro de la cahce
358#@warning  Salidas de errores no determinada
359#@warning   printf no soportado por busybox
360#@attention
361#@version 0.1   Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga
362#*/
363function ogInfoCache ()
364{
365local info infoFilesystem infoSize infoUsed infoUsedPorcet infoMountedOn content
366if ogMountCache
367then
368        info=`df -h | grep $OGCAC`
369        infoFilesystem=`echo $info | cut -f1 -d" "`
370        infoSize=`echo $info | cut -f2 -d" "`
371        infoUsed=`echo $info | cut -f3 -d" "`
372        infoAvail=`echo $info | cut -f4 -d" "`
373        infoUsedPorcet=`echo $info | cut -f5 -d" "`
374        infoMountedOn=`echo $info | cut -f2 -d" "`
375        if `ls  ${OGCAC}$OGIMG > /dev/null 2>&1`
376        then
377               cd ${OGCAC}${OPENGNSYS}
378                #content=`find images/ -type f -printf "%h/  %f  %s \n"`   busybox no soporta printf
379                content=`find images/ -type f`
380                cd /
381                echo $info
382                echo -ne $content
383                echo " "
384                #echo "$info" > ${OGLOG}/${IP}-InfoCache
385                #echo "$content" >> {$OGLOG}/${IP}-InfoCache
386        else
387                echo $info
388                #echo "$info" > {$OGLOG}/${IP}-InfoCache
389        fi
390        ogUnmountCache
391else
392        echo " "
393        #echo " " > {$OGLOG}/${IP}-InfoCache
394
395fi
396}
397
Note: See TracBrowser for help on using the repository browser.