Improve syslog parsing; Implement basic main loop.
This commit is contained in:
+1
-3
@@ -32,9 +32,7 @@ const std::string Facility::readFromStream(std::istream& src) {
|
|||||||
if (c == '.') {
|
if (c == '.') {
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
if (c != ' ' && c != '\t') {
|
ret.push_back(c);
|
||||||
ret.push_back(c);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
|
|||||||
+3
-8
@@ -28,16 +28,11 @@ SOFTWARE.
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <boost/noncopyable.hpp>
|
#include <boost/noncopyable.hpp>
|
||||||
|
|
||||||
class Reader : boost::noncopyable {
|
class SyslogMessage;
|
||||||
|
|
||||||
|
class Reader : private boost::noncopyable {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
virtual ~Reader() {
|
|
||||||
};
|
|
||||||
|
|
||||||
virtual std::shared_ptr<SyslogMessage> nextMessage() = 0;
|
virtual std::shared_ptr<SyslogMessage> nextMessage() = 0;
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* READER_H */
|
#endif /* READER_H */
|
||||||
|
|||||||
+2
-2
@@ -30,10 +30,10 @@ SOFTWARE.
|
|||||||
class Severity {
|
class Severity {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
Severity(const char* src) : Severity(std::string(src)) {
|
Severity(const std::string& src) : _value(readFromString(src)) {
|
||||||
};
|
};
|
||||||
|
|
||||||
Severity(const std::string& src) : _value(readFromString(src)) {
|
Severity(const char* src) : Severity(std::string(src)) {
|
||||||
};
|
};
|
||||||
|
|
||||||
Severity(std::istream& source) : Severity(readFromStream(source)) {
|
Severity(std::istream& source) : Severity(readFromStream(source)) {
|
||||||
|
|||||||
@@ -23,8 +23,11 @@ SOFTWARE.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "SyslogBulkUploader.h"
|
#include "SyslogBulkUploader.h"
|
||||||
|
#include "Reader.h"
|
||||||
|
#include "Writer.h"
|
||||||
|
|
||||||
SyslogBulkUploader::SyslogBulkUploader() {
|
void SyslogBulkUploader::run() {
|
||||||
|
while (auto msg = _reader.nextMessage()) {
|
||||||
|
_writer.sendMessage(msg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -27,11 +27,19 @@ SOFTWARE.
|
|||||||
|
|
||||||
#include <boost/noncopyable.hpp>
|
#include <boost/noncopyable.hpp>
|
||||||
|
|
||||||
|
class Reader;
|
||||||
|
class Writer;
|
||||||
|
|
||||||
class SyslogBulkUploader : boost::noncopyable {
|
class SyslogBulkUploader : boost::noncopyable {
|
||||||
public:
|
public:
|
||||||
SyslogBulkUploader();
|
|
||||||
private:
|
|
||||||
|
|
||||||
|
SyslogBulkUploader(Reader& reader, Writer& writer) : _reader(reader), _writer(writer) {
|
||||||
|
};
|
||||||
|
void run();
|
||||||
|
|
||||||
|
private:
|
||||||
|
Reader& _reader;
|
||||||
|
Writer& _writer;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* SYSLOGBULKUPLOADER_H */
|
#endif /* SYSLOGBULKUPLOADER_H */
|
||||||
|
|||||||
@@ -25,14 +25,13 @@ SOFTWARE.
|
|||||||
#include "SyslogMessage.h"
|
#include "SyslogMessage.h"
|
||||||
|
|
||||||
void skipWhitespace(std::istream& src) {
|
void skipWhitespace(std::istream& src) {
|
||||||
bool whitespace = true;
|
while (src) {
|
||||||
while (src && whitespace) {
|
|
||||||
std::istream::char_type c = src.peek();
|
std::istream::char_type c = src.peek();
|
||||||
if (c == ' ' || c == '\t') {
|
if (c == ' ' || c == '\t') {
|
||||||
src.get();
|
src.get();
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
whitespace = false;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -55,6 +54,7 @@ const boost::posix_time::ptime SyslogMessage::readTimestamp(std::istream& src) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const Facility SyslogMessage::readFacility(std::istream& src) {
|
const Facility SyslogMessage::readFacility(std::istream& src) {
|
||||||
|
skipWhitespace(src);
|
||||||
return Facility(src);
|
return Facility(src);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -45,22 +45,18 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
const Facility facility() const {
|
const Facility facility() const {
|
||||||
|
|
||||||
return _facility;
|
return _facility;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string message() const {
|
const std::string message() const {
|
||||||
|
|
||||||
return _message;
|
return _message;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Severity severity() const {
|
const Severity severity() const {
|
||||||
|
|
||||||
return _severity;
|
return _severity;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string source() const {
|
const std::string source() const {
|
||||||
|
|
||||||
return _source;
|
return _source;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2015 Marko Živanović
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef WRITER_H
|
||||||
|
#define WRITER_H
|
||||||
|
|
||||||
|
#include "SyslogMessage.h"
|
||||||
|
|
||||||
|
class Writer : private boost::noncopyable {
|
||||||
|
public:
|
||||||
|
virtual void sendMessage(std::shared_ptr<SyslogMessage>) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* WRITER_H */
|
||||||
|
|
||||||
@@ -9,6 +9,7 @@ add_executable(SyslogBulkUploaderTests SyslogBulkUploaderTests.cpp)
|
|||||||
target_link_libraries(SyslogBulkUploaderTests
|
target_link_libraries(SyslogBulkUploaderTests
|
||||||
slbu-lib
|
slbu-lib
|
||||||
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
|
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
|
||||||
|
${Boost_DATE_TIME_LIBRARY}
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(SyslogMessageTests SyslogMessageTests.cpp)
|
add_executable(SyslogMessageTests SyslogMessageTests.cpp)
|
||||||
|
|||||||
@@ -23,9 +23,48 @@ SOFTWARE.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "../src/SyslogBulkUploader.h"
|
#include "../src/SyslogBulkUploader.h"
|
||||||
|
#include "../src/Reader.h"
|
||||||
|
#include "../src/Writer.h"
|
||||||
#define BOOST_TEST_MODULE SyslogBulkUploaderTests
|
#define BOOST_TEST_MODULE SyslogBulkUploaderTests
|
||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(fakeTest) {
|
class MockReader : public Reader {
|
||||||
BOOST_CHECK(true);
|
public:
|
||||||
|
|
||||||
|
virtual std::shared_ptr<SyslogMessage> nextMessage() {
|
||||||
|
if (_pos >= _messages.size()) {
|
||||||
|
return std::shared_ptr<SyslogMessage>();
|
||||||
|
} else {
|
||||||
|
std::stringstream ss(_messages[_pos++]);
|
||||||
|
return std::shared_ptr<SyslogMessage>(new SyslogMessage(dynamic_cast<std::istream&> (ss)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
std::vector<std::string> _messages = {
|
||||||
|
"2015-09-02 13:33:11 Local4.Critical 192.168.0.1 Kiwi_Syslog_Server %ASA-2-106007: Deny inbound UDP from 1.2.3.4/22084 to 4.3.2.1/53 due to DNS Query",
|
||||||
|
"2015-09-02 13:33:11 Local4.Critical 192.168.0.1 Kiwi_Syslog_Server %ASA-2-106007: Deny inbound UDP from 1.2.3.4/22084 to 4.3.2.1/53 due to DNS Query",
|
||||||
|
"2015-09-02 13:33:11 Local4.Critical 192.168.0.1 Kiwi_Syslog_Server %ASA-2-106007: Deny inbound UDP from 1.2.3.4/22084 to 4.3.2.1/53 due to DNS Query"
|
||||||
|
};
|
||||||
|
size_t _pos;
|
||||||
|
};
|
||||||
|
|
||||||
|
class MockWriter : public Writer {
|
||||||
|
public:
|
||||||
|
|
||||||
|
virtual void sendMessage(std::shared_ptr<SyslogMessage> msg) {
|
||||||
|
_messages.push_back(msg);
|
||||||
|
};
|
||||||
|
std::vector<std::shared_ptr<SyslogMessage>> _messages;
|
||||||
|
};
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(test_run) {
|
||||||
|
MockReader r;
|
||||||
|
MockWriter w;
|
||||||
|
SyslogBulkUploader ul(r, w);
|
||||||
|
ul.run();
|
||||||
|
BOOST_CHECK_EQUAL(w._messages.size(), 3);
|
||||||
|
for (size_t i = 0; i < w._messages.size(); i++) {
|
||||||
|
std::cout << *(w._messages[i].get()) << std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,15 +27,15 @@ SOFTWARE.
|
|||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(constructor_and_getters) {
|
BOOST_AUTO_TEST_CASE(parsing) {
|
||||||
std::stringstream source("2015-09-02 13:33:11 Local4.Critical 192.168.0.1 Kiwi_Syslog_Server %ASA-2-106007: Deny inbound UDP from 138.28.4.18/22084 to 9.28.3.28/53 due to DNS Query");
|
std::stringstream source("2015-09-02 13:33:11 Local4.Critical 192.168.0.1 Kiwi_Syslog_Server %ASA-2-106007: Deny inbound UDP from 1.2.3.4/22084 to 4.3.2.1/53 due to DNS Query");
|
||||||
boost::posix_time::ptime timestamp(boost::posix_time::time_from_string("2015-09-02 13:33:11"));
|
boost::posix_time::ptime timestamp(boost::posix_time::time_from_string("2015-09-02 13:33:11"));
|
||||||
SyslogMessage m(source);
|
SyslogMessage m(source);
|
||||||
BOOST_CHECK_EQUAL(m.timestamp(), timestamp);
|
BOOST_CHECK_EQUAL(m.timestamp(), timestamp);
|
||||||
BOOST_CHECK_EQUAL(m.facility(), Facility("Local4"));
|
BOOST_CHECK_EQUAL(m.facility(), Facility("Local4"));
|
||||||
BOOST_CHECK_EQUAL(m.severity(), Severity("Critical"));
|
BOOST_CHECK_EQUAL(m.severity(), Severity("Critical"));
|
||||||
BOOST_CHECK_EQUAL(m.source(), "192.168.0.1");
|
BOOST_CHECK_EQUAL(m.source(), "192.168.0.1");
|
||||||
BOOST_CHECK_EQUAL(m.message(), "Kiwi_Syslog_Server %ASA-2-106007: Deny inbound UDP from 138.28.4.18/22084 to 9.28.3.28/53 due to DNS Query");
|
BOOST_CHECK_EQUAL(m.message(), "Kiwi_Syslog_Server %ASA-2-106007: Deny inbound UDP from 1.2.3.4/22084 to 4.3.2.1/53 due to DNS Query");
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(invalid_params) {
|
BOOST_AUTO_TEST_CASE(invalid_params) {
|
||||||
|
|||||||
Reference in New Issue
Block a user