FD.io VPP  v19.04-6-g6f05f72
Vector Packet Processing
vpp_prometheus_export.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * vpp_get_stats.c
4  *
5  * Copyright (c) 2018 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19 
20 #include <arpa/inet.h>
21 #include <sys/epoll.h>
22 #include <stdbool.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <netdb.h>
30 #include <sys/socket.h>
32 #include <vlib/vlib.h>
33 #include <ctype.h>
34 
35 /* https://github.com/prometheus/prometheus/wiki/Default-port-allocations */
36 #define SERVER_PORT 9482
37 
38 static char *
39 prom_string (char *s)
40 {
41  char *p = s;
42  while (*p)
43  {
44  if (!isalnum (*p))
45  *p = '_';
46  p++;
47  }
48  return s;
49 }
50 
51 static void
52 dump_metrics (FILE * stream, u8 ** patterns)
53 {
55  int i, j, k;
56  static u32 *stats = 0;
57 
58 retry:
59  res = stat_segment_dump (stats);
60  if (res == 0)
61  { /* Memory layout has changed */
62  if (stats)
63  vec_free (stats);
64  stats = stat_segment_ls (patterns);
65  goto retry;
66  }
67 
68  for (i = 0; i < vec_len (res); i++)
69  {
70  switch (res[i].type)
71  {
73  fformat (stream, "# TYPE %s counter\n", prom_string (res[i].name));
74  for (k = 0; k < vec_len (res[i].simple_counter_vec); k++)
75  for (j = 0; j < vec_len (res[i].simple_counter_vec[k]); j++)
76  fformat (stream, "%s{thread=\"%d\",interface=\"%d\"} %lld\n",
77  prom_string (res[i].name), k, j,
78  res[i].simple_counter_vec[k][j]);
79  break;
80 
82  fformat (stream, "# TYPE %s_packets counter\n",
83  prom_string (res[i].name));
84  fformat (stream, "# TYPE %s_bytes counter\n",
85  prom_string (res[i].name));
86  for (k = 0; k < vec_len (res[i].simple_counter_vec); k++)
87  for (j = 0; j < vec_len (res[i].combined_counter_vec[k]); j++)
88  {
89  fformat (stream,
90  "%s_packets{thread=\"%d\",interface=\"%d\"} %lld\n",
91  prom_string (res[i].name), k, j,
92  res[i].combined_counter_vec[k][j].packets);
93  fformat (stream,
94  "%s_bytes{thread=\"%d\",interface=\"%d\"} %lld\n",
95  prom_string (res[i].name), k, j,
96  res[i].combined_counter_vec[k][j].bytes);
97  }
98  break;
100  fformat (stream, "# TYPE %s counter\n", prom_string (res[i].name));
101  fformat (stream, "%s{thread=\"0\"} %lld\n",
102  prom_string (res[i].name), res[i].error_value);
103  break;
104 
106  fformat (stream, "# TYPE %s counter\n", prom_string (res[i].name));
107  fformat (stream, "%s %.2f\n", prom_string (res[i].name),
108  res[i].scalar_value);
109  break;
110 
111  default:
112  fformat (stderr, "Unknown value %d\n", res[i].type);
113  ;
114  }
115  }
117 
118 }
119 
120 
121 #define ROOTPAGE "<html><head><title>Metrics exporter</title></head><body><ul><li><a href=\"/metrics\">metrics</a></li></ul></body></html>"
122 #define NOT_FOUND_ERROR "<html><head><title>Document not found</title></head><body><h1>404 - Document not found</h1></body></html>"
123 
124 static void
125 http_handler (FILE * stream, u8 ** patterns)
126 {
127  char status[80] = { 0 };
128  if (fgets (status, sizeof (status) - 1, stream) == 0)
129  {
130  fprintf (stderr, "fgets error: %s %s\n", status, strerror (errno));
131  return;
132  }
133  char *saveptr;
134  char *method = strtok_r (status, " \t\r\n", &saveptr);
135  if (method == 0 || strncmp (method, "GET", 4) != 0)
136  {
137  fputs ("HTTP/1.0 405 Method Not Allowed\r\n", stream);
138  return;
139  }
140  char *request_uri = strtok_r (NULL, " \t", &saveptr);
141  char *protocol = strtok_r (NULL, " \t\r\n", &saveptr);
142  if (protocol == 0 || strncmp (protocol, "HTTP/1.", 7) != 0)
143  {
144  fputs ("HTTP/1.0 400 Bad Request\r\n", stream);
145  return;
146  }
147  /* Read the other headers */
148  for (;;)
149  {
150  char header[1024];
151  if (fgets (header, sizeof (header) - 1, stream) == 0)
152  {
153  fprintf (stderr, "fgets error: %s\n", strerror (errno));
154  return;
155  }
156  if (header[0] == '\n' || header[1] == '\n')
157  {
158  break;
159  }
160  }
161  if (strcmp (request_uri, "/") == 0)
162  {
163  fprintf (stream, "HTTP/1.0 200 OK\r\nContent-Length: %lu\r\n\r\n",
164  (unsigned long) strlen (ROOTPAGE));
165  fputs (ROOTPAGE, stream);
166  return;
167  }
168  if (strcmp (request_uri, "/metrics") != 0)
169  {
170  fprintf (stream,
171  "HTTP/1.0 404 Not Found\r\nContent-Length: %lu\r\n\r\n",
172  (unsigned long) strlen (NOT_FOUND_ERROR));
173  fputs (NOT_FOUND_ERROR, stream);
174  return;
175  }
176  fputs ("HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n", stream);
177  dump_metrics (stream, patterns);
178 }
179 
180 static int
182 {
183  struct sockaddr_in6 serveraddr;
184  int addrlen = sizeof (serveraddr);
185  int enable = 1;
186 
187  int listenfd = socket (AF_INET6, SOCK_STREAM, 0);
188  if (listenfd == -1)
189  {
190  perror ("Failed opening socket");
191  return -1;
192  }
193 
194  int rv =
195  setsockopt (listenfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof (int));
196  if (rv < 0)
197  {
198  perror ("Failed setsockopt");
199  close (listenfd);
200  return -1;
201  }
202 
203  clib_memset (&serveraddr, 0, sizeof (serveraddr));
204  serveraddr.sin6_family = AF_INET6;
205  serveraddr.sin6_port = htons (port);
206  serveraddr.sin6_addr = in6addr_any;
207 
208  if (bind (listenfd, (struct sockaddr *) &serveraddr, addrlen) < 0)
209  {
210  fprintf (stderr, "bind() error %s\n", strerror (errno));
211  close (listenfd);
212  return -1;
213  }
214  if (listen (listenfd, 1000000) != 0)
215  {
216  fprintf (stderr, "listen() error for %s\n", strerror (errno));
217  close (listenfd);
218  return -1;
219  }
220  return listenfd;
221 }
222 
223 /* Socket epoll, linux-specific */
225 {
226  struct sockaddr_storage storage;
227  struct sockaddr addr;
228  struct sockaddr_in sin_addr;
229  struct sockaddr_in6 sin6_addr;
230 };
231 
232 
233 
234 int
235 main (int argc, char **argv)
236 {
237  unformat_input_t _argv, *a = &_argv;
238  u8 *stat_segment_name, *pattern = 0, **patterns = 0;
239  int rv;
240 
241  /* Allocating 32MB heap */
242  clib_mem_init (0, 32 << 20);
243 
244  unformat_init_command_line (a, argv);
245 
246  stat_segment_name = (u8 *) STAT_SEGMENT_SOCKET_FILE;
247 
249  {
250  if (unformat (a, "socket-name %s", &stat_segment_name))
251  ;
252  else if (unformat (a, "%s", &pattern))
253  {
254  vec_add1 (patterns, pattern);
255  }
256  else
257  {
258  fformat (stderr,
259  "%s: usage [socket-name <name>] <patterns> ...\n",
260  argv[0]);
261  exit (1);
262  }
263  }
264 
265  if (vec_len (patterns) == 0)
266  {
267  fformat (stderr,
268  "%s: usage [socket-name <name>] <patterns> ...\n", argv[0]);
269  exit (1);
270  }
271 
272  rv = stat_segment_connect ((char *) stat_segment_name);
273  if (rv)
274  {
275  fformat (stderr, "Couldn't connect to vpp, does %s exist?\n",
276  stat_segment_name);
277  exit (1);
278  }
279 
280  int fd = start_listen (SERVER_PORT);
281  if (fd < 0)
282  {
283  exit (1);
284  }
285  for (;;)
286  {
287  int conn_sock = accept (fd, NULL, NULL);
288  if (conn_sock < 0)
289  {
290  fprintf (stderr, "Accept failed: %s", strerror (errno));
291  continue;
292  }
293  else
294  {
295  struct sockaddr_in6 clientaddr = { 0 };
296  char address[INET6_ADDRSTRLEN];
297  socklen_t addrlen;
298  getpeername (conn_sock, (struct sockaddr *) &clientaddr, &addrlen);
299  if (inet_ntop
300  (AF_INET6, &clientaddr.sin6_addr, address, sizeof (address)))
301  {
302  fprintf (stderr, "Client address is [%s]:%d\n", address,
303  ntohs (clientaddr.sin6_port));
304  }
305  }
306 
307  FILE *stream = fdopen (conn_sock, "r+");
308  if (stream == NULL)
309  {
310  fprintf (stderr, "fdopen error: %s\n", strerror (errno));
311  close (conn_sock);
312  continue;
313  }
314  /* Single reader at the moment */
315  http_handler (stream, patterns);
316  fclose (stream);
317  }
318 
320  close (fd);
321 
322  exit (0);
323 }
324 
325 /*
326  * fd.io coding-style-patch-verification: ON
327  *
328  * Local Variables:
329  * eval: (c-set-style "gnu")
330  * End:
331  */
typedef address
Definition: ip_types.api:30
int stat_segment_connect(const char *socket_name)
Definition: stat_client.c:166
a
Definition: bitmap.h:538
static int start_listen(u16 port)
Optimized string handling code, including c11-compliant "safe C library" variants.
void stat_segment_data_free(stat_segment_data_t *res)
Definition: stat_client.c:286
counter_t ** simple_counter_vec
Definition: stat_client.h:50
#define NULL
Definition: clib.h:58
#define NOT_FOUND_ERROR
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
int i
clib_memset(h->entries, 0, sizeof(h->entries[0])*entries)
static void dump_metrics(FILE *stream, u8 **patterns)
unsigned char u8
Definition: types.h:56
int main(int argc, char **argv)
unsigned int u32
Definition: types.h:88
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
void unformat_init_command_line(unformat_input_t *input, char *argv[])
Definition: unformat.c:1007
#define SERVER_PORT
u8 name[64]
Definition: memclnt.api:152
void stat_segment_disconnect(void)
Definition: stat_client.c:180
word fformat(FILE *f, char *fmt,...)
Definition: format.c:462
void * clib_mem_init(void *heap, uword size)
Definition: mem_dlmalloc.c:206
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
#define STAT_SEGMENT_SOCKET_FILE
Definition: stat_client.h:38
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
struct sockaddr addr
uint32_t * stat_segment_ls(uint8_t **patterns)
Definition: stat_client.c:391
stat_segment_data_t * stat_segment_dump(uint32_t *stats)
Definition: stat_client.c:426
struct sockaddr_in6 sin6_addr
static char * prom_string(char *s)
struct sockaddr_in sin_addr
static void http_handler(FILE *stream, u8 **patterns)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define ROOTPAGE
struct sockaddr_storage storage
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
u8 protocol
Definition: ipsec.api:96
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:170