向电报发送ICinga2警报

icinga2telegram的Python项目详细描述


ICinga2图

icinga2telegram是一个小的python脚本,用于将Icinga2警报发送到 电报。它还将emojis添加到警报中,以突出显示警报的类型和重要性。 (您可以使用--no-emojis禁用它。)

Screenshot of icinga2telegram

为什么是python而不是简单的bash脚本?

其他一些人通过编写一个简单的bash实现了icinga2电报通知 脚本。大多数时候这是可行的,但有一些事情我不喜欢 bash解决方案:

  1. 他们使用两个内容几乎相同的脚本。
  2. 当警报输出包含特殊字符时,bash可能会搞砸。
  3. bash解决方案基于环境变量。这与本机不兼容 ICinga2控制器,因为它只支持命令定义中的参数。

显然,python实现比bash解决方案占用的空间更大。它 需要一个python解释器并安装几个python包作为依赖项。这 对于一个资源非常有限但大多数ICinga2的嵌入式系统来说可能是个问题 实例运行在功能强大的机器上,而ICinga2Telegram的足迹在那里并不重要。 作为回报,您将获得一个可维护且健壮的脚本,可用于主机和 服务通知。

安装和设置

安装icinga2telegram

建议(但不是必需)在python virtualenv中安装icinga2telegram。

pip install icinga2telegram

运行icinga2telegram --help查看所有可用选项。

创建一个电报机器人

你需要通过和电报之父交谈来创建一个电报机器人。请跟随 以及official instructions 复制你的机器人代币。你将需要它的ICinga2配置。

获取聊天ID

现在你需要和你的新机器人谈谈,因为机器人不能开始新的聊天。打开一个 与你的机器人聊天并发送消息。之后你必须获取 你和你的机器人之间的对话。用实际的令牌替换令牌。

curl -s 'https://api.telegram.org/botTOKEN/getUpdates'

如果您已经安装了jq,那么使用:

curl -s 'https://api.telegram.org/botTOKEN/getUpdates' | jq '.result[].message.chat'
{
  "id": CHAT-ID,
  "first_name": "Max",
  "username": "exampleuser",
  "type": "private"
}

复制您的chat-id。您将需要它来进行icinga2配置。 如果要添加更多用户,每个人都必须向您的机器人发送消息,并且您需要 提取如上所示的聊天ID。 (你也可以邀请你的机器人加入群组,但是你必须写一条类似@yourbot foo的信息。) 发送消息(使用聊天室ID)给您的机器人。

ICinga2示例配置

这只是一种使用ICinga2Telegram发送通知的方法。你当然可以用另一个 将用户、聊天ID和实际通知粘合在一起的方法。

以chat-id作为变量的用户定义

object User "max" {
    display_name = "Max"
    enable_notifications = true
    vars.telegram_chat_id = "CHAT-ID"
}

主机

的通知定义
apply Notification "Max (Host)" to Host {
    command = "icinga2telegram Host"
    assign where host.address
    users = [ "max" ]
}

服务的通知定义

apply Notification "Max (Service)" to Service {
    command = "icinga2telegram Service"
    assign where host.address
    users = [ "max" ]
}

主机的命令定义

object NotificationCommand "icinga2telegram Host" {
    import "plugin-notification-command"
    command = [ "/opt/icinga2telegram/bin/icinga2telegram" ]
    timeout = 1m
    arguments += {
        "--address" = "$address$"
        "--chat" = "$user.vars.telegram_chat_id$"
        "--hostdisplayname" = "$host.display_name$"
        "--hostname" = "$host.name$"
        "--hostoutput" = "$host.output$"
        "--hoststate" = "$host.state$"
        "--icingaweb2url" = "https://monitoring.example.com/icingaweb2"
        "--notification-author" = "$notification.author$"
        "--notification-comment" = "$notification.comment$"
        "--notification-type" = "$notification.type$"
        "--time" = "$icinga.timet$"
        "--token" = "TOKEN"
    }
}

服务的命令定义

object NotificationCommand "icinga2telegram Service" {
    import "plugin-notification-command"
    command = [ "/opt/icinga2telegram/bin/icinga2telegram" ]
    timeout = 1m
    arguments += {
        "--address" = "$address$"
        "--chat" = "$user.vars.telegram_chat_id$"
        "--hostdisplayname" = "$host.display_name$"
        "--hostname" = "$host.name$"
        "--hostoutput" = "$host.output$"
        "--hoststate" = "$host.state$"
        "--icingaweb2url" = "https://monitoring.example.com/icingaweb2"
        "--notification-author" = "$notification.author$"
        "--notification-comment" = "$notification.comment$"
        "--notification-type" = "$notification.type$"
        "--servicedisplayname" = "$service.display_name$"
        "--servicename" = "$service.name$"
        "--serviceoutput" = "$service.output$"
        "--servicestate" = "$service.state$"
        "--time" = "$icinga.timet$"
        "--token" = "TOKEN"
    }
}

许可证

Send your Icinga2 alerts to Telegram
Copyright (C) 2018  Max Rosin

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Grid loadmask(true)在gxt中不起作用   java将字符串索引转换为整数   为什么Java泛型适用于基元数组,但不适用于基元数组?   java如何让Azure应用程序服务Tomcat将所有80/443流量转发到运行在同一应用程序服务上的JBoss   具有多个值的java转换映射到树?   java如何设置SQL server连接URL?   java设置了多个相互独立的JFrame   安全性在JAVA中如何使用RSAPrivateKey的密码短语?   java不能使用比Apache Velocity中更大的条件   如何在Java中打印字符串的所有排列   停止Android Studio自动导入java。sql。日期   对象简单计算器程序java   java如何在安卓中使用“&”符号作为字符串?   java Connect,为我的安卓应用程序从REST API发布和获取数据