forked from nvorobev/pg_metricus_c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg_metricus.c
69 lines (51 loc) · 1.82 KB
/
pg_metricus.c
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
#include <postgres.h>
#include <fmgr.h>
#include <utils/guc.h>
#include <arpa/inet.h>
#include <unistd.h>
#if PG_VERSION_NUM >= 90600
/* GetConfigOptionByName has a new signature from 9.6 on */
#define GetConfigOptionByName(name, varname) GetConfigOptionByName(name, varname, false)
#endif /* PG_VERSION_NUM */
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
char *dup_pgtext(text *what);
extern Datum send_metric(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(send_metric);
Datum
send_metric(PG_FUNCTION_ARGS)
{
int sockfd, port;
char *message, *host;
struct sockaddr_in serv_addr;
if (PG_ARGISNULL(0))
ereport(ERROR, (errmsg("null message not accepted")));
message = dup_pgtext(PG_GETARG_TEXT_P(0));
if (strlen(message) == 0)
ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("invalid input message")));
host = GetConfigOptionByName("pg_metricus.host", NULL);
port = atoi(GetConfigOptionByName("pg_metricus.port", NULL));
if (port < 0 || strlen(host) == 0)
ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("invalid host or port")));
if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
ereport(ERROR, (errmsg("create socket")));
bzero(&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
if (inet_pton(AF_INET, host, &serv_addr.sin_addr) <= 0)
ereport(ERROR, (errmsg("inet_pton, invalid host format")));
if (sendto(sockfd, message, strlen(message), 0, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1)
ereport(ERROR, (errmsg("send to socket")));
close(sockfd);
PG_RETURN_VOID();
}
char *
dup_pgtext(text *what)
{
size_t len = VARSIZE(what)-VARHDRSZ;
char *dup = palloc(len+1);
memcpy(dup, VARDATA(what), len);
dup[len] = 0;
return dup;
}