| 1 |
/* |
|---|
| 2 |
* Copyright (c) 2007-2010, OmniTI Computer Consulting, Inc. |
|---|
| 3 |
* All rights reserved. |
|---|
| 4 |
* |
|---|
| 5 |
* Redistribution and use in source and binary forms, with or without |
|---|
| 6 |
* modification, are permitted provided that the following conditions are |
|---|
| 7 |
* met: |
|---|
| 8 |
* |
|---|
| 9 |
* * Redistributions of source code must retain the above copyright |
|---|
| 10 |
* notice, this list of conditions and the following disclaimer. |
|---|
| 11 |
* * Redistributions in binary form must reproduce the above |
|---|
| 12 |
* copyright notice, this list of conditions and the following |
|---|
| 13 |
* disclaimer in the documentation and/or other materials provided |
|---|
| 14 |
* with the distribution. |
|---|
| 15 |
* * Neither the name OmniTI Computer Consulting, Inc. nor the names |
|---|
| 16 |
* of its contributors may be used to endorse or promote products |
|---|
| 17 |
* derived from this software without specific prior written |
|---|
| 18 |
* permission. |
|---|
| 19 |
* |
|---|
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 21 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 24 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 25 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 26 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 27 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 28 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 30 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 31 |
*/ |
|---|
| 32 |
|
|---|
| 33 |
#include "noit_defines.h" |
|---|
| 34 |
|
|---|
| 35 |
#include <stdio.h> |
|---|
| 36 |
#include <unistd.h> |
|---|
| 37 |
#include <errno.h> |
|---|
| 38 |
#include <assert.h> |
|---|
| 39 |
#include <math.h> |
|---|
| 40 |
|
|---|
| 41 |
#include "noit_module.h" |
|---|
| 42 |
#include "noit_check.h" |
|---|
| 43 |
#include "noit_check_tools.h" |
|---|
| 44 |
#include "utils/noit_log.h" |
|---|
| 45 |
#include "utils/noit_hash.h" |
|---|
| 46 |
|
|---|
| 47 |
#include <libpq-fe.h> |
|---|
| 48 |
|
|---|
| 49 |
/* Ripped from postgres 8.3.3 */ |
|---|
| 50 |
#ifndef BOOLOID |
|---|
| 51 |
#define BOOLOID 16 |
|---|
| 52 |
#endif |
|---|
| 53 |
#ifndef INT2OID |
|---|
| 54 |
#define INT2OID 21 |
|---|
| 55 |
#endif |
|---|
| 56 |
#ifndef INT4OID |
|---|
| 57 |
#define INT4OID 23 |
|---|
| 58 |
#endif |
|---|
| 59 |
#ifndef INT8OID |
|---|
| 60 |
#define INT8OID 20 |
|---|
| 61 |
#endif |
|---|
| 62 |
#ifndef FLOAT4OID |
|---|
| 63 |
#define FLOAT4OID 700 |
|---|
| 64 |
#endif |
|---|
| 65 |
#ifndef FLOAT8OID |
|---|
| 66 |
#define FLOAT8OID 701 |
|---|
| 67 |
#endif |
|---|
| 68 |
#ifndef NUMERICOID |
|---|
| 69 |
#define NUMERICOID 1700 |
|---|
| 70 |
#endif |
|---|
| 71 |
|
|---|
| 72 |
typedef struct { |
|---|
| 73 |
noit_module_t *self; |
|---|
| 74 |
noit_check_t *check; |
|---|
| 75 |
stats_t current; |
|---|
| 76 |
PGconn *conn; |
|---|
| 77 |
PGresult *result; |
|---|
| 78 |
int rv; |
|---|
| 79 |
double connect_duration_d; |
|---|
| 80 |
double *connect_duration; |
|---|
| 81 |
double query_duration_d; |
|---|
| 82 |
double *query_duration; |
|---|
| 83 |
noit_hash_table attrs; |
|---|
| 84 |
int timed_out; |
|---|
| 85 |
char *error; |
|---|
| 86 |
} postgres_check_info_t; |
|---|
| 87 |
|
|---|
| 88 |
static noit_log_stream_t nlerr = NULL; |
|---|
| 89 |
static noit_log_stream_t nldeb = NULL; |
|---|
| 90 |
|
|---|
| 91 |
static void postgres_cleanup(noit_module_t *self, noit_check_t *check) { |
|---|
| 92 |
postgres_check_info_t *ci = check->closure; |
|---|
| 93 |
if(ci) { |
|---|
| 94 |
if(ci->result) PQclear(ci->result); |
|---|
| 95 |
if(ci->conn) PQfinish(ci->conn); |
|---|
| 96 |
noit_check_release_attrs(&ci->attrs); |
|---|
| 97 |
if(ci->error) free(ci->error); |
|---|
| 98 |
memset(ci, 0, sizeof(*ci)); |
|---|
| 99 |
} |
|---|
| 100 |
} |
|---|
| 101 |
static void postgres_ingest_stats(postgres_check_info_t *ci) { |
|---|
| 102 |
if(ci->rv == PGRES_TUPLES_OK) { |
|---|
| 103 |
/* metrics */ |
|---|
| 104 |
int nrows, ncols, i, j; |
|---|
| 105 |
nrows = PQntuples(ci->result); |
|---|
| 106 |
ncols = PQnfields(ci->result); |
|---|
| 107 |
noit_stats_set_metric(&ci->current, "row_count", METRIC_INT32, &nrows); |
|---|
| 108 |
for (i=0; i<nrows; i++) { |
|---|
| 109 |
noitL(nldeb, "postgres: row %d [%d cols]:\n", i, ncols); |
|---|
| 110 |
if(ncols<2) continue; |
|---|
| 111 |
if(PQgetisnull(ci->result, i, 0)) continue; |
|---|
| 112 |
for (j=1; j<ncols; j++) { |
|---|
| 113 |
Oid coltype; |
|---|
| 114 |
int iv, *piv; |
|---|
| 115 |
int64_t lv, *plv; |
|---|
| 116 |
double dv, *pdv; |
|---|
| 117 |
char *sv; |
|---|
| 118 |
char mname[128]; |
|---|
| 119 |
|
|---|
| 120 |
snprintf(mname, sizeof(mname), "%s`%s", |
|---|
| 121 |
PQgetvalue(ci->result, i, 0), PQfname(ci->result, j)); |
|---|
| 122 |
coltype = PQftype(ci->result, j); |
|---|
| 123 |
noitL(nldeb, "postgres: col %d (%s) type %d:\n", j, mname, coltype); |
|---|
| 124 |
switch(coltype) { |
|---|
| 125 |
case BOOLOID: |
|---|
| 126 |
if(PQgetisnull(ci->result, i, j)) piv = NULL; |
|---|
| 127 |
else { |
|---|
| 128 |
iv = strcmp(PQgetvalue(ci->result, i, j), "f") ? 1 : 0; |
|---|
| 129 |
piv = &iv; |
|---|
| 130 |
} |
|---|
| 131 |
noit_stats_set_metric(&ci->current, mname, METRIC_INT32, piv); |
|---|
| 132 |
break; |
|---|
| 133 |
case INT2OID: |
|---|
| 134 |
case INT4OID: |
|---|
| 135 |
case INT8OID: |
|---|
| 136 |
if(PQgetisnull(ci->result, i, j)) plv = NULL; |
|---|
| 137 |
else { |
|---|
| 138 |
lv = strtoll(PQgetvalue(ci->result, i, j), NULL, 10); |
|---|
| 139 |
plv = &lv; |
|---|
| 140 |
} |
|---|
| 141 |
noit_stats_set_metric(&ci->current, mname, METRIC_INT64, plv); |
|---|
| 142 |
case FLOAT4OID: |
|---|
| 143 |
case FLOAT8OID: |
|---|
| 144 |
case NUMERICOID: |
|---|
| 145 |
if(PQgetisnull(ci->result, i, j)) pdv = NULL; |
|---|
| 146 |
else { |
|---|
| 147 |
dv = atof(PQgetvalue(ci->result, i, j)); |
|---|
| 148 |
pdv = &dv; |
|---|
| 149 |
} |
|---|
| 150 |
noit_stats_set_metric(&ci->current, mname, METRIC_DOUBLE, pdv); |
|---|
| 151 |
default: |
|---|
| 152 |
if(PQgetisnull(ci->result, i, j)) sv = NULL; |
|---|
| 153 |
else sv = PQgetvalue(ci->result, i, j); |
|---|
| 154 |
noit_stats_set_metric(&ci->current, mname, METRIC_GUESS, sv); |
|---|
| 155 |
break; |
|---|
| 156 |
} |
|---|
| 157 |
} |
|---|
| 158 |
} |
|---|
| 159 |
} |
|---|
| 160 |
} |
|---|
| 161 |
static void postgres_log_results(noit_module_t *self, noit_check_t *check) { |
|---|
| 162 |
struct timeval duration; |
|---|
| 163 |
postgres_check_info_t *ci = check->closure; |
|---|
| 164 |
|
|---|
| 165 |
gettimeofday(&ci->current.whence, NULL); |
|---|
| 166 |
sub_timeval(ci->current.whence, check->last_fire_time, &duration); |
|---|
| 167 |
ci->current.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000; |
|---|
| 168 |
ci->current.available = NP_UNAVAILABLE; |
|---|
| 169 |
ci->current.state = NP_BAD; |
|---|
| 170 |
if(ci->connect_duration) |
|---|
| 171 |
noit_stats_set_metric(&ci->current, "connect_duration", METRIC_DOUBLE, |
|---|
| 172 |
ci->connect_duration); |
|---|
| 173 |
if(ci->query_duration) |
|---|
| 174 |
noit_stats_set_metric(&ci->current, "query_duration", METRIC_DOUBLE, |
|---|
| 175 |
ci->query_duration); |
|---|
| 176 |
if(ci->error) ci->current.status = ci->error; |
|---|
| 177 |
else if(ci->timed_out) ci->current.status = "timeout"; |
|---|
| 178 |
else if(ci->rv == PGRES_COMMAND_OK) { |
|---|
| 179 |
ci->current.available = NP_AVAILABLE; |
|---|
| 180 |
ci->current.state = NP_GOOD; |
|---|
| 181 |
ci->current.status = "command ok"; |
|---|
| 182 |
} |
|---|
| 183 |
else if(ci->rv == PGRES_TUPLES_OK) { |
|---|
| 184 |
ci->current.available = NP_AVAILABLE; |
|---|
| 185 |
ci->current.state = NP_GOOD; |
|---|
| 186 |
ci->current.status = "tuples ok"; |
|---|
| 187 |
} |
|---|
| 188 |
else ci->current.status = "internal error"; |
|---|
| 189 |
|
|---|
| 190 |
noit_check_set_stats(self, check, &ci->current); |
|---|
| 191 |
} |
|---|
| 192 |
|
|---|
| 193 |
#define FETCH_CONFIG_OR(key, str) do { \ |
|---|
| 194 |
if(!noit_hash_retr_str(check->config, #key, strlen(#key), &key)) \ |
|---|
| 195 |
key = str; \ |
|---|
| 196 |
} while(0) |
|---|
| 197 |
|
|---|
| 198 |
#define AVAIL_BAIL(str) do { \ |
|---|
| 199 |
PGconn *conn_swap = ci->conn; \ |
|---|
| 200 |
PGresult *result_swap = ci->result; \ |
|---|
| 201 |
if(ci->result) { ci->result = NULL; PQclear(result_swap); } \ |
|---|
| 202 |
if(ci->conn) { ci->conn = NULL; PQfinish(conn_swap); } \ |
|---|
| 203 |
ci->timed_out = 0; \ |
|---|
| 204 |
ci->error = strdup(str); \ |
|---|
| 205 |
return 0; \ |
|---|
| 206 |
} while(0) |
|---|
| 207 |
|
|---|
| 208 |
static int postgres_drive_session(eventer_t e, int mask, void *closure, |
|---|
| 209 |
struct timeval *now) { |
|---|
| 210 |
const char *dsn, *sql; |
|---|
| 211 |
char sql_buff[8192]; |
|---|
| 212 |
char dsn_buff[512]; |
|---|
| 213 |
struct timeval t1, t2, diff; |
|---|
| 214 |
postgres_check_info_t *ci = closure; |
|---|
| 215 |
noit_check_t *check = ci->check; |
|---|
| 216 |
|
|---|
| 217 |
if(mask & (EVENTER_READ | EVENTER_WRITE)) { |
|---|
| 218 |
/* this case is impossible from the eventer. It is called as |
|---|
| 219 |
* such on the synchronous completion of the event. |
|---|
| 220 |
*/ |
|---|
| 221 |
postgres_log_results(ci->self, ci->check); |
|---|
| 222 |
postgres_cleanup(ci->self, ci->check); |
|---|
| 223 |
check->flags &= ~NP_RUNNING; |
|---|
| 224 |
return 0; |
|---|
| 225 |
} |
|---|
| 226 |
|
|---|
| 227 |
switch(mask) { |
|---|
| 228 |
case EVENTER_ASYNCH_WORK: |
|---|
| 229 |
noit_check_stats_clear(&ci->current); |
|---|
| 230 |
ci->connect_duration = NULL; |
|---|
| 231 |
ci->query_duration = NULL; |
|---|
| 232 |
|
|---|
| 233 |
FETCH_CONFIG_OR(dsn, ""); |
|---|
| 234 |
noit_check_interpolate(dsn_buff, sizeof(dsn_buff), dsn, |
|---|
| 235 |
&ci->attrs, check->config); |
|---|
| 236 |
ci->conn = PQconnectdb(dsn_buff); |
|---|
| 237 |
if(!ci->conn) AVAIL_BAIL("PQconnectdb failed"); |
|---|
| 238 |
if(PQstatus(ci->conn) != CONNECTION_OK) |
|---|
| 239 |
AVAIL_BAIL(PQerrorMessage(ci->conn)); |
|---|
| 240 |
|
|---|
| 241 |
FETCH_CONFIG_OR(sql, ""); |
|---|
| 242 |
noit_check_interpolate(sql_buff, sizeof(sql_buff), sql, |
|---|
| 243 |
&ci->attrs, check->config); |
|---|
| 244 |
gettimeofday(&t1, NULL); |
|---|
| 245 |
sub_timeval(t1, check->last_fire_time, &diff); |
|---|
| 246 |
ci->connect_duration_d = diff.tv_sec * 1000.0 + diff.tv_usec / 1000.0; |
|---|
| 247 |
ci->connect_duration = &ci->connect_duration_d; |
|---|
| 248 |
|
|---|
| 249 |
ci->result = PQexec(ci->conn, sql_buff); |
|---|
| 250 |
|
|---|
| 251 |
gettimeofday(&t2, NULL); |
|---|
| 252 |
sub_timeval(t2, t1, &diff); |
|---|
| 253 |
ci->query_duration_d = diff.tv_sec * 1000.0 + diff.tv_usec / 1000.0; |
|---|
| 254 |
ci->query_duration = &ci->query_duration_d; |
|---|
| 255 |
|
|---|
| 256 |
if(!ci->result) AVAIL_BAIL("PQexec failed"); |
|---|
| 257 |
ci->rv = PQresultStatus(ci->result); |
|---|
| 258 |
switch(ci->rv) { |
|---|
| 259 |
case PGRES_TUPLES_OK: |
|---|
| 260 |
postgres_ingest_stats(ci); |
|---|
| 261 |
case PGRES_COMMAND_OK: |
|---|
| 262 |
break; |
|---|
| 263 |
default: |
|---|
| 264 |
AVAIL_BAIL(PQresultErrorMessage(ci->result)); |
|---|
| 265 |
} |
|---|
| 266 |
if(ci->result) { |
|---|
| 267 |
PGresult *result_swap = ci->result; |
|---|
| 268 |
ci->result = NULL; |
|---|
| 269 |
PQclear(result_swap); |
|---|
| 270 |
} |
|---|
| 271 |
if(ci->conn) { |
|---|
| 272 |
PGconn *conn_swap = ci->conn; |
|---|
| 273 |
ci->conn = NULL; |
|---|
| 274 |
PQfinish(conn_swap); |
|---|
| 275 |
} |
|---|
| 276 |
ci->timed_out = 0; |
|---|
| 277 |
return 0; |
|---|
| 278 |
break; |
|---|
| 279 |
case EVENTER_ASYNCH_CLEANUP: |
|---|
| 280 |
/* This sets us up for a completion call. */ |
|---|
| 281 |
e->mask = EVENTER_READ | EVENTER_WRITE; |
|---|
| 282 |
break; |
|---|
| 283 |
default: |
|---|
| 284 |
abort(); |
|---|
| 285 |
} |
|---|
| 286 |
return 0; |
|---|
| 287 |
} |
|---|
| 288 |
|
|---|
| 289 |
static int postgres_initiate(noit_module_t *self, noit_check_t *check) { |
|---|
| 290 |
postgres_check_info_t *ci = check->closure; |
|---|
| 291 |
struct timeval __now; |
|---|
| 292 |
|
|---|
| 293 |
/* We cannot be running */ |
|---|
| 294 |
assert(!(check->flags & NP_RUNNING)); |
|---|
| 295 |
check->flags |= NP_RUNNING; |
|---|
| 296 |
|
|---|
| 297 |
ci->self = self; |
|---|
| 298 |
ci->check = check; |
|---|
| 299 |
|
|---|
| 300 |
ci->timed_out = 1; |
|---|
| 301 |
ci->rv = -1; |
|---|
| 302 |
noit_check_make_attrs(check, &ci->attrs); |
|---|
| 303 |
gettimeofday(&__now, NULL); |
|---|
| 304 |
memcpy(&check->last_fire_time, &__now, sizeof(__now)); |
|---|
| 305 |
|
|---|
| 306 |
/* Register a handler for the worker */ |
|---|
| 307 |
noit_check_run_full_asynch(check, postgres_drive_session); |
|---|
| 308 |
return 0; |
|---|
| 309 |
} |
|---|
| 310 |
|
|---|
| 311 |
static int postgres_initiate_check(noit_module_t *self, noit_check_t *check, |
|---|
| 312 |
int once, noit_check_t *parent) { |
|---|
| 313 |
if(!check->closure) check->closure = calloc(1, sizeof(postgres_check_info_t)); |
|---|
| 314 |
INITIATE_CHECK(postgres_initiate, self, check); |
|---|
| 315 |
return 0; |
|---|
| 316 |
} |
|---|
| 317 |
|
|---|
| 318 |
static int postgres_onload(noit_image_t *self) { |
|---|
| 319 |
nlerr = noit_log_stream_find("error/postgres"); |
|---|
| 320 |
nldeb = noit_log_stream_find("debug/postgres"); |
|---|
| 321 |
if(!nlerr) nlerr = noit_stderr; |
|---|
| 322 |
if(!nldeb) nldeb = noit_debug; |
|---|
| 323 |
|
|---|
| 324 |
eventer_name_callback("http/postgres_drive_session", postgres_drive_session); |
|---|
| 325 |
return 0; |
|---|
| 326 |
} |
|---|
| 327 |
|
|---|
| 328 |
#include "postgres.xmlh" |
|---|
| 329 |
noit_module_t postgres = { |
|---|
| 330 |
{ |
|---|
| 331 |
NOIT_MODULE_MAGIC, |
|---|
| 332 |
NOIT_MODULE_ABI_VERSION, |
|---|
| 333 |
"postgres", |
|---|
| 334 |
"PostgreSQL Checker", |
|---|
| 335 |
postgres_xml_description, |
|---|
| 336 |
postgres_onload |
|---|
| 337 |
}, |
|---|
| 338 |
NULL, |
|---|
| 339 |
NULL, |
|---|
| 340 |
postgres_initiate_check, |
|---|
| 341 |
postgres_cleanup |
|---|
| 342 |
}; |
|---|
| 343 |
|
|---|