Skip to content

Commit dab7a7e

Browse files
committed
WIP: add QH command to access the query handler via livestatus
this PR adds access to the query handler via livestatus. Since livestatus is commonly used it would be nice to subscribe to events via livestatus too. So we simply pass through query handler commands and therefor allow things like: echo "QH help" | nc -U tmp/run/live or echo "QH @nerd subscribe servicechecks" | nc -U tmp/run/live TODO: - It is not yet interactive, so you send a command once and will get the results. - implement proper shutdown Signed-off-by: Sven Nierlein <sven@nierlein.de>
1 parent 10f8797 commit dab7a7e

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

src/Store.cc

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void Store::registerDowntime(nebstruct_downtime_data *d)
113113
_table_downtimes.addDowntime(d);
114114
}
115115

116-
bool Store::answerRequest(InputBuffer *input, OutputBuffer *output)
116+
bool Store::answerRequest(InputBuffer *input, OutputBuffer *output, int fd)
117117
{
118118
output->reset();
119119
int r = input->readRequest();
@@ -135,6 +135,10 @@ bool Store::answerRequest(InputBuffer *input, OutputBuffer *output)
135135
answerCommandRequest(lstrip((char *)line + 8), output);
136136
output->setDoKeepalive(true);
137137
}
138+
else if (!strncmp(line, "QH ", 3)) {
139+
output->setDoKeepalive(true);
140+
answerQueryHandlerRequest(lstrip((char *)line + 3), output, fd);
141+
}
138142
else if (!strncmp(line, "LOGROTATE", 9)) {
139143
logger(LG_INFO, "Forcing logfile rotation");
140144
rotate_log_file(time(0));
@@ -171,6 +175,33 @@ void Store::answerCommandRequest(const char *command, OutputBuffer *output)
171175
return;
172176
}
173177

178+
void Store::answerQueryHandlerRequest(const char *command, OutputBuffer *output, int fd)
179+
{
180+
int ret, sd;
181+
char buf[4096];
182+
sd = nsock_unix(qh_socket_path, NSOCK_TCP | NSOCK_CONNECT);
183+
if (sd < 0) {
184+
logger(LG_INFO, "Failed to connect to query socket '%s': %s: %s", qh_socket_path, nsock_strerror(sd), strerror(errno));
185+
return;
186+
}
187+
ret = nsock_printf_nul(sd, "%s", command);
188+
if (ret < 0) {
189+
logger(LG_INFO, "failed to submit command by query handler");
190+
}
191+
output->reset();
192+
while(TRUE) {
193+
ret = read(sd, buf, 4095);
194+
if(ret <= 0)
195+
break;
196+
buf[ret] = 0;
197+
dprintf(fd, "%s", buf);
198+
}
199+
close(sd);
200+
return;
201+
}
202+
203+
204+
174205

175206
void Store::answerGetRequest(InputBuffer *input, OutputBuffer *output, const char *tablename)
176207
{

src/Store.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,13 @@ class Store
6969
void registerHostgroup(hostgroup *);
7070
void registerComment(nebstruct_comment_data *);
7171
void registerDowntime(nebstruct_downtime_data *);
72-
bool answerRequest(InputBuffer *, OutputBuffer *);
72+
bool answerRequest(InputBuffer *, OutputBuffer *, int);
7373

7474
private:
7575
Table *findTable(string name);
7676
void answerGetRequest(InputBuffer *, OutputBuffer *, const char *);
7777
void answerCommandRequest(const char *, OutputBuffer *);
78+
void answerQueryHandlerRequest(const char *, OutputBuffer *, int);
7879
};
7980

8081
#endif // Store_h

src/module.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ void *client_thread(void *data)
192192
while (keepalive && !g_should_terminate) {
193193
if (g_debug_level >= 2 && requestnr > 1)
194194
logger(LG_INFO, "Handling request %d on same connection", requestnr);
195-
keepalive = store_answer_request(input_buffer, output_buffer);
195+
keepalive = store_answer_request(input_buffer, output_buffer, cc);
196196
flush_output_buffer(output_buffer, cc);
197197
g_counters[COUNTER_REQUESTS]++;
198198
requestnr ++;

src/store.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ void store_register_downtime(nebstruct_downtime_data *d)
7575
g_store->registerDowntime(d);
7676
}
7777

78-
int store_answer_request(void *ib, void *ob)
78+
int store_answer_request(void *ib, void *ob, int fd)
7979
{
80-
return g_store->answerRequest((InputBuffer *)ib, (OutputBuffer *)ob);
80+
return g_store->answerRequest((InputBuffer *)ib, (OutputBuffer *)ob, fd);
8181
}
8282

8383
void *create_outputbuffer(int *termination_flag)

src/store.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ extern "C"
3636
void store_deinit();
3737
void store_register_comment(nebstruct_comment_data *);
3838
void store_register_downtime(nebstruct_downtime_data *);
39-
int store_answer_request(void *input_buffer, void *output_buffer);
39+
int store_answer_request(void *input_buffer, void *output_buffer, int fd);
4040
void *create_outputbuffer(int *termination_flag);
4141
void flush_output_buffer(void *ob, int fd);
4242
void delete_outputbuffer(void *);

0 commit comments

Comments
 (0)