root/src/modules/postgres.c

Revision cce9b78f30caffb883a0f4e601bccf312b3f3001, 6.8 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

name it proper, refs #9

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  */
5
6 #include "noit_defines.h"
7
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <assert.h>
12 #include <math.h>
13
14 #include "noit_module.h"
15 #include "noit_check.h"
16 #include "noit_check_tools.h"
17 #include "utils/noit_log.h"
18 #include "utils/noit_hash.h"
19
20 #include <libpq-fe.h>
21 #include <postgres.h>
22 #include <fmgr.h>
23 #include <catalog/pg_type.h>
24
25 typedef struct {
26   noit_module_t *self;
27   noit_check_t *check;
28   PGconn *conn;
29   PGresult *result;
30   int rv;
31   noit_hash_table attrs;
32   int timed_out;
33   char *error;
34 } postgres_check_info_t;
35
36 static noit_log_stream_t nlerr = NULL;
37 static noit_log_stream_t nldeb = NULL;
38
39 static void postgres_cleanup(noit_module_t *self, noit_check_t *check) {
40   postgres_check_info_t *ci = check->closure;
41   if(ci) {
42     if(ci->result) PQclear(ci->result);
43     if(ci->conn) PQfinish(ci->conn);
44     noit_check_release_attrs(&ci->attrs);
45     if(ci->error) free(ci->error);
46     memset(ci, 0, sizeof(*ci));
47   }
48 }
49 static void postgres_log_results(noit_module_t *self, noit_check_t *check) {
50   stats_t current;
51   struct timeval duration;
52   postgres_check_info_t *ci = check->closure;
53
54   noit_check_stats_clear(&current);
55
56   gettimeofday(&current.whence, NULL);
57   sub_timeval(current.whence, check->last_fire_time, &duration);
58   current.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000;
59   current.available = NP_UNAVAILABLE;
60   if(ci->error) current.status = ci->error;
61   else if(ci->timed_out) current.status = "timeout";
62   else if(ci->rv == PGRES_COMMAND_OK) {
63     current.available = NP_AVAILABLE;
64     current.status = "command ok";
65   }
66   else if(ci->rv == PGRES_TUPLES_OK) {
67     current.available = NP_AVAILABLE;
68     current.status = "tuples ok";
69   }
70   else current.status = "internal error";
71
72   if(ci->rv == PGRES_TUPLES_OK) {
73     /* metrics */
74     int nrows, ncols, i, j;
75     nrows = PQntuples(ci->result);
76     ncols = PQnfields(ci->result);
77     for (i=0; i<nrows; i++) {
78       if(j<2) continue;
79       if(!PQgetisnull(ci->result, i, 0)) continue;
80       for (j=1; j<ncols; j++) {
81         Oid coltype;
82         int iv, *piv;
83         int64_t lv, *plv;
84         double dv, *pdv;
85         char *sv;
86         char mname[128];
87  
88         snprintf(mname, sizeof(mname), "%s`%s",
89                  PQgetvalue(ci->result, i, 0), PQfname(ci->result, j));
90         coltype = PQftype(ci->result, j);
91         switch(coltype) {
92           case BOOLOID:
93             if(PQgetisnull(ci->result, i, j)) piv = NULL;
94             else {
95               iv = strcmp(PQgetvalue(ci->result, i, j), "f") ? 1 : 0;
96               piv = &iv;
97             }
98             noit_stats_set_metric(&current, mname, METRIC_INT32, piv);
99             break;
100           case INT2OID:
101           case INT4OID:
102           case INT8OID:
103             if(PQgetisnull(ci->result, i, j)) plv = NULL;
104             else {
105               lv = strtoll(PQgetvalue(ci->result, i, j), NULL, 10);
106               plv = &lv;
107             }
108             noit_stats_set_metric(&current, mname, METRIC_INT64, plv);
109           case FLOAT4OID:
110           case FLOAT8OID:
111           case NUMERICOID:
112             if(PQgetisnull(ci->result, i, j)) pdv = NULL;
113             else {
114               dv = atof(PQgetvalue(ci->result, i, j));
115               pdv = &dv;
116             }
117             noit_stats_set_metric(&current, mname, METRIC_DOUBLE, pdv);
118           default:
119             if(PQgetisnull(ci->result, i, j)) sv = NULL;
120             else sv = PQgetvalue(ci->result, i, j);
121             noit_stats_set_metric(&current, mname, METRIC_STRING, sv);
122             break;
123         }
124       }
125     }
126   }
127   noit_check_set_stats(self, check, &current);
128 }
129
130 #define FETCH_CONFIG_OR(key, str) do { \
131   if(!noit_hash_retrieve(check->config, #key, strlen(#key), (void **)&key)) \
132     key = str; \
133 } while(0)
134
135 #define AVAIL_BAIL(str) do { \
136   ci->timed_out = 0; \
137   ci->error = strdup(str); \
138   return 0; \
139 } while(0)
140
141 static int postgres_drive_session(eventer_t e, int mask, void *closure,
142                                   struct timeval *now) {
143   const char *dsn, *sql;
144   char sql_buff[8192];
145   char dsn_buff[512];
146   postgres_check_info_t *ci = closure;
147   noit_check_t *check = ci->check;
148
149   if(mask & (EVENTER_READ | EVENTER_WRITE)) {
150     /* this case is impossible from the eventer.  It is called as
151      * such on the synchronous completion of the event.
152      */
153     postgres_log_results(ci->self, ci->check);
154     postgres_cleanup(ci->self, ci->check);
155     check->flags &= ~NP_RUNNING;
156     return 0;
157   }
158   switch(mask) {
159     case EVENTER_ASYNCH_WORK:
160       FETCH_CONFIG_OR(dsn, "");
161       noit_check_interpolate(dsn_buff, sizeof(dsn_buff), dsn,
162                              &ci->attrs, check->config);
163       ci->conn = PQconnectdb(dsn);
164       if(!ci->conn) AVAIL_BAIL("PQconnectdb failed");
165       if(PQstatus(ci->conn) != CONNECTION_OK)
166         AVAIL_BAIL(PQerrorMessage(ci->conn));
167
168       FETCH_CONFIG_OR(sql, "");
169       noit_check_interpolate(sql_buff, sizeof(sql_buff), sql,
170                              &ci->attrs, check->config);
171       ci->result = PQexec(ci->conn, sql);
172       if(!ci->result) AVAIL_BAIL("PQexec failed");
173       ci->rv = PQresultStatus(ci->result);
174       switch(ci->rv) {
175        case PGRES_TUPLES_OK:
176        case PGRES_COMMAND_OK:
177         break;
178        default:
179         AVAIL_BAIL(PQresultErrorMessage(ci->result));
180       }
181       ci->timed_out = 0;
182       return 0;
183       break;
184     case EVENTER_ASYNCH_CLEANUP:
185       /* This sets us up for a completion call. */
186       e->mask = EVENTER_READ | EVENTER_WRITE;
187       break;
188     default:
189       abort();
190   }
191   return 0;
192 }
193
194 static int postgres_initiate(noit_module_t *self, noit_check_t *check) {
195   postgres_check_info_t *ci = check->closure;
196   struct timeval __now;
197
198   /* We cannot be running */
199   assert(!(check->flags & NP_RUNNING));
200   check->flags |= NP_RUNNING;
201
202   ci->self = self;
203   ci->check = check;
204
205   ci->timed_out = 1;
206   ci->rv = -1;
207   noit_check_make_attrs(check, &ci->attrs);
208   gettimeofday(&__now, NULL);
209   memcpy(&check->last_fire_time, &__now, sizeof(__now));
210
211   /* Register a handler for the worker */
212   noit_check_run_full_asynch(check, postgres_drive_session);
213   return 0;
214 }
215
216 static int postgres_initiate_check(noit_module_t *self, noit_check_t *check,
217                                    int once, noit_check_t *parent) {
218   if(!check->closure) check->closure = calloc(1, sizeof(postgres_check_info_t));
219   INITIATE_CHECK(postgres_initiate, self, check);
220   return 0;
221 }
222
223 static int postgres_onload(noit_module_t *self) {
224   nlerr = noit_log_stream_find("error/postgres");
225   nldeb = noit_log_stream_find("debug/postgres");
226   if(!nlerr) nlerr = noit_stderr;
227   if(!nldeb) nldeb = noit_debug;
228
229   eventer_name_callback("http/postgres_drive_session", postgres_drive_session);
230   return 0;
231 }
232
233 noit_module_t postgres = {
234   NOIT_MODULE_MAGIC,
235   NOIT_MODULE_ABI_VERSION,
236   "postgres",
237   "PostgreSQL Checker",
238   postgres_onload,
239   NULL,
240   NULL,
241   postgres_initiate_check,
242   postgres_cleanup
243 };
244
Note: See TracBrowser for help on using the browser.