-
Notifications
You must be signed in to change notification settings - Fork 635
/
generator.sh
executable file
·66 lines (60 loc) · 1.97 KB
/
generator.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash
# Copyright 2017 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This script generate kernel log in given rate.
# Note that this only works for journald.
# Note that this script can't be used to test multi-line patterns, because
# the test kernel log lines may be split by real kernel log.
# PROBLEM is the path to the problem log.
PROBLEM=${PROBLEM:-""}
# LINES_PER_SECOND is the log generating rate.
LINES_PER_SECOND=${LINES_PER_SECOND:-1}
# LOG_PATH is the path to generate kernel logs.
LOG_PATH=${LOG_PATH:-"/dev/kmsg"}
# RUN_ONCE specifies whether to keep generating problem logs or only
# generate once.
RUN_ONCE=${RUN_ONCE:-"false"}
if [[ -z ${PROBLEM} ]]; then
echo "$(basename $0): PROBLEM is empty"
exit 1
fi
# now returns the current unix time in micro seconds
now() {
echo $(($(date +'%s%N') / 1000))
}
prefix="kernel: "
logs=$(<${PROBLEM})
lines=$(printf "%s\n" "${logs}" | wc -l)
current_lines=0
start=$(now)
while true; do
while read log; do
echo "${prefix}${log}" >> ${LOG_PATH}
((current_lines++))
if [[ ${current_lines} == ${LINES_PER_SECOND} ]]; then
current_lines=0
current=$(now)
duration=$((1000000 - current + start))
if [[ ${duration} -gt 0 ]]; then
echo "generated ${LINES_PER_SECOND} lines of log, sleep for ${duration}"
usleep ${duration}
fi
start=$(now)
fi
done <<< "$logs"
if [[ "${RUN_ONCE}" == "true" ]]; then
echo "stop the generator"
exit 0
fi
done