вторник, 16 января 2018 г.

Zabbix telegram bot. Return last 10 active trigers and monitored hosts CPU utilization


This bot allow you easy monitor status of your servers in zabbix, and control their load.


There step by step instruction to setup:

1)Create own bot. It will send you messages. Write /Start to @BotFather in telegram 

2)Get your telegram id. Write /Start to @MyTelegramID_bot 

3)Install telethon library, using
#apt-get install python3-pip python-pip
#pip install telethon
#pip install pyzabbix
 
4)copy code below and set your variables. Lines with variables marked yellow.
 
5)Bot can ansewer for two commands:
/last_issue
/monitored hosts

Write me back for help in telegram @r0mk_h0ze 




# -*- codin g: utf-8 -*-
import sys  

reload(sys)  
sys.setdefaultencoding('utf8')
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater,  CallbackQueryHandler
#token like 'xxxxxxxxx:XXXXXX-XXXXXXXXXXXXXXXXXX'
updater = Updater(token='')
dispatcher = updater.dispatcher
import logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
import time
from datetime import date
from pyzabbix import ZabbixAPI
from telegram.ext import MessageHandler, Filters
from telegram.ext import CommandHandler
z = ZabbixAPI('http://127.0.0.1/zabbix', user='Admin', password='password')

def last_issue(bot, update):
    z = ZabbixAPI('http://127.0.0.1/zabbix', user='Admin', password='password')
    hosts = z.trigger.get(only_true=1,
        skipDependent=1,
        monitored=1,
        active=1,
        filter={'value':1},
        output='extend',
        expandDescription=1,
        selectHosts=['host'],
        limit=10,
        sortfield = 'lastchange',
        sortorder = 'DESC')

    reply = 'Last 10 unsolved issues:\n\n' 
    for host in hosts:
        name = (host['hosts'])
        reply=reply + ''.join(host['description'] + "\n\n")
    bot.sendMessage(chat_id=update.message.chat_id, text=reply)


def active_hosts(bot, update):
    z = ZabbixAPI('http://127.0.0.1/zabbix', user='Admin', password='password')
    hosts = z.host.get(
        filter={'value':1},
        output='extend',
        monitored_hosts=1,
        expandDescription=1,
        limit=100,
        sortorder = 'DESC')

    reply = 'Monitored:\n\n'
    for host in hosts:
        name = (host['host'])
        items = z.item.get(
        filter={'value':1},
        output='extend',
        hostids=host['hostid'],
        expandDescription=1,
        search={'key_': 'system.cpu.util[,idle]'},
        limit=100,
        sortorder = 'DESC')
        cpu_load = str(round(float(100 - float(items[0]['lastvalue'])),2))
        if host['available'] == str(1):
            reply=reply + 'CPU load: ' + ''.join(cpu_load) + '  ' +''.join(host['name']) + ' status UP ' + "\n\n"
        else:
            reply=reply + ''.join(host['name']) + ' status not available' + "\n\n"
    bot.sendMessage(chat_id=update.message.chat_id, text=reply)

def start(bot, update):
    bot.sendMessage(chat_id=update.message.chat_id, text="/last_issue - last 10 acitive issues\n /active_hosts - Monitored hots")

def help(bot, update):
    update.message.reply_text("/last_issue - last 10 acitive issues\n /active_hosts - Monitored hots")


last_issue_handler = CommandHandler('last_issue', last_issue)
dispatcher.add_handler(last_issue_handler)

active_hosts_handler = CommandHandler('active_hosts', active_hosts)
dispatcher.add_handler(active_hosts_handler)


start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)

help_handler = CommandHandler('help', help)
dispatcher.add_handler(help_handler)

updater.start_polling()

def unknown(bot, update):
    bot.sendMessage(chat_id=update.message.chat_id, text="Sorry, I didn't understand that command. Use /help")

unknown_handler = MessageHandler(Filters.command, unknown)
dispatcher.add_handler(unknown_handler)

1 комментарий:

  1. Welcome to Casino Queen
    Welcome to Casino クイーンカジノ Queen. bk8 We welcome you to a world of online casino games and a world of planet win 365 mobile casino games. The best casino, the best Jan 22, 2022Duel Casino Queen

    ОтветитьУдалить