diff options
Diffstat (limited to 'web/attachments/183683-check_pgsql.c')
-rw-r--r-- | web/attachments/183683-check_pgsql.c | 164 |
1 files changed, 164 insertions, 0 deletions
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 @@ | |||
1 | /***************************************************************** | ||
2 | * | ||
3 | * Program: check_pgsql.c | ||
4 | * License: GPL | ||
5 | * | ||
6 | * Written by J. Javier Sianes - skyo@rotxa.org | ||
7 | * Based on MySQL check program written by Tim Weippert | ||
8 | * | ||
9 | * Command line: check_pgsql <host> user passwd [db] [port] | ||
10 | * <host> can be the FQDN or the IP-Adress | ||
11 | * [db] and [port] are optional | ||
12 | * | ||
13 | * Description: | ||
14 | * | ||
15 | * This plugin attempts to connect to an PostgreSQL Server | ||
16 | * with the optional specified parameters db and port. | ||
17 | * | ||
18 | * The plugin returns | ||
19 | * STATE_OK and the Version Number of the Server when all is fine | ||
20 | * STATE_CRITICAL if the Connection can't be esablished | ||
21 | * STATE_WARNING if the connection was established but the | ||
22 | * program can't get the Versoin Number | ||
23 | * STATE_UNKNOWN if to many parameters are given | ||
24 | * | ||
25 | * Copyright (c) 2006 by J. Javier Sianes | ||
26 | * | ||
27 | *******************************************************************/ | ||
28 | |||
29 | /***************************************************************** | ||
30 | * | ||
31 | * Note that all includes are related to Nagios and PostgreSQL default installation. | ||
32 | * If you have installed them on a different location, you may change | ||
33 | * the following include lines in order to make it works. | ||
34 | * | ||
35 | * To compile the agent, in a shell use the following command, for example: | ||
36 | * | ||
37 | * gcc -lpq -o check_pgsql check_pgsql.c | ||
38 | * | ||
39 | *******************************************************************/ | ||
40 | |||
41 | #include <stdio.h> | ||
42 | #include <stdlib.h> | ||
43 | #include "/usr/include/nagios/config.h" | ||
44 | #include "/usr/include/nagios/common.h" | ||
45 | #include "/usr/include/nagios/nagios.h" | ||
46 | #include "/usr/include/libpq-fe.h" | ||
47 | #define TAM_BLK 1024 | ||
48 | |||
49 | PGconn *ConexionPG=NULL; | ||
50 | |||
51 | char *PGVersion(int version) { | ||
52 | int i=0,j=0,k=0,len,par=1,cont=0; | ||
53 | char *cad=NULL,*res=NULL,num[3]; | ||
54 | |||
55 | cad=(char *)malloc(TAM_BLK*sizeof(char)); | ||
56 | res=(char *)malloc(TAM_BLK*sizeof(char)); | ||
57 | if ((cad==NULL)||(res==NULL)){ | ||
58 | free(cad); | ||
59 | free(res); | ||
60 | return NULL; | ||
61 | } | ||
62 | |||
63 | for (k=0;k<TAM_BLK;k++) { cad[k]='\0'; res[k]='\0'; } | ||
64 | for (k=0;k<3;k++) { num[k]='\0'; } | ||
65 | |||
66 | sprintf(cad,"%d",version); | ||
67 | len=strlen(cad); | ||
68 | if (len%2!=0) { par=0; } | ||
69 | |||
70 | for (i=0;i<len;i++) { | ||
71 | if (cont==2) { | ||
72 | cont=0; | ||
73 | if (num[1]=='0') { | ||
74 | res[j++]=num[0]; | ||
75 | res[j++]='.'; | ||
76 | } else { | ||
77 | res[j++]=num[0]; | ||
78 | res[j++]=num[1]; | ||
79 | res[j++]='.'; | ||
80 | } | ||
81 | for (k=0;k<3;k++) { num[k]='\0'; } | ||
82 | } | ||
83 | |||
84 | num[cont]=cad[i]; | ||
85 | |||
86 | if (i==(len-1)) { | ||
87 | if (par) { | ||
88 | res[j++]=num[0]; | ||
89 | res[j++]=num[1]; | ||
90 | } else { | ||
91 | res[j++]=num[0]; | ||
92 | } | ||
93 | } | ||
94 | |||
95 | cont++; | ||
96 | } | ||
97 | |||
98 | free(cad); | ||
99 | return res; | ||
100 | } | ||
101 | |||
102 | int main(int argc, char **argv) | ||
103 | { | ||
104 | uint i = 0; | ||
105 | int version,mport; | ||
106 | char *host; | ||
107 | char *user; | ||
108 | char *passwd; | ||
109 | char *db; | ||
110 | char *PgConnectChain=NULL; | ||
111 | char *cversion=NULL; | ||
112 | |||
113 | PgConnectChain=(char *)malloc(TAM_BLK*sizeof(char)); | ||
114 | |||
115 | if (PgConnectChain==NULL) { | ||
116 | printf("PGSQL UNKNOWN - Not enough memory to allocate data !"); | ||
117 | return STATE_UNKNOWN; | ||
118 | } | ||
119 | |||
120 | if ( ( argc > 6 ) || ( argc < 4 ) ) { | ||
121 | printf("Incorrect number of arguments supplied - %i .\n", argc); | ||
122 | printf("Usage: %s <host> user passwd [db] [port]\n", argv[0]); | ||
123 | free(PgConnectChain); | ||
124 | return STATE_UNKNOWN; | ||
125 | } | ||
126 | |||
127 | host = argv[1]; | ||
128 | user = argv[2]; | ||
129 | passwd = argv[3]; | ||
130 | (db = argv[4]) || (db = "postgres"); | ||
131 | if (argc==6) { mport = atoi(argv[5]); } else { mport = 5432; } | ||
132 | |||
133 | sprintf(PgConnectChain, "host=%s port=%d dbname=%s user=%s password=%s",host,mport,db,user,passwd); | ||
134 | ConexionPG=PQconnectdb(PgConnectChain); | ||
135 | |||
136 | if (PQstatus(ConexionPG) != CONNECTION_OK) | ||
137 | { | ||
138 | printf("PGSQL ERROR - Connection to database '%s' failed: %s",db,PQerrorMessage(ConexionPG)); | ||
139 | free(PgConnectChain); | ||
140 | PQfinish(ConexionPG); | ||
141 | return STATE_CRITICAL; | ||
142 | } | ||
143 | |||
144 | version=PQserverVersion(ConexionPG); | ||
145 | if (version > 0 ) { | ||
146 | cversion=PGVersion(version); | ||
147 | if (cversion==NULL) { | ||
148 | printf("PGSQL UNKNOWN - Not enough memory to allocate data !"); | ||
149 | free(PgConnectChain); | ||
150 | PQfinish(ConexionPG); | ||
151 | return STATE_UNKNOWN; | ||
152 | } | ||
153 | printf("PGSQL OK - Running Version: postmaster (PostgreSQL) %s\n",cversion); | ||
154 | } else { | ||
155 | printf("Connect OK, but can't get Serverinfo ... something wrong !\n"); | ||
156 | free(PgConnectChain); | ||
157 | PQfinish(ConexionPG); | ||
158 | return STATE_WARNING; | ||
159 | } | ||
160 | |||
161 | free(PgConnectChain); | ||
162 | PQfinish(ConexionPG); | ||
163 | return STATE_OK; | ||
164 | } | ||