nagios插件之监控多个tomcat线程数

本文介绍了一个脚本,用于监控并比较五个不同的Apache Tomcat服务(包括客户服务、金融、接口、支付和7.0.41版本)的并发请求数量,并根据预设阈值评估其性能状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vi check_multi_tomcat_threads.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 MIN_LEN 100

#define CMD1 "ps -efL | grep apache-tomcat-cuservice |grep -v grep | wc -l"
#define CMD2 "ps -efL | grep apache-tomcat-finance |grep -v grep | wc -l"
#define CMD3 "ps -efL | grep apache-tomcat-interface |grep -v grep | wc -l"
#define CMD4 "ps -efL | grep apache-tomcat-payment |grep -v grep | wc -l"
#define CMD5 "ps -efL | grep apache-tomcat-7.0.41 |grep -v grep | wc -l"

int i=0;

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

char cuservice_count[MIN_LEN]={0};
char finance_count[MIN_LEN]={0};
char interface_count[MIN_LEN]={0};
char payment_count[MIN_LEN]={0};
char tomcat7041_count[MIN_LEN]={0};

char status_information[LEN]={0};
char performance_data[LEN]={0};

char *cmd_array[]={"ps -efL | grep apache-tomcat-cuservice |grep -v grep | wc -l","ps -efL | grep apache-tomcat-finance |grep -v grep | wc -l","ps -efL | grep apache-tomcat-interface |grep -v grep | wc -l","ps -efL | grep apache-tomcat-payment |grep -v grep | wc -l","ps -efL | grep apache-tomcat-7.0.41 |grep -v grep | wc -l"};


int parse_cmd(char *cmd_array) {
//	printf("%s\n",cmd_array);

        int ret;
        FILE *fp;
        char readbuf[MIN_LEN]={0};

	char cmd_string[MIN_LEN]={0};

//	strncpy(cmd_string,cmd_array,MIN_LEN);
//	sprintf(cmd_string,cmd_array,strlen(cmd_array));
//	printf("cmd_string=%s\n",cmd_string);

//	int i;
//	char *p,*str;

//	memset(readbuf,0,MIN_LEN);

	fp=popen(cmd_array,"r");
//	fp=popen(cmd_string,"r");
        if(fp==NULL) {
                fprintf(stderr,"popen() error.\n");
                return -1;
        }

       // while(fgets(readbuf,1024,fp)!=NULL) {
		/*
                for(p=strtok(readbuf," ");p;p=strtok(NULL," ")) {
                //      str=p;

                        sprintf(status_information,"active call=%s",p);

                        sprintf(performance_data,"call=%s;;;;",p);

                        break;
                }

                break;
		*/

	//	readbuf[strlen(readbuf)-1]=0;
		ret=fscanf(fp,"%s",readbuf);
		if(ret!=1) {
			fprintf(stderr,"fscanf() error.\n");
		}
	//	printf("readbuf=%s\n",readbuf);

	//	printf("i=%d\n",i);
		switch (i) {
			case 0:
				strncpy(cuservice_count,readbuf,MIN_LEN);
			//	printf("0000000000\n");
				break;

			case 1:
				strncpy(finance_count,readbuf,MIN_LEN);
			//	printf("1111111111\n");
				break;

			case 2:
				strncpy(interface_count,readbuf,MIN_LEN);
			//	printf("2222222222\n");
				break;

			case 3:
				strncpy(payment_count,readbuf,MIN_LEN);
			//	printf("3333333333\n");
				break;

			case 4:
				strncpy(tomcat7041_count,readbuf,MIN_LEN);
			//	printf("4444444444\n");
				break;

			default:
			//	printf("5555555555\n");
				break;
		} 

       // }

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

	return 0;
}

int main() {
        int ret;

	// clean 
//	memset(cuservice_count,0,MIN_LEN);
//	memset(finance_count,0,MIN_LEN);
//	memset(interface_count,0,MIN_LEN);
//	memset(payment_count,0,MIN_LEN);
//	memset(tomcat7041_count,0,MIN_LEN);

	for(i=0;i<5;i++) {
		ret=parse_cmd(cmd_array[i]);
		if(ret!=0) {
			fprintf(stderr,"parse_cmd() error.\n");
			// exitstatus=CRITICAL;
			// printf("%s: - %s | %s\n",exit_status[exitstatus],status_information,performance_data);
			exit(-1);
		}
	}

//	printf("cuservice_count=%s\n",cuservice_count);
//	printf("finance_count=%s\n",finance_count);
//	printf("interface_count=%s\n",interface_count);
//	printf("payment_count=%s\n",payment_count);
//	printf("tomcat7041_count=%s\n",tomcat7041_count);

	if(atoi(cuservice_count)<400 && atoi(finance_count)<400 && atoi(interface_count)<400 && atoi(payment_count)<400 && atoi(tomcat7041_count)<400) {
		exitstatus=OK;
	}
	else if(atoi(cuservice_count)>=400 && atoi(cuservice_count)<450 || atoi(finance_count)>=400 && atoi(finance_count)<450 || atoi(interface_count)>=400 && atoi(interface_count)<450 || atoi(payment_count)>=400 && atoi(payment_count)<450 || atoi(tomcat7041_count)>=400 && atoi(tomcat7041_count)<450) {
		exitstatus=WARNING;
	}
	else if(atoi(cuservice_count)>=450 || atoi(finance_count)>=450 || atoi(interface_count)>=450 || atoi(payment_count)>=450 || atoi(tomcat7041_count)>=450) {
		exitstatus=CRITICAL;
	}

	sprintf(status_information,"cuservice_count=%s, finance_count=%s, interface_count=%s, payment_count=%s, tomcat7041_count=%s",cuservice_count,finance_count,interface_count,payment_count,tomcat7041_count);

	sprintf(performance_data,"cuservice_count=%s;400;450;0; finance_count=%s;400;450;0; interface_count=%s;400;450;0; payment_count=%s;400;450;0; tomcat7041_count=%s;400;450;0;",cuservice_count,finance_count,interface_count,payment_count,tomcat7041_count);

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

        return exitstatus;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值