1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3 /* Fluent Bit
4 * ==========
5 * Copyright (C) 2019-2020 The Fluent Bit Authors
6 * Copyright (C) 2015-2018 Treasure Data Inc.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21 #include <fluent-bit/flb_info.h>
22 #include <fluent-bit/flb_mem.h>
23 #include <fluent-bit/flb_log.h>
24 #include <fluent-bit/flb_utils.h>
25 #include <fluent-bit/flb_http_server.h>
26
27 #include <monkey/mk_lib.h>
28 #include "api/v1/register.h"
29 #include "api/v1/health.h"
30
31 static void cb_root(mk_request_t *request, void *data)
32 {
33 struct flb_hs *hs = data;
34
35 mk_http_status(request, 200);
36 mk_http_send(request, hs->ep_root_buf, hs->ep_root_size, NULL);
37 mk_http_done(request);
38 }
39
40 /* Ingest pipeline metrics into the web service context */
41 int flb_hs_push_pipeline_metrics(struct flb_hs *hs, void *data, size_t size)
42 {
43 mk_mq_send(hs->ctx, hs->qid_health, data, size);
44 return mk_mq_send(hs->ctx, hs->qid_metrics, data, size);
45 }
46
47 /* Ingest storage metrics into the web service context */
48 int flb_hs_push_storage_metrics(struct flb_hs *hs, void *data, size_t size)
49 {
50 return mk_mq_send(hs->ctx, hs->qid_storage, data, size);
51 }
52
53 /* Create ROOT endpoints */
54 struct flb_hs *flb_hs_create(const char *listen, const char *tcp_port,
55 struct flb_config *config)
56 {
57 int vid;
58 char tmp[32];
59 struct flb_hs *hs;
60
61 hs = flb_calloc(1, sizeof(struct flb_hs));
62 if (!hs) {
63 flb_errno();
64 return NULL;
65 }
66 hs->config = config;
67
68 /* Setup endpoint specific data */
69 flb_hs_endpoints(hs);
70
71 /* Create HTTP server context */
72 hs->ctx = mk_create();
73 if (!hs->ctx) {
74 flb_error("[http_server] could not create context");
75 flb_free(hs);
76 return NULL;
77 }
78
79 /* Compose listen address */
80 snprintf(tmp, sizeof(tmp) -1, "%s:%s", listen, tcp_port);
81 mk_config_set(hs->ctx, "Listen", tmp, NULL);
82 vid = mk_vhost_create(hs->ctx, NULL);
83 hs->vid = vid;
84
85 /* Setup virtual host */
86 mk_vhost_set(hs->ctx, vid,
87 "Name", "fluent-bit",
88 NULL);
89
plugin_get_current()90
91 /* Register all api/v1 */
92 api_v1_registration(hs);
93
94 /* Root */
95 mk_vhost_handler(hs->ctx, vid, "/", cb_root, hs);
96
97 return hs;
98 }
99
plugin_push_current($p_base_name)100 int flb_hs_start(struct flb_hs *hs)
101 {
102 int ret;
103 struct flb_config *config = hs->config;
104
105 ret = mk_start(hs->ctx);
106
107 if (ret == 0) {
108 flb_info("[http_server] listen iface=%s tcp_port=%s",
109 config->http_listen, config->http_port);
110 }
111 return ret;
112 }
113
114 int flb_hs_destroy(struct flb_hs *hs)
115 {
116 if (!hs) {
117 return 0;
118 }
plugin_get_force_installed()119 flb_hs_health_destroy();
120 mk_stop(hs->ctx);
121 mk_destroy(hs->ctx);
122
123 flb_hs_endpoints_free(hs);
124 flb_free(hs);
125
126
127 return 0;
128 }
129