#include #include #include #include #include #include #include #define MAX_STR 64 char *DEFAULT_HOST = "localhost"; u_char *DEFAULT_COMMUNITY = (u_char *) "public"; oid OID[256] = {1, 3, 6, 1, 2, 1, 4, 21, 1, 1, 0, 0, 0, 0}; oid OID_LEN = 10; int ip[4]; char tmp[MAX_STR]; /* funkce pro prevod IP adresy ulozene ve strukture variable_list na retezec s ciselnym vyjadrenim */ void str2ip(struct variable_list *vars, char *str) { char tmp[MAX_STR]; int i; for (i = 0; i < vars->val_len - 1; i++) { sprintf(tmp, "%d.", *(vars->val.string + i)); strcat(str, tmp); // ip[i] = atoi(tmp); } sprintf(tmp, "%d", *(vars->val.string + i)); strcat(str, tmp); // ip[3] = atoi(tmp); } int main(int argc, char** argv) { struct snmp_session session, *ss; struct snmp_pdu *pdu = NULL; struct snmp_pdu *response = NULL; struct variable_list *vars = NULL; struct timeval sendtv, rectv; struct tm *tm; FILE *logfile; int status; int interface = -1; char dest[MAX_STR]; char next[MAX_STR]; char mask[MAX_STR]; dest[0] = '\0'; next[0] = '\0'; mask[0] = '\0'; long last = -1; long current = -1; int loop = 1; clock_t starttime, stoptime; struct tms cas; int clk_tck; float diff; float min = FLT_MAX; float max = 0; float sum = 0; int count = 0; init_snmp("snmpclient"); snmp_sess_init(&session); /* zpracovani vstupu a nastaveni serveru a community */ switch (argc) { case 1: session.peername = DEFAULT_HOST; session.community = DEFAULT_COMMUNITY; break; case 2: session.peername = argv[1]; session.community = DEFAULT_COMMUNITY; break; case 3: session.peername = argv[1]; session.community = (u_char*) argv[2]; break; default: printf("Usage: snmpclient [host [community]]\n"); exit(1); } session.community_len = strlen((char*) session.community); session.version = SNMP_VERSION_1; /* vytvoreni spojeni */ ss = snmp_open(&session); if (!ss) { printf("Could not connect to %s\n", session.peername); exit(1); } /* otevreni logovaciho souboru */ if ((logfile = fopen("snmpclient.log", "w")) == NULL) { printf("Can not open logfile.\n"); exit(1); } /* zjisteni poctu tiku casovace za sekundu */ clk_tck = sysconf(_SC_CLK_TCK); /* vypis hlavicky vystupu */ printf("hostname: %s\n", session.peername); printf("community: %s\n", session.community); printf("Tabulka: .iso.org.dod.internet.mgmt.mib-2.ip.ipRouteTable\n"); printf("OID: .1.3.6.1.2.1.4.21\n\n"); printf("Sloupecek: ipRouteDest (OID: .1.3.6.1.2.1.4.21.1.1)\n"); printf("Sloupecek: ipRouteIfIndex (OID: .1.3.6.1.2.1.4.21.1.2)\n"); printf("Sloupecek: ipRouteNextHop (OID: .1.3.6.1.2.1.4.21.1.7)\n"); printf("Sloupecek: ipRouteMask (OID: .1.3.6.1.2.1.4.21.1.11)\n\n"); printf("ipRouteIfIndex ipRouteDest ipRouteNextHop ipRouteMask\n"); printf("------------------------------------------------------------\n"); fprintf(logfile, "request time response time OID\n"); fprintf(logfile, "-------------------------------------------------------------\n"); while (loop) { /* vytvoreni pozadavku */ pdu = snmp_pdu_create(SNMP_MSG_GETNEXT); snmp_add_null_var(pdu, OID, OID_LEN); pdu->variables->type = 6; /* odeslani pozadavku */ starttime = times(&cas); gettimeofday(&sendtv, NULL); status = snmp_synch_response(ss, pdu, &response); stoptime = times(&cas); gettimeofday(&rectv, NULL); /* zapis hodnot do logu */ tm = localtime(&sendtv.tv_sec); fprintf(logfile, "%.2d:%.2d:%.2d.%.3d ", tm->tm_hour, tm->tm_min, tm->tm_sec, (int) sendtv.tv_usec); tm = localtime(&rectv.tv_sec); fprintf(logfile, "%.2d:%.2d:%.2d.%.3d ", tm->tm_hour, tm->tm_min, tm->tm_sec, (int) rectv.tv_usec); fprint_objid(logfile, OID, OID_LEN); /* vypocet casovych statistik */ diff = (stoptime - starttime) / (double) clk_tck; sum += diff; if (diff < min) { min = diff; } if (diff > max) { max = diff; } count++; /* zpracovani odpovedi */ if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) { vars = response->variables; if (vars->type == ASN_APPLICATION) { vars->val.string[vars->val_len] = '\0'; switch (OID[9]) { case 1: dest[0] = '\0'; str2ip(vars, dest); current = *vars->val.integer; int i; for (i = 0; i < vars->val_len; i++) { sprintf(tmp, "%d.", *(vars->val.string + i)); ip[i] = atoi(tmp); } break; case 7: next[0] = '\0'; str2ip(vars, next); break; case 11: mask[0] = '\0'; str2ip(vars, mask); break; } } else if (OID[9] == 2) { interface = (int) * vars->val.integer; } else if (OID[9] == 1) { loop = 0; } } else if (status == STAT_SUCCESS) { fprintf(stderr, "Error: %s\n", snmp_errstring(response->errstat)); } else { snmp_sess_perror("snmpget", ss); } if (response) { snmp_free_pdu(response); } if (OID[9] == 11) { if (current != last || 1) { printf("%-14d %-16s %-16s %-16s\n", interface, dest, next, mask); } last = current; } switch (OID[9]) { case 1: OID[9] = 2; break; case 2: OID[9] = 7; break; case 7: OID[9] = 11; break; case 11: OID[9] = 1; OID_LEN = 14; int i; for (i = 0; i < 4; i++) { OID[10 + i] = ip[i]; } break; } } snmp_close(ss); /* zapis statistik do logu */ fprintf(logfile, "\nMinimum response time: %d ms\n", (int) (min * 1000)); fprintf(logfile, "Maximum response time: %d ms\n", (int) (max * 1000)); fprintf(logfile, "Average response time: %d ms\n", (int) (sum * 1000 / count)); fclose(logfile); printf("\n"); return 0; }