/* * Custom keys pg.index.* (for each field in pg_stat_all_indexes) * * Returns the requested statistic for the specified index * * Parameters: * 0: connection string * 1: connection database * 2: filter by index name (default: sum of all indexes) * * Returns: u */ int PG_STAT_ALL_INDEXES(AGENT_REQUEST *request, AGENT_RESULT *result) { int ret = SYSINFO_RET_FAIL; // Request result code const char *__function_name = "PG_STAT_ALL_INDEXES"; // Function name for log file char *index = NULL; PGconn *conn = NULL; PGresult *res = NULL; char *field; char query[MAX_STRING_LEN]; char *buffer = NULL; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); // Get stat field from requested key name "pb.table.<field>" field = &request->key[9]; // Build query index = get_rparam(request, PARAM_FIRST); if(strisnull(index)) zbx_snprintf(query, sizeof(query), "SELECT SUM(%s) FROM pg_stat_all_indexes", field); else zbx_snprintf(query, sizeof(query), "SELECT %s FROM pg_stat_all_indexes WHERE indexrelname = $1", field); // Connect to PostreSQL if(NULL == (conn = pg_connect_request(request))) goto out; // Execute a query res = pg_exec(conn, query, param_new(index)); if(PQresultStatus(res) != PGRES_TUPLES_OK) { zabbix_log(LOG_LEVEL_ERR, "Failed to execute PostgreSQL query in %s() with: %s", __function_name, PQresultErrorMessage(res)); goto out; } if(0 == PQntuples(res)) { zabbix_log(LOG_LEVEL_ERR, "No results returned for query \"%s\" in %s()", query, __function_name); goto out; } // Set result buffer = strdup(PQgetvalue(res, 0, 0)); SET_UI64_RESULT(result, atoi(buffer)); ret = SYSINFO_RET_OK; out: PQclear(res); PQfinish(conn); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); return ret; }
static SCM pg_one_row(SCM conn, SCM query) { struct pg_res *pgr; SCM res, row; scm_assert_smob_type(pg_conn_tag, conn); res = pg_exec(conn, query); pgr = (struct pg_res *)SCM_SMOB_DATA(res); if (pgr->cursor < pgr->tuples) row = build_row(pgr); else row = SCM_BOOL_F; PQclear(pgr->res); pgr->res = NULL; scm_remember_upto_here_2(res, row); scm_remember_upto_here_2(conn, query); return row; }
/* * Function: pg_get_int * * Executes a PostgreSQL Query using connection details from a Zabbix agent * request structure and updates the agent result structure with the integer * value of the first column of the first row returned. * * Parameter [request]: Zabbix agent request structure. * Passed to pg_connect to fetch as valid PostgreSQL * server connection * * Parameter [result]: Zabbix agent result structure * * Paramater [query]: PostgreSQL query to execute. Query should return a * single scalar integer value * * Returns: SYSINFO_RET_OK or SYSINFO_RET_FAIL on error */ int pg_get_int(AGENT_REQUEST *request, AGENT_RESULT *result, const char *query) { int ret = SYSINFO_RET_FAIL; // Request result code const char *__function_name = "pg_get_int"; // Function name for log file PGconn *conn = NULL; PGresult *res = NULL; char *buffer = NULL; zabbix_log(LOG_LEVEL_DEBUG, "In %s(%s)", __function_name, request->key); // Connect to PostreSQL if(NULL == (conn = pg_connect(request))) goto out; // Execute a query res = pg_exec(conn, query); if(PQresultStatus(res) != PGRES_TUPLES_OK) { zabbix_log(LOG_LEVEL_ERR, "Failed to execute PostgreSQL query in %s(%s) with: %s", __function_name, request->key, PQresultErrorMessage(res)); goto out; } if(0 == PQntuples(res)) { zabbix_log(LOG_LEVEL_DEBUG, "No results returned for query \"%s\" in %s(%s)", query, __function_name, request->key); goto out; } // Set result buffer = strdup(PQgetvalue(res, 0, 0)); // Convert E Notation if(1 < strlen(buffer) && '.' == buffer[1]) { double dbl = strtod(buffer, NULL); SET_UI64_RESULT(result, (unsigned long long) dbl); } else { SET_UI64_RESULT(result, strtoull(buffer, NULL, 10)); } ret = SYSINFO_RET_OK; out: PQclear(res); PQfinish(conn); zabbix_log(LOG_LEVEL_DEBUG, "End of %s(%s)", __function_name, request->key); return ret; }
/* * Function: pg_get_string * * Executes a PostgreSQL Query using connection details from a Zabbix agent * request structure and updates the agent result structure with the string * value of the first column of the first row returned. * * Parameter [request]: Zabbix agent request structure. * Passed to pg_connect to fetch as valid PostgreSQL * server connection * * Parameter [result]: Zabbix agent result structure * * Paramater [query]: PostgreSQL query to execute. Query should return a * single scalar string value * * Returns: SYSINFO_RET_OK or SYSINFO_RET_FAIL on error */ int pg_get_string(AGENT_REQUEST *request, AGENT_RESULT *result, const char *query) { int ret = SYSINFO_RET_FAIL; // Request result code const char *__function_name = "pg_get_string"; // Function name for log file PGconn *conn = NULL; PGresult *res = NULL; char *buffer = NULL; zabbix_log(LOG_LEVEL_DEBUG, "In %s(%s)", __function_name, request->key); // Connect to PostreSQL if(NULL == (conn = pg_connect(request))) goto out; // Execute a query res = pg_exec(conn, query); if(PQresultStatus(res) != PGRES_TUPLES_OK) { zabbix_log(LOG_LEVEL_ERR, "Failed to execute PostgreSQL query in %s(%s) with: %s", __function_name, request->key, PQresultErrorMessage(res)); goto out; } if(0 == PQntuples(res)) { zabbix_log(LOG_LEVEL_DEBUG, "No results returned for query \"%s\" in %s(%s)", query, __function_name, request->key); goto out; } // Set result buffer = strdup(PQgetvalue(res, 0, 0)); SET_STR_RESULT(result, buffer); ret = SYSINFO_RET_OK; out: PQclear(res); PQfinish(conn); zabbix_log(LOG_LEVEL_DEBUG, "End of %s(%s)", __function_name, request->key); return ret; }
long int pg_version(AGENT_REQUEST *request) { PGconn *conn = NULL; PGresult *res = NULL; long int version = 0; // connect to PostgreSQL conn = pg_connect(request); if (NULL == conn) return 0; // get server version res = pg_exec(conn, "SELECT setting FROM pg_settings WHERE name='server_version_num'", NULL); if(0 == PQntuples(res)) { zabbix_log(LOG_LEVEL_ERR, "Failed to get PostgreSQL server version"); return 0; } // convert to integer version = atol(PQgetvalue(res, 0, 0)); zabbix_log(LOG_LEVEL_DEBUG, "PostgreSQL server version: %lu", version); return version; }
/* * Function: pg_get_discovery * * Executes a PostgreSQL Query using connection details from a Zabbix agent * request structure and updates the agent result structure with the JSON * discovery data for each returned row. * * Parameter [request]: Zabbix agent request structure. * Passed to pg_connect to fetch as valid PostgreSQL * server connection * * Parameter [result]: Zabbix agent result structure * * Paramater [query]: PostgreSQL query to execute. Query should column names that match the desired discovery fields. * * Returns: SYSINFO_RET_OK or SYSINFO_RET_FAIL on error */ int pg_get_discovery(AGENT_REQUEST *request, AGENT_RESULT *result, const char *query) { int ret = SYSINFO_RET_FAIL; // Request result code const char *__function_name = "pg_get_discovery"; // Function name for log file PGconn *conn = NULL; PGresult *res = NULL; struct zbx_json j; // JSON response for discovery rule int i = 0, x = 0, columns = 0, rows = 0; char *colname = NULL, *c = NULL; char buffer[MAX_STRING_LEN]; zabbix_log(LOG_LEVEL_DEBUG, "In %s(%s)", __function_name, request->key); // Connect to PostreSQL if(NULL == (conn = pg_connect(request))) goto out; // Execute a query res = pg_exec(conn, query); if(PQresultStatus(res) != PGRES_TUPLES_OK) { zabbix_log(LOG_LEVEL_ERR, "Failed to execute PostgreSQL query in %s(%s) with: %s", __function_name, request->key, PQresultErrorMessage(res)); goto out; } rows = PQntuples(res); columns = PQnfields(res); // Create JSON array of discovered objects zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA); // create discovery instance for each row for(i = 0; i < rows; i++) { zbx_json_addobject(&j, NULL); // add each fields as a discovery field for(x = 0; x < columns; x++) { // set discovery key name to uppercase column name zbx_snprintf(buffer, sizeof(buffer), "{#%s}", PQfname(res, x)); for(c = &buffer[0]; *c; c++) *c = toupper(*c); zbx_json_addstring(&j, buffer, PQgetvalue(res, i, x), ZBX_JSON_TYPE_STRING); } zbx_json_close(&j); } // Finalize JSON response zbx_json_close(&j); SET_STR_RESULT(result, strdup(j.buffer)); zbx_json_free(&j); ret = SYSINFO_RET_OK; out: PQclear(res); PQfinish(conn); zabbix_log(LOG_LEVEL_DEBUG, "End of %s(%s)", __function_name, request->key); return ret; }
/* * Function: pg_get_result * * Executes a PostgreSQL Query using connection details from a Zabbix agent * request structure and updates the agent result structure with the value of * the first column of the first row returned. * * type may be * * Query parameters may be provided as a NULL terminated sequence of *char * values in the ... parameter. * * Parameter [request]: Zabbix agent request structure. * Passed to pg_connect to fetch as valid PostgreSQL * server connection * * Parameter [result]: Zabbix agent result structure * * Parameter [type]: Result type to set. May be one of AR_STRING, AR_UINT64 * or AR_DOUBLE. * * Paramater [query]: PostgreSQL query to execute. Query should return a * single scalar string value. Parameters defined using PostgreSQL's * '$n' notation will be replaced with the corresponding variadic * argument provided in ... * * Returns: SYSINFO_RET_OK or SYSINFO_RET_FAIL on error */ int pg_get_result(AGENT_REQUEST *request, AGENT_RESULT *result, int type, const char *query, PGparams params) { int ret = SYSINFO_RET_FAIL; // Request result code const char *__function_name = "pg_get_result"; // Function name for log file PGconn *conn = NULL; PGresult *res = NULL; char *value = NULL; zabbix_log(LOG_LEVEL_DEBUG, "In %s(%s)", __function_name, request->key); // Connect to PostreSQL if(NULL == (conn = pg_connect(request))) goto out; // Execute a query res = pg_exec(conn, query, params); if(PQresultStatus(res) != PGRES_TUPLES_OK) { zabbix_log(LOG_LEVEL_ERR, "Failed to execute PostgreSQL query in %s(%s) with: %s", __function_name, request->key, PQresultErrorMessage(res)); goto out; } if(0 == PQntuples(res)) { zabbix_log(LOG_LEVEL_DEBUG, "No results returned for query \"%s\" in %s(%s)", query, __function_name, request->key); goto out; } // get scalar value (freed later by PQclear) value = PQgetvalue(res, 0, 0); // Set result switch(type) { case AR_STRING: // string result (zabbix will clean the strdup'd buffer) SET_STR_RESULT(result, strdup(value)); break; case AR_UINT64: // integer result // Convert E Notation if(1 < strlen(value) && '.' == value[1]) { double dbl = strtod(value, NULL); SET_UI64_RESULT(result, (unsigned long long) dbl); } else { SET_UI64_RESULT(result, strtoull(value, NULL, 10)); } break; case AR_DOUBLE: // double result SET_DBL_RESULT(result, strtold(value, NULL)); break; default: // unknown result type zabbix_log(LOG_LEVEL_ERR, "Unsupported result type: 0x%0X in %s", type, __function_name); goto out; } ret = SYSINFO_RET_OK; out: PQclear(res); PQfinish(conn); zabbix_log(LOG_LEVEL_DEBUG, "End of %s(%s)", __function_name, request->key); return ret; }
void pg_DrawPolygon(pg_struct *pg, u8g_t *u8g) { if ( pg_prepare(pg) == 0 ) return; pg_exec(pg, u8g); }