-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path_send_telegram.sh
executable file
·172 lines (142 loc) · 7 KB
/
_send_telegram.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
#!/usr/bin/env bash
#==============================================
# Send message jobs to telegram bot
# Author: Wanderlei Hüttel
# Email: wanderlei.huttel@gmail.com
# Version: 1.6 - 18/08/2022
#==============================================
# Debug Messages
debug=false # false-Disable debug messages / true-Enable debug messages
#==============================================
# Executable config
bconsole=$(which bconsole)
curl=$(which curl)
bc=$(which bc)
# Telegram config
# Use @BotFather to create a bot an get the API token
# Send a message to the Bot @userinfobot to get your chat_id
# Or send a message to a bot created and
# Open in browser the url https://api.telegram.org/bot${api_token}/getUpdates
# and get the value of id of user where the message line is.
api_token="change_with_your_api_key"
chat_id="change_with_your_chat_id"
log="/var/log/bacula/telegram.log"
#==============================================
# Function to convert bytes for human readable
human_bytes(){
extension="Bytes, KB, MB, GB, TB, PB, EB, ZB, YB"
index=1
val=$( $bc <<< "scale=2; $1 / 1")
vint=$( bc <<< "$val / 1024" )
while [[ $vint != 0 ]]; do
(( index++ ))
val=$( $bc <<< "scale=2; $val / 1024")
vint=$( $bc <<< "$val / 1024" )
done
echo "$val $( cut -f $index -d, <<< $extension )"
}
# end function
# Function do debug script
message_debug(){
if [[ $debug == true ]]; then
echo -ne "$1\n"
fi
}
# end function
message_debug "Debug: human_bytes '$(human_bytes 1024)'"
message_debug "Debug: bconsole - '$bconsole'"
message_debug "Debug: curl - '$curl'"
message_debug "Debug: bc - '$bc'"
message_debug "Debug: api_token - '$api_token'"
message_debug "Debug: chat_id - '$chat_id'"
#==============================================
# SQL query to get data from Job (MySQL)
query_mysql="select Job.Name, Job.JobId,(select Client.Name from Client where Client.ClientId = Job.ClientId) as Client, Job.JobBytes, Job.JobFiles, case when Job.Level = 'F' then 'Full' when Job.Level = 'I' then 'Incremental' when Job.Level = 'D' then 'Differential' end as Level, (select Pool.Name from Pool where Pool.PoolId = Job.PoolId) as Pool, (select Storage.Name from JobMedia left join Media on (Media.MediaId = JobMedia.MediaId) left join Storage on (Media.StorageId = Storage.StorageId) where JobMedia.JobId = Job.JobId limit 1 ) as Storage, date_format( Job.StartTime , '%d/%m/%Y %H:%i:%s' ) as StartTime, date_format( Job.EndTime , '%d/%m/%Y %H:%i:%s' ) as EndTime, sec_to_time(TIMESTAMPDIFF(SECOND,Job.StartTime,Job.EndTime)) as Duration, Job.JobStatus, (select Status.JobStatusLong from Status where Job.JobStatus = Status.JobStatus) as JobStatusLong from Job where Job.JobId=$1;"
message_debug "Debug: query_mysql - ['$query_mysql']\n"
#==============================================
# SQL query to get data from Job (PostgreSQL)
query_pgsql="select Job.Name, Job.JobId,(select Client.Name from Client where Client.ClientId = Job.ClientId) as Client, Job.JobBytes, Job.JobFiles, case when Job.Level = 'F' then 'Full' when Job.Level = 'I' then 'Incremental' when Job.Level = 'D' then 'Differential' end as Level, (select Pool.Name from Pool where Pool.PoolId = Job.PoolId) as Pool, (select Storage.Name from JobMedia left join Media on (Media.MediaId = JobMedia.MediaId) left join Storage on (Media.StorageId = Storage.StorageId) where JobMedia.JobId = Job.JobId limit 1 ) as Storage, to_char(Job.StartTime, 'DD/MM/YY HH24:MI:SS') as StartTime, to_char(Job.EndTime, 'DD/MM/YY HH24:MI:SS') as EndTime, to_char(endtime-starttime,'HH24:MI:SS') as Duration, Job.JobStatus, (select Status.JobStatusLong from Status where Job.JobStatus = Status.JobStatus) as JobStatusLong from Job where Job.JobId=$1;"
message_debug "Debug: query_pgsql - ['$query_pgsql']\n"
#==============================================
# Check database driver (PostgreSQL or MySQL)
check_database=$(echo "show catalog" | $bconsole | grep -oi "pgsql\|postgres\|postgresql" | wc -l)
message_debug "Debug: check_database - '$check_database'"
if [[ $check_database -eq 1 ]]; then
sql_query=$query_pgsql
message_debug "Debug: database type - 'PostgreSQL'"
else
sql_query=$query_mysql
message_debug "Debug: database type - 'MySQL'"
fi
#==============================================
# Execute command in bconsole
str=$( $bconsole <<EOF | grep "[\|]" | grep -vi "JobId" | sed 's/[[:space:]]*|[[:space:]]*/|/g' | sed -E 's/^\||\|$//g'
gui on
sqlquery
$sql_query
#DONT_DELETE_THIS_LINE#
exit
EOF
)
message_debug "Debug: str - ['$str']\n"
#==============================================
# Execute SQL and get data from bconsole
JobName=$(cut -d"|" -f1 <<< $str)
JobId=$(cut -d"|" -f2 <<< $str)
Client=$(cut -d"|" -f3 <<< $str)
JobBytes=$(human_bytes $(cut -d"|" -f4 <<< $str | sed 's/,//g'))
JobFiles=$(cut -d"|" -f5 <<< $str)
Level=$(cut -d"|" -f6 <<< $str)
Pool=$(cut -d"|" -f7 <<< $str)
Storage=$(cut -d"|" -f8 <<< $str)
StartTime=$(cut -d"|" -f9 <<< $str)
EndTime=$(cut -d"|" -f10 <<< $str)
Duration=$(cut -d"|" -f11 <<< $str)
JobStatus=$(cut -d"|" -f12 <<< $str)
Status=$(cut -d"|" -f13 <<< $str)
#==============================================
# Emojis
# OK
# http://emojipedia.org/white-heavy-check-mark/
# Not OK
# http://emojipedia.org/cross-mark/
# Floppy Disk
# http://emojipedia.org/floppy-disk/
# Different header in case of error
if [[ "${JobStatus}" == "T" ]] ; then
header=">>>>> 💾 BACULA BACKUP ✅ <<<<</n" # OK
else
header=">>>>> 💾 BACULA BACKUP ❌ <<<<</n" # Error
fi
message_debug "Debug: header - '$header'"
#==============================================
# Format output of message
message="$header/nJobName=$JobName/nJobid=$JobId/nClient=$Client/nJobBytes=$JobBytes/nJobFiles=$JobFiles/nLevel=$Level/nPool=$Pool/nStorage=$Storage/nStartTime=$StartTime/nEndTime=$EndTime/nDuration=$Duration/nJobStatus=$JobStatus/nStatus=$Status"
messagelog="Message: JobName=$JobName | Jobid=$JobId | Client=$Client | JobBytes=$JobBytes | Level=$Level | Status=$Status"
message=$(echo $message | sed 's|/n|%0A|g; s| |%20|g')
url="https://api.telegram.org/bot${api_token}/sendMessage?chat_id=${chat_id}&text=${message}"
message_debug "Debug: message - '$message'"
message_debug "Debug: messagelog - '$messagelog'"
message_debug "Debug: url - '$url'"
#==============================================
# Try to send message during 10 minutes
# in case of error
count=1
while [[ $count -le 20 ]]; do
echo "$(date +%d/%m/%Y\ %H:%M:%S) - Start message send (attempt $count) ..." >> $log
echo "$(date +%d/%m/%Y\ %H:%M:%S) - $messagelog" >> $log
$curl -s "$url" > /dev/null
ret=$?
message_debug "Debug: ret - '$ret'"
if [[ $ret -eq 0 ]]; then
echo "$(date +%d/%m/%Y\ %H:%M:%S) - Attempt $count executed successfully!" >> $log
message_debug "Debug: count - '$count' - Done"
exit 0
else
echo "$(date +%d/%m/%Y\ %H:%M:%S) - Attempt $count failed!" >> $log
echo "$(date +%d/%m/%Y\ %H:%M:%S) - Waiting 30 seconds before retry ..." >> $log
message_debug "Debug: count - '$count'"
sleep 30
(( count++ ))
fi
done