nagios插件之监控udp端口状态

使用方法:check_udp_port udp port

vi check_udp_port.c

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

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

#define LEN 1000

//#define TCL_CMD "/home/weihu/tcl/"
#define TCL_CMD "/usr/local/nagios/libexec/"

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

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

//
char loss_packet[32];
char ping_min[32];
char ping_avg[32];
char ping_max[32];
char ping_unit[32];

int mark=0;
char readbuf[LEN];

//int parse_status(char *sh_cmd,char *active_status,char *active_ip_addr) {
int parse_status(char *sh_cmd,char *argv1_str,char *argv2_str) {
	int ret;
        FILE *fp;

        char *p,*str;

	int line=0;
	int mark1=0;
	int mark2=0;

	int i;

//	fp=popen("/home/neo/check_log/tcl/auto_ssh.sh","r");
        fp=popen(sh_cmd,"r");
        if(fp==NULL) {
                fprintf(stderr,"popen() error. ");
                exitstatus=CRITICAL;
                printf("%s: - %s | %s\n",exit_status[exitstatus],status_information,performance_data);
                exit(exitstatus);
        }

        while(fgets(readbuf,LEN,fp)!=NULL) {
		line++;
	//	printf("line=%d,readbuf=%s",line,readbuf);
		
		if(strstr(readbuf,argv1_str) && strstr(readbuf,argv2_str)) {
			mark=1;
		//	printf("---------------------------------------------------------------------\n");

			//delete "\n"
			i=strlen(readbuf);
			if(readbuf[i-1]=='\n') {
				readbuf[i-1]='\0';
			}
		}
        }

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

	return 0;
}

int main(int argc, char *argv[]) {
        int ret;
	char sh_cmd[LEN];

	char active_status[LEN];
	char active_ip_addr[LEN];

	char argv1_str[LEN];
	char argv2_str[LEN];

	if(argc<3) {
		printf("%s %s\n",argv[0],"udp port");
		exit(-1);
	}

	strcpy(argv1_str,argv[1]);
	strcpy(argv2_str,argv[2]);
//	printf("argv1_str=%s,argv2_str=%s\n",argv1_str,argv2_str);

//	sprintf(sh_cmd,"%s%s",TCL_CMD,argv[1]);
//	sprintf(sh_cmd,"%s%s%s","netstat -utupl | grep ",argv[1],argv[2]);
//	sprintf(sh_cmd,"%s%s","netstat -ntul | grep -w ",argv[1]);
	sprintf(sh_cmd,"%s%s","netstat -nul | grep -w ",argv[1]);
//	sprintf(sh_cmd,"%s%s","| grep",argv[2]);
	strcat(sh_cmd,"| grep -w ");
	strcat(sh_cmd,"\"");
	strcat(sh_cmd,argv[2]);
	strcat(sh_cmd,"\"");
//	printf("sh_cmd=%s\n",sh_cmd);

//	ret=parse_status(sh_cmd,active_status,active_ip_addr);
	ret=parse_status(sh_cmd,argv1_str,argv1_str);
	if(ret!=0) {
		fprintf(stderr,"parse_status() error.\n");
		exit(-1);
	}	

//	printf("active_status=%s\n",active_status);
//	printf("active_ip_addr=%s\n",active_ip_addr);

	memset(status_information,0,LEN);
	memset(performance_data,0,LEN);

	if(mark==1) {
		exitstatus=OK;	
		sprintf(status_information,"%s",readbuf);
	}
	else if(mark==0) {
		exitstatus=CRITICAL;	
		sprintf(status_information,"%s%s",argv[2]," is not exist.");
	}
	else	{
		exitstatus=UNKNOWN;	
		sprintf(status_information,"%s%s",argv[2]," is unknow.");
	}



//	sprintf(performance_data,"rta=%s%s;200.000;500.000;0; pl=%s;40;80;; rtmax=%s%s;;;; rtmin=%s%s;;;;",ping_avg,ping_unit,loss_packet,ping_max,ping_unit,ping_min,ping_unit); 

	//|rta=0.056ms;200.000;500.000;0; pl=0%;40;80;; rtmax=0.084ms;;;; rtmin=0.029ms;;;; 

/*
	if(strstr(argv[1],"80_49")) {
        	printf("%s - 10.7.0.186: %s | %s\n",exit_status[exitstatus],status_information,performance_data);
	}

	if(strstr(argv[1],"80_50")) {
        	printf("%s - 10.7.0.195: %s | %s\n",exit_status[exitstatus],status_information,performance_data);
	}
*/

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

        return exitstatus;
}


你可能感兴趣的:(nagios插件之监控udp端口状态)