summaryrefslogtreecommitdiffstats
path: root/web/attachments/183683-check_pgsql.c
blob: 0879cf6b3f1adc229ed768614f50b912d1369a8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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 <host> user passwd [db] [port]
 *      <host> 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 <stdio.h>
#include <stdlib.h>
#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<TAM_BLK;k++) { cad[k]='\0'; res[k]='\0'; } 
  for (k=0;k<3;k++) { num[k]='\0'; }

  sprintf(cad,"%d",version);
  len=strlen(cad);
  if (len%2!=0) { par=0; }

  for (i=0;i<len;i++) {
    if (cont==2) {
      cont=0;
      if (num[1]=='0') {
        res[j++]=num[0];
        res[j++]='.';
      } else {
        res[j++]=num[0];
        res[j++]=num[1];
        res[j++]='.';
      }
      for (k=0;k<3;k++) { num[k]='\0'; }
    }

    num[cont]=cad[i];
    
    if (i==(len-1)) {
      if (par) {
        res[j++]=num[0];
        res[j++]=num[1];
      } else {
        res[j++]=num[0];
      }
    }

    cont++; 
  }

  free(cad);
  return res;
}

int main(int argc, char **argv)
{
  uint i = 0;
  int version,mport;
  char *host;
  char *user;
  char *passwd;
  char *db;
  char *PgConnectChain=NULL;
  char *cversion=NULL;
  
  PgConnectChain=(char *)malloc(TAM_BLK*sizeof(char));

  if (PgConnectChain==NULL) {
    printf("PGSQL UNKNOWN - Not enough memory to allocate data !");
    return STATE_UNKNOWN;
  }

  if ( ( argc > 6 ) || ( argc < 4 ) ) {
    printf("Incorrect number of arguments supplied - %i .\n", argc);
    printf("Usage: %s <host> 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;
}