使用方法:f5_session_cngw_prod -H host -o oid
vi f5_session_cngw_prod.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #define OK 0 #define WARNING 1 #define CRITICAL 2 #define UNKNOWN 3 #define LEN 1000 #define LEN_SHORT 512 int exitstatus=OK; char *exit_status[4]={"OK","WARNING","CRITICAL","UNKNOWN"}; char *power_status[4]={"active","deactive","not_install","unsupport"}; char status_information[LEN]; char performance_data[LEN]; int sum_conn; char sum_conn_all[LEN_SHORT]; char sum_conn_170_245[LEN_SHORT]; char sum_conn_171_245[LEN_SHORT]; void print_usage(char str[32]) { printf("Usage: %s [options]\n",str); printf(" e.g. %s -H 1.1.1.1 -o .1.3.6...\n",str); printf("Options:\n"); printf("\t -H ip address\n"); printf("\t -o snmp oid\n"); } /* int check_argv() { while((ch=getopt(argc,argv,"H:o:"))!=-1) { switch(ch) { case 'H': // printf("option H:%s\n",optarg); break; case 'o': // printf("option o:%s\n",optarg); break; default: // printf("other option :%c\n",ch); break; } } return -1; } */ int parse_status(char *sh_cmd) { int ret; int line=0; FILE *fp; char readbuf[LEN_SHORT]; // fp=popen("/usr/bin/snmpwalk -v2c -c zb.186.nms 114.66.80.55 1.3.6.1.4.1.2011.2.23.1.9.1.2.1.2","r"); // fp=popen(command,"r"); fp=popen(sh_cmd,"r"); if(fp==NULL) { fprintf(stderr,"fopen() error.\n"); exitstatus=CRITICAL; return exitstatus; /* sprintf(status_information,"active_status=%s",active_status); sprintf(performance_data,"active_status=%s;;;0;100","0"); sprintf(status_information,"conn_170_245=%s conn_171_245=%s all_conn=%s",sum_conn_170_245,sum_conn_171_245,sum_conn_all); sprintf(performance_data,"conn_170_245=%s;;;0;100 conn_171_245=%s;;;0;100 all_conn=%s;;;0;100",sum_conn_170_245,sum_conn_171_245,sum_conn_all); printf("%s: %s | %s\n",exit_status[exitstatus],status_information,performance_data); */ } /* str=fgets(readbuf,LEN_SHORT,fp); if(str==NULL) { perror("fgets() error"); } printf("readbuf=%s",readbuf); ret=sscanf(readbuf,"SNMPv2-SMI::enterprises.3375.2.1.1.1.1.19.0 = INTEGER: %s",active_status); if(ret<0) { fprintf(stderr,"sscanf() error.\n"); exitstatus=UNKNOWN; sprintf(status_information,"active_status=%s",active_status); sprintf(performance_data,"active_status=%s;;;0;100","0"); printf("%s: %s | %s\n",exit_status[exitstatus],status_information,performance_data); return exitstatus; } //SNMPv2-SMI::enterprises.3375.2.1.1.1.1.19.0 = INTEGER: */ while(fgets(readbuf,LEN_SHORT,fp)!=NULL) { line++; // printf("line=%d,readbuf=%s",line,readbuf); if(line==1) { // sscanf(readbuf,"SNMPv2-SMI::enterprises.3375.2.1.3.2.2.2.1.2.1 = INTEGER: %s",power1_value); // sscanf("SNMPv2-SMI::enterprises.3375.2.2.5.4.3.1.9.7.77.81.65.95.73.78.84.1.4.103.4.170.245.8021 = Counter64: 23","%*[^=]%*[^:]: %s",str1); sscanf(readbuf,"%*[^=]%*[^:]: %s",sum_conn_170_245); } else if (line==2) { // sscanf(readbuf,"SNMPv2-SMI::enterprises.3375.2.1.3.2.2.2.1.2.2 = INTEGER: %s",power2_value); sscanf(readbuf,"%*[^=]%*[^:]: %s",sum_conn_171_245); } /* if(line==2) { break; } */ } // printf("line=%d\n",line); ret=pclose(fp); if(ret==-1) { perror("pclose"); } return 0; } int main(int argc,char *argv[]) { int ret; int ch; int mark=0; char *str; char command[LEN]; if(argc==1 || argc!=5) { printf("%s -H host -o oid\n",argv[0]); print_usage(argv[0]); return -1; } while((ch=getopt(argc,argv,"H:o:"))!=-1) { switch(ch) { case 'H': // printf("option H:%s\n",optarg); break; case 'o': // printf("option o:%s\n",optarg); break; default: // printf("other option :%c\n",ch); mark=1; break; } } if(mark==1) { return -1; } // sprintf(command,"/usr/bin/snmpwalk -v2c -c public %s %s | grep 245.6717",argv[2],argv[4]); sprintf(command,"/usr/bin/snmpwalk -v2c -c public %s %s | grep 50.52.53.6717",argv[2],argv[4]); // snmpwalk -v 2c -c zb.186.nms 114.66.80.53 1.3.6.1.4.1.3375.2.2.5.4.3.1.11 | grep "50.52.53.6717" // printf("command=%s\n",command); ret=parse_status(command); if(ret!=0) { fprintf(stderr,"parse_status() error.\n"); } sum_conn=atoi(sum_conn_170_245)+atoi(sum_conn_171_245); sprintf(sum_conn_all,"%d",sum_conn); /* printf("sum_conn_170_245=%s\n",sum_conn_170_245); printf("sum_conn_171_245=%s\n",sum_conn_171_245); printf("sum_conn_all=%s\n",sum_conn_all); */ //status_information && performance_data // if(atoi(sum_conn_170_245)<=50 && atoi(sum_conn_171_245)<=50) { exitstatus=OK; sprintf(status_information,"conn_170_245=%s conn_171_245=%s all_conn=%s",sum_conn_170_245,sum_conn_171_245,sum_conn_all); sprintf(performance_data,"conn_170_245=%s;;;0; conn_171_245=%s;;;0; all_conn=%s;;;0;",sum_conn_170_245,sum_conn_171_245,sum_conn_all); // } /* else if(atoi(sum_conn_170_245)>50 && atoi(sum_conn_170_245)<100 || atoi(sum_conn_171_245)>50 && atoi(sum_conn_171_245)<100) { exitstatus=WARNING; sprintf(status_information,"conn_170_245=%s conn_171_245=%s all_conn=%s",sum_conn_170_245,sum_conn_171_245,sum_conn_all); sprintf(performance_data,"conn_170_245=%s;;;0;100 conn_171_245=%s;;;0;100 all_conn=%s;;;0;100",sum_conn_170_245,sum_conn_171_245,sum_conn_all); } else if(atoi(sum_conn_170_245)>100 || atoi(sum_conn_171_245)>100) { exitstatus=CRITICAL; sprintf(status_information,"conn_170_245=%s conn_171_245=%s all_conn=%s",sum_conn_170_245,sum_conn_171_245,sum_conn_all); sprintf(performance_data,"conn_170_245=%s;;;0;100 conn_171_245=%s;;;0;100 all_conn=%s;;;0;100",sum_conn_170_245,sum_conn_171_245,sum_conn_all); } */ printf("%s: %s | %s\n",exit_status[exitstatus],status_information,performance_data); return exitstatus; }
时间: 2024-12-29 11:30:37