vi f5_active_status_snmp.c
使用方法:f5_active_status_snmp -H $HOSTADDRESS$ -o 1.3.6.1.4.1.3375.2.1.14.3.3.0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OK 0
#define WARNING 1
#define CRITICAL 2
#define UNKNOWN 3
#define LEN 1000L
#define LEN_SHORT 32L
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];
void print_usage(char str[LEN_SHORT]) {
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,char *active_status,char f5_ip_addr[LEN_SHORT]) {
int ret;
FILE *fp;
char readbuf[LEN];
char *p,*str;
int line=0;
// 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(sh_cmd,"r");
if(fp==NULL) {
fprintf(stderr,"fopen() error.\n");
exitstatus=UNKNOWN;
sprintf(status_information,"active_status=%s","Unknown");
sprintf(performance_data,"active_status=%s;;;0;100","");
printf("%s: %s | %s\n",exit_status[exitstatus],status_information,performance_data);
return exitstatus;
}
str=fgets(readbuf,LEN,fp);
if(str==NULL) {
perror("fgets() error");
}
ret=sscanf(readbuf,"SNMPv2-SMI::enterprises.3375.2.1.14.3.3.0 = INTEGER: %s",active_status);
if(ret<0) {
fprintf(stderr,"sscanf() error.\n");
exitstatus=UNKNOWN;
sprintf(status_information,"active_status=%s","Unknown");
sprintf(performance_data,"active_status=%s;;;0;100","");
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,256,fp)!=NULL) {
line++;
if(line==1) {
sscanf(readbuf,"SNMPv2-SMI::enterprises.3375.2.1.3.2.2.2.1.2.1 = INTEGER: %s",power1_value);
}
else if (line==2) {
sscanf(readbuf,"SNMPv2-SMI::enterprises.3375.2.1.3.2.2.2.1.2.2 = INTEGER: %s",power2_value);
}
}
*/
// printf("line=%d\n",line);
ret=pclose(fp);
if(ret==-1) {
perror("pclose");
return -1;
}
// printf("active_status=%s\n",active_status);
//TSP GGLC (is active)
if(!strcmp(f5_ip_addr,"114.66.80.53")) {
if(atoi(active_status)==0) {
exitstatus=OK;
sprintf(status_information,"f5 active_status is Active");
sprintf(performance_data,"f5 active_value=%s;;;0;100",active_status);
}
else if(atoi(active_status)==4) {
exitstatus=CRITICAL;
sprintf(status_information,"f5 active_status is Standby");
sprintf(performance_data,"f5 active_value=%s;;;0;100",active_status);
}
else {
exitstatus=UNKNOWN;
sprintf(status_information,"f5 active_status is UNKNOWN");
sprintf(performance_data,"f5 active_value=%s;;;0;100",active_status);
}
}
//TSP YZ (is standby)
else if(!strcmp(f5_ip_addr,"114.66.80.52")) {
if(atoi(active_status)==4) {
exitstatus=OK;
sprintf(status_information,"f5 active_status is Standby");
sprintf(performance_data,"f5 active_value=%s;;;0;100",active_status);
}
else if(atoi(active_status)==0) {
exitstatus=CRITICAL;
sprintf(status_information,"f5 active_status is Active");
sprintf(performance_data,"f5 active_value=%s;;;0;100",active_status);
}
else {
exitstatus=UNKNOWN;
sprintf(status_information,"f5 active_status is UNKNOWN");
sprintf(performance_data,"f5 active_value=%s;;;0;100",active_status);
}
}
//CUCC GGLC (active,ospf is active)
else if(!strcmp(f5_ip_addr,"192.168.10.14")) {
if(atoi(active_status)==0) {
exitstatus=OK;
sprintf(status_information,"f5 active_status is Active");
sprintf(performance_data,"f5 active_value=%s;;;0;100",active_status);
}
else if(atoi(active_status)==4) {
exitstatus=CRITICAL;
sprintf(status_information,"f5 active_status is Standby");
sprintf(performance_data,"f5 active_value=%s;;;0;100",active_status);
}
else {
exitstatus=UNKNOWN;
sprintf(status_information,"f5 active_status is UNKNOWN");
sprintf(performance_data,"f5 active_value=%s;;;0;100",active_status);
}
}
//CUCC YZ (active,ospf is active)
else if(!strcmp(f5_ip_addr,"192.168.10.29")) {
if(atoi(active_status)==0) {
exitstatus=OK;
sprintf(status_information,"f5 active_status is Active");
sprintf(performance_data,"f5 active_value=%s;;;0;100",active_status);
}
else if(atoi(active_status)==4) {
exitstatus=CRITICAL;
sprintf(status_information,"f5 active_status is Standby");
sprintf(performance_data,"f5 active_value=%s;;;0;100",active_status);
}
else {
exitstatus=UNKNOWN;
sprintf(status_information,"f5 active_status is UNKNOWN");
sprintf(performance_data,"f5 active_value=%s;;;0;100",active_status);
}
}
return 0;
}
int main(int argc,char *argv[]) {
int ret;
FILE *fp;
int ch;
int mark=0;
char *str;
int line=0;
char readbuf[LEN];
char sh_cmd[LEN];
char active_status[LEN_SHORT];
char active_ip_addr[LEN_SHORT];
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;
}
/*
fp=fopen("1.txt","r");
if(fp==NULL) {
fprintf(stderr,"fopen() error.\n");
return -1;
}
*/
sprintf(sh_cmd,"/usr/bin/snmpwalk -v2c -c zb.186.nms %s %s",argv[2],argv[4]);
// printf("sh_cmd=%s\n",sh_cmd);
ret=parse_status(sh_cmd,active_status,argv[2]);
if(ret!=0) {
fprintf(stderr,"parse_status() error.\n");
sprintf(status_information,"f5 active_status is Unknown");
sprintf(performance_data,"f5 active_value=%s;;;0;100","");
exitstatus=CRITICAL;
printf("%s - %s | %s\n", exit_status[exitstatus], status_information, performance_data);
exit(exitstatus);
}
printf("%s: %s | %s\n",exit_status[exitstatus],status_information,performance_data);
return exitstatus;
}