forked from gyp/ssb_rest_api_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtail.sh
executable file
·63 lines (49 loc) · 1.71 KB
/
tail.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
#!/bin/bash
. ssb_credentials
LOGSPACE=center
SLEEP=2
LINES_PER_FETCH=1000
SEARCH_EXPRESSION=$1
SESSID=`wget -q --no-check-certificate -O - "https://$SSB_IP/api/1/login" --post-data="username=$USERNAME&password=$PASSWORD" | jq '.result' -r`
LAST_MINUTE=0
function api_request {
REQUEST=$@
wget -q --no-check-certificate -O - --header "Cookie: AUTHENTICATION_TOKEN=$SESSID" "https://$SSB_IP/api/1/search/logspace/$REQUEST" | jq '.result' -r
}
function fetch_logs {
fl_SEARCH_EXPRESSION=$1
fl_FROM=$2
fl_TO=$3
fl_OFFSET=$4
fl_LIMIT=$5
api_request "filter/$LOGSPACE?search_expression=$fl_SEARCH_EXPRESSION&from=$fl_FROM&to=$fl_TO&offset=$fl_OFFSET&limit=$fl_LIMIT"
}
function print_logs {
RAW_LOGS=$@
echo $RAW_LOGS | jq -c -M '.[].message'
#echo $RAW_LOGS | jq -c -M '.[] | {timestamp: .processed_timestamp, message: .message}'
}
# let's find the last message and their start timestamp
TO=9999999999
NUM_OF_MESSAGES=$(api_request "number_of_messages/$LOGSPACE?search_expression=$SEARCH_EXPRESSION&from=0&to=$TO")
OFFSET=$[$NUM_OF_MESSAGES - 1]
if [ $OFFSET -lt 0 ]
then
OFFSET=0
fi
FROM=$(api_request "filter/$LOGSPACE?search_expression=$SEARCH_EXPRESSION&from=0&to=$TO&offset=$OFFSET&limit=1" | jq '.[0].processed_timestamp')
NUM_OF_MESSAGES=$(api_request "number_of_messages/$LOGSPACE?search_expression=$SEARCH_EXPRESSION&from=$FROM&to=$TO")
# let's display the last 10 messages again...
OFFSET=$[$NUM_OF_MESSAGES - 10]
if [ $OFFSET -lt 0 ]
then
OFFSET=0
fi
while true
do
LOGS=$(fetch_logs "$SEARCH_EXPRESSION" $FROM $TO $OFFSET $LINES_PER_FETCH)
NUM_OF_LINES=`echo $LOGS | jq '. | length'`
OFFSET=$[$OFFSET + $NUM_OF_LINES]
print_logs $LOGS
sleep $SLEEP
done