aboutsummaryrefslogtreecommitdiffstats
path: root/configd/src/apps/cmd/main.cpp
blob: 9ee7130a06eccc30077f1b62452fab9bd26e3673 (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
190
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include <vespa/vespalib/util/signalhandler.h>
#include <vespa/vespalib/util/exception.h>
#include <vespa/fnet/frt/supervisor.h>
#include <vespa/fnet/frt/target.h>
#include <vespa/fnet/frt/rpcrequest.h>

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

namespace {
struct Method {
    const char * name;
    const char * rpcMethod;
    bool noArgNeeded;
    bool needsTimeoutArg;
};
const Method methods[] = {
    { "list", "sentinel.ls", true, false },
    { "restart", "sentinel.service.restart", false, false },
    { "start", "sentinel.service.start", false, false },
    { "stop", "sentinel.service.stop", false, false },
    { "connectivity", "sentinel.report.connectivity", true, true }
};

}


class Cmd
{
private:
    std::unique_ptr<fnet::frt::StandaloneFRT> _server;
    FRT_Target *_target;

public:
    Cmd() : _server(), _target(nullptr) {}
    ~Cmd();
    int run(const Method &cmd, const char *arg);
    void initRPC(const char *spec);
    void finiRPC();
};

Cmd::~Cmd()
{
    LOG_ASSERT(! _server);
    LOG_ASSERT(_target == nullptr);
}

void usage()
{
    fprintf(stderr, "usage: vespa-sentinel-cmd <cmd> [arg]\n");
    fprintf(stderr, "with cmd one of:\n");
    fprintf(stderr, "  list\n");
    fprintf(stderr, "  restart {service}\n");
    fprintf(stderr, "  start {service}\n");
    fprintf(stderr, "  stop {service}\n");
    fprintf(stderr, "  connectivity [milliseconds]\n");
}

void
Cmd::initRPC(const char *spec)
{
    _server = std::make_unique<fnet::frt::StandaloneFRT>();
    _target     = _server->supervisor().GetTarget(spec);
}


void
Cmd::finiRPC()
{
    if (_target != nullptr) {
        _target->internal_subref();
        _target = nullptr;
    }
    _server.reset();
}


int
Cmd::run(const Method &cmd, const char *arg)
{
    int retval = 0;
    try {
        initRPC("tcp/localhost:19097");
    } catch (vespalib::Exception &e) {
        fprintf(stderr, "vespa-sentinel-cmd: exception in network initialization: %s\n",
                e.what());
        return 2;
    }
    FRT_RPCRequest *req = _server->supervisor().AllocRPCRequest();
    req->SetMethodName(cmd.rpcMethod);

    int pingTimeoutMs = 5000;
    if (cmd.needsTimeoutArg) {
        if (arg) {
            pingTimeoutMs = atoi(arg);
        }
        req->GetParams()->AddInt32(pingTimeoutMs);
    } else if (arg) {
        // one param
        req->GetParams()->AddString(arg);
    }
    _target->InvokeSync(req, 2 * pingTimeoutMs * 0.001);

    if (req->IsError()) {
        fprintf(stderr, "vespa-sentinel-cmd '%s' error %d: %s\n",
                cmd.name, req->GetErrorCode(), req->GetErrorMessage());
        retval = 1;
    } else {
        FRT_Values &answer = *(req->GetReturn());
        const char *atypes = answer.GetTypeString();
        fprintf(stderr, "vespa-sentinel-cmd '%s' OK.\n", cmd.name);
        if (atypes && (strcmp(atypes, "SS") == 0)) {
            uint32_t numHosts = answer[0]._string_array._len;
            uint32_t numStats = answer[1]._string_array._len;
            FRT_StringValue *hosts = answer[0]._string_array._pt;
            FRT_StringValue *stats = answer[1]._string_array._pt;
            uint32_t ml = 0;
            uint32_t j;
            for (j = 0; j < numHosts; ++j) {
                uint32_t hl = strlen(hosts[j]._str);
                if (hl > ml) ml = hl;
            }
            for (j = 0; j < numHosts && j < numStats; ++j) {
                printf("%-*s -> %s\n", ml, hosts[j]._str, stats[j]._str);
            }
            for (; j < numHosts; ++j) {
                printf("Extra host: %s\n", hosts[j]._str);
            }
            for (; j < numStats; ++j) {
                printf("Extra stat: %s\n", stats[j]._str);
            }
        } else {
            uint32_t idx = 0;
            while (atypes != nullptr && *atypes != '\0') {
                switch (*atypes) {
                case 's':
                    printf("%s\n", answer[idx]._string._str);
                    break;
                default:
                    printf("BAD: unknown type %c\n", *atypes);
                }
                ++atypes;
            ++idx;
            }
        }
    }
    req->internal_subref();
    finiRPC();
    return retval;
}

const Method *
parseCmd(const char *arg)
{
    for (const auto & method : methods) {
        if (strcmp(arg, method.name) == 0) {
            return &method;
        }
    }
    return nullptr;
}

void hookSignals() {
    using SIG = vespalib::SignalHandler;
    SIG::PIPE.ignore();
}

int main(int argc, char** argv)
{
    int retval = 1;
    const Method *cmd = nullptr;
    if (argc > 1) {
        cmd = parseCmd(argv[1]);
    }
    const char *extraArg = (argc > 2 ? argv[2] : nullptr);
    if (cmd && (extraArg || cmd->noArgNeeded)) {
        hookSignals();
        Cmd runner;
        retval = runner.run(*cmd, extraArg);
    } else {
        usage();
    }
    return retval;
}