-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnutNotifyFct.sh
208 lines (187 loc) · 5.5 KB
/
nutNotifyFct.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
196
197
198
199
200
201
202
203
204
205
206
207
208
# add to log
function addLog {
if [ "$logfile" == "" ] ; then
echo "Can't write to log !" 1>&2
return 1
else
echo "$(date +'%a %d %H:%M:%S') $1" >> $logfile
return $?
fi
}
function writeLog {
addLog "$HOSTNAME : UPS $ups state $argument"
return $?
}
# send mail
# $1 subjectMail (optional) - default $subjectMail
# $2 text/HTML body message - default $textMail
function sendMail {
# replace default mesg
if [ "$1" != "" ] ; then
subjectMail=$1
fi
if [ "$2" != "" ] ; then
textMail=$2
fi
# var verification
if [ "$FROM" == "" ] || [ "$MAILTO" == "" ] || [ "$subjectMail" == "" ] || [ "$textMail" == "" ] ; then
echo "Can't send mail without complete variables" 1>&2
addLog "Can't send mail without complete variables"
return 1
fi
echo "$textMail" | mail -s "$subjectMail" -r "$FROM" "$MAILTO"
return $?
}
# send sms
# $1 text body message - default $textSms
function sendSms {
# replace default mesg
if [ "$1" != "" ] ; then
textSms=$1
fi
# var verification
if [ "$providerSms" == "" ] || [ "$usernameSms" == "" ] || [ "$passwordSms" == "" ] || [ "$fromSms" == "" ] || [ "$toSms" == "" ] || [ "$textSms" == "" ] ; then
echo "Can't send SMS without complete variables" 1>&2
addLog "Can't send mail without complete variables"
return 1
fi
textSms=$(echo $textSms | sed -e 's/ /%20/g' | iconv -t iso-8859-1)
url="https://www.$providerSms/myaccount/sendsms.php?username=$usernameSms&password=$passwordSms&from=$fromSms&to=$toSms&text=$textSms"
tempfile=$(tempfile -p 'nutNotifySms-')
curl -s -o $tempfile $url
result=$(cat $tempfile | grep '<result>' | awk -F "[<>]" '{print $3}')
description=$(cat $tempfile | grep '<description>' | awk -F "[<>]" '{print $3}')
if [ "$result" == 1 ] ; then
addLog "sendSMS : success"
return 0
else
echo "sendSMS : fail - $description" 1>&2
addLog "sendSMS : fail - $description"
return 1
fi
rm $tempfile
}
# send push notification with pushbullet
# see https://www.pushbullet.com
# $1 title
# $2 text body - default $textPushBullet
function sendPushBullet {
#replace default mesg
if [ "$1" != "" ] ; then
subjectPushBullet=$1
fi
if [ "$2" != "" ] ; then
textPushBullet=$2
fi
#var verification
if [ "$pushbulletProviderApi" == "" ] || [ "$pushbulletAccessToken" == "" ] ; then
echo "Can't send push notification without complete variables for PushBullet" 1>&2
addLog "Can't send push notification without complete variables for PushBullet"
return 1
fi
tempfile=$(mktemp --suffix '.nutNotifyPushBullet')
curl -s -o "$tempfile" --header "Access-Token: $pushbulletAccessToken" --header 'Content-Type: application/json' --request POST --data-binary "{\"type\":\"note\",\"title\":\"$HOSTNAME - $subjectPushBullet\",\"body\":\"$textPushBullet\"}" "$pushbulletProviderApi"
returnCurl=$?
if [ $returnCurl -ne 0 ] ; then cat $tempfile ; fi
rm $tempfile
return $?
}
#send push notification with Telegram
# $1 message
# $2 title
# $3 emoji
function sendTelegram {
#replace default mesg
if [ "$2" != "" ] && [ "$3" != "" ] ; then
local textTelegram=$(echo -e "$3 $HOSTNAME $3 $2\n$1")
elif [ "$2" != "" ] ; then
local textTelegram=$(echo -e "$HOSTNAME - $2 \n$1")
else
local textTelegram="$3$HOSTNAME : $1"
fi
#var verification
if [ "$telegramProviderApi" == "" ] || [ "$telegramAccessToken" == "" ] ; then
echo "Can't send notification without complete variables for Telegram" 1>&2
addLog "Can't send notification without complete variables for Telegram"
return 1
fi
tempfile=$(mktemp --suffix '.telegram-notification')
curl -s -o "$tempfile" --data "chat_id=${telegramChatID}" --data "text=${textTelegram}" "${telegramProviderApi}/bot${telegramAccessToken}/sendMessage"
returnCurl=$?
if [ $returnCurl -ne 0 ] ; then cat $tempfile ; fi
rm $tempfile
return $returnCurl
}
function sendPushover {
#replace default mesg
if [ "$1" != "" ] ; then
subjectPushover=$1
fi
if [ "$2" != "" ] ; then
textPushover=$2
fi
#var verification
if [ "$pushoverProviderApi" == "" ] || [ "$pushoverAppToken" == "" ] || [ "$pushoverUserkey" == "" ] ; then
echo "Can't send push notification without complete variables for Pushover" 1>&2
addLog "Can't send push notification without complete variables for Pushover"
return 1
fi
tempfile=$(mktemp --suffix '.nutNotifyPushOver')
curl -s \
--form-string "token=$pushoverAppToken" \
--form-string "user=$pushoverUserkey" \
--form-string "title=$subjectPushover" \
--form-string "message=$textPushover" \
$pushoverProviderApi
returnCurl=$?
if [ $returnCurl -ne 0 ] ; then cat $tempfile ; fi
rm $tempfile
return $returnCurl
}
function conditionalNotification {
local event=$1
local text=$2
local emoji=$3
case "$event" in
ONLINE)
method=${methodOnline[*]}
;;
ONBATT)
method=${methodOnbatt[*]}
;;
LOWBATT)
method=${methodLowbatt[*]}
;;
FSD)
method=${methodFsd[*]}
;;
SHUTDOWN)
method=${methodShutdown[*]}
;;
COMMOK|COMMBAD|REPLBATT|NOCOMM)
method=${methodComm[*]}
;;
SERVERONLINE)
method=${methodServerOnline[*]}
;;
*)
echo "Error : this event is not managed" 1>&2
;;
esac
#echo "${method[*]}"
if [[ " ${method[*]} " =~ " mail " ]] ; then
sendMail "$subjectMail" "$text"
fi
if [[ " ${method[*]} " =~ " pushbullet " ]] ; then
sendPushBullet "$pushbulletSubject" "$text"
fi
if [[ " ${method[*]} " =~ " pushover " ]] ; then
sendPushover "$pushoverSubject" "$text"
fi
if [[ " ${method[*]} " =~ " telegram " ]] ; then
sendTelegram "$text" "$telegramSubject" "$emoji"
fi
if [[ " ${method[*]} " =~ " sms " ]] ; then
sendSms "$text"
fi
}