Module: check_mk
Branch: master
Commit: 799aeddc8b028020cab9d177a3641e571f8e6c53
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=799aeddc8b0280…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Wed Jun 4 09:51:40 2014 +0200
A few adaptions for the stathist cache
---
livestatus/src/LogCache.cc | 11 ++++++----
livestatus/src/LogEntry.h | 4 ++--
livestatus/src/Logfile.cc | 48 ++++++++++++++++++++++++++++++++++++++++++++
livestatus/src/Logfile.h | 1 +
4 files changed, 58 insertions(+), 6 deletions(-)
diff --git a/livestatus/src/LogCache.cc b/livestatus/src/LogCache.cc
index 900ce25..0d23a8d 100644
--- a/livestatus/src/LogCache.cc
+++ b/livestatus/src/LogCache.cc
@@ -22,13 +22,15 @@
// to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
// Boston, MA 02110-1301 USA.
-#include <time.h>
-#include <sys/types.h>
#include <dirent.h>
-#include <unistd.h>
-#include <stddef.h>
+#include <fcntl.h>
#include <stdarg.h>
+#include <stddef.h>
#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <time.h>
+#include <unistd.h>
#include "nagios.h"
#include "logger.h"
@@ -297,3 +299,4 @@ void LogCache::handleNewMessage(Logfile *logfile, time_t since
__attribute__ ((_
num_cached_log_messages, _max_cached_messages);
}
+
diff --git a/livestatus/src/LogEntry.h b/livestatus/src/LogEntry.h
index 7c45f87..305cced 100644
--- a/livestatus/src/LogEntry.h
+++ b/livestatus/src/LogEntry.h
@@ -87,6 +87,8 @@ struct LogEntry
LogEntry(unsigned lineno, char *line);
~LogEntry();
unsigned updateReferences();
+ static int serviceStateToInt(char *s);
+ static int hostStateToInt(char *s);
private:
bool handleStatusEntry();
@@ -98,8 +100,6 @@ private:
bool handleLogversionEntry();
bool handleInfoEntry();
bool handleTextEntry();
- int serviceStateToInt(char *s);
- int hostStateToInt(char *s);
};
#endif // LogEntry_h
diff --git a/livestatus/src/Logfile.cc b/livestatus/src/Logfile.cc
index 8895845..8a56e77 100644
--- a/livestatus/src/Logfile.cc
+++ b/livestatus/src/Logfile.cc
@@ -267,3 +267,51 @@ void Logfile::updateReferences()
#endif
}
+// Read complete file into newly allocated buffer. Returns a pointer
+// to a malloced buffer, that the caller must free (or 0, in case of
+// an error). The buffer is 2 bytes larger then the file. One byte
+// at the beginning and at the end of the buffer are '\0'.
+char *Logfile::readIntoBuffer(int *size)
+{
+ int fd = open(_path, O_RDONLY);
+ if (fd < 0) {
+ logger(LOG_WARNING, "Cannot open %s for reading: %s", _path,
strerror(errno));
+ return 0;
+ }
+
+ off_t o = lseek(fd, 0, SEEK_END);
+ if (o == -1) {
+ logger(LOG_WARNING, "Cannot seek to end of %s: %s", _path,
strerror(errno));
+ close(fd);
+ return 0;
+ }
+
+ *size = o;
+ lseek(fd, 0, SEEK_SET);
+
+ char *buffer = (char *)malloc(*size + 2); // add space for binary 0 at beginning and
end
+ if (!buffer) {
+ logger(LOG_WARNING, "Cannot malloc buffer for reading %s: %s", _path,
strerror(errno));
+ close(fd);
+ return 0;
+ }
+
+ int r = read(fd, buffer + 1, *size);
+ if (r < 0) {
+ logger(LOG_WARNING, "Cannot read %d bytes from %s: %s", *size, _path,
strerror(errno));
+ free(buffer);
+ close(fd);
+ return 0;
+ }
+ else if (r != *size) {
+ logger(LOG_WARNING, "Read only %d out of %d bytes from %s", r, *size,
_path);
+ free(buffer);
+ close(fd);
+ return 0;
+ }
+ buffer[0] = 0;
+ buffer[*size+1] = 0; // zero-terminate
+
+ close(fd);
+ return buffer;
+}
diff --git a/livestatus/src/Logfile.h b/livestatus/src/Logfile.h
index 931b03a..8c4994d 100644
--- a/livestatus/src/Logfile.h
+++ b/livestatus/src/Logfile.h
@@ -61,6 +61,7 @@ public:
~Logfile();
char *path() { return _path; }
+ char *readIntoBuffer(int *size);
void load(LogCache *LogCache, time_t since, time_t until, unsigned logclasses);
void flush();
time_t since() { return _since; }