示例#1
0
def _zabbix_unacked_triggers(zabbix_config):
    """Retrieves the unacked triggers from zabbix.

    :param zabbix_config: zabbix configuration
    :type zabbix_config: dict
    :return: messages to return to matrix
    """
    messages = []
    triggers = zabbix.get_unacked_triggers(zabbix_config)
    color_config = {}
    for key, value in matrix.read_config(config['config'])['colors'].items():
        if key.startswith('zabbix'):
            key = key.replace('zabbix_', '')
            color_config[key] = value

    for trigger in triggers:
        message = ("{prio} {name} {desc}: {value} "
                   "({triggerid})").format(prio=trigger['priority'],
                                           name=trigger['hostname'],
                                           desc=trigger['description'],
                                           value=trigger['prevvalue'],
                                           triggerid=trigger['trigger_id'])
        formatted_message = matrix_alert.colorize(color_config, message)
        messages.append(formatted_message)

    return "<br />".join(messages)
示例#2
0
def _zabbix_config(room_id):
    """Returns the zabbix configuration for a given room_id.

    :param room_id: the Matrix room id
    :type room_id: str
    :return: zabbix config directionary
    """
    zabbix_realm = config[room_id]
    zabbix_config = matrix.read_config(config['config'], zabbix_realm)
    logging.debug('using zabbix realm: %s\nconfig:\n%s', zabbix_realm,
                  zabbix_config)
    return zabbix_config
示例#3
0
def _dnsjedi_chunks_summary(zabbix_config):
    """Returns a summary about the clustermanager chunks.

    :param zabbix_config: the zabbix configuration
    :type zabbix_config: dict
    :return: message to send back
    """
    color_config = {}
    for key, value in matrix.read_config(args['config'], 'Colors').items():
        if key.startswith('dnsjedi'):
            key = key.replace('dnsjedi_', '')
            color_config[key] = value

    logging.debug(color_config)

    lines = []
    clustermanagers = zabbix.get_itemvalues_for_group(
        zabbix_config, 'Clustermanagers', [
            'cms.chunks_left',
            'cms.co_queue_len_forecast',
        ])

    if clustermanagers is not None:
        for name, value in sorted(clustermanagers.items()):
            chunks_left = value[0]
            time_left = _dnsjedi_forecast_format(value[1])
            if chunks_left != '0':
                line = "{0}: {1} chunks left, {2}".format(
                    name, chunks_left, time_left)

            else:
                line = "{0}: done".format(name)

            formatted_line = matrix_alert.colorize(color_config, line)
            lines.append(formatted_line)

    return "<br />".join(lines)
示例#4
0
def send_message(room, message):
    config = matrix.read_config('/etc/matrix.conf')
    config['message'] = message  # .replace('\n', '<br />')
    config['room'] = room
    client, room = matrix.setup(config)
    matrix.send_message(config, room)
        color, emoji, message)
    return formatted_message


if __name__ == '__main__':
    locale.setlocale(locale.LC_CTYPE, 'en_US.UTF-8')
    args = matrix.flags()
    args['message'] = " ".join(args['message'])
    if args['debug'] is True:
        matrix.set_log_level('DEBUG')

    else:
        matrix.set_log_level()

    try:
        config = matrix.merge_config(args, matrix.read_config(args['config']))
        config['matrix'] = matrix.merge_config(args, config['matrix'])

    except FileNotFoundError:
        config = args
        if None in [config['username'], config['password'], config['room']]:
            raise

    color_config = {}
    for key, value in config['colors'].items():
        if key.startswith('zabbix'):
            key = key.replace('zabbix_', '')
            color_config[key] = value

    logging.debug(color_config)
    config['matrix']['message'] = colorize(color_config, config['message'])
示例#6
0
        token=token,
    )

    # Add a !zabbix handler
    zabbix_handler = MRegexHandler("^!zabbix", zabbix_callback)
    bot.add_handler(zabbix_handler)

    # Start polling
    while True:
        thread = bot.start_polling()
        thread.join()
        logging.warning(
            'thread died, waiting five seconds before connecting again...')
        time.sleep(5)


if __name__ == "__main__":
    # TODO: Move out of global scope
    # while accesible to callback functions.
    args = flags()
    if args['debug'] is True:
        set_log_level('DEBUG')

    else:
        set_log_level()

    config = matrix.read_config(args['config'])
    matrix_config = config['matrix']

    main()
示例#7
0
    bot.add_handler(zabbix_handler)

    # Add a !dnsjedi handler
    dnsjedi_handler = MRegexHandler("^!dnsjedi", dnsjedi_callback)
    bot.add_handler(dnsjedi_handler)

    # Start polling
    while True:
        thread = bot.start_polling()
        thread.join()
        logging.warning(
            'thread died, waiting five seconds before connecting again...')
        time.sleep(5)


if __name__ == "__main__":
    args = flags()
    if args['debug'] is True:
        set_log_level('DEBUG')

    else:
        set_log_level()

    zabbix.logging = logging
    matrix.logging = logging
    config = matrix.read_config(args['config'], 'Zabbix-Bot')
    config['config'] = args['config']
    matrix_config = matrix.read_config(args['config'], 'Matrix')
    logging.debug('config:\n%s', config)
    main()