Back to index

nagios-plugins  1.4.16
Defines | Enumerations | Functions | Variables
check_pgsql.c File Reference
#include "common.h"
#include "utils.h"
#include "netutils.h"
#include <libpq-fe.h>
#include <pg_config_manual.h>

Go to the source code of this file.

Defines

#define DEFAULT_DB   "template1"
#define DEFAULT_HOST   "127.0.0.1"

Enumerations

enum  { DEFAULT_PORT = 5432, DEFAULT_WARN = 2, DEFAULT_CRIT = 8 }

Functions

int process_arguments (int, char **)
int validate_arguments (void)
void print_usage (void)
void print_help (void)
int is_pg_dbname (char *)
int is_pg_logname (char *username)
 the tango program should eventually create an entity here based on the function prototype
int main (int argc, char **argv)

Variables

const char * progname = "check_pgsql"
const char * copyright = "1999-2007"
const char * email = "nagiosplug-devel@lists.sourceforge.net"
char * pghost = NULL
char * pgport = NULL
int default_port = DEFAULT_PORT
char * pgoptions = NULL
char * pgtty = NULL
char dbName [NAMEDATALEN] = DEFAULT_DB
char * pguser = NULL
char * pgpasswd = NULL
double twarn = (double)DEFAULT_WARN
double tcrit = (double)DEFAULT_CRIT
int verbose = 0
PGconn * conn

Define Documentation

#define DEFAULT_DB   "template1"

Definition at line 42 of file check_pgsql.c.

#define DEFAULT_HOST   "127.0.0.1"

Definition at line 43 of file check_pgsql.c.


Enumeration Type Documentation

anonymous enum
Enumerator:
DEFAULT_PORT 
DEFAULT_WARN 
DEFAULT_CRIT 

Definition at line 45 of file check_pgsql.c.

     {
       DEFAULT_PORT = 5432,
       DEFAULT_WARN = 2,
       DEFAULT_CRIT = 8
};

Function Documentation

int is_pg_dbname ( char *  dbname)

Definition at line 356 of file check_pgsql.c.

{
       char txt[NAMEDATALEN];
       char tmp[NAMEDATALEN];
       if (strlen (dbname) > NAMEDATALEN - 1)
              return (FALSE);
       strncpy (txt, dbname, NAMEDATALEN - 1);
       txt[NAMEDATALEN - 1] = 0;
       if (sscanf (txt, "%[_a-zA-Z]%[^_a-zA-Z0-9-]", tmp, tmp) == 1)
              return (TRUE);
       if (sscanf (txt, "%[_a-zA-Z]%[_a-zA-Z0-9-]%[^_a-zA-Z0-9-]", tmp, tmp, tmp) ==
                     2) return (TRUE);
       return (FALSE);
}
int is_pg_logname ( char *  username)

the tango program should eventually create an entity here based on the function prototype

@- <sect3> <title>is_pg_logname</title>

Given a username, this function returns TRUE if the string is a valid PostgreSQL username, and returns false if it is not. Valid PostgreSQL usernames are less than characters long and consist of letters, numbers, dashes, and underscores, plus possibly some other characters.

Currently this function only checks string length. Additional checks should be added.

</sect3> -@

Definition at line 398 of file check_pgsql.c.

{
       if (strlen (username) > NAMEDATALEN - 1)
              return (FALSE);
       return (TRUE);
}
int main ( int  argc,
char **  argv 
)

Definition at line 133 of file check_pgsql.c.

{
       int elapsed_time;
       int status = STATE_UNKNOWN;

       /* begin, by setting the parameters for a backend connection if the
        * parameters are null, then the system will try to use reasonable
        * defaults by looking up environment variables or, failing that,
        * using hardwired constants */

       pgoptions = NULL;  /* special options to start up the backend server */
       pgtty = NULL;      /* debugging tty for the backend server */

       setlocale (LC_ALL, "");
       bindtextdomain (PACKAGE, LOCALEDIR);
       textdomain (PACKAGE);

       /* Parse extra opts if any */
       argv=np_extra_opts (&argc, argv, progname);

       if (process_arguments (argc, argv) == ERROR)
              usage4 (_("Could not parse arguments"));
       if (verbose > 2)
              printf("Arguments initialized\n");

       /* Set signal handling and alarm */
       if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
              usage4 (_("Cannot catch SIGALRM"));
       }
       alarm (timeout_interval);

       if (verbose)
              printf("Connecting to database:\n DB: %s\n User: %s\n Host: %s\n Port: %d\n", dbName,
              (pguser != NULL) ? pguser : "unspecified",
              (pghost != NULL) ? pghost : "unspecified",
              (pgport != NULL) ? atoi(pgport) : DEFAULT_PORT);

       /* make a connection to the database */
       time (&start_time);
       conn =
              PQsetdbLogin (pghost, pgport, pgoptions, pgtty, dbName, pguser, pgpasswd);
       time (&end_time);
       elapsed_time = (int) (end_time - start_time);
       if (verbose)
              printf("Time elapsed: %d\n", elapsed_time);

       /* check to see that the backend connection was successfully made */
       if (verbose)
              printf("Verifying connection\n");
       if (PQstatus (conn) == CONNECTION_BAD) {
              printf (_("CRITICAL - no connection to '%s' (%s).\n"),
                      dbName,      PQerrorMessage (conn));
              PQfinish (conn);
              return STATE_CRITICAL;
       }
       else if (elapsed_time > tcrit) {
              status = STATE_CRITICAL;
       }
       else if (elapsed_time > twarn) {
              status = STATE_WARNING;
       }
       else {
              status = STATE_OK;
       }
       if (verbose)
              printf("Closing connection\n");
       PQfinish (conn);
       printf (_(" %s - database %s (%d sec.)|%s\n"),
               state_text(status), dbName, elapsed_time,
               fperfdata("time", elapsed_time, "s",
                        (int)twarn, twarn, (int)tcrit, tcrit, TRUE, 0, FALSE,0));
       return status;
}

Here is the call graph for this function:

void print_help ( void  )
void print_usage ( void  )

Definition at line 389 of file check_cpqarray.c.

{
  printf("cpqarrayd [options]\n");
  printf("   -h         prints this text\n");
  printf("   -d         enables debugging\n");
}
int process_arguments ( int  argc,
char **  argv 
)
int validate_arguments ( void  )

Variable Documentation

PGconn* conn

Definition at line 72 of file check_pgsql.c.

const char* copyright = "1999-2007"

Definition at line 32 of file check_pgsql.c.

char dbName[NAMEDATALEN] = DEFAULT_DB

Definition at line 65 of file check_pgsql.c.

Definition at line 62 of file check_pgsql.c.

const char* email = "nagiosplug-devel@lists.sourceforge.net"

Definition at line 33 of file check_pgsql.c.

char* pghost = NULL

Definition at line 60 of file check_pgsql.c.

char* pgoptions = NULL

Definition at line 63 of file check_pgsql.c.

char* pgpasswd = NULL

Definition at line 67 of file check_pgsql.c.

char* pgport = NULL

Definition at line 61 of file check_pgsql.c.

char* pgtty = NULL

Definition at line 64 of file check_pgsql.c.

char* pguser = NULL

Definition at line 66 of file check_pgsql.c.

const char* progname = "check_pgsql"

Definition at line 31 of file check_pgsql.c.

double tcrit = (double)DEFAULT_CRIT

Definition at line 69 of file check_pgsql.c.

double twarn = (double)DEFAULT_WARN

Definition at line 68 of file check_pgsql.c.

int verbose = 0

Definition at line 70 of file check_pgsql.c.