[9f29ba6] | 1 | #!/bin/bash |
---|
| 2 | #/** |
---|
| 3 | #@file System.lib |
---|
[9f57de01] | 4 | #@brief Librería o clase System |
---|
[9f29ba6] | 5 | #@class System |
---|
| 6 | #@brief Funciones básicas del sistema. |
---|
| 7 | #@version 0.9 |
---|
| 8 | #@warning License: GNU GPLv3+ |
---|
| 9 | #*/ |
---|
| 10 | |
---|
[2e15649] | 11 | |
---|
[9f29ba6] | 12 | #/** |
---|
[cfeabbf] | 13 | # ogEcho [loglevel] message... |
---|
[9f57de01] | 14 | #@brief Muestra mensajes en consola y lo registra en fichero de incidencias. |
---|
| 15 | #@arg \c str_loglevel nivel de registro de incidencias. |
---|
| 16 | #@arg \c str_message mensaje (puede recibir más de 1 parámetro. |
---|
| 17 | #@return Mensaje mostrado. |
---|
[aae34f6] | 18 | #@note El nivel de ayuda \c (help) no se registra en el fichero de incidencias. |
---|
[cfeabbf] | 19 | #@version 0.9 - Primera versión para OpenGNSys |
---|
[aae34f6] | 20 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
[9f57de01] | 21 | #@date 2009-07-23 |
---|
[9f29ba6] | 22 | #*/ |
---|
[9f57de01] | 23 | function ogEcho () { |
---|
[9f29ba6] | 24 | |
---|
[59f9ad2] | 25 | # Variables locales |
---|
[cfeabbf] | 26 | local LOGLEVEL LOGFILE DATETIME |
---|
[9f57de01] | 27 | |
---|
[59f9ad2] | 28 | #/// Selección del nivel de registro (opcional). |
---|
[9f57de01] | 29 | case "$1" in |
---|
[d071d9b] | 30 | help) shift ;; |
---|
| 31 | info) LOGLEVEL=$1; shift ;; |
---|
| 32 | warning) LOGLEVEL=$1; shift ;; |
---|
| 33 | error) LOGLEVEL=$1; shift ;; |
---|
| 34 | *) ;; |
---|
[9f57de01] | 35 | esac |
---|
| 36 | |
---|
| 37 | #/// Registro de incidencias. |
---|
[cfeabbf] | 38 | #[ -w "$OGLOGFILE" ] && LOGFILE="-f $OGLOGFILE" |
---|
| 39 | #DATETIME=$(date +"%F %T") # pendiente en cliente Initrd |
---|
| 40 | if [ -n "$LOGLEVEL" ]; then |
---|
| 41 | logger -s -t "OpenGNSys $LOGLEVEL" $LOGFILE "$DATETIME $*" |
---|
| 42 | else |
---|
| 43 | echo "$*" |
---|
[9f57de01] | 44 | fi |
---|
[9f29ba6] | 45 | } |
---|
| 46 | |
---|
| 47 | |
---|
[9fb5086] | 48 | ##### PRUEBAS |
---|
| 49 | # ogGetHostname |
---|
| 50 | function ogGetHostname () { |
---|
| 51 | local HOST |
---|
| 52 | #/// Tomar nombre de la variable \c HOSTNAME |
---|
| 53 | HOST="$HOSTNAME" |
---|
| 54 | #/// Si no, tomar del DHCP, opción \c host-name |
---|
| 55 | [ -z "$HOST" ] && HOST=$(awk -F\" '/option host-name/ {gsub(/;/,""); host=$2} |
---|
| 56 | END {print host} |
---|
| 57 | ' /var/lib/dhcp3/dhclient.leases) |
---|
| 58 | #/// Si no, tomar del parámetro del kernel \c hostname |
---|
| 59 | [ -z "$HOST" ] && HOST=$(awk 'BEGIN {RS=""; FS="="} |
---|
| 60 | $1~/hostname/ {print $2}' /proc/cmdline) |
---|
| 61 | [ "$HOSTNAME" != "$HOST" ] && export HOSTNAME="$HOST" |
---|
| 62 | echo $HOST |
---|
| 63 | } |
---|
| 64 | |
---|
[3458879] | 65 | # ogGetIpAddress |
---|
| 66 | function ogGetIpAddress () { |
---|
| 67 | local IP |
---|
| 68 | IP=$(awk '/fixed-address/ {gsub(/;/,""); host=$2} |
---|
| 69 | END {print host} |
---|
| 70 | ' /var/lib/dhcp3/dhclient.leases) |
---|
| 71 | echo $IP; |
---|
| 72 | } |
---|
| 73 | |
---|
[9fb5086] | 74 | |
---|
[9f29ba6] | 75 | #/** |
---|
[a5df9b9] | 76 | # ogRaiseError errcode ["errmessage" ...] |
---|
[9f57de01] | 77 | #@brief Devuelve el mensaje y el código de error correspondiente. |
---|
| 78 | #@arg \c int_errcode código de error. |
---|
[a5df9b9] | 79 | #@arg \c str_errmessage mensajes complementarios de error. |
---|
[9f57de01] | 80 | #@return Mensaje de error. |
---|
| 81 | #@warning No definidas |
---|
[aae34f6] | 82 | #@note Mensajes internacionales del fichero de idiomas. |
---|
| 83 | #@version 0.9 - Primera versión para OpenGNSys. |
---|
| 84 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
[9f57de01] | 85 | #@date 2009-07-21 |
---|
[9f29ba6] | 86 | #*/ |
---|
[9f57de01] | 87 | function ogRaiseError () { |
---|
[9f29ba6] | 88 | |
---|
[59f9ad2] | 89 | # Variables locales |
---|
[a5df9b9] | 90 | local MSG CODE |
---|
[2e15649] | 91 | |
---|
[5dbb046] | 92 | #/// Obtener código y mensaje de error. |
---|
[a5df9b9] | 93 | CODE=$1 |
---|
| 94 | case "$CODE" in |
---|
[d42638a] | 95 | $OG_ERR_FORMAT) MSG="$MSG_ERR_FORMAT \"$2\"" ;; |
---|
[a5df9b9] | 96 | $OG_ERR_NOTFOUND) MSG="$MSG_ERR_NOTFOUND \"$2\"" ;; |
---|
[aae34f6] | 97 | $OG_ERR_PARTITION) MSG="$MSG_ERR_PARTITION \"$2\"" ;; |
---|
[a79dd508] | 98 | $OG_ERR_LOCKED) MSG="$MSG_ERR_LOCKED \"$2\"" ;; |
---|
[5ceca9c] | 99 | $OG_ERR_IMAGE) MSG="$MSG_ERR_IMAGE \"$2\"" ;; |
---|
[be81649] | 100 | $OG_ERR_NOTOS) MSG="$MSG_ERR_NOTOS \"$2\"" ;; |
---|
| 101 | $OG_ERR_NOTEXEC) MSG="$MSG_ERR_NOTEXEC \"$2\"" ;; |
---|
[cfeabbf] | 102 | *) MSG="$MSG_ERR_GENERIC"; CODE=$OG_ERR_GENERIC ;; |
---|
[2e15649] | 103 | esac |
---|
| 104 | |
---|
[5dbb046] | 105 | #/// Mostrar mensaje de error y salir con el código indicado. |
---|
[aae34f6] | 106 | ogEcho error "${FUNCNAME[1]}: $MSG" >&2 |
---|
[a5df9b9] | 107 | return $CODE |
---|
[9f29ba6] | 108 | } |
---|
| 109 | |
---|
| 110 | |
---|
[aae34f6] | 111 | #/** |
---|
| 112 | # ogHelp ["function" ["format" ["example" ... ]]] |
---|
| 113 | #@brief Muestra mensaje de ayuda para una función determinda. |
---|
| 114 | #@arg \c str_function Nombre de la función. |
---|
| 115 | #@arg \c str_format Formato de ejecución de la función. |
---|
| 116 | #@arg \c str_example Ejemplo de ejecución de la función. |
---|
| 117 | #@return Salida de ayuda. |
---|
| 118 | #@note Si no se indican parámetros, la función se toma de la variable \c $FUNCNAME |
---|
| 119 | #@note La descripción de la función se toma de la variable compuesta por \c MSG_FUNC_$función incluida en el fichero de idiomas. |
---|
| 120 | #@note Pueden especificarse varios mensajes con ejemplos. |
---|
| 121 | #@version 0.9 - Primera versión para OpenGNSys. |
---|
| 122 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 123 | #@date 2009-07-27 |
---|
| 124 | #*/ |
---|
| 125 | function ogHelp () { |
---|
| 126 | |
---|
[59f9ad2] | 127 | # Variables locales. |
---|
[aae34f6] | 128 | local FUNC MSG |
---|
| 129 | |
---|
| 130 | #/// Mostrar función, descripción y formato. |
---|
| 131 | FUNC="${1:-${FUNCNAME[${#FUNCNAME[*]}-1]}}" |
---|
| 132 | MSG="MSG_HELP_$FUNC" |
---|
| 133 | ogEcho help "$MSG_FUNCTION $FUNC: ${!MSG}" |
---|
| 134 | [ -n "$2" ] && ogEcho help " $MSG_FORMAT: $2" |
---|
| 135 | #/// Mostrar ejemplos. |
---|
| 136 | shift 2 |
---|
| 137 | while [ $# -gt 0 ]; do |
---|
| 138 | ogEcho help " $MSG_EXAMPLE: $1" |
---|
| 139 | shift |
---|
| 140 | done |
---|
| 141 | } |
---|
| 142 | |
---|