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 [loglevel] message... |
---|
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. |
---|
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 | |
---|
37 | #/// Registro de incidencias. |
---|
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 "$*" |
---|
44 | fi |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
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 | |
---|
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 | |
---|
74 | |
---|
75 | #/** |
---|
76 | # ogRaiseError errcode ["errmessage" ...] |
---|
77 | #@brief Devuelve el mensaje y el código de error correspondiente. |
---|
78 | #@arg \c int_errcode código de error. |
---|
79 | #@arg \c str_errmessage mensajes complementarios de error. |
---|
80 | #@return Mensaje de error. |
---|
81 | #@warning No definidas |
---|
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 |
---|
85 | #@date 2009-07-21 |
---|
86 | #*/ |
---|
87 | function ogRaiseError () { |
---|
88 | |
---|
89 | # Variables locales |
---|
90 | local MSG CODE |
---|
91 | |
---|
92 | #/// Obtener código y mensaje de error. |
---|
93 | CODE=$1 |
---|
94 | case "$CODE" in |
---|
95 | $OG_ERR_FORMAT) MSG="$MSG_ERR_FORMAT \"$2\"" ;; |
---|
96 | $OG_ERR_NOTFOUND) MSG="$MSG_ERR_NOTFOUND \"$2\"" ;; |
---|
97 | $OG_ERR_PARTITION) MSG="$MSG_ERR_PARTITION \"$2\"" ;; |
---|
98 | $OG_ERR_LOCKED) MSG="$MSG_ERR_LOCKED \"$2\"" ;; |
---|
99 | $OG_ERR_IMAGE) MSG="$MSG_ERR_IMAGE \"$2\"" ;; |
---|
100 | $OG_ERR_NOTOS) MSG="$MSG_ERR_NOTOS \"$2\"" ;; |
---|
101 | $OG_ERR_NOTEXEC) MSG="$MSG_ERR_NOTEXEC \"$2\"" ;; |
---|
102 | *) MSG="$MSG_ERR_GENERIC"; CODE=$OG_ERR_GENERIC ;; |
---|
103 | esac |
---|
104 | |
---|
105 | #/// Mostrar mensaje de error y salir con el código indicado. |
---|
106 | ogEcho error "${FUNCNAME[1]}: $MSG" >&2 |
---|
107 | return $CODE |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
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 | |
---|
127 | # Variables locales. |
---|
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 | |
---|