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 #3
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_priority_format(self):
     """Check PRI is correctly formatted"""
     pri = netsyslog.PriPart(syslog.LOG_LOCAL4, syslog.LOG_NOTICE)
     self.assertEqual(str(pri), "<165>")
        """Check single space inserted infront of CONTENT if necessary"""
        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"""
def test_priority_format():
    """Check PRI is correctly formatted"""
    pri = netsyslog.PriPart(syslog.LOG_LOCAL4, syslog.LOG_NOTICE)
    assert str(pri) == "<165>"