nagios插件之计算vp当前通话量

vi check_vp_call.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

#define OK       0   
#define WARNING  1   
#define CRITICAL 2   
#define UNKNOWN  3   

#define LEN 1023 

#define LOG_FILE "/home/craft/check_log/check_vp/temp.txt"

int main(void) {
	FILE *fp;
	int ret;
	int fd;

	char *p,*str;
	char readbuf[LEN];

	int m,n;
	int max=0;
	int call_max=0;
	int session_max=0;

	int i=0,j=0,k=0;

	char *temp;

	char *date[LEN];
	char *session[LEN];
	char *call[LEN];

	struct tm *tm_p;
	time_t timep;
	char year[LEN];
	char new_filename[LEN];
	char call_filename[LEN];

	int exitstatus=OK;

	char *exit_status[4]={"OK","WARNING","CRITICAL","UNKNOWN"}; 

	char status_information[LEN];
	char performance_data[LEN];


	fp=fopen(LOG_FILE,"r+");
	if(fp==NULL) {
		fprintf(stderr,"fopen() error. ");
		exitstatus=CRITICAL;
		printf("%s: - %s | %s\n",exit_status[exitstatus],status_information,performance_data);
		return exitstatus;
	}

	fd=fileno(fp);

	ret=lockf(fd,F_LOCK,0);
	if(ret==-1) {
		perror("lockf() F_LOCK");
		exit(-1);
	}
	else {
		timep=time(NULL);
		tm_p=localtime(&timep);

	//	printf("%s\n",new_filename);
	//
	//	printf("%s\n",call_filename);
		
		sprintf(year,"%d",1900+tm_p->tm_year);
	//	printf("year=%s\n",year);

		while(fgets(readbuf,LEN,fp)!=NULL) {
		//	if(strstr(readbuf,"2013")) {
			if(strstr(readbuf,year)) {
				//	for(p=strtok(readbuf," \n");p;p=strtok(NULL," \n"))
				//		str=p;

				temp=(char *)malloc(128);

				//	m=strlen(readbuf);
				readbuf[strlen(readbuf)-1]='\0';
				strcpy(temp,readbuf);

				date[i++]=temp;
			}
			else if(strstr(readbuf,"session")) {
				for(p=strtok(readbuf," \n");p;p=strtok(NULL," \n"))
					str=p;

				temp=(char *)malloc(128);

				strcpy(temp,str);

				session[j++]=temp;
			}
			else if(strstr(readbuf,"call")) {
				for(p=strtok(readbuf," \n");p;p=strtok(NULL," \n")) 
					str=p;

				temp=(char *)malloc(128);

				strcpy(temp,str);

				call[k++]=temp;
			}
		}
	}

//	unlink file
	ret=unlink(LOG_FILE);
	if(ret==-1) {
		perror("unlink()");
	}

//	unlock
	ret=lockf(fd,F_ULOCK,0);
	if(ret==-1) {
		perror("lockf() F_ULOCK");
		exit(-1);
	}

	ret=fclose(fp);
	if(ret==EOF) {
		fprintf(stderr,"fclose() error.\n");
		exit(-1);
	}

	//	printf("i=%d\n",i);

	//	for(n=0;n<i;n++)
	//		printf("%s,%s,%s\n",date[n],session[n],call[n]);

	//----------------------------------------

	//	printf("max=%d\n",max);

	for(n=0;n<i;n++) {
		if(atoi(call[n])>=max) {
			max=atoi(call[n]);
			m=i;
		}

	}

	//	printf("m=%d\n",m);
	//	printf("max=%d\n",max);

	for(n=0;n<i;n++) {
		if(atoi(call[n])==max) {
			//	printf("%s,session=%s,call=%s\n",date[n],session[n],call[n]);

			if(atoi(session[n])>=session_max) {
				call_max=n;
				session_max=atoi(session[n]);
			}
		}
	}

	sprintf(status_information,"current call=%s, session=%s",call[call_max],session[call_max]);
	//	printf("status_information=%s\n",status_information);

	sprintf(performance_data,"call=%s;;;; session=%s;;;;",call[call_max],session[call_max]);
	//	printf("performance_data=%s\n",performance_data);

	printf("%s: %s | %s\n",exit_status[exitstatus],status_information,performance_data);

	return exitstatus;
}

你可能感兴趣的:(nagios插件之计算vp当前通话量)