|
执行:sbc_calls_status_new auto_ssh_sbc_10_17.sh | auto_ssh_sbc_11_17.sh
vi sbc_calls_status_new.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define OK 0
#define WARNING 1
#define CRITICAL 2
#define UNKNOWN 3
#define LEN 1000
#define MIN_LEN 20
#define SHORT_TIME 1// unit of minute
#define LONG_TIME 10// unit of minute
//#define TCL_CMD "/home/weihu/tcl/"
#define TCL_CMD "/usr/local/nagios/libexec/"
#define SBC_GG_SHORT_TIME_FILE "/usr/local/nagios/libexec/sbc_gg_call_short_time_file.tmp"
#define SBC_YZ_SHORT_TIME_FILE "/usr/local/nagios/libexec/sbc_yz_call_short_time_file.tmp"
//#define SBC_SHORT_TIME_FILE "/home/tssp/sbc/sbc_call_short_time_file.tmp"
#define SBC_GG_LONG_TIME_FILE "/usr/local/nagios/libexec/sbc_gg_call_long_time_file.tmp"
#define SBC_YZ_LONG_TIME_FILE "/usr/local/nagios/libexec/sbc_yz_call_long_time_file.tmp"
//#define SBC_LONG_TIME_FILE "/home/tssp/sbc/sbc_call_long_time_file.tmp"
int exitstatus=OK;
char *exit_status[4]={"OK","WARNING","CRITICAL","UNKNOWN"};
char status_information[LEN];
char performance_data[LEN];
//char sbc_gg_old_value[MIN_LEN]={0};
//char sbc_yz_old_value[MIN_LEN]={0};
int read_time_file(int minute,char *file) {
int ret;
FILE *fp;
char *ch;
char readbuf[20]={0};
int now_minute=0;
//fp=fopen(file,"a+");
fp=fopen(file,"r+");
if(fp==NULL) {
fprintf(stderr,"fopen() error.\n");
return -1;
}
ch=fgets(readbuf,20,fp);
if(ch==NULL) {
fprintf(stderr,"fgets() error.\n");
//return -1;
}
printf("readbuf=%s\n",readbuf);
now_minute=atoi(readbuf);
printf("now_minute=%d\n",now_minute);
if(!strcmp(readbuf,"")) {
if(minute==SHORT_TIME) {
if(now_minute<SHORT_TIME-1) {
exitstatus=WARNING;
}
else if(now_minute>=SHORT_TIME-1) {
exitstatus=CRITICAL;
}
}
else if(minute==LONG_TIME) {
/*if(now_minute<LONG_TIME-1) {
exitstatus=WARNING;
}
else if(now_minute>=LONG_TIME-1) {
*/
if(now_minute>=LONG_TIME-1) {
exitstatus=CRITICAL;
}
}
fprintf(fp,"%s","1");
printf("file is null,value=1\n");
}
else {
if(minute==SHORT_TIME) {
if(now_minute<SHORT_TIME-1) {
exitstatus=WARNING;
ret=fseek(fp,0,SEEK_SET);
fprintf(fp,"%d",++now_minute);
/*
fclose(fp);
fp=fopen(file,"w+");
fprintf(fp,"%d",now_minute++);
*/
}
else if(now_minute>=SHORT_TIME-1) {
exitstatus=CRITICAL;
ret=fseek(fp,0,SEEK_SET);
fprintf(fp,"%d",++now_minute);
}
}
else if(minute==LONG_TIME) {
if(now_minute<LONG_TIME-1) {
//exitstatus=WARNING;
exitstatus=OK;
ret=fseek(fp,0,SEEK_SET);
fprintf(fp,"%d",++now_minute);
}
else if(now_minute>=LONG_TIME-1) {
//if(now_minute>=LONG_TIME-1) {
exitstatus=CRITICAL;
ret=fseek(fp,0,SEEK_SET);
fprintf(fp,"%d",++now_minute);
}
}
}
ret=fclose(fp);
if(ret!=0) {
fprintf(stderr,"fclose() error.\n");
return -1;
}
return 0;
}
int parse_cmd(char *sh_cmd,char *active_count,char *high_count,char *total_count) {
int ret;
FILE *fp;
char tmpbuf[LEN];
char readbuf[LEN];
char *p,*str;
int i=0;
int line=0;
int mark1=0;
int mark2=0;
//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++;
if(strstr(readbuf,"SIP Sessions")) {
memset(tmpbuf,0,LEN);
strcpy(tmpbuf,readbuf);
//printf("%s",tmpbuf);
for(p=strtok(tmpbuf," ");p;p=strtok(NULL," "),i++) {
if(i==2) {
//printf("%s\n",p);
strcpy(active_count,p);
}
if(i==3) {
//printf("%s\n",p);
strcpy(high_count,p);
}
if(i==4) {
//printf("%s\n",p);
strcpy(total_count,p);
break;
}
}
}
if(i==4) {
break;
}
/*
line++;
//printf("line=%d,readbuf=%s",line,readbuf);
if(line==3) {
for(p=strtok(readbuf," ");p;p=strtok(NULL," ")) {
// str=p;
//Sun
mark1++;
if(mark1==2) {
//printf("p=%s\n",p);
strcpy(active_status,p);
//printf("active_status=%s\n",active_status);
}
}
}
if(line==4) {
for(p=strtok(readbuf," ");p;p=strtok(NULL," /")) {
mark2++;
if(mark2==2) {
//printf("p=%s\n",p);
strcpy(active_ip_addr,p);
//printf("active_ip_addr=%s\n",active_ip_addr);
}
}
break;
}
*/
}
ret=fclose(fp);
if(fp==NULL) {
fprintf(stderr,"popen() error.\n");
return -1;
}
return 0;
}
int main(int argc, char *argv[]) {
int ret;
int hour;
char str_hour[LEN];
char sh_cmd[LEN];
char active_count[LEN];
char high_count[LEN];
char total_count[LEN];
char active_ip_addr[LEN];
time_t timestamp;
struct tm *tp;
FILE *fp;
if(argc<=1) {
printf("%s %s\n",argv[0],"auto_ssh_sbc_10_17.sh | auto_ssh_sbc_11_17.sh");
exit(-1);
}
timestamp=time(NULL);
tp=localtime(×tamp);
sprintf(str_hour,"%d",tp->tm_hour);
//sprintf(str_hour,"%d",3);
hour=atoi(str_hour);
//printf("hour=%d\n",hour);
sprintf(sh_cmd,"%s%s",TCL_CMD,argv[1]);
//printf("sh_cmd=%s\n",sh_cmd);
ret=parse_cmd(sh_cmd,active_count,high_count,total_count);
if(ret!=0) {
fprintf(stderr,"parse_cmd() error.\n");
exit(-1);
}
//strcpy(active_count,"0");
//strcpy(active_count,"1");
/*
printf("active_count=%s\n",active_count);
printf("high_count=%s\n",high_count);
printf("total_count=%s\n",total_count);
*/
if(!strcmp(active_count,"0")) {
if(hour>=0 && hour<=6 || hour==23) {
if(!strcmp("auto_ssh_sbc_10_17.sh",argv[1])){
ret=read_time_file(LONG_TIME,SBC_GG_LONG_TIME_FILE);
if(ret!=0) {
fprintf(stderr,"read_time_file(SBC_GG_LONG_TIME_FILE) error.\n");
}
}
else if(!strcmp("auto_ssh_sbc_11_17.sh",argv[1])){
ret=read_time_file(LONG_TIME,SBC_YZ_LONG_TIME_FILE);
if(ret!=0) {
fprintf(stderr,"read_time_file(SBC_YZ_LONG_TIME_FILE) error.\n");
}
}
}
else if(hour>6 && hour<=22){
if(!strcmp("auto_ssh_sbc_10_17.sh",argv[1])){
ret=read_time_file(SHORT_TIME,SBC_GG_SHORT_TIME_FILE);
if(ret!=0) {
fprintf(stderr,"read_time_file(SBC_GG_SHORT_TIME_FILE) error.\n");
}
}
else if(!strcmp("auto_ssh_sbc_11_17.sh",argv[1])){
ret=read_time_file(SHORT_TIME,SBC_YZ_SHORT_TIME_FILE);
if(ret!=0) {
fprintf(stderr,"read_time_file(SBC_YZ_SHORT_TIME_FILE) error.\n");
}
}
}
else {
exitstatus=UNKNOWN;
}
}
else {
if(!strcmp("auto_ssh_sbc_10_17.sh",argv[1])){
fp=fopen(SBC_GG_SHORT_TIME_FILE,"w+");
fprintf(fp,"%d",0);
fp=fopen(SBC_GG_LONG_TIME_FILE,"w+");
fprintf(fp,"%d",0);
}
else if(!strcmp("auto_ssh_sbc_11_17.sh",argv[1])){
fp=fopen(SBC_YZ_SHORT_TIME_FILE,"w+");
fprintf(fp,"%d",0);
fp=fopen(SBC_YZ_LONG_TIME_FILE,"w+");
fprintf(fp,"%d",0);
}
ret=fclose(fp);
}
//exitstatus=OK;
sprintf(status_information,"active_count=%s, high_count=%s, total_count=%s",active_count,high_count,total_count);
sprintf(performance_data,"active_count=%s;;;; high_count=%s;;;; total_count=%s;;;;",active_count,high_count,total_count);
/*
if(strstr(argv[1],"148")) {
if(!strcmp(active_ip_addr,"114.66.80.148") && !strcmp(active_status,"active")) {
exitstatus=OK;
sprintf(status_information,"f5 Active=%s, Standby=%s","114.66.80.148","114.66.80.149");
sprintf(performance_data,"Master=%d;;;; Slave=%d;;;;",1,0);
}
//else if(!strcmp(active_ip_addr,"114.66.80.149") && !strcmp(active_status,"active")) {
else if(!strcmp(active_ip_addr,"114.66.80.148") && !strcmp(active_status,"standby")) {
exitstatus=WARNING;
sprintf(status_information,"f5 Active=%s, Standby=%s","114.66.80.149","114.66.80.148");
sprintf(performance_data,"Master=%d;;;; Slave=%d;;;;",0,1);
}
}
else if(strstr(argv[1],"149")) {
if(!strcmp(active_ip_addr,"114.66.80.149") && !strcmp(active_status,"standby")) {
exitstatus=OK;
sprintf(status_information,"f5 Active=%s, Standby=%s","114.66.80.148","114.66.80.149");
sprintf(performance_data,"Master=%d;;;; Slave=%d;;;;",1,0);
}
//else if(!strcmp(active_ip_addr,"114.66.80.148") && !strcmp(active_status,"active")) {
else if(!strcmp(active_ip_addr,"114.66.80.149") && !strcmp(active_status,"active")) {
exitstatus=WARNING;
sprintf(status_information,"f5 Active=%s, Standby=%s","114.66.80.149","114.66.80.148");
sprintf(performance_data,"Master=%d;;;; Slave=%d;;;;",0,1);
}
}
*/
printf("%s: %s | %s\n",exit_status[exitstatus],status_information,performance_data);
return exitstatus;
}
vi auto_ssh_sbc_10_17.sh
#!/usr/bin/expect -f
#set port 22
set user user
set host 10.127.10.17
#set host 114.66.80.149
set password password
#set timeout -1
set timeout 10
#spawn ssh -D $port $user@$host
#spawn ssh $user@$host date
#spawn ssh $user@$host b failover show \; ip ad | grep 'inet 114.66.80.14'
##spawn ssh $user@$host show sessions
#spawn ssh $user@$host
spawn telnet $host
expect "*assword:*"
send "$password\r"
expect '*>*'
send "show sessions\r"
expect eof
vi auto_ssh_sbc_11_17.sh
#!/usr/bin/expect -f
#set port 22
set user user
set host 10.127.11.17
#set host 114.66.80.149
set password password
#set timeout -1
set timeout 10
#spawn ssh -D $port $user@$host
#spawn ssh $user@$host date
#spawn ssh $user@$host b failover show \; ip ad | grep 'inet 114.66.80.14'
##spawn ssh $user@$host show sessions
##spawn ssh $user@$host
spawn telnet $host
expect "*assword:*"
send "$password\r"
expect '*>*'
send "show sessions\r"
expect eof
|
|