-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfreenas-alerta.sh
executable file
·112 lines (83 loc) · 2.12 KB
/
freenas-alerta.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
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
source ./.secrets
#__constants__#
ENVIRONMENT="FreeNAS"
ORIGIN="FreeNAS"
SERVICE="FreeNAS Alert"
DEBUG=false # set to true for debug messages. anything else means no debug messages
# modify below truenas/alerta severity/level mappings
declare -A LEVEL
LEVEL[INFO]=informational
LEVEL[WARNING]=warning
LEVEL[NOTICE]=minor
LEVEL[ERROR]=major
LEVEL[CRITICAL]=critical
LEVEL[ALERT]=major
LEVEL[EMERGENCY]=critical
#__helper functions__#
function debugecho {
if [ "$DEBUG" == "true" ]
then
echo "$*"
fi
}
# get the number of alerts
num=$( midclt call alert.list | jq length )
for (( i=0; i<=$num-1; i++ ))
do
debugecho Record# ${i}
alerts=$(midclt call alert.list | jq ".[$i]")
debugecho "${alerts}"
uuid="$(jq -r '.uuid' <<< ${alerts})"
source="$(jq -r '.klass' <<< ${alerts})"
node="$(jq -r '.node' <<< ${alerts})"
level=${LEVEL[$(jq -r '.level' <<< ${alerts})]}
formatted="$(jq -r '.formatted' <<< ${alerts} | jq -sR)"
dismissed="$(jq -r '.dismissed' <<< ${alerts})"
if [[ ${dismissed} = "true" ]]; then
status="closed"
level="cleared"
else
echo dis = "${dismissed}"
fi
# note that this is a literal template. i.e $ENVIRONMENT is not a bash variable
# it's a jq template value name
JSON_TEMPLATE='{
environment: $ENVIRONMENT,
event: $source,
origin: $ORIGIN,
resource: $node,
service: [
$SERVICE
],
severity: $level,
status: $status,
value: $dismissed,
text: $formatted,
type: "exceptionAlert"}'
JSON=$( jq -n \
--arg ENVIRONMENT "$ENVIRONMENT" \
--arg source "$source" \
--arg ORIGIN "$ORIGIN" \
--arg node "$node" \
--arg SERVICE "$SERVICE" \
--arg level "$level" \
--arg status "$status" \
--arg dismissed "$dismissed" \
--arg formatted "$formatted" \
"${JSON_TEMPLATE}")
echo ${JSON}
debugecho ${JSON} |jq -r .
debugecho calling alerta
curl -XPOST ${ALERTA_ENDPOINT} \
-H 'Authorization: Key '${ALERTA_KEY}'' \
-H 'Content-type: application/json' \
-d "${JSON}"
debugecho dismissed on freenas: ${dismissed}
#echo enter to continue ...
#read input
done
# TODO