From 0b6423f9c99d9edf8c96fefd0f6c453859395aa1 Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Mon, 30 Sep 2013 00:03:24 +0200 Subject: Import Nagios Plugins site Import the Nagios Plugins web site, Cronjobs, infrastructure scripts, and configuration files. --- web/attachments/183683-check_pgsql.c | 164 +++++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 web/attachments/183683-check_pgsql.c (limited to 'web/attachments/183683-check_pgsql.c') diff --git a/web/attachments/183683-check_pgsql.c b/web/attachments/183683-check_pgsql.c new file mode 100644 index 0000000..0879cf6 --- /dev/null +++ b/web/attachments/183683-check_pgsql.c @@ -0,0 +1,164 @@ +/***************************************************************** + * + * Program: check_pgsql.c + * License: GPL + * + * Written by J. Javier Sianes - skyo@rotxa.org + * Based on MySQL check program written by Tim Weippert + * + * Command line: check_pgsql user passwd [db] [port] + * can be the FQDN or the IP-Adress + * [db] and [port] are optional + * + * Description: + * + * This plugin attempts to connect to an PostgreSQL Server + * with the optional specified parameters db and port. + * + * The plugin returns + * STATE_OK and the Version Number of the Server when all is fine + * STATE_CRITICAL if the Connection can't be esablished + * STATE_WARNING if the connection was established but the + * program can't get the Versoin Number + * STATE_UNKNOWN if to many parameters are given + * + * Copyright (c) 2006 by J. Javier Sianes + * + *******************************************************************/ + +/***************************************************************** + * + * Note that all includes are related to Nagios and PostgreSQL default installation. + * If you have installed them on a different location, you may change + * the following include lines in order to make it works. + * + * To compile the agent, in a shell use the following command, for example: + * + * gcc -lpq -o check_pgsql check_pgsql.c + * + *******************************************************************/ + +#include +#include +#include "/usr/include/nagios/config.h" +#include "/usr/include/nagios/common.h" +#include "/usr/include/nagios/nagios.h" +#include "/usr/include/libpq-fe.h" +#define TAM_BLK 1024 + +PGconn *ConexionPG=NULL; + +char *PGVersion(int version) { + int i=0,j=0,k=0,len,par=1,cont=0; + char *cad=NULL,*res=NULL,num[3]; + + cad=(char *)malloc(TAM_BLK*sizeof(char)); + res=(char *)malloc(TAM_BLK*sizeof(char)); + if ((cad==NULL)||(res==NULL)){ + free(cad); + free(res); + return NULL; + } + + for (k=0;k 6 ) || ( argc < 4 ) ) { + printf("Incorrect number of arguments supplied - %i .\n", argc); + printf("Usage: %s user passwd [db] [port]\n", argv[0]); + free(PgConnectChain); + return STATE_UNKNOWN; + } + + host = argv[1]; + user = argv[2]; + passwd = argv[3]; + (db = argv[4]) || (db = "postgres"); + if (argc==6) { mport = atoi(argv[5]); } else { mport = 5432; } + + sprintf(PgConnectChain, "host=%s port=%d dbname=%s user=%s password=%s",host,mport,db,user,passwd); + ConexionPG=PQconnectdb(PgConnectChain); + + if (PQstatus(ConexionPG) != CONNECTION_OK) + { + printf("PGSQL ERROR - Connection to database '%s' failed: %s",db,PQerrorMessage(ConexionPG)); + free(PgConnectChain); + PQfinish(ConexionPG); + return STATE_CRITICAL; + } + + version=PQserverVersion(ConexionPG); + if (version > 0 ) { + cversion=PGVersion(version); + if (cversion==NULL) { + printf("PGSQL UNKNOWN - Not enough memory to allocate data !"); + free(PgConnectChain); + PQfinish(ConexionPG); + return STATE_UNKNOWN; + } + printf("PGSQL OK - Running Version: postmaster (PostgreSQL) %s\n",cversion); + } else { + printf("Connect OK, but can't get Serverinfo ... something wrong !\n"); + free(PgConnectChain); + PQfinish(ConexionPG); + return STATE_WARNING; + } + + free(PgConnectChain); + PQfinish(ConexionPG); + return STATE_OK; +} -- cgit v1.2.3-74-g34f1