Exemple #1
0
    def log_config_key_changed(key_name,
                               old_val_str="(absent)",
                               new_val_str="(absent)"):
        '''
        Log a config key change for key_name from old_val_str to new_val_str.
        If the key was added or deleted, don't specify old_val_str or
        new_val_str.

        Logs an info-level summary, and a full debug log.
        '''
        logging.info("updated config for key '{}' from '{}' to '{}'".format(
            key_name, summarise_string(old_val_str),
            summarise_string(new_val_str)))
        logging.debug(
            "updated config for key '{}' (full values) from '{}' to '{}'".
            format(key_name, old_val_str, new_val_str))
Exemple #2
0
def is_list_valid(field_name,
                  fields,
                  event_desc,
                  is_mandatory=False,
                  min_count=None,
                  max_count=None):
    '''
    Check that fields[field_name] passes is_field_valid(), and has between
    min_count and max_count elements (inclusive). Use None for no count check.
    
    Assumes a zero-length value is a list with no items.

    Don't pass floating-point values for min_count and max_count: they can
    be inaccurate when compared with integer counts.

    Return values are like is_string_valid.
    '''
    if not is_field_valid(
            field_name, fields, event_desc, is_mandatory=is_mandatory):
        return False
    if field_name not in fields:
        # valid optional field, keep on processing
        return True
    field_value = fields[field_name]
    field_value_log = summarise_string(field_value, 20)
    # Assume a zero-length value is a list with no items
    if len(field_value) > 0:
        list_count = field_value.count(',') + 1
    else:
        list_count = 0
    if min_count is not None and list_count < min_count:
        logging.warning(
            "Ignored {} '{}', must have at least {} items {}".format(
                field_name, field_value_log, min_count, event_desc))
        logging.debug(
            "Ignored {} (full list) '{}', must have at least {} items {}".
            format(field_name, field_value, min_count, event_desc))
        # we can't process it
        return False
    if max_count is not None and list_count > max_count:
        logging.warning(
            "Ignored {} '{}', must have at most {} items {}".format(
                field_name, field_value_log, max_count, event_desc))
        logging.debug(
            "Ignored {} (full list) '{}', must have at most {} items {}".
            format(field_name, field_value, max_count, event_desc))
        # we can't process it
        return False
    # it is valid and we want to keep on processing
    return True
Exemple #3
0
def is_string_valid(field_name,
                    fields,
                    event_desc,
                    is_mandatory=False,
                    min_len=None,
                    max_len=None):
    '''
    Check that fields[field_name] passes is_field_valid() and has a length
    between min_len and max_len (inclusive). Use None for no length check.
    Don't pass floating-point values for min_len and max_len: they can be
    inaccurate when compared with integer lengths.

    If any check fails, return False (the event is ignored), and log a
    warning using event_desc.
    Otherwise, return True (the event should be processed).
    '''
    if not is_field_valid(
            field_name, fields, event_desc, is_mandatory=is_mandatory):
        return False
    if field_name not in fields:
        # valid optional field, keep on processing other fields
        return True
    field_value = fields[field_name]
    field_len = len(field_value)
    field_value_log = summarise_string(field_value, 20)
    if min_len is not None and field_len < min_len:
        logging.warning(
            "Ignored {} length {}: '{}', must be at least {} characters {}".
            format(field_name, field_len, field_value_log, min_len,
                   event_desc))
        logging.debug(
            "Ignored {} length {} (full string): '{}', must be at least {} characters {}"
            .format(field_name, field_len, field_value, min_len, event_desc))
        # we can't process it
        return False
    if max_len is not None and field_len > max_len:
        logging.warning(
            "Ignored {} length {}: '{}', must be at most {} characters {}".
            format(field_name, field_len, field_value_log, max_len,
                   event_desc))
        logging.debug(
            "Ignored {} length {} (full string): '{}', must be at most {} characters {}"
            .format(field_name, field_len, field_value, max_len, event_desc))
        # we can't process it
        return False
    # it is valid and we want to keep on processing
    return True
Exemple #4
0
 def _flush_now(self, msg):
     if (self.protocol is not None and self.protocol.transport is not None
         and self.protocol.transport.connected and self.injecting):
         # update event times so the data seems fresh to privcount
         this_time_start, this_time_end = self._get_event_times(msg)
         now = time()
         alive = this_time_end - this_time_start
         msg_adjusted_times = self._set_event_times(msg, now-alive, now)
         event = "650 {}".format(msg_adjusted_times)
         self.output_event_count += 1
         logging.info("sending event {} '{}'"
                      .format(self.output_event_count,
                              summarise_string(event, 100)))
         logging.debug("sending event {} (full event) '{}'"
                       .format(self.output_event_count, event))
         self.protocol.sendLine(event)
     elif self.injecting:
         # No connection: stop sending
         self.stop_injecting()