Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib/kdump
File: kdump-logger.sh
#!/bin/bash
[0] Fix | Delete
#
[1] Fix | Delete
# This comes from the dracut-logger.sh
[2] Fix | Delete
#
[3] Fix | Delete
# The logger defined 4 logging levels:
[4] Fix | Delete
# - ddebug (4)
[5] Fix | Delete
# The DEBUG Level designates fine-grained informational events that are most
[6] Fix | Delete
# useful to debug an application.
[7] Fix | Delete
# - dinfo (3)
[8] Fix | Delete
# The INFO level designates informational messages that highlight the
[9] Fix | Delete
# progress of the application at coarse-grained level.
[10] Fix | Delete
# - dwarn (2)
[11] Fix | Delete
# The WARN level designates potentially harmful situations.
[12] Fix | Delete
# - derror (1)
[13] Fix | Delete
# The ERROR level designates error events that might still allow the
[14] Fix | Delete
# application to continue running.
[15] Fix | Delete
#
[16] Fix | Delete
# Logging is controlled by following global variables:
[17] Fix | Delete
# - @var kdump_stdloglvl - logging level to standard error (console output)
[18] Fix | Delete
# - @var kdump_sysloglvl - logging level to syslog (by logger command)
[19] Fix | Delete
# - @var kdump_kmsgloglvl - logging level to /dev/kmsg (only for boot-time)
[20] Fix | Delete
#
[21] Fix | Delete
# If any of the variables is not set, the function dlog_init() sets it to default:
[22] Fix | Delete
# - In the first kernel:
[23] Fix | Delete
# - @var kdump_stdloglvl = 3 (info)
[24] Fix | Delete
# - @var kdump_sysloglvl = 0 (no logging)
[25] Fix | Delete
# - @var kdump_kmsgloglvl = 0 (no logging)
[26] Fix | Delete
#
[27] Fix | Delete
# -In the second kernel:
[28] Fix | Delete
# - @var kdump_stdloglvl = 0 (no logging)
[29] Fix | Delete
# - @var kdump_sysloglvl = 3 (info)
[30] Fix | Delete
# - @var kdump_kmsgloglvl = 0 (no logging)
[31] Fix | Delete
#
[32] Fix | Delete
# First of all you have to start with dlog_init() function which initializes
[33] Fix | Delete
# required variables. Don't call any other logging function before that one!
[34] Fix | Delete
#
[35] Fix | Delete
[36] Fix | Delete
# Define vairables for the log levels in this module.
[37] Fix | Delete
kdump_stdloglvl=""
[38] Fix | Delete
kdump_sysloglvl=""
[39] Fix | Delete
kdump_kmsgloglvl=""
[40] Fix | Delete
[41] Fix | Delete
# The dracut-lib.sh is only available in the second kernel, and it won't
[42] Fix | Delete
# be used in the first kernel because the dracut-lib.sh is invisible in
[43] Fix | Delete
# the first kernel.
[44] Fix | Delete
if [ -f /lib/dracut-lib.sh ]; then
[45] Fix | Delete
. /lib/dracut-lib.sh
[46] Fix | Delete
fi
[47] Fix | Delete
[48] Fix | Delete
# @brief Get the log level from kernel command line.
[49] Fix | Delete
# @retval 1 if something has gone wrong
[50] Fix | Delete
# @retval 0 on success.
[51] Fix | Delete
#
[52] Fix | Delete
get_kdump_loglvl()
[53] Fix | Delete
{
[54] Fix | Delete
(type -p getarg) && kdump_sysloglvl=$(getarg rd.kdumploglvl)
[55] Fix | Delete
[ -z "$kdump_sysloglvl" ] && return 1;
[56] Fix | Delete
[57] Fix | Delete
(type -p isdigit) && isdigit $kdump_sysloglvl
[58] Fix | Delete
[ $? -ne 0 ] && return 1;
[59] Fix | Delete
[60] Fix | Delete
return 0
[61] Fix | Delete
}
[62] Fix | Delete
[63] Fix | Delete
# @brief Check the log level.
[64] Fix | Delete
# @retval 1 if something has gone wrong
[65] Fix | Delete
# @retval 0 on success.
[66] Fix | Delete
#
[67] Fix | Delete
check_loglvl()
[68] Fix | Delete
{
[69] Fix | Delete
case "$1" in
[70] Fix | Delete
0|1|2|3|4)
[71] Fix | Delete
return 0
[72] Fix | Delete
;;
[73] Fix | Delete
*)
[74] Fix | Delete
return 1
[75] Fix | Delete
;;
[76] Fix | Delete
esac
[77] Fix | Delete
}
[78] Fix | Delete
[79] Fix | Delete
# @brief Initializes Logger.
[80] Fix | Delete
# @retval 1 if something has gone wrong
[81] Fix | Delete
# @retval 0 on success.
[82] Fix | Delete
#
[83] Fix | Delete
dlog_init() {
[84] Fix | Delete
local ret=0; local errmsg
[85] Fix | Delete
[86] Fix | Delete
if [ -s /proc/vmcore ];then
[87] Fix | Delete
get_kdump_loglvl
[88] Fix | Delete
if [ $? -ne 0 ];then
[89] Fix | Delete
logger -t "kdump[$$]" -p warn -- "Kdump is using the default log level(3)."
[90] Fix | Delete
kdump_sysloglvl=3
[91] Fix | Delete
fi
[92] Fix | Delete
kdump_stdloglvl=0
[93] Fix | Delete
kdump_kmsgloglvl=0
[94] Fix | Delete
else
[95] Fix | Delete
kdump_stdloglvl=$KDUMP_STDLOGLVL
[96] Fix | Delete
kdump_sysloglvl=$KDUMP_SYSLOGLVL
[97] Fix | Delete
kdump_kmsgloglvl=$KDUMP_KMSGLOGLVL
[98] Fix | Delete
fi
[99] Fix | Delete
[100] Fix | Delete
[ -z "$kdump_stdloglvl" ] && kdump_stdloglvl=3
[101] Fix | Delete
[ -z "$kdump_sysloglvl" ] && kdump_sysloglvl=0
[102] Fix | Delete
[ -z "$kdump_kmsgloglvl" ] && kdump_kmsgloglvl=0
[103] Fix | Delete
[104] Fix | Delete
for loglvl in "$kdump_stdloglvl" "$kdump_kmsgloglvl" "$kdump_sysloglvl"; do
[105] Fix | Delete
check_loglvl "$loglvl"
[106] Fix | Delete
if [ $? -ne 0 ]; then
[107] Fix | Delete
echo "Illegal log level: $kdump_stdloglvl $kdump_kmsgloglvl $kdump_sysloglvl"
[108] Fix | Delete
return 1
[109] Fix | Delete
fi
[110] Fix | Delete
done
[111] Fix | Delete
[112] Fix | Delete
# Skip initialization if it's already done.
[113] Fix | Delete
[ -n "$kdump_maxloglvl" ] && return 0
[114] Fix | Delete
[115] Fix | Delete
if [[ $UID -ne 0 ]]; then
[116] Fix | Delete
kdump_kmsgloglvl=0
[117] Fix | Delete
kdump_sysloglvl=0
[118] Fix | Delete
fi
[119] Fix | Delete
[120] Fix | Delete
if [[ $kdump_sysloglvl -gt 0 ]]; then
[121] Fix | Delete
if [[ -d /run/systemd/journal ]] \
[122] Fix | Delete
&& type -P systemd-cat &>/dev/null \
[123] Fix | Delete
&& systemctl --quiet is-active systemd-journald.socket &>/dev/null; then
[124] Fix | Delete
readonly _systemdcatfile="/var/tmp/systemd-cat"
[125] Fix | Delete
mkfifo "$_systemdcatfile" &>/dev/null
[126] Fix | Delete
readonly _dlogfd=15
[127] Fix | Delete
systemd-cat -t 'kdump' --level-prefix=true <"$_systemdcatfile" &
[128] Fix | Delete
exec 15>"$_systemdcatfile"
[129] Fix | Delete
elif ! [ -S /dev/log -a -w /dev/log ] || ! command -v logger >/dev/null; then
[130] Fix | Delete
# We cannot log to syslog, so turn this facility off.
[131] Fix | Delete
kdump_kmsgloglvl=$kdump_sysloglvl
[132] Fix | Delete
kdump_sysloglvl=0
[133] Fix | Delete
ret=1
[134] Fix | Delete
errmsg="No '/dev/log' or 'logger' included for syslog logging"
[135] Fix | Delete
fi
[136] Fix | Delete
fi
[137] Fix | Delete
[138] Fix | Delete
local lvl; local maxloglvl_l=0
[139] Fix | Delete
for lvl in $kdump_stdloglvl $kdump_sysloglvl $kdump_kmsgloglvl; do
[140] Fix | Delete
[[ $lvl -gt $maxloglvl_l ]] && maxloglvl_l=$lvl
[141] Fix | Delete
done
[142] Fix | Delete
readonly kdump_maxloglvl=$maxloglvl_l
[143] Fix | Delete
export kdump_maxloglvl
[144] Fix | Delete
[145] Fix | Delete
if [[ $kdump_stdloglvl -lt 4 ]] && [[ $kdump_kmsgloglvl -lt 4 ]] && [[ $kdump_sysloglvl -lt 4 ]]; then
[146] Fix | Delete
unset ddebug
[147] Fix | Delete
ddebug() { :; };
[148] Fix | Delete
fi
[149] Fix | Delete
[150] Fix | Delete
if [[ $kdump_stdloglvl -lt 3 ]] && [[ $kdump_kmsgloglvl -lt 3 ]] && [[ $kdump_sysloglvl -lt 3 ]]; then
[151] Fix | Delete
unset dinfo
[152] Fix | Delete
dinfo() { :; };
[153] Fix | Delete
fi
[154] Fix | Delete
[155] Fix | Delete
if [[ $kdump_stdloglvl -lt 2 ]] && [[ $kdump_kmsgloglvl -lt 2 ]] && [[ $kdump_sysloglvl -lt 2 ]]; then
[156] Fix | Delete
unset dwarn
[157] Fix | Delete
dwarn() { :; };
[158] Fix | Delete
unset dwarning
[159] Fix | Delete
dwarning() { :; };
[160] Fix | Delete
fi
[161] Fix | Delete
[162] Fix | Delete
if [[ $kdump_stdloglvl -lt 1 ]] && [[ $kdump_kmsgloglvl -lt 1 ]] && [[ $kdump_sysloglvl -lt 1 ]]; then
[163] Fix | Delete
unset derror
[164] Fix | Delete
derror() { :; };
[165] Fix | Delete
fi
[166] Fix | Delete
[167] Fix | Delete
[ -n "$errmsg" ] && derror "$errmsg"
[168] Fix | Delete
[169] Fix | Delete
return $ret
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
## @brief Converts numeric level to logger priority defined by POSIX.2.
[173] Fix | Delete
#
[174] Fix | Delete
# @param lvl Numeric logging level in range from 1 to 4.
[175] Fix | Delete
# @retval 1 if @a lvl is out of range.
[176] Fix | Delete
# @retval 0 if @a lvl is correct.
[177] Fix | Delete
# @result Echoes logger priority.
[178] Fix | Delete
_lvl2syspri() {
[179] Fix | Delete
case "$1" in
[180] Fix | Delete
1) echo error;;
[181] Fix | Delete
2) echo warning;;
[182] Fix | Delete
3) echo info;;
[183] Fix | Delete
4) echo debug;;
[184] Fix | Delete
*) return 1;;
[185] Fix | Delete
esac
[186] Fix | Delete
}
[187] Fix | Delete
[188] Fix | Delete
## @brief Converts logger numeric level to syslog log level
[189] Fix | Delete
#
[190] Fix | Delete
# @param lvl Numeric logging level in range from 1 to 4.
[191] Fix | Delete
# @retval 1 if @a lvl is out of range.
[192] Fix | Delete
# @retval 0 if @a lvl is correct.
[193] Fix | Delete
# @result Echoes kernel console numeric log level
[194] Fix | Delete
#
[195] Fix | Delete
# Conversion is done as follows:
[196] Fix | Delete
#
[197] Fix | Delete
# <tt>
[198] Fix | Delete
# none -> LOG_EMERG (0)
[199] Fix | Delete
# none -> LOG_ALERT (1)
[200] Fix | Delete
# none -> LOG_CRIT (2)
[201] Fix | Delete
# ERROR(1) -> LOG_ERR (3)
[202] Fix | Delete
# WARN(2) -> LOG_WARNING (4)
[203] Fix | Delete
# none -> LOG_NOTICE (5)
[204] Fix | Delete
# INFO(3) -> LOG_INFO (6)
[205] Fix | Delete
# DEBUG(4) -> LOG_DEBUG (7)
[206] Fix | Delete
# </tt>
[207] Fix | Delete
#
[208] Fix | Delete
# @see /usr/include/sys/syslog.h
[209] Fix | Delete
_dlvl2syslvl() {
[210] Fix | Delete
local lvl
[211] Fix | Delete
[212] Fix | Delete
case "$1" in
[213] Fix | Delete
1) lvl=3;;
[214] Fix | Delete
2) lvl=4;;
[215] Fix | Delete
3) lvl=6;;
[216] Fix | Delete
4) lvl=7;;
[217] Fix | Delete
*) return 1;;
[218] Fix | Delete
esac
[219] Fix | Delete
[220] Fix | Delete
# The number is constructed by multiplying the facility by 8 and then
[221] Fix | Delete
# adding the level.
[222] Fix | Delete
# About The Syslog Protocol, please refer to the RFC5424 for more details.
[223] Fix | Delete
echo $((24+$lvl))
[224] Fix | Delete
}
[225] Fix | Delete
[226] Fix | Delete
## @brief Prints to stderr, to syslog and/or /dev/kmsg given message with
[227] Fix | Delete
# given level (priority).
[228] Fix | Delete
#
[229] Fix | Delete
# @param lvl Numeric logging level.
[230] Fix | Delete
# @param msg Message.
[231] Fix | Delete
# @retval 0 It's always returned, even if logging failed.
[232] Fix | Delete
#
[233] Fix | Delete
# @note This function is not supposed to be called manually. Please use
[234] Fix | Delete
# dinfo(), ddebug(), or others instead which wrap this one.
[235] Fix | Delete
#
[236] Fix | Delete
# This is core logging function which logs given message to standard error
[237] Fix | Delete
# and/or syslog (with POSIX shell command <tt>logger</tt>) and/or to /dev/kmsg.
[238] Fix | Delete
# The format is following:
[239] Fix | Delete
#
[240] Fix | Delete
# <tt>X: some message</tt>
[241] Fix | Delete
#
[242] Fix | Delete
# where @c X is the first letter of logging level. See module description for
[243] Fix | Delete
# details on that.
[244] Fix | Delete
#
[245] Fix | Delete
# Message to syslog is sent with tag @c kdump. Priorities are mapped as
[246] Fix | Delete
# following:
[247] Fix | Delete
# - @c ERROR to @c error
[248] Fix | Delete
# - @c WARN to @c warning
[249] Fix | Delete
# - @c INFO to @c info
[250] Fix | Delete
# - @c DEBUG to @c debug
[251] Fix | Delete
_do_dlog() {
[252] Fix | Delete
local lvl="$1"; shift
[253] Fix | Delete
local msg="$*"
[254] Fix | Delete
[255] Fix | Delete
[[ $lvl -le $kdump_stdloglvl ]] && printf -- 'kdump: %s\n' "$msg" >&2
[256] Fix | Delete
[257] Fix | Delete
if [[ $lvl -le $kdump_sysloglvl ]]; then
[258] Fix | Delete
if [[ "$_dlogfd" ]]; then
[259] Fix | Delete
printf -- "<%s>%s\n" "$(($(_dlvl2syslvl $lvl) & 7))" "$msg" >&$_dlogfd
[260] Fix | Delete
else
[261] Fix | Delete
logger -t "kdump[$$]" -p $(_lvl2syspri $lvl) -- "$msg"
[262] Fix | Delete
fi
[263] Fix | Delete
fi
[264] Fix | Delete
[265] Fix | Delete
[[ $lvl -le $kdump_kmsgloglvl ]] && \
[266] Fix | Delete
echo "<$(_dlvl2syslvl $lvl)>kdump[$$] $msg" >/dev/kmsg
[267] Fix | Delete
}
[268] Fix | Delete
[269] Fix | Delete
## @brief Internal helper function for _do_dlog()
[270] Fix | Delete
#
[271] Fix | Delete
# @param lvl Numeric logging level.
[272] Fix | Delete
# @param msg Message.
[273] Fix | Delete
# @retval 0 It's always returned, even if logging failed.
[274] Fix | Delete
#
[275] Fix | Delete
# @note This function is not supposed to be called manually. Please use
[276] Fix | Delete
# dinfo(), ddebug(), or others instead which wrap this one.
[277] Fix | Delete
#
[278] Fix | Delete
# This function calls _do_dlog() either with parameter msg, or if
[279] Fix | Delete
# none is given, it will read standard input and will use every line as
[280] Fix | Delete
# a message.
[281] Fix | Delete
#
[282] Fix | Delete
# This enables:
[283] Fix | Delete
# dwarn "This is a warning"
[284] Fix | Delete
# echo "This is a warning" | dwarn
[285] Fix | Delete
dlog() {
[286] Fix | Delete
[ -z "$kdump_maxloglvl" ] && return 0
[287] Fix | Delete
[[ $1 -le $kdump_maxloglvl ]] || return 0
[288] Fix | Delete
[289] Fix | Delete
if [[ $# -gt 1 ]]; then
[290] Fix | Delete
_do_dlog "$@"
[291] Fix | Delete
else
[292] Fix | Delete
while read line || [ -n "$line" ]; do
[293] Fix | Delete
_do_dlog "$1" "$line"
[294] Fix | Delete
done
[295] Fix | Delete
fi
[296] Fix | Delete
}
[297] Fix | Delete
[298] Fix | Delete
## @brief Logs message at DEBUG level (4)
[299] Fix | Delete
#
[300] Fix | Delete
# @param msg Message.
[301] Fix | Delete
# @retval 0 It's always returned, even if logging failed.
[302] Fix | Delete
ddebug() {
[303] Fix | Delete
set +x
[304] Fix | Delete
dlog 4 "$@"
[305] Fix | Delete
[ -n "$debug" ] && set -x || :
[306] Fix | Delete
}
[307] Fix | Delete
[308] Fix | Delete
## @brief Logs message at INFO level (3)
[309] Fix | Delete
#
[310] Fix | Delete
# @param msg Message.
[311] Fix | Delete
# @retval 0 It's always returned, even if logging failed.
[312] Fix | Delete
dinfo() {
[313] Fix | Delete
set +x
[314] Fix | Delete
dlog 3 "$@"
[315] Fix | Delete
[ -n "$debug" ] && set -x || :
[316] Fix | Delete
}
[317] Fix | Delete
[318] Fix | Delete
## @brief Logs message at WARN level (2)
[319] Fix | Delete
#
[320] Fix | Delete
# @param msg Message.
[321] Fix | Delete
# @retval 0 It's always returned, even if logging failed.
[322] Fix | Delete
dwarn() {
[323] Fix | Delete
set +x
[324] Fix | Delete
dlog 2 "$@"
[325] Fix | Delete
[ -n "$debug" ] && set -x || :
[326] Fix | Delete
}
[327] Fix | Delete
[328] Fix | Delete
## @brief It's an alias to dwarn() function.
[329] Fix | Delete
#
[330] Fix | Delete
# @param msg Message.
[331] Fix | Delete
# @retval 0 It's always returned, even if logging failed.
[332] Fix | Delete
dwarning() {
[333] Fix | Delete
set +x
[334] Fix | Delete
dwarn "$@"
[335] Fix | Delete
[ -n "$debug" ] && set -x || :
[336] Fix | Delete
}
[337] Fix | Delete
[338] Fix | Delete
## @brief Logs message at ERROR level (1)
[339] Fix | Delete
#
[340] Fix | Delete
# @param msg Message.
[341] Fix | Delete
# @retval 0 It's always returned, even if logging failed.
[342] Fix | Delete
derror() {
[343] Fix | Delete
set +x
[344] Fix | Delete
dlog 1 "$@"
[345] Fix | Delete
[ -n "$debug" ] && set -x || :
[346] Fix | Delete
}
[347] Fix | Delete
[348] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function