aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/net/http/simple_health_producer.cpp
blob: 3c1959608e9abba3704222a93a47857764b97b2b (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "simple_health_producer.h"
#include <vespa/defaults.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

namespace {
struct DiskPing {
    std::string path;

    DiskPing()
      : path(vespa::Defaults::underVespaHome("var/run/diskping."))
    {
        int pid = getpid();
        while (pid > 0) {
            char c = '0' + (pid % 10);
            path.append(1, c);
            pid /= 10;
        }
    }
    bool failed() {
        const char *fn = path.c_str();
        ::unlink(fn);
        int fd = ::creat(fn, S_IRWXU);
        if (fd < 0) {
            return true;
        }
        int wr = ::write(fd, "foo\n", 4);
        int cr = ::close(fd);
        ::unlink(fn);
        return (wr != 4 || cr != 0);
    }
};

bool diskFailed() {
    static DiskPing disk;
    return disk.failed();
}

}

namespace vespalib {

SimpleHealthProducer::SimpleHealthProducer()
    : _lock(),
      _health(true, "")
{
    setOk();
}

SimpleHealthProducer::~SimpleHealthProducer() = default;

void
SimpleHealthProducer::setOk()
{
    std::lock_guard guard(_lock);
    _health = Health(true, "All OK");
}

void
SimpleHealthProducer::setFailed(const vespalib::string &msg)
{
    std::lock_guard guard(_lock);
    _health = Health(false, msg);
}

HealthProducer::Health
SimpleHealthProducer::getHealth() const
{
    std::lock_guard guard(_lock);
    if (_health.ok && diskFailed()) {
        return Health(false, "disk ping failed");
    }
    return _health;
}

} // namespace vespalib