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