-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetAlerts.sh
executable file
·196 lines (168 loc) · 3.94 KB
/
getAlerts.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
#!/bin/bash
# To set .accuknox.cfg check https://github.com/accuknox/tools/tree/main/api-samples
. ${ACCUKNOX_CFG:-~/.accuknox.cfg}
. util.sh
# Other params
TMP=$DIR/$(basename $0)
clusterspec=".*" # regex for cluster names
usage() {
echo "Usage: $0 " 1>&2
echo " -f <json filter list>" 1>&2
echo " -p <page number>" 1>&2
echo " -g <per page>" 1>&2
echo " -t <from time>" 1>&2
echo " -e <to time>" 1>&2
echo " -c <cluster name>" 1>&2
echo " -v <any string for verbose>" 1>&2
echo 1>&2;
echo -n "Example: ./getAlerts.sh -f " 1>&2
echo -n "'{\"field\": \"Action\", \"value\": \"Block\", \"op\": \"match\"}'" 1>&2
echo ' -p 1 -g 100 -t $(date +%s) -e $(( $(date +%s) - 86400)) -c "nrs"' 1>&2
exit 1;
}
# api url
# https://cwpp.nrs.accuknox.com/monitors/v1/alerts/events?orderby=desc
# api response example
#{
# "Namespace": [],
# "FromTime": 1742426700,
# "ToTime": 1743031500,
# "PageId": 1,
# "PageSize": 20,
# "Filters": [
# {
# "field": "Action",
# "value": "Audit",
# "op": "match"
# },
# {
# "field": "Source",
# "value": "/usr/sbin/sshd",
# "op": "match"
# }
# ],
# "ClusterID": [],
# "View": "List",
# "Type": "kubearmor",
# "WorkloadType": [],
# "WorkloadName": [],
# "WorkspaceID": 63,
# "LogType": "active"
#}
# api error examples
# {"errors":"Key: 'AlertsLog.ClusterID' Error:Field validation for 'ClusterID' failed on the 'required' tag\n
# Key: 'AlertsLog.PageID' Error:Field validation for 'PageID' failed on the 'required' tag\n
# Key: 'AlertsLog.PageSize' Error:Field validation for 'PageSize' failed on the 'required' tag\n
# Key: 'AlertsLog.FromTime' Error:Field validation for 'FromTime' failed on the 'required' tag\n
# Key: 'AlertsLog.ToTime' Error:Field validation for 'ToTime' failed on the 'required' tag
while getopts ":p:e:t:f:c:g:v" o; do
case "${o}" in
f)
flts=${OPTARG}
;;
p)
page=${OPTARG}
;;
g)
perPage=${OPTARG}
;;
t)
ft=${OPTARG}
;;
e)
tt=${OPTARG}
;;
c)
cluster=${OPTARG}
;;
v)
v=1
;;
*)
usage
;;
esac
done
# "$flts" "$page" "$perPage" "$ft" "$tt" "$cluster" $v
if [[ "${flts}x" = "x" ]]
then
flts=""
fi
if [[ "${page}x" = "x" ]]
then
page=1
fi
if [[ "${perPage}x" = "x" ]]
then
perPage=100
fi
if [[ "${ft}x" = "x" ]]
then
ft=$( date +%s )
fi
if [[ "${tt}x" = "x" ]]
then
tt=$(( $ft - 86400 ))
fi
if [[ "${cluster}x" = "x" ]]
then
cluster="nrs"
fi
# verbose
if [[ ${v} = 1 ]]
then
echo "f: $flts p: $page g: $perPage t: $ft e:$tt c: $cluster v: $v"
fi
get_alarm_list()
{
flt=$1
page=$2
perPage=$3
tt=$4
ft=$5
clus=$6
vb=$7
data_raw=$( jq -n \
--argjson ft $ft \
--argjson tt $tt \
--argjson pid $page \
--argjson pp $perPage \
--argjson cid "[\"$cid\"]" \
'{"Namespace":[],"FromTime":$ft,"ToTime":$tt,"PageId":$pid,"PageSize":$pp,"Filters":[],"ClusterID":$cid,"View":"List","Type":"kubearmor","WorkloadType":[],"WorkloadName":[],"WorkspaceID":63,"LogType":"active"}' )
if [[ "${flt}x" != "x" ]]
then
full_query=$(echo $data_raw | jq ".Filters += [${flt}]")
data_raw=$full_query
fi
if [[ $vb = 1 ]]
then
echo
echo
echo $data_raw;
echo
echo
fi
ak_api "$CWPP_URL/monitors/v1/alerts/events?orderby=desc"
echo $json_string
}
get_cluster_id()
{
ak_api "$CWPP_URL/cluster-onboarding/api/v1/get-onboarded-clusters?wsid=$TENANT_ID"
while read cline; do
cid=${cline/ */}
cname=${cline/* /}
if [[ $cname = $6 ]]
then
[[ ! $cname =~ $clusterspec ]] && echo "ignoring cluster [$cname] ..." && continue
[[ -z $6 ]] && [[ ! $6 == $cname ]] && continue
get_alarm_list "$1" "$2" "$3" "$4" "$5" "$6" $7
fi
done < <(echo $json_string | jq -r '.[] | "\(.ID) \(.ClusterName)"')
}
main()
{
ak_prereq
get_cluster_id "$1" "$2" "$3" "$4" "$5" "$6" $7
}
# start here
main "$flts" "$page" "$perPage" "$ft" "$tt" "$cluster" $v