def test_incorrect_characters_disallowed(localtime, gethostname):
    """Check only valid characters are used in the HEADER"""
    # Only allowed characters are ABNF VCHAR values and space.
    # Basically, if ord() returns between 32 and 126 inclusive
    # it's okay.
    bad_char = "\x1f"  # printable, ord() returns 31
    header = netsyslog.HeaderPart()
    header.timestamp = header.timestamp[:-1] + bad_char
    assert str(header) == " ".join((DEFAULT_TIMESTAMP, DEFAULT_HOSTNAME))
    def test_send_packets_by_hand(self):
        """Check we can send a hand crafted log packet"""
        hostname = "localhost"
        address = (hostname, netsyslog.Logger.PORT)
        packet_text = "<165>Jan  1 10:00:00 myhost myprog: hello"
        self.mock_sock.expects(once()).sendto(eq(packet_text), eq(address))

        pri = netsyslog.PriPart(syslog.LOG_LOCAL4, syslog.LOG_NOTICE)
        header = netsyslog.HeaderPart("Jan  1 10:00:00", "myhost")
        msg = netsyslog.MsgPart("myprog", "hello")
        packet = netsyslog.Packet(pri, header, msg)
        logger = netsyslog.Logger()
        logger.add_host(hostname)
        logger.send_packet(packet)
        self.mock_sock.verify()
def test_send_packets_by_hand(sendto):
    """Check we can send a hand crafted log packet"""
    hostname = "localhost"
    address = (hostname, netsyslog.Logger.PORT)
    packet_text = "<165>Jan  1 10:00:00 myhost myprog: hello"

    pri = netsyslog.PriPart(syslog.LOG_LOCAL4, syslog.LOG_NOTICE)
    header = netsyslog.HeaderPart("Jan  1 10:00:00", "myhost")
    msg = netsyslog.MsgPart("myprog", "hello")
    packet = netsyslog.Packet(pri, header, msg)
    logger = netsyslog.Logger()
    logger.add_host(hostname)
    logger.send_packet(packet)
    sendto.assert_called_once_with(bytes(packet_text, encoding='ascii'),
                                   address)
Beispiel #4
0
def StartLogging():

    global messages, args

    smoothEPS = args.eps
    maxTime = 1

    # We need to smooth out the EPS, if we chuck all the logs as fast as we
    # can, we end up filling up the buffers and packets get dropped. So we
    # limit the output
    if args.eps > 100:
        smoothEPS = args.eps / 100
        maxTime = 0.01

    logger = netsyslog.Logger()

    for server in args.server:
        if ':' in server:
            host, port = server.split(":")

            if host:
                print "Adding host: " + host
                logger.add_host(host)
                if port:
                    print "Adding port: " + port
                    logger.PORT = int(port)
        else:
            logger.add_host(server)

    for server in _SERVERS:
        host = server
        port = 514

        if ":" in server:
            host, port = server.split(":")

        if host:
            print "Adding host: " + host
            logger.add_host(host)
            if port:
                print "Adding port: " + str(port)
                logger.PORT = int(port)
        else:
            logger.add_host(server)

    packetsSent = 0
    eps_time_start = time.time()

    facility = SyslogFacility(args.facility)
    priority = SyslogPriority(args.priority)

    while 1:
        smooth_time_start = time.time()

        messages_sent = 0
        for x in range(0, smoothEPS):
            messages_sent +=1
            message = messages[random.randrange(0, len(messages))]
            pri = netsyslog.PriPart(facility, priority)
            header = netsyslog.HeaderPart(message["Date"], message["Host"])
            msg = netsyslog.MsgPart(tag="", content=message["Msg"])
            packet = netsyslog.Packet(pri, header, msg)
            logger.send_packet(packet)

        time_taken = time.time() - smooth_time_start
        if time_taken < maxTime:
            time.sleep(maxTime - time_taken)

        time_taken = time.time() - eps_time_start
        if time_taken > 1:
            if _EPS > 100:
                print "Current EPS=" + str(messages_sent * 100)
            else:
                print "Current EPS=" + str(messages_sent)

            eps_time_start = time.time()

        packetsSent += messages_sent
        if args.once and packetsSent > args.eps:
            break
 def test_set_hostname_manually(self):
     """Check it is possible to set the hostname in HEADER manually"""
     hostname = "otherhost"
     header = netsyslog.HeaderPart(hostname=hostname)
     self.assertEqual(str(header), "%s %s" % (DEFAULT_TIMESTAMP, hostname))
 def test_set_timestamp_manually(self):
     """Check it is possible to set the timestamp in HEADER manually"""
     timestamp = "Jan 31 18:12:34"
     header = netsyslog.HeaderPart(timestamp=timestamp)
     self.assertEqual(str(header), "%s %s" % (timestamp, DEFAULT_HOSTNAME))
 def test_automatic_timestamp(self):
     """Check HEADER is automatically calculated if not set"""
     header = netsyslog.HeaderPart()
     self.assertEqual(str(header),
                      " ".join((DEFAULT_TIMESTAMP, DEFAULT_HOSTNAME)))
        msg = netsyslog.MsgPart("program", content="hello")
        self.assertEqual(str(msg), "program: hello")

    def test_space_only_added_if_necessary(self):
        """Check space only added to CONTENT if necessary"""
        msg = netsyslog.MsgPart("program", content=" hello")
        self.assertEqual(str(msg), "program hello")

    def test_include_pid(self):
        """Check the program's pid can be included in CONTENT"""
        msg = netsyslog.MsgPart("program", "hello", pid=MOCK_PID)
        self.assertEqual(str(msg), "program[%d]: hello" % (MOCK_PID))


DEFAULT_PRI = netsyslog.PriPart(syslog.LOG_LOCAL4, syslog.LOG_NOTICE)
DEFAULT_HEADER = netsyslog.HeaderPart(DEFAULT_TIMESTAMP, DEFAULT_HOSTNAME)
DEFAULT_MSG = netsyslog.MsgPart(DEFAULT_TAG, "hello")


class PacketTest(unittest.TestCase):

    def test_message_format(self):
        """Check syslog message is correctly constructed"""
        packet = netsyslog.Packet(DEFAULT_PRI, DEFAULT_HEADER, DEFAULT_MSG)
        header = " ".join((DEFAULT_TIMESTAMP, DEFAULT_HOSTNAME))
        start_of_packet = "<165>%s %s" % (header, DEFAULT_TAG)
        self.assert_(str(packet).startswith(start_of_packet))

    def test_max_length(self):
        """Check that no syslog packet is longer than 1024 bytes"""
        message = "a" * 2048
def test_set_hostname_manually(localtime, gethostname):
    """Check it is possible to set the hostname in HEADER manually"""
    hostname = "otherhost"
    header = netsyslog.HeaderPart(hostname=hostname)
    assert str(header) == "%s %s" % (DEFAULT_TIMESTAMP, hostname)
def test_set_timestamp_manually(localtime, gethostname):
    """Check it is possible to set the timestamp in HEADER manually"""
    timestamp = "Jan 31 18:12:34"
    header = netsyslog.HeaderPart(timestamp=timestamp)
    assert str(header) == "%s %s" % (timestamp, DEFAULT_HOSTNAME)
def test_automatic_timestamp(localtime, gethostname):
    """Check HEADER is automatically calculated if not set"""
    header = netsyslog.HeaderPart()
    assert str(header) == " ".join((DEFAULT_TIMESTAMP, DEFAULT_HOSTNAME))