Repository navigation
Expand file tree
/
Copy pathtemp_overlay.sh
More file actions
398 lines (348 loc) · 11.9 KB
/
Copy pathtemp_overlay.sh
File metadata and controls
398 lines (348 loc) · 11.9 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/bin/bash
#Variable to keep track of version for auditing purposes
SCRIPT_VERSION=0.1.0
SCRIPT_DATE="2017-10-16"
#--------------------------------------------------------------------------
# FUNCTION redirect_output
# SYNTAX redirect_output <LOG_FILE>
# DESCRIPTION Manages redirection/prepending of all stdOut/stdErr to a logfile and screen
#
# VARIABLE
# DEPENDENCIES init_call
#
# FUNCTION
# DEPENDENCIES n/a
#--------------------------------------------------------------------------
function redirect_output
{
#Validate the number of passed variables
if [[ $# -lt 1 ]]
then
#Invalid number of arguments
>&2 echo "Received an invalid number of arguments"
exit 1
fi
typeset str_log_file=$1; shift
if [[ ${init_call} -eq 0 ]]
then
#Initial execution
init_call=1
#Save our stdOut/stdErr values
exec 3>&1
exec 4>&2
elif [[ -z ${init_call} ]]
then
#The required init_call global variable is not set
>&2 echo "The function redirect_output requires an init_call global variable set to 0"
exit 1
fi
#Restore stdOut/stdErr to normal
exec 1>&3 3>&-
exec 2>&4 4>&-
#Only redirect output if we can
if [[ "${str_log_file}" != "STOP" ]]
then
#Redirect our stdOut/stdErr
exec 3>&1 1> >(tee ${str_log_file})
exec 4>&2 2> >(tee ${str_log_file})
else
#We are essentially back to normal now. Reset our init_call variable
init_call=0
fi
}
#--------------------------------------------------------------------------
# FUNCTION find_local_node_name
# SYNTAX variable=$(find_local_node_name)
# DESCRIPTION Finds the local node name and strips any FQDN
#
# VARIABLE
# DEPENDENCIES n/a
#
# FUNCTION
# DEPENDENCIES n/a
#--------------------------------------------------------------------------
function find_local_node_name
{
#Setup variables
typeset str_local_node
#Get the local node name and strip any suffix
str_local_node=$(hostname -s)
#echo the str_local_node name
echo ${str_local_node}
return 0
}
#--------------------------------------------------------------------------
# FUNCTION script_exit
# SYNTAX script_exit <exitCode>
# DESCRIPTION Cleans up logs and output redirection and performs any other exit tasks
#
# VARIABLE
# DEPENDENCIES N/A
#
# FUNCTION
# DEPENDENCIES redirect_output
#--------------------------------------------------------------------------
function script_exit
{
#Validate the number of passed variables
if [[ $# -gt 1 ]]
then
#Invalid number of arguments
#We're just echoing this as a note, we still want the script to exit
>&2 echo "Received an invalid number of arguments"
fi
#Unset our redirection of stdout/stderr
redirect_output "STOP"
sync
#Define variables as local first
typeset int_exit_code=$1; shift
#Reset signal handlers to default actions
trap - 0 1 2 3 15
#Exit
exit $int_exit_code
}
#--------------------------------------------------------------------------
# FUNCTION script_usage
# SYNTAX usage
# DESCRIPTION Displays proper usage syntax for the script
#
# VARIABLE
# DEPENDENCIES n/a
#
# FUNCTION
# DEPENDENCIES n/a
#--------------------------------------------------------------------------
function script_usage
{
>&2 echo "Usage: ./${SCRIPT_NAME}.${SCRIPT_EXTENSION} -f <config_file>"
>&2 echo " -f : Config file"
>&2 echo ""
script_exit 1
}
#--------------------------------------------------------------------------
# FUNCTION check_execution
# SYNTAX check_execution <PID_FILE> <wait>
# DESCRIPTION This function identifies if there is already an instance of this script running
# 0=Success
# 1=Failure
# 2=Error
#--------------------------------------------------------------------------
function check_execution
{
#Validate the number of passed variables
if [[ $# -ne 1 ]]
then
#Invalid number of arguments
>&2 echo "Received an invalid number of arguments"
exit 2
fi
#Assign variables passed
typeset str_pid_file=$1
#Use a file descriptor to track a file for locking so we can utilize flock
exec 9>${str_pid_file}
#Acquire an exclusive lock to file descriptor 9
flock -n 9 2>/dev/null
int_return_code=$?
#Check if there is already an intance of the script running
if [[ $int_return_code -eq 0 ]]
then
echo "The script ${SCRIPT_NAME} is not already running. Locking ${str_pid_file}"
else
>&2 echo "An instance of ${SCRIPT_NAME} is already running"
exit 1
fi
}
#--------------------------------------------------------------------------
# FUNCTION setup_log_file
# SYNTAX setup_log_file <log_file_name>
# DESCRIPTION Accepts full logfile path and verifies if it can be written to
# 0=Success
# 1=Failure
# 2=Error
# VARIABLE
# DEPENDENCIES n/a
#
# FUNCTION
# DEPENDENCIES n/a
#--------------------------------------------------------------------------
function setup_log_file
{
#Validate the number of passed variables
if [[ $# -ne 1 ]]
then
#Invalid number of arguments
>&2 echo "Received an invalid number of arguments"
return 2
fi
#Assign variables passed
typeset str_log_path=$1
typeset str_log_dir
typeset str_log_file
#Find the directory and filename from the full path
str_log_dir=$(dirname ${str_log_path})
if [[ $? -ne 0 ]]
then
#dirname failed
>&2 echo "Could not parse the directory path from logpath ${str_log_path}"
return 2
fi
str_log_file=$(basename ${str_log_path})
if [[ $? -ne 0 ]]
then
#basename failed
>&2 echo "Could not parse the file name from logpath ${str_log_path}"
return 2
fi
#Check for the directory
if [[ ! -a ${str_log_dir} ]]
then
#The directory doesn't exist, try to create it
mkdir -p ${str_log_dir} 1>/dev/null 2>&1
#Re-check for the directory
if [[ ! -a ${str_log_dir} ]]
then
#The directory could not be created
>&2 echo "The directory ${str_log_dir} does not exist and could not be created"
return 1
fi
fi
#Check if the direcotory is writeable
if [[ ! -w ${str_log_dir} ]]
then
#The directory is not writeable, lets try to change that
chmod ugo+w ${str_log_dir} 1>/dev/null 2>&1
#Re-check for write permissions
if [[ ! -w ${str_log_dir} ]]
then
#The directory can still not be written to
>&2 echo "The direcotry ${str_log_dir} can not be written to"
return 1
fi
fi
#Check if the file already exists
if [[ -a ${str_log_path} ]]
then
#The file already exists, is it writable?
if [[ ! -w ${str_log_path} ]]
then
#The file exists but is NOT writeable, lets try changing it
chmod ugo+w ${str_log_path} 1>/dev/null 2>&1
#Now, recheck if the file is writeable
if [[ ! -w ${str_log_path} ]]
then
#Still can't write to the file
>&2 echo "The file ${str_log_path} can not be written to"
return 1
fi
fi
else
#The file does not exist, lets touch it
touch ${str_log_path}
fi
#No Error
return 0
}
#--------------------------------------------------------------------------
# FUNCTION main
# SYNTAX main "$@"
# DESCRIPTION Performs main steps
#
# VARIABLE
# DEPENDENCIES N/A
#
# FUNCTION
# DEPENDENCIES N/A
#--------------------------------------------------------------------------
function main
{
#Check Incoming Variables and Usage
while getopts f: options
do
case $options in
f)
CONFIG_FILE=$OPTARG
if [[ -z $CONFIG_FILE ]]
then
>&2 echo "CONFIG_FILE (-f) was provided on the command line, but the value provided is null"
script_usage
fi
;;
\?)
clear
;;
*)
clear
;;
esac
done
#ENSURE CONFIG_FILE WAS PROVIDED IN THE COMMAND LINE
if [[ -z $CONFIG_FILE ]]
then
>&2 echo "CONFIG_FILE (-f) value is null"
script_usage
fi
#Find influxDB configuration
str_db_config_raw=$(egrep '^db' ${CONFIG_FILE} | head -n 1)
db_ip=$(echo ${str_db_config_raw} | cut -d',' -f2)
db_port=$(echo ${str_db_config_raw} | cut -d',' -f3)
db_name=$(echo ${str_db_config_raw} | cut -d',' -f4)
#Parse cameras and update if necessary
int_i=0
while read cam
do
str_cam_config_raw=${cam}
cam_ip[${int_i}]=$(echo ${str_cam_config_raw} | cut -d',' -f2)
cam_username[${int_i}]=$(echo ${str_cam_config_raw} | cut -d',' -f3)
cam_password[${int_i}]=$(echo ${str_cam_config_raw} | cut -d',' -f4)
cam_sensor_id[${int_i}]=$(echo ${str_cam_config_raw} | cut -d',' -f5)
cam_overlay_template[${int_i}]=$(echo ${str_cam_config_raw} | cut -d',' -f6)
cam_sensor_last[${int_i}]=$(echo ${str_cam_config_raw} | cut -d',' -f7)
db_sensor_last[${int_i}]=$(curl -s -G "http://${db_ip}:${db_port}/query?pretty=true" --data-urlencode "db=${db_name}" --data-urlencode "q=SELECT last(\"value\") FROM \"temperature\" WHERE \"deviceId\"='${cam_sensor_id[${int_i}]}'" | jq '.results[].series[].values[][-1]' | cut -d'.' -f1)
if [[ "${cam_sensor_last[${int_i}]}" -eq "${db_sensor_last[${int_i}]}" ]]
then
continue
else
temp_file=$(mktemp)
sed -e "s/REPLACE_ME/${db_sensor_last[${int_i}]}F/" ${cam_overlay_template[${int_i}]} > ${temp_file}
curl -s -T ${temp_file} "http://${cam_username[${int_i}]}:${cam_password[${int_i}]}@${cam_ip[${int_i}]}/Video/inputs/channels/1/overlays/text/1" >/dev/null
sed -i "/${cam_ip[${int_i}]}/s/,${cam_sensor_last[${int_i}]}$/,${db_sensor_last[${int_i}]}/" ${CONFIG_FILE}
fi
((int_i+=1))
done < <(egrep '^cam' ${CONFIG_FILE})
}
#See if this script is already running
SCRIPT_NAME=$(echo $(basename ${0}) | rev | cut -d'.' -f2- | rev)
PID_FILE=/var/run/${SCRIPT_NAME}.pid
check_execution ${PID_FILE}
#Set signal handlers to run our script_exit Function
trap 'rc=$?; script_exit $rc' 0 1 2 3 15
#Required for the redirect_output Function
init_call=0
#Pulls the script name without directory paths or extension
SCRIPT_FILE=$(basename ${0})
SCRIPT_EXTENSION=$(echo $(basename ${0}) | rev | cut -d'.' -f1 | rev)
SCRIPT_PATH=$(readlink -f $0)
SCRIPT_BASE=$(dirname ${SCRIPT_PATH})
SCRIPT_FLAGS=$@
#Pulls the local node name, not including any suffix
LOCAL_NODE=$(find_local_node_name)
#Script directory
SCRIPT_DIR_BASE=/usr/local/bin
SCRIPT_DIR_PATH=${SCRIPT_DIR_BASE}/${SCRIPT_NAME}
#Timestamp
DATE_STAMP=$(date +"%Y.%m.%d")
TIME_STAMP=$(date +"%Y.%m.%d.%H.%M.%S")
#Various log files
LOG_FILE_PATH=${SCRIPT_DIR_PATH}
LOG_FILE=${LOG_FILE_PATH}/${SCRIPT_NAME}.${TIME_STAMP}.log
#Setup Logs
if [[ $(setup_log_file ${LOG_FILE}) -ne 0 ]]
then
script_exit 1
fi
#Utilize execOutputHandler to redirect/prepend all our stdOut/stdErr
redirect_output ${LOG_FILE}
#Execute main
main "$@"
#Exit
script_exit 0