#include #include #include #include /* for snmp */ #include /* for snmp */ #include #include #include #include /* for snmp and other stuff */ #include /* for snmp */ #include #include "asn1.h" #include "snmp_api.h" #include "snmp_impl.h" #include "snmp_client.h" #include "mib.h" #include "snmp.h" /* START OF USER DEFINABLE SETTINGS */ #define SNMP_SET "/usr/local/bin/snmpset -v 1" #define CONF "/path/to/maxchk.cfg" /* Default: chassis_group-auto/manual-max.log */ #define LOG_FILE_KILL "/tmp/.%s-%s-max.log" /* where to log autokills */ /* and web-based port kills */ #define LOG_FILE "/tmp/.%s-max.log" /* where to log any other stuff */ /* the max number of chassises you will be checking */ #define MAX_CHASSISES 20 /* 137 * number of chassises available to check in this run */ #define SESSION_LIST_SIZE (137*MAX_CHASSISES) /* END OF USER DEFINABLE SETTINGS */ #define UPTIME_OID ".1.3.6.1.4.1.529.9.2.0" #define USERNAME_OID ".1.3.6.1.4.1.529.12.2.1.3" #define SESTIME_OID ".1.3.6.1.4.1.529.11.2.1.3" /* enterprises.529.11.2.1.3 */ #define CALLREF_OID ".1.3.6.1.4.1.529.11.2.1.4" #define SCALLREF_OID ".1.3.6.1.4.1.529.12.2.1.7" #define HANGUP_OID ".1.3.6.1.4.1.529.12.3.1.3" #define TXRATE_OID ".1.3.6.1.4.1.529.11.2.1.14" #define RXRATE_OID ".1.3.6.1.4.1.529.11.2.1.5" #define IPADDY_OID ".1.3.6.1.4.1.529.12.2.1.4" #define SLOTNUM_OID ".1.3.6.1.4.1.529.11.2.1.6" #define SLOTLINE_OID ".1.3.6.1.4.1.529.11.2.1.7" int count_groups(char *str); int convert_time(char *str); char *strip_user(char *str); char *strip_ip(char *str); char *strip_user2(char *str); char *strip_html(char *buffer); char *get_time_as_string(void); char *format_time(unsigned long up, unsigned long session); char *send_request_get(char *objid, struct snmp_session *ss); void send_request_walk(char *objid, struct snmp_session *ss); void set_snmp_stuff(struct snmp_session *session); void clear_structs(void); void clear_results(void); void read_config2(void); void usage(void); void move_results(int mode); void remove_first(char *inpstr); void remove_first_comma(char *inpstr); void remove_first_underscore(char *inpstr); void midcpy(char *strf, char *strt, int fr, int to); void move_down(int pos); void copy_list(); void print_header(); void print_users(); void clear_list(); /* for killing prototypes */ int find_free(); char *find_name(char *str, int wport); void wlog(char *str); char high_total='4'; int MAXSES=0; int MAX_CHASSIS=0; int MUST_BE_AT_TIME=0; /* for killing */ int DEBUG=0; int DEBUG2=0; int SNMP_DEBUG=0; char READ_COMM[20]; char RW_COMM[20]; char URL[200]; char except_list_shown[500]; char except_list_killed[500]; char except_list_all[500]; char connlist[200][500]; char templist[200][500]; char results[SESSION_LIST_SIZE][100]; int hostindex=0; int listindex=0; int listindex2=0; int loop_limit=137; int HTML=1; int BASIC=0; int KILLING=0; int M_KILLING=0; unsigned long uptime; unsigned int hightime=0; char mess[500]; char *progname; /* struct snmp_session session, *ss; */ struct { char hostname[32]; char groupname[32]; char name[32]; int linestotal; int beforekill; /* for killing */ char wanline[10]; int wanport1; int wanport2; int wanport3; int wanport4; int needslotnum; /* do we need to get slotnums? we need to get */ /* these in chassises where lines are plugged */ /* into ports 3 and/or 4..if not, slotnum and */ /* slotline are the same thing (1 or 2), and */ /* no need to waste precious time getting */ /* slotnums */ } pwtable[MAX_CHASSISES]; struct { int num; int slotnum; int slotnum2; int slotline; int slotline2; int wanport; unsigned int timeonnum; /* for killing */ char hostname[32]; /* for killing */ char username[80]; char username2[80]; unsigned long session_time; unsigned long session_time2; char timeon[10]; unsigned long callref; unsigned long scallref; unsigned long txrate; unsigned long txrate2; unsigned long rxrate; unsigned long rxrate2; unsigned long uptime; /* for killing */ char ip[50]; char ip2[50]; } list[SESSION_LIST_SIZE]; void main(int argc, char **argv) { int tempcount=0,timeon=0,timeon2=0; int i=1,xx=0,a=0,b=0,count=0,pricount=0,found=0,j=0,high_user; int group_pos=0; int group_count=0; int group_at=0; int group_lines=0; int ch, num_opts=0; char chunk0[500]; char host[32]; char high_host[32]; char timemess[30]; char hostname[64]; /* for manual killing */ char username[80]; /* for manual killing */ char callref[15]; /* for manual killing */ unsigned long callrefnum; /* for manual killing */ char *t; char line[257]; char list_buf[257]; char templine[500]; char line2[500]; char junk1[256]; char junk2[256]; char junk3[256]; char junk4[256]; char command[256]; char objid[2048]; struct snmp_session session; struct snmp_session *ss; time_t tm; t = strrchr(argv[0], '/'); progname = (t != NULL) ? t+1 : argv[0]; if ((argc==2) && (strstr(argv[1],"_")) && (strstr(argv[1],"."))) { /* looks like a port kill from the web page */ M_KILLING=1; /* Parse fields */ for (i=0;i num_opts+1) { if (M_KILLING) { fprintf(stdout,"Content-type: text/html\n\n"); fprintf(stdout,"\n"); } else if (HTML && !KILLING) { fprintf(stdout,"Content-type: text/html\n\n"); fprintf(stdout,"\n"); fprintf(stdout,"\n"); fprintf(stdout,"\n"); fprintf(stdout,"\n"); fprintf(stdout,"
\n"); fprintf(stdout,"Ascend Max Port Usage
\n"); fprintf(stdout,"%s\n", get_time_as_string()); fprintf(stdout,"

\n"); } } else { fprintf(stdout,"No hosts specified to check!\n"); usage(); exit(0); } for (a=0;a\n",host,group_at,group_count,group_pos); } break; } } /* end of for */ if (!found) { printf("Cant find host %s in table!\n",host); if (HTML) printf("\n"); exit(0); } else { found=0; hostindex=a; a=0; } tempcount=0; timeon=0; timeon2=0; line2[0]=0; chunk0[0]=0; command[0]=0; line[0]=0; templine[0]=0; junk1[0]=0; junk2[0]=0; junk3[0]=0; junk4[0]=0; uptime=0; count=0; pricount=0; a=0; b=0; j=0; found=0; hightime=0; /* for killing */ high_user=0; /* for killing */ if (KILLING) listindex=listindex2; set_snmp_stuff(&session); /* snmp_synch_setup(&session); */ if (SNMP_DEBUG) { printf("opening socket to %s with %s..\n",session.peername,session.community); } ss = snmp_open(&session); if (SNMP_DEBUG) { if (ss == NULL){ snmp_sess_perror("maxlist_open", &session); exit(1); } else printf("done!\n"); } strcpy(objid,UPTIME_OID); if (DEBUG2) { printf("GETTING UPTIME\n"); } if (SNMP_DEBUG) { printf("sending request..\n"); } uptime = atol(send_request_get(objid,ss)); if (DEBUG2) { printf("Uptime: %ld\n",uptime); } /* get calls */ /* session time */ if (DEBUG2) { printf("GETTING SESSION TIME\n"); } if (!BASIC || KILLING || M_KILLING) { set_snmp_stuff(&session); /* snmp_synch_setup(&session); */ objid[0]=0; strcpy(objid,SESTIME_OID); send_request_walk(objid,ss); move_results(0); clear_results(); } /* call reference number */ if (DEBUG2) { printf("GETTING CALLREFs\n"); } set_snmp_stuff(&session); /* snmp_synch_setup(&session); */ objid[0]=0; strcpy(objid,CALLREF_OID); send_request_walk(objid,ss); move_results(1); clear_results(); /* scall reference number */ if (DEBUG2) { printf("GETTING SCALLREFs\n"); } set_snmp_stuff(&session); /* snmp_synch_setup(&session); */ objid[0]=0; strcpy(objid,SCALLREF_OID); send_request_walk(objid,ss); move_results(2); clear_results(); /* txrate */ if (!BASIC && !KILLING && !M_KILLING) { if (DEBUG2) { printf("GETTING TXRATEs\n"); } set_snmp_stuff(&session); /* snmp_synch_setup(&session); */ objid[0]=0; strcpy(objid,TXRATE_OID); send_request_walk(objid,ss); move_results(3); clear_results(); /* rxrate */ if (DEBUG2) { printf("GETTING RXRATEs\n"); } set_snmp_stuff(&session); /* snmp_synch_setup(&session); */ objid[0]=0; strcpy(objid,RXRATE_OID); send_request_walk(objid,ss); move_results(4); clear_results(); } /* slotnum */ if (pwtable[hostindex].needslotnum) { if (DEBUG2) { printf("GETTING SLOTNUMs for %s\n",pwtable[hostindex].hostname); } set_snmp_stuff(&session); /* snmp_synch_setup(&session); */ objid[0]=0; strcpy(objid,SLOTNUM_OID); send_request_walk(objid,ss); } move_results(5); clear_results(); /* slotline */ if (DEBUG2) { printf("GETTING SLOTLINEs\n"); } set_snmp_stuff(&session); /* snmp_synch_setup(&session); */ objid[0]=0; strcpy(objid,SLOTLINE_OID); send_request_walk(objid,ss); move_results(6); clear_results(); /* ip address */ if (!KILLING && !M_KILLING) { if (DEBUG2) { printf("GETTING IP ADDRESS\n"); } set_snmp_stuff(&session); /* snmp_synch_setup(&session); */ objid[0]=0; strcpy(objid,IPADDY_OID); send_request_walk(objid,ss); move_results(8); clear_results(); } /* username */ if (DEBUG2) { printf("GETTING USERNAME\n"); } set_snmp_stuff(&session); /* snmp_synch_setup(&session); */ objid[0]=0; strcpy(objid,USERNAME_OID); send_request_walk(objid,ss); move_results(9); clear_results(); /* close out snmp session */ snmp_close(ss); a=0; /* raw data list, if we're testing */ if (DEBUG2) { printf("Username\tIP\t\tCALLREF\tSCALLREF\tTXRATE\tSLOTNUM SLOTLINE SESSION_T\n"); for (a=0;a 7) { printf("%d: %s\t%s\t%ld\t%ld\t%ld %d %d %ld\n",a,list[a].username,list[a].ip, list[a].callref,list[a].scallref,list[a].txrate,list[a].slotnum,list[a].slotline ,list[a].session_time); } else { printf("%d: %s\t\t%s\t%ld\t%ld\t%ld %d %d %ld\n",a,list[a].username,list[a].ip, list[a].callref,list[a].scallref,list[a].txrate,list[a].slotnum, list[a].slotline,list[a].session_time); } /* end of else */ } /* end of for */ } /* end of DEBUG2 */ /* match up call reference numbers for sessions */ for (a=listindex;a 7) { printf("%s\t%s\t%ld %ld %d %d %d\n",list[a].username,list[a].ip, list[a].callref,list[a].scallref,list[a].session_time,list[a].slotnum,list[a].slotline); } else { printf("%s\t\t%s\t%ld %ld %d %d %d\n",list[a].username,list[a].ip, list[a].callref,list[a].scallref,list[a].session_time,list[a].slotnum,list[a].slotline); } /* end of else */ } /* end of if strlen */ } /* end of for */ } /* end of DEBUG2 */ /* go through list looking for valid status flags */ /* and setting uptimes */ for (a=listindex;a"); sprintf(mess,URL,pwtable[hostindex].hostname,list[a].username,list[a].callref); sprintf(templine,"%-16s",mess,list[a].username); strcat(connlist[count],templine); if (!BASIC) { sprintf(templine,"%-6ld",list[a].txrate); strcat(connlist[count],templine); sprintf(templine,"%-6ld",list[a].rxrate); strcat(connlist[count],templine); } sprintf(templine,"%-16s",list[a].ip); strcat(connlist[count],templine); if (!BASIC) { sprintf(templine,"%-3d",list[a].wanport); strcat(connlist[count],templine); if (!HTML) { /* we put the session ID in the text-only list in case the admin wants */ /* to kill them from the command line. In the HTML table they can just */ /* click on their name, so we wont inlcude it */ sprintf(templine,"%-11d",list[a].callref); strcat(connlist[count],templine); } if ((strlen(list[a].timeon) > 4) || ((strlen(list[a].timeon) == 4) && (list[a].timeon[0]>=high_total) )) { sprintf(templine,"%s",list[a].timeon); strcat(connlist[count],templine); } else { sprintf(templine,"%s",list[a].timeon); strcat(connlist[count],templine); } } strcat(connlist[count],"\n"); connlist[count][strlen(connlist[count])]=0; count++; } /* end of if !KILLING */ } /* end of if strlen username */ } /* end of sub for */ if (!KILLING && !M_KILLING) { a=0; /* sort session list by time */ j=0; tempcount=0; found=0; if (strlen(connlist[0])) { strcpy(templist[tempcount],connlist[j]); } j++; while (strlen(connlist[j])) { a=0; b=0; timeon=0; line2[0]=0; chunk0[0]=0; /* check time from stored line */ strcpy(line2,connlist[j]); for (a=0;a= timeon2) { move_down(tempcount); COPYEND: strcpy(templist[tempcount],connlist[j]); found=1; } else { tempcount++; } } /* end of sub-while */ j++; tempcount=0; found=0; } /* end of while */ j=0; copy_list(); print_header(); print_users(); /* print cumulative modem usage */ if (HTML) { fprintf(stdout,"Active modems: %d    Free modems: %d", count,pwtable[hostindex].linestotal-count); fprintf(stdout,"\n"); } else { fprintf(stdout,"Active modems: %-4d Free modems: %d\n", count,pwtable[hostindex].linestotal-count); } clear_list(); } /* end of if !KILLING */ if (KILLING) { if (group_at) { group_lines+=pricount; if (DEBUG) { printf("Got %d active lines from %s, Total at %d
\n",pricount,pwtable[hostindex].hostname,group_lines); } } } if (!group_at) { if (KILLING) { if (pricount < pwtable[hostindex].beforekill) continue; } else if (M_KILLING) { } else continue; } else if (group_at==group_count) { if (DEBUG) { printf("This is the last host in group (%s:%d)
\n",pwtable[hostindex].name,group_at); } if (KILLING) { if (group_lines < pwtable[hostindex].beforekill) { if (DEBUG) { printf("Group lines (%d) less than required kill lines (%d)
\n",group_lines,pwtable[hostindex].beforekill); } group_pos=0; group_count=0; group_at=0; continue; } /* end of if compare */ /* ready for kill */ if (DEBUG) { printf("Group lines (%d) MORE than required kill lines (%d)
\n",group_lines,pwtable[hostindex].beforekill); } } /* end of if KILLING */ else { group_pos=0; group_count=0; group_at=0; continue; } } /* end of else if */ else if (group_at) { goto START; } if (KILLING) { /* main calculations for auto-killer */ /* go through list looking for valid status flags */ /* and setting uptimes */ if (DEBUG) printf("Looking for high user
\n"); for (a=0;a 0) list[a].timeonnum=(list[a].uptime-list[a].session_time)/60; else list[a].timeonnum=0; strcpy(list[a].timeon,format_time(list[a].uptime,list[a].session_time)); /* users that wont be killed */ found=0; strcpy(list_buf,except_list_killed); if (strlen(list_buf)) { while (strlen(list_buf)) { sscanf(list_buf,"%s ",templine); remove_first(list_buf); if (strstr(list[a].username,templine)) { found=1; break; } } if (found) { found=0; continue; } } /* users that wont be anything */ found=0; strcpy(list_buf,except_list_all); if (strlen(list_buf)) { while (strlen(list_buf)) { sscanf(list_buf,"%s ",templine); remove_first(list_buf); if (strstr(list[a].username,templine)) { found=1; break; } } if (found) { found=0; continue; } } if ((list[a].timeonnum > hightime) && (strlen(list[a].username))) { hightime=list[a].timeonnum; high_user=a; strcpy(high_host,list[a].hostname); } } /* end of if valid */ } /* end of sub for */ a=0; if (DEBUG) printf("High user is %s with online time of %s (%u)
\n",list[high_user].username,list[high_user].timeon,list[high_user].timeonnum); time(&tm); sprintf(timemess,"%s",ctime(&tm)); timemess[strlen(timemess)-1]=0; if (group_at) a=group_lines; else a=pricount; /* is the user on for the lower limit? */ if (list[high_user].timeonnum < MUST_BE_AT_TIME) { sprintf(junk1,"%s MAX-killer didn't disconnect user %s not high enough time on %s (%s:%d) with time %s (%u). Active lines: %d\n", timemess,list[high_user].username,find_name(high_host,list[high_user].wanport),high_host,list[high_user].wanport, list[high_user].timeon,list[high_user].timeonnum,a); wlog(junk1); } else { sprintf(command,"%s %s %s %s.%ld i 1 1>/dev/null",SNMP_SET,high_host,RW_COMM,HANGUP_OID,list[high_user].callref); system(command); sprintf(junk1,"\n%s MAX-killer disconnected user %s on %s (%s:%d) with time %s (%u). Active lines: %d\n", timemess,list[high_user].username,find_name(high_host,list[high_user].wanport),high_host,list[high_user].wanport, list[high_user].timeon,list[high_user].timeonnum,a); wlog(junk1); /* puts(junk1); */ } /* end of else */ group_pos=0; group_count=0; group_at=0; pricount=0; a=0; found=0; } /* end of if killing */ else if (M_KILLING) { found=0; /* main calculations for manual killer */ /* go through list looking for valid status flags */ /* and setting uptimes */ for (a=0;a 0) list[a].timeonnum=(list[a].uptime-list[a].session_time)/60; else list[a].timeonnum=0; strcpy(list[a].timeon,format_time(list[a].uptime,list[a].session_time)); found=1; break; } /* end of if is callref */ } /* end of if valid */ } /* end of sub for */ if (!found) { printf("Can't find call reference number %u!
\n",callrefnum); printf("\n"); exit(0); } time(&tm); sprintf(timemess,"%s",ctime(&tm)); timemess[strlen(timemess)-1]=0; sprintf(command,"%s %s %s %s.%ld i 1 1>/dev/null",SNMP_SET,list[a].hostname,RW_COMM,HANGUP_OID,list[a].callref); system(command); printf("User %s disconnected from %s
\n", list[a].username,pwtable[hostindex].name); sprintf(junk1,"\n%s Port-killer disconnected user %s on %s (%s:%d) with time %s (%u). Active lines: %d\n", timemess,list[a].username,pwtable[hostindex].name, list[a].hostname,list[a].wanport,list[a].timeon,list[a].timeonnum, pricount); wlog(junk1); group_pos=0; group_count=0; group_at=0; pricount=0; a=0; found=0; } /* end of if M_KILLING */ } /* end of main argc for */ if (HTML && !KILLING) fprintf(stdout,"\n\n\n"); } /* end of main */ void midcpy(char *strf, char *strt, int fr, int to) { int f; for (f=fr;f<=to;++f) { if (!strf[f]) { strt[f-fr]='\0'; return; } strt[f-fr]=strf[f]; } strt[f-fr]='\0'; } char *strip_user(char *str) { int zz=0; static char usern[100]; midcpy(str,str,1,strlen(str)); for (zz=0;zz=0;--aa) { if (strlen(templist[aa])) { found=1; break; } } if (found) { while (aa >= pos) { strcpy(templist[aa+1],templist[aa]); aa--; } /* end of while */ } /* end of if */ found=0; aa=0; } void copy_list() { int i=0; int j=0; /* now copy them all back to the original list */ for (i=0;i
\n"); fprintf(stdout,"", pwtable[hostindex].name,pwtable[hostindex].hostname); if (!BASIC) { fprintf(stdout,"\n\n"); } else { fprintf(stdout,"\n\n"); } } else { if (!BASIC) fprintf(stdout,"%-16s TX RX Hostname PRI SessionID Session Time\n","Username"); else fprintf(stdout,"%-15s %s\n","Username","Hostname"); } } char *get_time_as_string(void) { time_t now; char *timestring; time(&now); timestring = ctime(&now); timestring[strlen(timestring) - 1] = '\0'; /*trim newline at end*/ return timestring; } void read_config2(void) { int yy=0,zz=0,i=0,j=0; char fileline[257]; char readbuf[256]; char temp[5]; FILE *fp2; /* if (!(fp2=fopen("/servers/www/fasttrack/bb/www/max/cgi-bin/cygnus/config.cfg","r"))) */ if (!(fp2=fopen(CONF,"r"))) { printf("Can't open config file!
\n\n"); exit(0); } else { yy=0; zz=0; while (fgets(fileline,256,fp2) != NULL) { fileline[strlen(fileline)-1]=0; if (fileline[0]=='#' || fileline[0]==' ' || !strlen(fileline)) continue; else { if (yy==0) { MAXSES=atoi(fileline); } else if (yy==1) { MUST_BE_AT_TIME=atoi(fileline); } else if (yy==2) { strcpy(READ_COMM,fileline); } else if (yy==3) { strcpy(RW_COMM,fileline); } else if (yy==4) { strcpy(URL,fileline); } else if (yy==5) { strcpy(except_list_shown,fileline); } else if (yy==6) { strcpy(except_list_killed,fileline); } else if (yy==7) { strcpy(except_list_all,fileline); } else if (yy >= 8) { sscanf(fileline,"%s ",pwtable[zz].hostname); remove_first(fileline); sscanf(fileline,"%s ",readbuf); pwtable[zz].linestotal=atoi(readbuf); remove_first(fileline); /* scan in beforekill line */ sscanf(fileline,"%s ",readbuf); pwtable[zz].beforekill=atoi(readbuf); remove_first(fileline); /* before kill */ sscanf(fileline,"%s ",pwtable[zz].wanline); if (strstr(pwtable[zz].wanline,",")) { for (i=0;i= 5) { remove_first(fileline); /* host */ remove_first(fileline); /* lines total */ remove_first(fileline); /* before kill */ remove_first(fileline); /* wanports */ sscanf(fileline,"%s ",readbuf); if (!strcmp(readbuf,str)) number++; } i++; } /* end of else */ } /* end of while */ } fclose(fp2); return number; } char *strip_html(char *buffer) { int i=0,j=0; int pos=0; static char buffer2[300]; buffer2[0]=0; if (!strstr(buffer,"<") || !strstr(buffer,">")) { strcpy(buffer2,buffer); goto JUMP; } j=0; pos=0; for (i=0;i')) { pos=0; continue; } else if (pos==1) { continue; } if (buffer[i]=='<') { pos=1; continue; } else { buffer2[j]=buffer[i]; j++; } } /* end of for */ buffer2[j]='\0'; JUMP: return buffer2; } /* show usage info */ void usage(void) { fprintf(stderr, "Usage: %s [-PBk123] [hub/group1 hub/group2...] or [hub_username_sessionid]\n",progname); fprintf(stderr, " -P\tPlain - DONT add html codes to the output\n"); fprintf(stderr, " -B\tBasic - Only show username/IP information\n"); fprintf(stderr, " -k\tAuto-kill - Kill highest user on specified boxes if all lines used\n"); fprintf(stderr, " \tPort-kill - use \"hub_username_sessionid\" option\n"); fprintf(stderr, "Debugging:\n"); fprintf(stderr, " -1 - Basic debugging\n"); fprintf(stderr, " -2 - Verbose debugging\n"); fprintf(stderr, " -3 - Verbose debugging and SNMP debugging\n"); fprintf(stderr, "\n"); exit(0); } char *send_request_get(char *objid, struct snmp_session *ss) { char buf[2048]; static char retval[2049]; int status=0; oid name[MAX_NAME_LEN]; struct variable_list *vars; struct snmp_pdu *pdu; struct snmp_pdu *response; size_t name_length = MAX_OID_LEN; retval[0]=0; buf[0]=0; if (DEBUG2) { printf("Requesting %s\n",objid); } /* printf("SNMP_MSG_GET: %d\n",SNMP_MSG_GET); */ pdu = snmp_pdu_create(SNMP_MSG_GET); if (SNMP_DEBUG) { printf("Created SNMP request\n"); } if(!read_objid(objid,name,&name_length)) { printf("Invalid OBJID: %s.\n",objid); return(NULL); } else { snmp_add_null_var(pdu,name,name_length); } if (DEBUG2) printf("OBJID passed\n"); if (DEBUG) { printf("getting from %s with %s..\n",ss->peername,ss->community); if (DEBUG2) snmp_sess_perror("maxlist_retrieve", ss); } retry: status = snmp_synch_response(ss, pdu, &response); if(status == STAT_SUCCESS) { if(response->errstat == SNMP_ERR_NOERROR) { for(vars = response->variables;vars;vars = vars->next_variable) sprint_value(buf,vars->name,vars->name_length,vars); } else { /* printf("Error in packet\n"); printf("Reason: %s\n",snmp_errstring(response->errstat)); */ if ((pdu = snmp_fix_pdu(response, SNMP_MSG_GET)) != NULL) goto retry; } /* end of else */ } /* end of status if */ else if (status == STAT_TIMEOUT) { printf("No Response from %s.\n", ss->peername); } else { /* status == STAT_ERROR */ snmp_sess_perror("maxlist", ss); printf("An error occurred\n"); } if (response) snmp_free_pdu(response); /* snmp_close(ss); */ if (SNMP_DEBUG) { printf("Freed\n"); } /* retval = new char[strlen(buf)+1]; */ bzero(retval,strlen(buf)+1); if (SNMP_DEBUG) { printf("zeroed\n"); } strncpy(retval,buf,strlen(buf)); if (SNMP_DEBUG) printf("copied\n"); return(retval); } void send_request_walk(char *objid, struct snmp_session *ss) { char tempresult[200]; char *tempresult2; int running,count,y=0; char buf[2048]; static char retval[2049]; int status=0; oid name[MAX_NAME_LEN]; size_t name_length = MAX_OID_LEN; oid root[MAX_NAME_LEN]; size_t rootlen = MAX_OID_LEN; struct variable_list *vars; struct snmp_pdu *pdu; struct snmp_pdu *response; retval[0]=0; buf[0]=0; if (SNMP_DEBUG) printf("OBJID: %s\n",objid); if (!read_objid(objid, root, &rootlen)){ fprintf(stderr, "Invalid root OBJID: %s\n", objid); return; } /* get first object to start walk */ memmove(name, root, rootlen * sizeof(oid)); name_length = rootlen; running = 1; count = 1; while(running){ /* create PDU for GETNEXT request and add object name to request */ pdu = snmp_pdu_create(SNMP_MSG_GETNEXT); if (SNMP_DEBUG) printf("Created\n"); snmp_add_null_var(pdu, name, name_length); /* do the request */ status = snmp_synch_response(ss, pdu, &response); if (status == STAT_SUCCESS){ if (SNMP_DEBUG) printf("success\n"); if (response->errstat == SNMP_ERR_NOERROR){ if (SNMP_DEBUG) printf("no error\n"); /* check resulting variables */ for(vars = response->variables; vars; vars = vars->next_variable){ if ((vars->name_length < rootlen) || (memcmp(root, vars->name, rootlen * sizeof(oid))!=0)) { /* not part of this subtree */ if (SNMP_DEBUG) printf("root %s not part of subtree\n",root); running = 0; continue; } if (SNMP_DEBUG) printf("getting variable\n"); sprint_variable(tempresult, vars->name, vars->name_length, vars); if (SNMP_DEBUG) printf("got\n"); tempresult2 = strchr(tempresult,'='); if (SNMP_DEBUG) printf("parsed\n"); midcpy(tempresult2,results[y],2,strlen(tempresult2)); if (SNMP_DEBUG) printf("Result %d: %s\n",y,results[y]); if (!strcmp(objid,SESTIME_OID)) listindex2++; y++; if ((vars->type != SNMP_ENDOFMIBVIEW) && (vars->type != SNMP_NOSUCHOBJECT) && (vars->type != SNMP_NOSUCHINSTANCE)){ /* not an exception value */ memmove((char *)name, (char *)vars->name, vars->name_length * sizeof(oid)); name_length = vars->name_length; } else /* an exception value, so stop */ running = 0; } /* end of for */ } else { /* error in response, print it */ running = 0; if (response->errstat == SNMP_ERR_NOSUCHNAME){ printf("End of MIB\n"); } else { fprintf(stderr, "Error in packet.\nReason: %s\n", snmp_errstring(response->errstat)); if (response->errstat == SNMP_ERR_NOSUCHNAME){ fprintf(stderr, "The request for this object identifier failed: "); for(count = 1, vars = response->variables; vars && count != response->errindex; vars = vars->next_variable, count++) /*EMPTY*/; if (vars) fprintf(stderr, "This: %s", (char *)vars->name); /* fprint_objid(stderr, vars->name, vars->name_length); */ fprintf(stderr, "\n"); } /* end of sub NOSUCHNAME if */ } /* end of NOSUCHNAME else */ } /* end of ERR_NOERROR else */ } else if (status == STAT_TIMEOUT){ fprintf(stderr, "No Response from %s\n", ss->peername); running = 0; } else { /* status == STAT_ERROR */ fprintf(stderr, "A SNMP error occurred\n"); running = 0; } /* end of status else */ if (response) snmp_free_pdu(response); } /* end of running while */ /* snmp_close(ss); */ /* if (SNMP_DEBUG) printf("Freed\n"); bzero(retval,strlen(buf)+1); if (SNMP_DEBUG) printf("zeroed\n"); strncpy(retval,buf,strlen(buf)); if (SNMP_DEBUG) printf("copied\n"); return(retval); */ } void set_snmp_stuff(struct snmp_session *session) { char *community; char *hostname; community = READ_COMM; hostname = pwtable[hostindex].hostname; if (SNMP_DEBUG) { printf("setting snmp stuff for set host %s (orig: %s)\n",hostname,pwtable[hostindex].hostname); } snmp_sess_init( session ); /* memset(session, 0, sizeof(struct snmp_session)); */ session->peername = (char *)hostname; session->version = 0; session->community = (unsigned char *)community; session->community_len = strlen((char *)community); /* session->remote_port = (u_short)161; session->retries = -1; */ session->timeout = (long)-1; /* session->authenticator = NULL; */ } void clear_results(void) { int jj=0; for (jj=0;jj
%s (%s)
Username TX RateRX Rate HostnamePRI # Session Time
Username Hostname