-
Notifications
You must be signed in to change notification settings - Fork 9
/
encore.sh
218 lines (171 loc) · 4.26 KB
/
encore.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/bin/sh
# debug
# set -x
# vars
pid=-1
configFilepath="estreamer.conf"
pybin="python3"
basepath="."
isRunning=0
# constants
configure="$pybin ./estreamer/configure.py $configFilepath"
diagnostics="$pybin ./estreamer/diagnostics.py $configFilepath"
service="$pybin ./estreamer/service.py $configFilepath"
preflight="$pybin ./estreamer/preflight.py $configFilepath"
pidFile="encore.pid"
EXIT_CODE_ERROR=1
# change pwd
cd $basepath
setup() {
$configure --enabled=true
read -p 'Would you like to output to (1) Splunk, (2) CEF or (3) JSON?' input
if [ "$input" = "1" ]
then
$configure --output=splunk
echo 'If you wish to change where data is written to then edit estreamer.conf '
echo 'and change $.handler.outputters[0].stream.uri'
echo
elif [ "$input" = "2" ]
then
$configure --output=cef
echo 'You need to set the target syslog server and port; edit estreamer.conf '
echo 'and change $.handler.outputters[0].stream.uri'
echo
elif [ "$input" = "3" ]
then
$configure --output=json
echo 'If you wish to change where data is written to then edit estreamer.conf '
echo 'and change $.handler.outputters[0].stream.uri'
echo
else
echo 'No changes made'
echo
exit $EXIT_CODE_ERROR
fi
}
init() {
pythonVersion=`$pybin -V 2>&1 | grep "Python 3*"`
# echo "Python Version " + sys.version()
if [ ! -e "$configFilepath" ]
then
cp default.conf $configFilepath
setup
fi
$preflight
ok=$?
if [ "$ok" -ne 0 ]
then
exit $EXIT_CODE_ERROR
fi
pidFile=`$configure --print pidFile`
pid=`$configure --print pid`
# Work out if we're running already
ps ax | grep -F -- $pid | grep -v 'grep' > /dev/null 2>&1
process=$?
if [ $pid = '-1' ]
then
: #echo "Checking pid.... none found."
elif [ $process -eq 1 ]
then
# echo "Stale pidFile ($pid). Removing"
rm $pidFile
pid=-1
elif [ $process -eq 0 ]
then
# echo "$service ($pid) is running."
isRunning=1
fi
}
diagnostics() {
$diagnostics
}
foreground() {
$service
}
start() {
if [ $isRunning -eq 0 ]
then
echo -n "Starting \"$service\". "
$service > /dev/null 2>&1 &
sleep 1
pid=`$configure --print pid`
echo "Started. pid=$pid"
else
echo "$service is already running."
fi
}
stop() {
if [ $isRunning -eq 0 ]
then
echo "Not running"
else
echo "Found pid. Terminating \"$service\" ($pid)"
kill -s INT $pid
# Wait for the process to finish
while [ 1 ]
do
# Do not redirect stdErr - Splunk no likey
ps ax | grep -F -- $pid | grep -v 'grep' > /dev/null #2>&1
process=$?
if [ $process -eq 1 ]
then
break
fi
sleep 0.5
done
pid='-1'
isRunning=0
sleep 1
fi
}
archive() {
stop
filename=$(date +%Y-%m-%d_%H-%M-%S%z%Z)
archive=$(tar -zcvf "encore-log.tar.gz" ./estreamer.log)
rm estreamer.log
mv "encore-log.tar.gz" "encore-log-$filename.tar.gz"
start
}
restart() {
stop
start
}
main() {
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
test)
diagnostics
;;
foreground)
foreground
;;
archive)
archive
;;
setup)
setup
;;
*)
echo $"Usage: $prog {start | stop | restart | foreground | test | setup}"
echo
echo ' start: starts eNcore as a background task'
echo ' stop: stop the eNcore background task'
echo ' restart: stop the eNcore background task'
echo ' foreground: runs eNcore in the foreground'
echo ' test: runs a quick test to check connectivity'
echo ' setup: change the output (splunk | cef | json)'
echo
echo $1
exit $EXIT_CODE_ERROR
esac
}
init
main $1