-
Notifications
You must be signed in to change notification settings - Fork 2
/
entries.sh
executable file
·81 lines (73 loc) · 2.07 KB
/
entries.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
#!/bin/bash
. /etc/nsclientsh.conf
# Init options
opt_Verbose=0
opt_SkipUpdate=0
opt_ForceUpdate=0
updated=0
declare arr_requests=()
result=""
get_entries_json() {
#rm $entries_json
wget $ns_url/api/v2/entries.json?count=36\&token=$token -qO $entries_json 2> /dev/null
}
secs2mins() {
((h=${1}/3600))
((m=(${1}%3600)/60))
echo $((($h/60)+$m))
}
[ -f "$entries_json" ] && [ -s "$entries_json" ] && jq empty $entries_json &> /dev/null
retVal=$?
if [ $retVal -ne 0 ]; then
[ $opt_Verbose -eq 1 ] && echo "Error parsing entries.json"
opt_SkipUpdate=0
opt_ForceUpdate=1
fi
# Update entries.json
if [ $opt_SkipUpdate -ne 1 ]; then
if [ $opt_ForceUpdate -eq 1 ]; then
[ $opt_Verbose -eq 1 ] && echo "Forcing update"
rm $entries_json
fi
if [ ! -f "$entries_json" ]; then
[ $opt_Verbose -eq 1 ] && echo "Updating entries.json"
get_entries_json
updated=1
else
mills=$(jq -r '.[0].mills' $entries_json)
age=$(($(date +%s%N | cut -b1-10) - ${mills::-3}))
age_mins=$(secs2mins $age)
[ $opt_Verbose -eq 1 ] && echo "Age of data: $age_mins minutes ($age seconds)"
if [ "$age_mins" -gt "4" ]; then
[ $opt_Verbose -eq 1 ] && echo "Updating entries.json"
get_entries_json
updated=1
fi
fi
elif [ $opt_Verbose -eq 1 ]; then
echo "Skipping update"
fi
# Second integrity check of entries.json, in case we updated it
if [ $updated -eq 1 ]; then
[ $opt_Verbose -eq 1 ] && echo "Updating file information"
[ -f "$entries_json" ] && [ -s "$entries_json" ] && jq empty $entries_json &> /dev/null
retVal=$?
if [ $retVal -ne 0 ]; then
[ $opt_Verbose -eq 1 ] && echo "Error parsing entries.json, exiting!"
exit 1
fi
# Update data age
mills=$(jq -r '.[0].mills' $entries_json)
age=$(($(date +%s%N | cut -b1-10) - ${mills::-3}))
age_mins=$(secs2mins $age)
fi
[ $opt_Verbose -eq 1 ] && echo "Fetching requests (.[0..35].sgv)"
for (( c=0; c<=35; c++ )); do
if [ -n "$result" ]; then
result="${result},"
fi
request=".[$c].sgv"
response=$(jq -r $request $entries_json)
result="${result}$response"
done
echo $result