vi mqa_thread_count.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OK 0
#define WARNING 1
#define CRITICAL 2
#define UNKNOWN 3
#define LEN 1023
#define CMD "ps -efL | grep mqa.InitAdapter | grep -v grep | wc -l"
int exitstatus=OK;
char *exit_status[4]={"OK","WARNING","CRITICAL","UNKNOWN"};
char status_information[LEN];
char performance_data[LEN];
int parse_cmd() {
int ret;
FILE *fp;
char readbuf[1024];
int i;
char *p,*str;
fp=popen(CMD,"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");
}
if(atoi(readbuf)<50) {
exitstatus=OK;
}
else if(atoi(readbuf)>=50 && atoi(readbuf)<100) {
exitstatus=WARNING;
}
else if(atoi(readbuf)>100) {
exitstatus=CRITICAL;
}
sprintf(status_information,"tomcat_threads=%s",readbuf);
sprintf(performance_data,"tomcat_threads=%s;;;;",readbuf);
// }
ret=pclose(fp);
if(fp==NULL) {
fprintf(stderr,"pclose() error.\n");
return -1;
}
}
int main() {
int ret;
ret=parse_cmd();
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("%s: %s | %s\n",exit_status[exitstatus],status_information,performance_data);
return exitstatus;
}
本文介绍了一个用于监控Tomcat应用服务器线程数目的Shell脚本。该脚本通过执行特定命令获取当前活动线程数量,并根据数目返回不同的状态码,包括OK、WARNING和CRITICAL,帮助管理员及时发现并解决性能瓶颈。
2572

被折叠的 条评论
为什么被折叠?



