| 12
 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
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 
 | /*
--------------------------------Headline----------------------------------
 
System/Module:
 
Name:  check_portail.c
 
Created by XXX
Creation date           03 Jul 2007
Version
 
Purpose:
Plugin permettant de tester le bon fonctionnement du portail
Prend en parametre :
 - l'url contenant l'@ DNS ou @ IP du server Web ainsi que le chemin vers
 le fichier de test (attention ne pas faire preceder l'adresse de http://)
 - les seuils critiques et de warning qui sont utilises pour pour tester
 les delais de transmission
 
 
 
*/
 
 
/*------------------------------Includes--------------------------------*/
 
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netdb.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/times.h>
#include <time.h>
 
#include <common.h>
 
 
/*------------------------------Defines---------------------------------*/
 
/*------------------------------Typedefs--------------------------------*/
 
/*----------------------------Internal data-----------------------------*/
 
const char *HTTP_OK = "200";
const char *HTTP_NOTFOUND = "404";
const char *path = "supervision/test.php";
 
 
/*----------------------------Exported data-----------------------------*/
 
/*--------------------Internal functions prototypes---------------------*/
 
 
 
/* Affichage de l'ecran d'aide si probleme lors de l'execution du check_portail */
 
void print_help() {
	fprintf(stdout,"\ncheck_portail : permet de tester le fonctionnement du portail\n");
	fprintf(stdout,"usage : check_portail -H <host> [-h -w ARGS -c ARGS]\n");
	fprintf(stdout,"        -H HOST          host ( ex : www.host.com ) \n");
	/*fprintf(stdout,"        -f FILE          file ( ex : test.php ) \n");*/
	fprintf(stdout,"----------------------------------------------------\n");
	fprintf(stdout,"options :\n");
	fprintf(stdout,"        -h               print this message\n");
	fprintf(stdout,"        -c ARGS          set critical values\n");
	fprintf(stdout,"        -w ARGS          set warning values\n");
}
 
 
/* Fonction qui prend en argument une chaine de caractere et un delimiteur
   et qui decoupe la chaine en sous chaine separe par le delimiteur et
   range ces sous chaines dans un tableau de chaine de caractere et
   positionne la derniere case du table de chaine a NULL */
 
char **str_split (char *s, const char *ct)
{
	char **tab = NULL;
 
	if (s && ct)
	{
		int i;
		char *cs = NULL;
		size_t size = 1;
 
		/* (1) */
		for (i = 0; (cs = strtok (s, ct)); i++)
		{
			if (size <= i + 1)
			{
				void *tmp = NULL;
 
				/* (2) */
				size <<= 1;
				tmp = realloc (tab, sizeof (*tab) * size);
				if (tmp)
				{
					tab = tmp;
				}
				else
				{
					fprintf (stderr, "Memoire insuffisante\n");
					free (tab);
					tab = NULL;
					exit (EXIT_FAILURE);
				}
			}
			/* (3) */
			tab[i] = cs;
			s = NULL;
		}
		tab[i] = NULL;
	}
	return tab;
}
 
 
 
 
/* ¨
   Programme principal
   Se charge d'extraire le nom du serveur ainsi que le fichier a aller
   lire puis se connecte sur le port 80 du serveur et effectue une
   requete http demandant le nom du fichier qu'il desire
   Il attend la reponse, et lit le 4 premiers octets du corps du
   document pour pouvoir verifier si ce sont bien les memes qu'il a
   envoye en parametre et renvoie le message en fonction du resultat
   */
 
 
 
 
 
int main(int argc,char *argv[])
{
 
	int state = STATE_UNKNOWN; /* Etat par defaut */
	int i;
	int ok, not_found;
	struct timeval t1,t2,total;
	//char **url;
	char *host;
	char *file;
 
 
 
	/* Variables pour le transfert HTTP */
	struct sockaddr_in serv_addr;
	struct hostent *he;					/* pour l'url */
	int sockfd,n,portno=80,httprequestlen,httpheaderlen=1;
	char *httprequest;
	httprequest = (char *) malloc(256*sizeof(char)); /* Laisse une taille raisonnable(?) de 178 char pour l'url */
	char *httpheader;
	httpheader = (char *) malloc(sizeof(char));
	memset((char *) httpheader, '\0', sizeof(char));
	char *httpheadertmp;
	char hbuf[4] = {0,0,0,0};
 
 
	/* Variables pour la gestion des options */
	opterr = 0;
	int c;
	char *wvalue = NULL;
	int wflag = 0 ;
	char *cvalue = NULL;
	int cflag = 0 ;
	char *host_value = NULL;
	int host_flag = 0;		// hostname
	char *hvalue = NULL;
	int hflag = 0;
	/*char *fvalue = NULL;
	  int fflag = 0;*/
 
	/* Initialisation de la fonction aleatoire, combinee avec la date epoch pour avoir un seed vraiment aleatoire */
	srand(time(0));
 
	/* Gestion des options a l'aide de la fonction getopt, suivant les recommandations Nagios*/
	/*while ((c = getopt(argc,argv,"hu:c:w:H:f:")) != -1)*/
	while ((c = getopt(argc,argv,"hu:c:w:H:")) != -1)
	{
		switch (c)
		{
			case 'w' :
				wvalue = optarg;
				wflag = 1;
				break;
			case 'c' :
				cvalue = optarg;
				cflag = 1;
				break;
			case 'H' :
				host_value = optarg;
				host_flag = 1;
				break;
			case 'h' :
				hvalue = optarg;
				hflag = 1;
				break;
				/*case 'f' :
				  fvalue = optarg;
				  fflag = 1;
				  break;*/
			case '?' :
				if (isprint(optopt))
					fprintf(stderr,"Option inconnue -%c.\n",optopt);
				else
					fprintf(stderr,"Caractere d'option inconnu \\x%x.\n",optopt);
				break;
			default :
				abort();
		}
	}
 
 
	/* Affiche le message d'aide */
	if (hflag) {
		print_help();
		return 0;
	}
 
	/* si l'utilisateur n'a pas donne l'adresse ou se connecter ou qu'il veut l'aide*/
	/*if ( !host_flag || !cflag || hflag || !fflag )*/
	if ( !host_flag || !cflag || hflag )
	{
		print_help();
		return 0;
	}
 
	host = host_value;
	/*	file = fvalue;*/
	/* On inscrit un nombre entier aleatoire au lieu du parametre f */
	file = (char *) malloc(5*sizeof(char));
	memset(file,0,5*sizeof(char));
	snprintf(file,5,"%i",rand());
 
	/* on recupere les informations sur l'hote */
	if ((he=gethostbyname(host)) == NULL) {
		printf("CRITICAL, La page de test du Portail est inacessible (Host inacessible)\n");
		return STATE_CRITICAL;
	}
 
 
	/* Ouverture d'un socket de categorie internet */
	sockfd = socket(AF_INET,SOCK_STREAM,0);
	if (sockfd < 0) {
		printf("CRITICAL, La page de test du Portail est inacessible\n");
		return STATE_CRITICAL;
	}
 
 
	/* Configuration de la connection, il faut imperativement utiliser une addresse IP comme parametre dans ce programme, genere un warning a la compilation, mais sans danger */
	memset((char *) &serv_addr,'\0',sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	bcopy(he->h_addr_list[0], &serv_addr.sin_addr, he->h_length);        // pour gerer l'url //
	serv_addr.sin_port = htons(portno);
 
 
	/* Connection au serveur */
	if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) {
		printf("CRITICAL, La page de test du Portail est inacessible\n");
		return STATE_CRITICAL;
	}
 
	/* Creation de la requete HTTP */
	/* Note au sujet d'HTTP : en l'absence d'utilisation de nom d'hotes/domaine, c'est le protocole http 1.0 et non 1.1 qui doit etre utilise (meme si la reponse est en http 1.1 */
 
	httprequestlen = sprintf(httprequest,"GET /%s?param=%s HTTP/1.0\r\n\r\n",path,file);
 
	/* Envoi de la requete HTTP */
	n = write(sockfd,httprequest,httprequestlen);
	if (n < 0) {
		printf("CRITICAL, La page de test du Portail est inacessible\n");
		return STATE_CRITICAL;
	}
 
 
 
	/* on recupere l'heure pr calculer le temps de reponse */
	gettimeofday(&t1,NULL);
 
 
	/* On teste le temps de reponse du serveur pour etablir des statistiques */
	n = read(sockfd,NULL,0);
	if (n < 0) {
		printf("CRITICAL, La page de test du Portail est inacessible\n");
		return STATE_CRITICAL;
	}
 
	/* Reception du header de la reponse du serveur */
	while (!((hbuf[0] == 13) && (hbuf[1] == 10) && (hbuf[2] == 13) && (hbuf[3] == 10))) {
		n = read(sockfd,&httpheadertmp,1);
 
 
		if (n < 0) {
			printf("CRITICAL, La page de test du Portail est inacessible\n");
			return STATE_CRITICAL;
		}
 
		httpheaderlen ++;
		httpheader = realloc(httpheader,httpheaderlen*sizeof(char));
		httpheader[httpheaderlen] = (char) httpheadertmp; /* Genere un warning, mais sans danger */
 
		for (i = 0;i < 3;i++)
			hbuf[i] = hbuf[i+1];
		hbuf[3] = (int) httpheadertmp;
	}
 
	/* on recupere l'heure pr calculer le temps de reponse */
	gettimeofday(&t2,NULL);
 
	total.tv_sec = t2.tv_sec-t1.tv_sec;
	total.tv_usec = t2.tv_usec-t1.tv_usec;
 
 
	/* Test du code retour HTTP */
	/* Est ce une erreur 404 : file not found */
	not_found=1;
	for (i = 0;i<3;i++) {
		if (httpheader[i+11] != HTTP_NOTFOUND[i])
			not_found = 0;
	}
 
	if (not_found)
	{    printf("CRITICAL, La page de test du Portail est inacessible, le serveur HTTP est OK\n");
		return STATE_CRITICAL;
	}
 
 
	/* Est ce ok ?? */
	ok=1;
	for (i = 0;i<3;i++) {
		if (httpheader[i+11] != HTTP_OK[i])
			ok = 0;
	}
 
 
	/* Allocation d'un espace memoire pour le document html et liberation de l'espace du header*/
 
	char *res = (char*)malloc(24*sizeof(char));
 
	/* Reception de la payload (charge utile) de la reponse du serveur */
	n = read(sockfd,res,strlen(file));
 
	if (n < 0) {
		printf("CRITICAL, Base de donnees inacessible. Portail inoperant\n");
		return STATE_CRITICAL;
	}
	res[n] = '\0';
 
	/* Pour une raison indeterminee, le premier element du string file est corrompu durant l'execution du programe. Vu l'etat du code, une reecriture from scratch du plugin serait certainement la meilleure solution, mais le temps pressant pour la livraison, il est efficace de mettre de cote ce caractere (il en reste 11 autres a verifier, ce qui est amplement suffisant */
 
	/* test sur le delai de reception */
	if (!strcmp(res,file))
	{
 
		// delai >= seuil critique
		if ( total.tv_sec >= atoi(cvalue) )
		{
			printf("CRITICAL, Temps de reponse du Portail %ld\n",total.tv_sec);
			return STATE_CRITICAL;
		}
 
		// delai > seuil warning
		else if ( total.tv_sec > atoi(wvalue))
		{
			printf("WARNING, Temps de reponse du Portail %ld\n",total.tv_sec);
			return STATE_WARNING;
		}
 
		// delai OK
		else {
			printf("OK, Temps de reponse du Portail %ld\n",total.tv_sec);
			return STATE_OK;
		}
	}
 
	else {
		printf("CRITICAL, Base de donnees inacessible. Portail inoperant\n");
		return STATE_CRITICAL;
	}
 
	/* On peut fermer le socket */
	close (sockfd);
 
 
	return state;
} | 
Partager