.
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # How to integrate Telegram and Zabbix
2 |
3 | ###### Pull requests are more than welcome!
4 |
5 | * [Introduction](#introduction)
6 | * [Zabbix](#zabbix)
7 | * [Telegram](#telegram)
8 | * [Telegram bot](#telegram-bot)
9 | * [Getting started](#getting-started)
10 | * [Dependencies](#dependencies)
11 | * [Installation](#installation)
12 | * [Creating a bot](#creating-a-bot)
13 | * [Configuration](#configuration)
14 | * [Bot configuration](#bot-configuration)
15 | * [Zabbix integration](#zabbix-integration)
16 | * [Python script](#python-script)
17 | * [Actions](#actions)
18 | * [Media type](#media-type)
19 | * [Users](#users)
20 | * [Telegram ID](#telegram-id)
21 | * [UserID](#userid)
22 | * [GroupID](#groupid)
23 | * [ChannelID](#channelid)
24 | * [More info](#more-info)
25 | * [Inspiration](#inspiration)
26 | * [pyTelegramBotAPI](#pytelegrambotapi)
27 | * [Thanks](#thanks)
28 |
29 | ## Introduction
30 |
31 | ### Zabbix
32 |
33 | Zabbix is the ultimate enterprise-level software designed for real-time monitoring of millions of metrics collected from tens of thousands of servers, virtual machines and network devices.
34 |
35 | Zabbix is Open Source and comes at no cost.
36 |
37 | http://zabbix.com
38 |
39 | ### Telegram
40 |
41 | Telegram is a cloud-based mobile and desktop messaging app with a focus on security and speed.
42 |
43 | http://telegram.org
44 |
45 | ### Telegram bot
46 |
47 | Bots are special Telegram accounts designed to handle messages automatically. Users can interact with bots by sending them command messages in private or group chats. You control your bots using HTTPS requests available on the [bot API](https://core.telegram.org/bots/api).
48 |
49 | To everything described here I'll use the following bot token:
50 | `158700146:AAHOPReqqTR8V7FXysa8mJCbQACUWSTBog8`.
51 |
52 | > Please note that this token is only an ___example___. You must generate your own by following the steps on [Creating a bot](#creating-a-bot).
53 |
54 | ## Getting started
55 |
56 | ### Dependencies
57 |
58 | The only thing you must have before following these instructions is a Zabbix Server running.
59 |
60 | ### Installation
61 |
62 | The only installation required is pyTelegramBotAPI. Choose one of the following methods:
63 |
64 | #### Using pip
65 | ```
66 | $ pip install pyTelegramBotAPI
67 | ```
68 | It is also recommended to install security packages. To do this, run:
69 | ```
70 | $ pip install pyopenssl ndg-httpsclient pyasn1
71 | ```
72 |
73 | It may be necessary to update the `requests` package (tested with Ubuntu 14.04.4):
74 | ```
75 | $ pip install -U requests
76 | ```
77 |
78 | #### From source
79 |
80 | ```
81 | $ git clone https://github.com/eternnoir/pyTelegramBotAPI.git
82 | $ cd pyTelegramBotAPI
83 | $ python setup.py install
84 | ```
85 | ### Creating a bot
86 |
87 | To create a bot, talk to [@BotFather](http://telegram.me/BotFather) on Telegram.
88 |
89 | Send ```/newbot``` and it will ask the bot's name. Choose any name.
90 |
91 | Then it will ask the bot's username. The username must end with ```bot``` and is unique.
92 |
93 | When everything is done, the [@BotFather](http://telegram.me/BotFather) will display the bot's _token_. Save it and keep it secret. Here is an example of a token:
94 | `158700146:AAHOPReqqTR8V7FXysa8mJCbQACUWSTBog8`
95 |
96 | In order to receive messages, the recipient user ___MUST___ send a message to the bot at least once.
97 |
98 | If a group will be used, add the bot on the group before testing.
99 |
100 | For channels, add the bot to the channel as an _administrator_.
101 |
102 | ## Configuration
103 |
104 | ### Bot configuration
105 |
106 | No bot configuration is needed, but there are optional settings.
107 |
108 | Commands available on [@BotFather](http://telegram.me/BotFather):
109 |
110 | - `/token` Shows your bot's token.
111 | - `/revoke` Revoke the token and give you a new one.
112 | - `/setname` Change the bot's name.
113 | - `/setdescription` Set the bot's description, a window displayed when a person opens a chat with the bot.
114 | - `/setabouttext` Set the bot's about message, displayed on the bot's profile page.
115 | - `/setuserpic` The bot's photo.
116 | - `/setcommands` The commands that will be listed as bot's options.
117 | - `/setjoingroups` Defines if the bot may or may not join groups.
118 | - `/setprivacy` Set if the bot reads all the messages on a group or only messages that it is cited.
119 | - `/deletebot` Used to destroy the bot.
120 |
121 | ### Zabbix integration
122 |
123 | #### Python script
124 |
125 | Create a file named `telegram_notification.py` on the folder `/usr/src/zabbixbot`:
126 | ```
127 | $ vi telegram_notification.py
128 | ```
129 | Then paste the code:
130 | ```
131 | #!/usr/bin/env python
132 |
133 | import telebot,sys
134 |
135 | BOT_TOKEN='158700146:AAHOPReqqTR8V7FXysa8mJCbQACUWSTBog8'
136 | DESTINATION=sys.argv[1]
137 | SUBJECT=sys.argv[2]
138 | MESSAGE=sys.argv[3]
139 |
140 | MESSAGE = MESSAGE.replace('/n','\n')
141 |
142 | tb = telebot.TeleBot(BOT_TOKEN)
143 | tb.send_message(DESTINATION,SUBJECT + '\n' + MESSAGE, disable_web_page_preview=True, parse_mode='HTML')
144 | ```
145 | Change the file permission and allow it to be executed
146 | ```
147 | # chown -R zabbix: /usr/src/zabbixbot/
148 | # chmod +x telegram_notification.py
149 | ```
150 | #### Zabbix_server.conf
151 | Go to file `/etc/zabbix/zabbix_server.conf`
152 | ```
153 | vi /etc/zabbix/server.conf
154 | ```
155 | find the line `AlertScriptsPath=`. And include:
156 | ```
157 | AlertScriptsPath=/usr/src/zabbixbot/
158 | ```
159 | _The easiest way to find is typing_ `/AlertScriptsPath=`.
160 |
161 | Restart Zabbix-server service.
162 | ```
163 | service zabbix-server restart
164 | ```
165 |
166 | #### Media type
167 |
168 | On the Zabbix interface, go to _Adminstration_, _Media types_, and click on _Create media type_.
169 |
170 | - Name: `telegram_notification.py`
171 | - Type: `Script`
172 | - Script name: `telegram_notification.py`
173 |
174 | ##### If using Zabbix 3.0.1:
175 |
176 | - Script Parameters
177 | - {ALERT.SENDTO}
178 | - {ALERT.SUBJECT}
179 | - {ALERT.MESSAGE}
180 |
181 | #### Actions
182 |
183 | Open Zabbix web interface, go to _Configuration_, _Actions_ and click on _Create Action_.
184 |
185 | - Name: `Telegram notification`
186 | - Subject: `#{HOSTNAME}: {TRIGGER.NAME} {TRIGGER.STATUS}`
187 | - Message:
188 | ```
189 | Value: {ITEM.VALUE} {TRIGGER.STATUS}
190 | Date: {EVENT.DATE} Time: {EVENT.TIME}
191 | ```
192 | The fields _Subject_ and _Message_ are supposed to be customized as your needs. HTML tags supported:
193 | ```
194 | bold, bold
195 | italic, italic
196 | inline URL
197 | inline fixed-width code
198 | pre-formatted fixed-width code block
199 | ```
200 |
201 | Go to tab _Conditions_ and add settings as your needs.
202 |
203 | Go to tab _Actions_ and add settings as your needs.
204 |
205 | ##### If using Zabbix 4.0.0:
206 |
207 | - Action
208 | - Name: Telegram
209 | - Conditions
210 | - Set as your needs
211 | - Operations
212 | - Operations: Send to Users (set the same as in [Users](#users))
213 | - Set message as your needs (Recommended [Actions](#actions))
214 | - Recovery operations
215 | - Recovery Operations
216 | - Set message as your needs (Recommended [Actions](#actions))
217 | - Notify all involved
218 | - Update operations
219 | - Set message as your needs (Recommended [Actions](#actions))
220 | - Notify all involved
221 |
222 |
223 | #### Users
224 |
225 | The last step is to set who will receive the alerts.
226 |
227 | Go to _Administration_, _Users_ and choose who will receive the notifications. Then, go to _Media_ and click on _Add_.
228 |
229 | - Type: `telegram_notification.py`
230 | - Send to: `ID` | Refer to [Telegram ID](telegram-id)
231 |
232 | ## Telegram ID
233 |
234 | Telegram needs `ids` to send messages. The easiest way to get this id is using the bot you have just created.
235 |
236 | Go to `https://api.telegram.org/bot158700146:AAHOPReqqTR8V7FXysa8mJCbQACUWSTBog8/getUpdates` using your browser.
237 |
238 | ### UserID
239 |
240 | If you want to get a user id, send a message from this user to the bot. Reload the page and the user id will be shown.
241 |
242 | Example:
243 | ```
244 | "message":{"message_id":59,"from":{"id":9083329,"first_name":"Gabriel","last_name":"R F","username":"GabrielRF"},"chat":{"id":9083329,"first_name":"Gabriel","last_name":"R F","username":"GabrielRF","type":"private"},"date":1446911853,"text":"\/start"}}]}
245 | ```
246 | In this case, the user id is `9083329`. So, on the step [Users](#users), the field `Send to` would be `9083329`.
247 |
248 | ### GroupID
249 |
250 | If you prefer to have your bot working on a group, then create the group and add the bot to it. Reload the page and you will see a message like:
251 | ```
252 | "message":{"message_id":60,"from":{"id":9083329,"first_name":"Gabriel","last_name":"R F","username":"GabrielRF"},"chat":{"id":-57169325,"title":"q31231","type":"group"},"date":1446912067,"group_chat_created":true}},{"update_id":727527785,
253 | ```
254 | In this case, the group id is `-57169325`. So, on the step [Users](#users), the field `Send to` would be `-57169325`.
255 |
256 | Note that a group id is always ___negative___. Only user's id are positive.
257 |
258 | ### ChannelID
259 |
260 | The last alternative is the easiest one. There's no need to get a channel id. Simply set the `Send to` field on the step [Users](#users) to `@ChannelID`. The `@` symbol is ___obligatory___.
261 |
262 | __If this method doesn't work or if you want to get an id from a private channel, please, visit https://github.com/GabrielRF/telegram-id__
263 |
264 | ## More info
265 |
266 | ### Inspiration
267 |
268 | Most of this method is based on a post from a Brazilian dev named Tobias. [Zabbix com notificações pelo Telegram (Portuguese only)](http://tobias.ws/blog/zabbix-com-notificacoes-pelo-telegram/)
269 |
270 | ### pyTelegramBotAPI
271 |
272 | A simple, but extensible Python implementation for the Telegram Bot API. [eternoir/pyTelegramBotAPI](https://github.com/eternnoir/pyTelegramBotAPI)
273 |
274 | ### Thanks
275 |
276 | Finally, I would like to thank [João Paulo Nascimento](https://github.com/JoaoPNascimento), from Sergipe, who was the first person to test this method, helping me to find bugs.
277 |
--------------------------------------------------------------------------------