/***************************************************************** * * 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; }