forked from awslabs/aws-ec2rescue-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.bash
117 lines (101 loc) · 3.47 KB
/
functions.bash
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
#!/bin/bash
# Copyright 2016-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
#
# or in the "license" file accompanying this file. This file 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.
##
## shared functions and boilerplate for ec2rl modules
## - improve readability of functions
## - reduce duplicated code
## -
##
## disable some bash builtins to clear the namespace
enable -n help
## debug printing
shopt -s expand_aliases
alias log='echo '
alias error='>&4 echo "Error: ${__name__}:"'
if $DEBUG; then
alias debug='>&4 '
else
alias debug='#'
fi
## set common variables by reading from modules' comment-block
__name__=${0##*/}
__list__="$(awk -F '#Q# ' '/^#Q#/{print $2}' ${0})"
__help__="$(awk -F '#H# ' '/^#H#/{print $2}' ${0})"
##
## logsearch
##
## Searches files in given location for a pattern.
## Automatically detects compression and decompresses as needed
## Returns nothing if no results found
##
## Usage:
## logsearch <locations> <search string> [grep opts]
##
## Example:
## logsearch "/var/log/messages* /var/log/syslog*" "panic" "-A30"
##
## arg1 = filepaths (required)
## arg2 = search pattern (required)
## arg3 = grep arguments (optional)
function logsearch {
if test -z "$1"; then
echo "No search locations given"
return 1
fi
if test -z "$2"; then
echo "No search pattern given"
return 2
fi
local filepaths=$1
local pattern=$2
local grepargs="$3"
for filename in $filepaths; do
if [ -e $filename ] && [ -r $filename ]; then
#file exists and is readable
local filetype=$(file $filename)
#This is where additional compression types should be added
case $filetype in
*gzip*)
if zgrep -v \/zgrep\ $filename | zgrep $grepargs "$pattern" >/dev/null 2>&1; then
echo -e "Match found in $filename"
zgrep $grepargs "$pattern" $filename
fi ;;
*xz*)
if grep -v \/xzgrep\ $filename | xzgrep $grepargs "$pattern" >/dev/null 2>&1; then
echo -e "Match found in $filename"
xzgrep $grepargs "$pattern" $filename
fi ;;
*)
#assume uncompressed
if grep -v \/grep\ $filename | grep $grepargs "$pattern" >/dev/null 2>&1; then
echo -e "Match found in $filename"
grep $grepargs "$pattern" $filename
fi ;;
esac
fi
done
# Disabled journal searching due to performance impact
# if which journalctl >/dev/null 2>&1; then
# if journalctl -ql | grep -v \/grep\ | grep $grepargs "$pattern" >/dev/null 2>&1; then
# echo -e "Match found in systemd journal"
# journalctl -ql | grep -v \/grep\ | grep $grepargs "$pattern"
# fi
# fi
}
##
## $EC2RL_INTERFACES
## Enumerates all of the interfaces on a device minus loopback
##
EC2RL_INTERFACES="$(find /sys/class/net/* | awk -F'/' '!/lo/ {print $5}')"