summaryrefslogtreecommitdiffstats
path: root/logd/src/logd/legacy_forwarder.cpp
blob: b512bab7fb6d7ed324b0c6e46e278b1b91989539 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "exceptions.h"
#include "legacy_forwarder.h"
#include "metrics.h"
#include <vespa/vespalib/component/vtag.h>
#include <vespa/vespalib/locale/c.h>
#include <unistd.h>

#include <vespa/log/log.h>
LOG_SETUP("");

using LogLevel = ns_log::Logger::LogLevel;

namespace logdemon {

LegacyForwarder::LegacyForwarder(Metrics &metrics)
    : _logserverfd(-1),
      _metrics(metrics),
      _forwardMap(),
      _levelparser(),
      _badLines(0)
{}
LegacyForwarder::~LegacyForwarder() = default;

void
LegacyForwarder::forwardText(const char *text, int len)
{
    int wsize = write(_logserverfd, text, len);

    if (wsize != len) {
        if (wsize > 0) {
            LOG(warning, "only wrote %d of %d bytes to logserver", wsize, len);
        } else {
            LOG(warning, "problem sending data to logserver: %s", strerror(errno));
        }

        throw ConnectionException("problem sending data");
    }
}

void
LegacyForwarder::sendMode()
{
    char buf[1024];
    snprintf(buf, 1024, "mode logd %s\n", vespalib::VersionTag);
    int len = strlen(buf);
    if (len < 100) {
        forwardText(buf, len);
    } else {
        LOG(warning, "too long mode line: %s", buf);
    }
}

void
LegacyForwarder::forwardLine(const char *line, const char *eol)
{
    int linelen = eol - line;

    assert(_logserverfd >= 0);
    assert (linelen > 0);
    assert (linelen < 1024*1024);
    assert (line[linelen - 1] == '\n');

    if (parseline(line, eol)) {
        forwardText(line, linelen);
    }
}

bool
LegacyForwarder::parseline(const char *linestart, const char *lineend)
{
    int llength = lineend - linestart;

    const char *fieldstart = linestart;
    // time
    const char *tab = strchr(fieldstart, '\t');
    if (tab == nullptr || tab == fieldstart) {
        LOG(spam, "bad logline no 1. tab: %.*s", llength, linestart);
        ++_badLines;
        return false;
    }
    char *eod;
    double logtime = vespalib::locale::c::strtod(fieldstart, &eod);
    if (eod != tab) {
        int fflen = tab - linestart;
        LOG(spam, "bad logline first field not strtod parsable: %.*s", fflen, linestart);
        ++_badLines;
        return false;
    }
    time_t now = time(nullptr);
    if (logtime - 864000 > now) {
        int fflen = tab - linestart;
        LOG(warning, "bad logline, time %.*s > 10 days in the future", fflen, linestart);
        ++_badLines;
        return false;
    }
    if (logtime + 8640000 < now) {
        int fflen = tab - linestart;
        LOG(warning, "bad logline, time %.*s > 100 days in the past", fflen, linestart);
        ++_badLines;
        return false;
    }

    // hostname
    fieldstart = tab + 1;
    tab = strchr(fieldstart, '\t');
    if (tab == nullptr) {
        LOG(spam, "bad logline no 2. tab: %.*s", llength, linestart);
        ++_badLines;
        return false;
    }

    // pid
    fieldstart = tab + 1;
    tab = strchr(fieldstart, '\t');
    if (tab == nullptr || tab == fieldstart) {
        LOG(spam, "bad logline no 3. tab: %.*s", llength, linestart);
        return false;
    }

    // service
    fieldstart = tab + 1;
    tab = strchr(fieldstart, '\t');
    if (tab == nullptr) {
        LOG(spam, "bad logline no 4. tab: %.*s", llength, linestart);
        ++_badLines;
        return false;
    }
    if (tab == fieldstart) {
        LOG(spam, "empty service in logline: %.*s", llength, linestart);
    }
    std::string service(fieldstart, tab-fieldstart);

    // component
    fieldstart = tab + 1;
    tab = strchr(fieldstart, '\t');
    if (tab == nullptr || tab == fieldstart) {
        LOG(spam, "bad logline no 5. tab: %.*s", llength, linestart);
        ++_badLines;
        return false;
    }
    std::string component(fieldstart, tab-fieldstart);

    // level
    fieldstart = tab + 1;
    tab = strchr(fieldstart, '\t');
    if (tab == nullptr || tab == fieldstart) {
        LOG(spam, "bad logline no 6. tab: %.*s", llength, linestart);
        ++_badLines;
        return false;
    }
    std::string level(fieldstart, tab-fieldstart);
    LogLevel l = _levelparser.parseLevel(level.c_str());

    // rest is freeform message, must be on this line:
    if (tab > lineend) {
        LOG(spam, "bad logline last tab after end: %.*s", llength, linestart);
        ++_badLines;
        return false;
    }

    _metrics.countLine(level, service);

    // Check overrides
    ForwardMap::iterator found = _forwardMap.find(l);
    if (found != _forwardMap.end()) {
        return found->second;
    }
    return false; // Unknown log level
}

LogLevel
LevelParser::parseLevel(const char *level)
{
    using ns_log::Logger;

    LogLevel l = Logger::parseLevel(level);
    if (l >= 0 && l <= Logger::NUM_LOGLEVELS) {
        return l;
    }
    if (_seenLevelMap.find(level) == _seenLevelMap.end()) {
        LOG(warning, "unknown level '%s'", level);
        _seenLevelMap.insert(level);
    }
    return Logger::fatal;
}

} // namespace