-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_log
143 lines (105 loc) · 4.61 KB
/
check_log
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/bash
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKOWN=3
isWarning=0
isCritical=0
man(){
echo -e "\n*******"
echo "Check if there is a certain text in a log file."
echo "The plugin will return WARNING status if a certain word is found in the indicated log."
echo "The plugin will return CRITICAL status if a certain word is found in the indicated log."
echo "*******"
echo -e "\n------- check_log written by Miguel Calvo (ES), v1.0, 29.03.2019 -------\n"
echo -e "\nSyntax:"
echo -e "\tcheck_log Word_to_search_warning_state Word_to_search_critical_state log_file\n"
echo "Examples: "
echo -e "\tcheck_log WARNING CRITICAL /var/log/nginx/access.log"
echo -e "\tcheck_log '404' '193.123.215.5' /var/log/nginx/access.log"
}
if [ $# == 3 ]
then
SEARCH_WARNING=$1
SEARCH_CRITICAL=$2
LOG_FILE=$3
else
man
exit $STATE_UNKOWN
fi
# Check if log file exists.
if [ ! -e "$LOG_FILE" ]
then
echo "File $LOG_FILE not found."
exit $STATE_UNKOWN
fi
# Remove spaces of warning/critical keywords.
STATE_WARNING_CLEAN=`echo $SEARCH_WARNING | sed "s/[^a-zA-Z*0-9]//g"`
STATE_CRITICAL_CLEAN=`echo $SEARCH_CRITICAL | sed "s/[^a-zA-Z*0-9]//g"`
# Create Lock and Temp file_manager
TEMP_FILE_WARNING="/tmp/log-$STATE_WARNING_CLEAN.tmp"
TEMP_FILE_CRITICAL="/tmp/log-$STATE_CRITICAL_CLEAN.tmp"
if [ ! -e "$TEMP_FILE_WARNING" ]
then
touch $TEMP_FILE_WARNING
fi
if [ ! -e "$TEMP_FILE_CRITICAL" ]
then
touch $TEMP_FILE_CRITICAL
fi
PIDT=`ps -ef |grep "tail -f $LOG_FILE" |grep -v grep`
# Check if the tail is running
if [ ! "$PIDT" == "" ]; then
message=""
while read -r line; do
warningLine=`echo ${line#*|}`
warningHash=`echo ${line%|*}`
isWarning=1
if [[ $warningHash == "[1]"* ]]; then
warningHash=${warningHash#'[1]'}
sed -i "/${warningHash}\|/c [2]${warningHash} | ${warningLine}" $TEMP_FILE_WARNING
elif [[ $warningHash == "[2]"* ]]; then
warningHash=${warningHash#'[2]'}
sed -i.bak "/${warningHash}/d" $TEMP_FILE_WARNING
else
sed -i "/${warningHash}\|/c [1]${warningHash} | ${warningLine}" $TEMP_FILE_WARNING
fi
message="$message {[WARNING LOG LINE]: '$warningLine'}, "
done < "$TEMP_FILE_WARNING"
while read -r line; do
criticalLine=`echo ${line#*/|}`
criticalHash=`echo ${line%/|*}`
isCritical=1
if [[ $criticalHash == "[1]"* ]]; then
criticalHash=${criticalHash#'[1]'}
sed -i "/${criticalHash}\|/c [2]${criticalHash} | ${criticalLine}" $TEMP_FILE_CRITICAL
elif [[ $criticalHash == "[2]"* ]]; then
criticalHash=${criticalHash#'[2]'}
sed -i.bak "/${criticalHash}/d" $TEMP_FILE_CRITICAL
else
sed -i "/${criticalHash}\|/c [1]${criticalHash} | ${criticalLine}" $TEMP_FILE_CRITICAL
fi
message="$message {[CRITICAL LOG LINE]: '$criticalLine'}, "
done < "$TEMP_FILE_CRITICAL"
else
if [ ! "$SEARCH_WARNING" == "" ] ; then
SEARCH_WARNING_CLEAN=$(echo $SEARCH_WARNING | sed 's/"//g')
`nohup tail -f $LOG_FILE |grep --line-buffered \""$SEARCH_WARNING_CLEAN"\" | while read line; do hash=$(echo -n "$line" | md5sum -) && echo ${hash:0:32} \| $line; done > "$TEMP_FILE_WARNING" &`
fi
if [ ! "$SEARCH_CRITICAL" == "" ] ; then
SEARCH_CRITICAL_CLEAN=$(echo $SEARCH_CRITICAL | sed 's/"//g')
`nohup tail -f $LOG_FILE |grep --line-buffered \""$SEARCH_CRITICAL_CLEAN"\" | while read line; do hash=$(echo -n "$line" | md5sum -) && echo ${hash:0:32} \| \| $line; done > "$TEMP_FILE_WARNING" &`
fi
echo "UNKOWN - Starting the script for $LOG_FILE. | SEARCH_WARNING=$SEARCH_WARNING_CLEAN;SEARCH_CRITICAL=$SEARCH_CRITICAL_CLEAN"
exit $STATE_UNKOWN
fi
if [ "$isCritical" == 1 ] ; then
echo "CRITICAL - There are critical errors in $LOG_FILE: $message | SEARCH_WARNING=$SEARCH_WARNING;SEARCH_CRITICAL=$SEARCH_CRITICAL"
exit $STATE_CRITICAL
elif [ "$isWarning" == 1 ] ; then
echo "WARNING - There are warnings in $LOG_FILE: $message | SEARCH_WARNING=$SEARCH_WARNING;SEARCH_CRITICAL=$SEARCH_CRITICAL"
exit $STATE_WARNING
else
echo "OK - There are no errors in $LOG_FILE. | SEARCH_WARNING=$SEARCH_WARNING;SEARCH_CRITICAL=$SEARCH_CRITICAL"
exit $STATE_OK
fi