Ejemplo n.º 1
0
def alarm_query_builder(begin_time,
                        end_time,
                        msg_text='',
                        utc=False,
                        state='',
                        priority='',
                        priority2=''):
    """Build wincc alarm query string

    >>> alarm_query_builder("2015-08-24 10:07:48", "2015-08-24 10:08:12", '', False, '')
    u"ALARMVIEW:SELECT * FROM ALGVIEWDEU WHERE MsgNr < 12508141 AND DateTime > '2015-08-24 08:07:48' AND DateTime < '2015-08-24 08:08:12'"

    >>> alarm_query_builder("2015-08-24 10:07:48", "2015-08-24 10:08:12", '', True, '')
    u"ALARMVIEW:SELECT * FROM ALGVIEWDEU WHERE MsgNr < 12508141 AND DateTime > '2015-08-24 10:07:48' AND DateTime < '2015-08-24 10:08:12'"

    >>> alarm_query_builder("2015-08-24 10:07:48", '', '', False, '')
    u"ALARMVIEW:SELECT * FROM ALGVIEWDEU WHERE MsgNr < 12508141 AND DateTime > '2015-08-24 08:07:48'"

    >>> alarm_query_builder("2015-08-24 10:07:48", '', 'Trogkettenf', False, '')
    u"ALARMVIEW:SELECT * FROM ALGVIEWDEU WHERE MsgNr < 12508141 AND DateTime > '2015-08-24 08:07:48' AND Text1 LIKE '%Trogkettenf%'"

    >>> alarm_query_builder("2015-08-24 10:07:48", '', '', False, '>2')
    u"ALARMVIEW:SELECT * FROM ALGVIEWDEU WHERE MsgNr < 12508141 AND DateTime > '2015-08-24 08:07:48' AND State >2"
    """

    dt_begin_time = str_to_datetime(begin_time)
    if not utc:
        dt_begin_time = local_time_to_utc(dt_begin_time)
    # MsgNr < 12508141 to filter out operator messages
    datetime_str = datetime_to_str_without_ms(dt_begin_time)
    query = u"ALARMVIEW:SELECT * FROM ALGVIEWDEU WHERE MsgNr < 12508141 AND "
    query += u"DateTime > '{0}'".format(datetime_str)

    if end_time != '':
        dt_end_time = str_to_datetime(end_time)
        if not utc:
            dt_end_time = local_time_to_utc(dt_end_time)
        dt_end_time_str = datetime_to_str_without_ms(dt_end_time)
        query += u" AND DateTime < '{0}'".format(dt_end_time_str)

    # if msg_id != '':
    #    query += " AND MsgNr = {id}".format(id=msg_id)

    if msg_text != '':
        query += u" AND Text1 LIKE '%{text}%'".format(text=msg_text)

    if state != '':
        query += u" AND State {state_condition}".format(state_condition=state)

    if priority != '' and priority2 != '':
        query += u" AND (Typename LIKE '%{prio}%'".format(prio=priority)
        query += u" OR Typename LIKE '%{prio}%')".format(prio=priority2)
    elif priority != '':
        query += u" AND Typename LIKE '%{priority}%'".format(priority=priority)

    return query
Ejemplo n.º 2
0
def tag2(tagid, begin_time, end_time, timestep, mode, utc, show, plot):
    """Parse user friendly tag query input and assemble wincc tag query"""
    if timestep and not end_time:
        end_time = datetime_to_str_without_ms(datetime.now())

    query = tag_query_builder(tagid, begin_time, end_time, timestep, mode, utc)
    if show:
        print(query)
        return

    toc = tic()
    try:
        w = wincc(host_info.address, host_info.database)
        w.connect()
        w.execute(query)

        records = w.create_tag_records()
        print("Fetched data in {time}.".format(time=round(toc(), 3)))
        # print(tags)
        # tags.plot()
        for record in records:
            print(record)
        if plot:
            plot_tag_records(records)

    except Exception as e:
        print(e)
        print(traceback.format_exc())
    finally:
        w.close()
Ejemplo n.º 3
0
def tag2(tagid, begin_time, end_time, timestep, mode, utc, show, plot):
    """Parse user friendly tag query input and assemble wincc tag query"""
    if timestep and not end_time:
        end_time = datetime_to_str_without_ms(datetime.now())

    query = tag_query_builder(tagid, begin_time, end_time, timestep, mode, utc)
    if show:
        print(query)
        return

    toc = tic()
    try:
        w = wincc(host_info.address, host_info.database)
        w.connect()
        w.execute(query)

        records = w.create_tag_records()
        print("Fetched data in {time}.".format(time=round(toc(), 3)))
        # print(tags)
        # tags.plot()
        for record in records:
            print(record)
        if plot:
            plot_tag_records(records)

    except Exception as e:
        print(e)
        print(traceback.format_exc())
    finally:
        w.close()
Ejemplo n.º 4
0
def tag2(tagid, begin_time, end_time, timestep, mode, utc, show, plot, outfile,
         outfile_col_name, outfile_time_zone):
    """Parse user friendly tag query input and assemble wincc tag query"""
    if timestep and not end_time:
        end_time = datetime_to_str_without_ms(datetime.now())

    query = tag_query_builder(tagid, begin_time, end_time, timestep, mode, utc)
    if show:
        print(query)
        return

    toc = tic()
    try:
        w = wincc(host_info.address, host_info.database)
        w.connect()
        w.execute(query)

        records = w.create_tag_records(utc)
        print("Fetched data in {time}.".format(time=round(toc(), 3)))

        if records:
            if (outfile != ''):
                with open(outfile, "w") as f:
                    # print(records.to_csv().encode("UTF-8"))
                    for rec in records:
                        f.write(
                            rec.to_csv(name=outfile_col_name.encode("UTF-8"),
                                       tz=outfile_time_zone))
            else:
                for record in records:
                    print(record)
                if plot:
                    plot_tag_records(records)
        else:
            logging.warning("No data returned.")

    except Exception as e:
        print(e)
        print(traceback.format_exc())
    finally:
        w.close()
Ejemplo n.º 5
0
def alarms(begin_time, end_time, text, utc, show, state, priority, priority2,
           report, report_hostname):
    """Read alarms from given host in given time."""
    query = alarm_query_builder(eval_datetime(begin_time),
                                eval_datetime(end_time),
                                text, utc, state, priority, priority2)

    if show:
        print(query)
        return

    try:
        toc = tic()
        w = wincc(host_info.address, host_info.database)
        w.connect()
        w.execute(query)

        if report:
            alarms = w.create_alarm_record()
            if report_hostname:
                host_description = report_hostname
            else:
                host_description = host_info.description
            if not end_time:
                end_time = datetime_to_str_without_ms(datetime.now())
            generate_alarms_report(alarms, begin_time, end_time,
                                   host_description, text)
            print(unicode(alarms))
        else:
            w.print_alarms()

        print("Fetched data in {time}.".format(time=round(toc(), 3)))
    except WinCCException as e:
        print(e)
        print(traceback.format_exc())
    finally:
        w.close()
Ejemplo n.º 6
0
def alarms(begin_time, end_time, text, utc, show, state, priority, priority2,
           report, report_hostname):
    """Read alarms from given host in given time."""
    query = alarm_query_builder(eval_datetime(begin_time),
                                eval_datetime(end_time), text, utc, state,
                                priority, priority2)

    if show:
        print(query)
        return

    try:
        toc = tic()
        w = wincc(host_info.address, host_info.database)
        w.connect()
        w.execute(query)

        if report:
            alarms = w.create_alarm_record()
            if report_hostname:
                host_description = report_hostname
            else:
                host_description = host_info.description
            if not end_time:
                end_time = datetime_to_str_without_ms(datetime.now())
            generate_alarms_report(alarms, begin_time, end_time,
                                   host_description, text)
            print(unicode(alarms))
        else:
            w.print_alarms()

        print("Fetched data in {time}.".format(time=round(toc(), 3)))
    except WinCCException as e:
        print(e)
        print(traceback.format_exc())
    finally:
        w.close()