-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogfile_monitor.sh
55 lines (49 loc) · 1.29 KB
/
logfile_monitor.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
#!/bin/bash
# variables
HASH_LINES=10
INTERNAL_FILE=/tmp/monitor_internal_file
function sameFile() {
type="$1"
hash="$2"
if [ ! -f ${INTERNAL_FILE} ]; then
echo "false"
else
new_hash=$(grep -e "^${type}_HASH:" $INTERNAL_FILE | sed -e "s/^${type}_HASH://g")
if [ -z "${new_hash}" ]; then
echo "false"
else
if [ "${new_hash}" = "${hash}" ]; then
echo "true"
else
echo "false"
fi
fi
fi
}
function getFileLine() {
type="$1"
filename="$2"
hash=$(head -n ${HASH_LINES} ${filename} | md5sum)
is_same_file=$(sameFile "${type}" "${hash}")
if [ "${is_same_file}" = true ]; then
line=$(grep -e "^${type}_LINE:" ${INTERNAL_FILE} | sed -e "s/^${type}_LINE://g")
if [ -z "${line}" ]; then
RC=0
else
RC=$line
fi
else
RC=0
fi
new_line=$(wc -l ${filename} | cut -f 1 -d ' ')
sed -i "/^${type}_LINE:/d" ${INTERNAL_FILE} 2>/dev/null
sed -i "/^${type}_HASH:/d" ${INTERNAL_FILE} 2>/dev/null
echo "${type}_HASH:${hash}" >> ${INTERNAL_FILE}
echo "${type}_LINE:${new_line}" >> ${INTERNAL_FILE}
echo $RC
}
my_log=/var/log/messages
line=$(getFileLine "SYSLOG" "${my_log}")
((line++))
new_lines=$(tail -n +${line} ${my_log})
echo "${new_lines}"