Zabbix com notificações pelo telegram

zabbix_telegram

Sobre

Zabbix é uma ferramenta de monitoramento de ativos computacionais, bastante robusta e expansível. Neste artigo será demonstrado como configurá-lo para enviar alertas através do telegram, que é um aplicativo de mensagens instantâneas disponível para Android, iOS, Windows Phone, OS X, Gnu/Linux e mais.

Ambiente

Este procedimento foi testado com servidor zabbix 2.2.x rodando no debian 7 (Wheezy) ou 8 (Jessie). Se você não estiver utilizando debian, visite a página do telegram-cli para obter instruções de quais bibliotecas instalar.

O commit utilizado foi o 3ced7bb, versão 1.3.1 do telegram-cli.

Dependências

Afim de sanar as dependências da compilação, abra um terminal e, como root, execute o comando:

# apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev libjansson-dev libpython-dev make

Instale o pacote git, caso não o tenha.

Baixar e compilar

# cd /usr/src
# git clone --recursive https://github.com/vysheng/tg.git
# cd tg
# ./configure
# make

A saída será parecida com esta:

root@debian8:/usr/src# git clone --recursive https://github.com/vysheng/tg.git
Cloning into 'tg'...
remote: Counting objects: 3513, done.
remote: Total 3513 (delta 0), reused 0 (delta 0), pack-reused 3513
Receiving objects: 100% (3513/3513), 2.45 MiB | 726.00 KiB/s, done.
Resolving deltas: 100% (2379/2379), done.
Checking connectivity... done.
Submodule 'tgl' (https://github.com/vysheng/tgl.git) registered for path 'tgl'
Cloning into 'tgl'...
remote: Counting objects: 702, done.
remote: Total 702 (delta 0), reused 0 (delta 0), pack-reused 702
Receiving objects: 100% (702/702), 685.69 KiB | 697.00 KiB/s, done.
Resolving deltas: 100% (490/490), done.
Checking connectivity... done.
Submodule path 'tgl': checked out 'b3dcce35110f5c995366318c2886065287815d09'
Submodule 'tl-parser' (https://github.com/vysheng/tl-parser) registered for path 'tl-parser'
Cloning into 'tl-parser'...
remote: Counting objects: 32, done.
remote: Total 32 (delta 0), reused 0 (delta 0), pack-reused 32
Unpacking objects: 100% (32/32), done.
Checking connectivity... done.
Submodule path 'tgl/tl-parser': checked out 'ec8a8ed7a4f22428b83e21a9d3b5815f7a6f3bd9'
root@debian8:/usr/src# cd tg
root@debian8:/usr/src/tg# ./configure 
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for sqrt in -lm... yes
checking for library containing clock_gettime... none required
checking for library containing backtrace... none required
checking for event_base_new in -levent... yes
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking event2/event.h usability... yes
checking event2/event.h presence... yes
checking for event2/event.h... yes
checking for pkg-config... /usr/bin/pkg-config
checking whether compiling and linking against OpenSSL works... yes
checking if zlib is wanted... yes
checking for inflateEnd in -lz... yes
checking zlib.h usability... yes
checking zlib.h presence... yes
checking for zlib.h... yes
checking for rl_save_prompt in -lreadline... yes
checking for libconfig... enabled
checking for config_init in -lconfig... yes
checking for liblua... enabled
checking for lua... /usr/bin/lua
checking if /usr/bin/lua is a Lua interpreter... yes
checking for lua version... 5.2
checking for lua platform... unknown
checking for lua script directory... ${prefix}/share/lua/5.2
checking for lua module directory... ${exec_prefix}/lib/lua/5.2
checking if LUA_VERSION is defined... yes
checking lua.h usability... no
checking lua.h presence... no
checking for lua.h... no
checking lualib.h usability... no
checking lualib.h presence... no
checking for lualib.h... no
checking lauxlib.h usability... no
checking lauxlib.h presence... no
checking for lauxlib.h... no
checking luaconf.h usability... no
checking luaconf.h presence... no
checking for luaconf.h... no
checking for Lua headers in... /usr/include/lua5.2
checking lua.h usability... yes
checking lua.h presence... yes
checking for lua.h... yes
checking lualib.h usability... yes
checking lualib.h presence... yes
checking for lualib.h... yes
checking lauxlib.h usability... yes
checking lauxlib.h presence... yes
checking for lauxlib.h... yes
checking luaconf.h usability... yes
checking luaconf.h presence... yes
checking for luaconf.h... yes
checking for Lua header version... 5.2
checking if Lua header version matches 5.2... yes
checking if LUA_VERSION is defined... yes
checking for library containing exp... none required
checking for library containing dlopen... -ldl
checking for library containing lua_load... -llua5.2
checking for libjansson... enabled
checking for json_array_set_new in -ljansson... yes
checking progname... default
checking execinfo.h usability... yes
checking execinfo.h presence... yes
checking for execinfo.h... yes
checking fcntl.h usability... yes
checking fcntl.h presence... yes
checking for fcntl.h... yes
checking malloc.h usability... yes
checking malloc.h presence... yes
checking for malloc.h... yes
checking netdb.h usability... yes
checking netdb.h presence... yes
checking for netdb.h... yes
checking for stdlib.h... (cached) yes
checking for string.h... (cached) yes
checking for unistd.h... (cached) yes
checking arpa/inet.h usability... yes
checking arpa/inet.h presence... yes
checking for arpa/inet.h... yes
checking mach/mach.h usability... no
checking mach/mach.h presence... no
checking for mach/mach.h... no
checking netinet/in.h usability... yes
checking netinet/in.h presence... yes
checking for netinet/in.h... yes
checking sys/file.h usability... yes
checking sys/file.h presence... yes
checking for sys/file.h... yes
checking sys/socket.h usability... yes
checking sys/socket.h presence... yes
checking for sys/socket.h... yes
checking termios.h usability... yes
checking termios.h presence... yes
checking for termios.h... yes
checking for size_t... yes
checking for uid_t in sys/types.h... yes
checking for inline... inline
checking for stdlib.h... (cached) yes
checking for GNU libc compatible malloc... yes
checking for stdlib.h... (cached) yes
checking for GNU libc compatible realloc... yes
checking for alarm... yes
checking for endpwent... yes
checking for memset... yes
checking for memmove... yes
checking for mkdir... yes
checking for select... yes
checking for socket... yes
checking for strdup... yes
checking for strndup... yes
checking for uname... yes
checking for __builtin_bswap32... yes
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
root@debian8:/usr/src/tg# make
cat tgl/scheme.tl tgl/encrypted_scheme.tl tgl/binlog.tl tgl/append.tl tgl/mtproto.tl > auto/scheme.tl
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl/tl-parser -c -MP -MD -MF dep/tl-parser.d -MQ objs/tl-parser.o -o objs/tl-parser.o tgl/tl-parser/tl-parser.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl/tl-parser -c -MP -MD -MF dep/tlc.d -MQ objs/tlc.o -o objs/tlc.o tgl/tl-parser/tlc.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl/tl-parser -c -MP -MD -MF dep/crc32.d -MQ objs/crc32.o -o objs/crc32.o tgl/tl-parser/crc32.c
gcc objs/tl-parser.o objs/tlc.o objs/crc32.o -L/usr/local/lib -L/usr/lib -L/usr/lib  -rdynamic -ggdb -levent -ljansson -lconfig -lz -levent -lm   -lreadline -llua5.2  -ldl -lssl -lcrypto  -o bin/tl-parser
bin/tl-parser -e auto/scheme.tlo auto/scheme.tl
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl -c -MP -MD -MF dep/generate.d -MQ objs/generate.o -o objs/generate.o tgl/generate.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl -c -MP -MD -MF dep/tools.d -MQ objs/tools.o -o objs/tools.o tgl/tools.c
gcc objs/generate.o objs/tools.o -L/usr/local/lib -L/usr/lib -L/usr/lib  -rdynamic -ggdb -levent -ljansson -lconfig -lz -levent -lm   -lreadline -llua5.2  -ldl -lssl -lcrypto  -o bin/generate
bin/generate -g skip-header auto/scheme.tlo > auto/auto-skip.h || rm auto/auto-skip.h
bin/generate -g fetch-header auto/scheme.tlo > auto/auto-fetch.h || rm auto/auto-fetch.h
bin/generate -g store-header auto/scheme.tlo > auto/auto-store.h || rm auto/auto-store.h
bin/generate -g autocomplete-header auto/scheme.tlo > auto/auto-autocomplete.h || rm auto/auto-autocomplete.h
bin/generate -g types-header auto/scheme.tlo > auto/auto-types.h || rm auto/auto-types.h
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -c -MP -MD -MF dep/main.d -MQ objs/main.o -o objs/main.o main.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -c -MP -MD -MF dep/loop.d -MQ objs/loop.o -o objs/loop.o loop.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -c -MP -MD -MF dep/interface.d -MQ objs/interface.o -o objs/interface.o interface.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -c -MP -MD -MF dep/lua-tg.d -MQ objs/lua-tg.o -o objs/lua-tg.o lua-tg.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -c -MP -MD -MF dep/json-tg.d -MQ objs/json-tg.o -o objs/json-tg.o json-tg.c
bin/tl-parser -E auto/scheme.tl 2> auto/scheme2.tl  || ( cat auto/scheme2.tl && rm auto/scheme2.tl && false )
awk -f ./tgl/gen_constants_h.awk < auto/scheme2.tl > auto/constants.h
bin/generate -g fetch-ds-header auto/scheme.tlo > auto/auto-fetch-ds.h || rm auto/auto-fetch-ds.h
bin/generate -g free-ds-header auto/scheme.tlo > auto/auto-free-ds.h || rm auto/auto-free-ds.h
bin/generate -g store-ds-header auto/scheme.tlo > auto/auto-store-ds.h || rm auto/auto-store-ds.h
bin/generate -g print-ds-header auto/scheme.tlo > auto/auto-print-ds.h || rm auto/auto-print-ds.h
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl -c -MP -MD -MF dep/mtproto-common.d -MQ objs/mtproto-common.o -o objs/mtproto-common.o tgl/mtproto-common.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl -c -MP -MD -MF dep/mtproto-client.d -MQ objs/mtproto-client.o -o objs/mtproto-client.o tgl/mtproto-client.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl -c -MP -MD -MF dep/queries.d -MQ objs/queries.o -o objs/queries.o tgl/queries.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl -c -MP -MD -MF dep/structures.d -MQ objs/structures.o -o objs/structures.o tgl/structures.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl -c -MP -MD -MF dep/binlog.d -MQ objs/binlog.o -o objs/binlog.o tgl/binlog.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl -c -MP -MD -MF dep/tgl.d -MQ objs/tgl.o -o objs/tgl.o tgl/tgl.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl -c -MP -MD -MF dep/updates.d -MQ objs/updates.o -o objs/updates.o tgl/updates.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl -c -MP -MD -MF dep/tgl-net.d -MQ objs/tgl-net.o -o objs/tgl-net.o tgl/tgl-net.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl -c -MP -MD -MF dep/tgl-timers.d -MQ objs/tgl-timers.o -o objs/tgl-timers.o tgl/tgl-timers.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl -c -MP -MD -MF dep/tg-mime-types.d -MQ objs/tg-mime-types.o -o objs/tg-mime-types.o tgl/tg-mime-types.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl -c -MP -MD -MF dep/mtproto-utils.d -MQ objs/mtproto-utils.o -o objs/mtproto-utils.o tgl/mtproto-utils.c
bin/generate -g skip auto/scheme.tlo > auto/auto-skip.c || rm auto/auto-skip.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl -c -MP -MD -MF dep/auto-skip.d -MQ objs/auto-skip.o -o objs/auto/auto-skip.o auto/auto-skip.c
bin/generate -g fetch auto/scheme.tlo > auto/auto-fetch.c || rm auto/auto-fetch.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl -c -MP -MD -MF dep/auto-fetch.d -MQ objs/auto-fetch.o -o objs/auto/auto-fetch.o auto/auto-fetch.c
bin/generate -g store auto/scheme.tlo > auto/auto-store.c || rm auto/auto-store.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl -c -MP -MD -MF dep/auto-store.d -MQ objs/auto-store.o -o objs/auto/auto-store.o auto/auto-store.c
bin/generate -g autocomplete auto/scheme.tlo > auto/auto-autocomplete.c || rm auto/auto-autocomplete.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl -c -MP -MD -MF dep/auto-autocomplete.d -MQ objs/auto-autocomplete.o -o objs/auto/auto-autocomplete.o auto/auto-autocomplete.c
bin/generate -g types auto/scheme.tlo > auto/auto-types.c || rm auto/auto-types.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl -c -MP -MD -MF dep/auto-types.d -MQ objs/auto-types.o -o objs/auto/auto-types.o auto/auto-types.c
bin/generate -g fetch-ds auto/scheme.tlo > auto/auto-fetch-ds.c || rm auto/auto-fetch-ds.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl -c -MP -MD -MF dep/auto-fetch-ds.d -MQ objs/auto-fetch-ds.o -o objs/auto/auto-fetch-ds.o auto/auto-fetch-ds.c
bin/generate -g free-ds auto/scheme.tlo > auto/auto-free-ds.c || rm auto/auto-free-ds.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl -c -MP -MD -MF dep/auto-free-ds.d -MQ objs/auto-free-ds.o -o objs/auto/auto-free-ds.o auto/auto-free-ds.c
bin/generate -g store-ds auto/scheme.tlo > auto/auto-store-ds.c || rm auto/auto-store-ds.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl -c -MP -MD -MF dep/auto-store-ds.d -MQ objs/auto-store-ds.o -o objs/auto/auto-store-ds.o auto/auto-store-ds.c
bin/generate -g print-ds auto/scheme.tlo > auto/auto-print-ds.c || rm auto/auto-print-ds.c
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2  -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -iquote ./tgl -c -MP -MD -MF dep/auto-print-ds.d -MQ objs/auto-print-ds.o -o objs/auto/auto-print-ds.o auto/auto-print-ds.c
rm -f libs/libtgl.a && ar ruv libs/libtgl.a objs/mtproto-common.o objs/mtproto-client.o objs/queries.o objs/structures.o objs/binlog.o objs/tgl.o objs/updates.o objs/tgl-net.o objs/tgl-timers.o objs/tg-mime-types.o objs/mtproto-utils.o objs/tools.o objs/auto/auto-skip.o objs/auto/auto-fetch.o objs/auto/auto-store.o objs/auto/auto-autocomplete.o objs/auto/auto-types.o objs/auto/auto-fetch-ds.o objs/auto/auto-free-ds.o objs/auto/auto-store-ds.o objs/auto/auto-print-ds.o
ar: creating libs/libtgl.a
a - objs/mtproto-common.o
a - objs/mtproto-client.o
a - objs/queries.o
a - objs/structures.o
a - objs/binlog.o
a - objs/tgl.o
a - objs/updates.o
a - objs/tgl-net.o
a - objs/tgl-timers.o
a - objs/tg-mime-types.o
a - objs/mtproto-utils.o
a - objs/tools.o
a - objs/auto/auto-skip.o
a - objs/auto/auto-fetch.o
a - objs/auto/auto-store.o
a - objs/auto/auto-autocomplete.o
a - objs/auto/auto-types.o
a - objs/auto/auto-fetch-ds.o
a - objs/auto/auto-free-ds.o
a - objs/auto/auto-store-ds.o
a - objs/auto/auto-print-ds.o
gcc objs/main.o objs/loop.o objs/interface.o objs/lua-tg.o objs/json-tg.o libs/libtgl.a -L/usr/local/lib -L/usr/lib -L/usr/lib  -rdynamic -ggdb -levent -ljansson -lconfig -lz -levent -lm   -lreadline -llua5.2  -ldl -lssl -lcrypto  -o bin/telegram-cli
root@debian8:/usr/src/tg#

Configurando

Copiando e criando os arquivos necessários.

# mkdir zabbix
# cp bin/telegram-cli zabbix/
# cp tg-server.pub zabbix/
# cd zabbix

Crie o arquivo telegram.config com o seguinte conteúdo:

default_profile = "zabbix";
 
zabbix = {
    config_directory = "/usr/src/tg/zabbix/";
    #test = false;
    #msg_num = true;
    #binlog_enabled = true;
    #log_level = 2;
};

Execute o telegram-cli. Você será redirecionado para o console da ferramenta, indicado por um “>” no início de cada linha.

# ./telegram-cli --rsa-key tg-server.pub --config telegram.config
change_user_group: can't find the user telegramd to switch to
Telegram-cli version 1.3.1, Copyright (C) 2013-2015 Vitaly Valtman
Telegram-cli comes with ABSOLUTELY NO WARRANTY; for details type `show_license'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show_license' for details.
Telegram-cli uses libtgl version 2.0.1
[/usr/src/tg/zabbix//downloads] create
>

Pode-se digitar help para ver todos os comandos disponíveis.

Neste momento é necessário aguardar até que o texto phone number apareça. Quando isso ocorrer, digite o número de telefone que está cadastrado no telegram, no formato +55 (prefixo para o Brasil) seguido do DDD e número. Exemplo:

phone number: +551122223333
code ('call' for phone call): 35620
User Tobias Sette updated username
User Tobias Sette updated photo
User Tobias Sette online (was online [2015/05/20 06:55:30])

Um detalhe interessante: o código também pode ser recebido pelo aplicativo instalado no celular e na versão para desktop, bastando ter um deles instalado e aberto.

Execute os comandos contact_list e dialog_list para que o telegram-cli carregue sua lista de contatos e as conversas atuais.

Caso você queira que o zabbix envie a mensagem para um determinado usuário, execute user_info seguido do nome do contato conforme está na sua agenda. Se, por outro lado, você quiser enviar para um grupo, execute chat_info seguido do nome do grupo. Observe que a tecla TAB pode ser utilizada para autocompletar as palavras.

Exemplos:

> user_info Tobias_Sette
User Tobias Sette updated photo
User Tobias Sette updated name
User Tobias Sette @usuarioTal (#1234567):
        real name: Tobias Sette
        phone: 551122223333
        online (was online [2015/05/03 07:16:54])
> chat_info Empresa_123 
Chat Empresa 123 updated admin
Chat Empresa 123 updated members
Chat Empresa 123 (id 1234567) members:
                Usuario Tal invited by Tobias Sette at [2015/05/14 02:48:45]
                Tobias Sette invited by Tobias Sette at [2015/05/14 02:48:45] admin
User Tobias Sette offline (was online [2015/05/14 02:53:59])

Nota: nos nomes de contatos e grupos, espaços são transformados em underscore (_), cerquilhas (#) em arrobas (@) e contatos com mesmo nome tem um #1 acrescentado ao nome (exemplo: Gnu#1, Gnu#2).

Perceba que na quarta linha de cada exemplo é exibido o código do usuário ou grupo, 1234567 nestes casos. Guarde este número, ele será utilizado mais a frente, na interface do Zabbix.

Agora o telegram-cli está configurado para utilizar a sua conta. Podemos sair do console dele digitando safe_quit.

Integrando ao zabbix

Para enviar uma mensagem, é necessário iniciar o binário telegram-cli, esperar que ele conecte à rede e envie a mensagem. Nos meus testes isso levou cerca de 7-10 segundos. Utilizo há cerca de três meses e funciona bem nos ambientes que tenho, inclusive o envio simultâneo de mensagens.

Para fazer este processo, criaremos um script em /usr/src/tg/zabbix/telegram_standalone.sh com o seguinte conteúdo:

#!/bin/bash
# See
# https://github.com/gnumoksha/zabbix_utils/blob/master/gnu%2Blinux/alertscripts/
 
MAIN_DIRECTORY="/usr/src/tg/zabbix/"
# Is better use: user#user_id. For example: user#141213
USER=$1
SUBJECT=$2
TEXT=$3
 
cd $MAIN_DIRECTORY
if [[ $? -ne 0 ]]; then
        echo "Error to enter in the main directory"
        exit 1
fi
 
./telegram-cli -k tg-server.pub -c telegram.config -WR -U zabbix -e "msg $USER $SUBJECT" || exit 1
 
exit 0

Usando o comando chmod +x telegram_standalone.sh, aplique permissões de execução ao script.

Altere o dono do diretório do telegram-cli e atribua um shell ao usuário zabbix:

# chown -R zabbix: /usr/src/tg/
# usermod --shell /bin/bash zabbix

 

Inclua uma linha com o texto AlertScriptsPath=/usr/src/tg/zabbix/ no arquivo de configuração do servidor zabbix (e.g. /etc/zabbix/zabbix_server.conf). Após isso, reinicie o servidor.

Na interface web do Zabbix, vá em Administração -> Tipos de mídia. Na lateral direita, clique no botão Criar tipo de mídia e preencha conforme a imagem.

tipos_de_midia

Agora vá em Administração -> Usuários, selecione o usuário que receberá as notificações via telegram, clique na aba Mídia e, por fim, clique em adicionar. Preencha de forma parecida com a da imagem.

tipos_de_midia_usuario

Repare que no campo Enviar para é utilizado o código que obtemos anteriormente, precedido do texto user# para usuários ou chat# para grupos.

Está feito. Para testar você pode criar uma trigger do tipo {Zabbix server:agent.ping.last(0)}=1 e marcar a opção Geração de múltiplos eventos de INCIDENTE.

Muito obrigado ao Vitaly Valtman pelo belo software e por ceder parte do seu tempo ao esclarecimento de dúvidas.

Referência

https://github.com/vysheng/tg/blob/master/README.md

Histórico

  • 03/05/2015 versão inicial
  • 11/05/2015 adicionada a dica do @GabRF de como enviar os alertas para um grupo
  • 14/05/2015 adicionado mais detalhes sobre o envio para grupos
  • 20/05/2015 atualizado da versão 1.2.0 (commit) para a versão 1.3.1 (commit) do telegram-cli