vi check_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; }
时间: 2024-10-08 20:04:40