]> git.pld-linux.org Git - packages/net-snmp.git/blob - net-snmp-snmpnetstat-getbulk.patch
- use getbulk to speedup query
[packages/net-snmp.git] / net-snmp-snmpnetstat-getbulk.patch
1 --- net-snmp-5.4.2.1/apps/snmpnetstat/inet.c    2006-04-21 15:30:47.000000000 +0300
2 +++ /home/glen/inet.c   2009-01-22 17:39:57.754383000 +0200
3 @@ -110,8 +110,10 @@
4  tcpprotopr(const char *name)
5  {
6      netsnmp_variable_list *var, *vp;
7 -    oid    tcpConnState_oid[] = { 1,3,6,1,2,1,6,13,1,1 };
8 -    size_t tcpConnState_len   = OID_LENGTH( tcpConnState_oid );
9 +       netsnmp_pdu    *pdu, *response;
10 +    oid             tcpConnState_mib[] = { 1,3,6,1,2,1,6,13,1,1 };
11 +    oid             tcpConnState_oid[MAX_OID_LEN];
12 +    size_t          tcpConnState_len;
13      int    state, width;
14      union {
15          struct in_addr addr;
16 @@ -121,61 +123,126 @@
17      struct in_addr localAddr, remoteAddr;
18      char  *cp;
19      int    first = 1;
20 +    int    running = 1;
21 +    int    status;
22 +
23 +
24 +    /*
25 +     * setup initial object name
26 +     */
27 +       memmove(tcpConnState_oid, tcpConnState_mib, sizeof(tcpConnState_mib));
28 +       tcpConnState_len = sizeof(tcpConnState_mib) / sizeof(oid);
29  
30      /*
31       * Walking the tcpConnState column will provide all
32       *   the necessary information.
33       */
34 -    var = NULL;
35 -    snmp_varlist_add_variable( &var, tcpConnState_oid, tcpConnState_len,
36 -                                   ASN_NULL, NULL,  0);
37 -    if (!var)
38 -        return;
39 -    if (netsnmp_query_walk( var, ss ) != SNMP_ERR_NOERROR)
40 -        return;
41 -
42 -    for (vp = var; vp ; vp=vp->next_variable) {
43 -        state = *vp->val.integer;
44 -        if (!aflag && state == MIB_TCPCONNSTATE_LISTEN)
45 -            continue;
46 +       while (running) {
47 +        /*
48 +         * create PDU for GETBULK request and add object name to request
49 +         */
50 +        pdu = snmp_pdu_create(SNMP_MSG_GETBULK);
51 +        pdu->non_repeaters = 0;
52 +        pdu->max_repetitions = 1024;    /* fill the packet */
53 +        snmp_add_null_var(pdu, tcpConnState_oid, tcpConnState_len);
54  
55 -        if (first) {
56 -            printf("Active Internet (%s) Connections", name);
57 -            if (aflag)
58 -                printf(" (including servers)");
59 -            putchar('\n');
60 -            width = Aflag ? 18 : 22;
61 -            printf("%-5.5s %*.*s %*.*s %s\n",
62 -                   "Proto", -width, width, "Local Address",
63 -                            -width, width, "Remote Address", "(state)");
64 -            first=0;
65 +        /*
66 +         * do the request
67 +         */
68 +        status = snmp_synch_response(ss, pdu, &response);
69 +        if (status == STAT_SUCCESS) {
70 +            if (response->errstat == SNMP_ERR_NOERROR) {
71 +                               for (vp = response->variables; vp ; vp=vp->next_variable) {
72 +
73 +                    if ((vp->name_length < sizeof(tcpConnState_mib) / sizeof(oid)) ||
74 +                                                       (memcmp(tcpConnState_mib, vp->name, sizeof(tcpConnState_mib)) != 0)) {
75 +                        /*
76 +                         * not part of this subtree
77 +                         */
78 +                        running = 0;
79 +                        continue;
80 +                    }
81 +
82 +                                       state = *vp->val.integer;
83 +                                       if (!aflag && state == MIB_TCPCONNSTATE_LISTEN) {
84 +                                               continue;
85 +                                       }
86 +
87 +                                       if (first) {
88 +                                               printf("Active Internet (%s) Connections", name);
89 +                                               if (aflag)
90 +                                                       printf(" (including servers)");
91 +                                               putchar('\n');
92 +                                               width = Aflag ? 18 : 22;
93 +                                               printf("%-5.5s %*.*s %*.*s %s\n",
94 +                                                          "Proto", -width, width, "Local Address",
95 +                                                                               -width, width, "Remote Address", "(state)");
96 +                                               first = 0;
97 +                                       }
98 +                                       
99 +                                       /* Extract the local/remote information from the index values */
100 +                                       cp = tmpAddr.data;
101 +                                       cp[0] = vp->name[ 10 ] & 0xff;
102 +                                       cp[1] = vp->name[ 11 ] & 0xff;
103 +                                       cp[2] = vp->name[ 12 ] & 0xff;
104 +                                       cp[3] = vp->name[ 13 ] & 0xff;
105 +                                       localAddr.s_addr = tmpAddr.addr.s_addr;
106 +                                       localPort        = ntohs(vp->name[ 14 ]);
107 +                                       cp = tmpAddr.data;
108 +                                       cp[0] = vp->name[ 15 ] & 0xff;
109 +                                       cp[1] = vp->name[ 16 ] & 0xff;
110 +                                       cp[2] = vp->name[ 17 ] & 0xff;
111 +                                       cp[3] = vp->name[ 18 ] & 0xff;
112 +                                       remoteAddr.s_addr = tmpAddr.addr.s_addr;
113 +                                       remotePort        = ntohs(vp->name[ 19 ]);
114 +
115 +                                       printf("%-5.5s", name);
116 +                                       inetprint(&localAddr,  localPort,  name, 1);
117 +                                       inetprint(&remoteAddr, remotePort, name, 0);
118 +                                       if (state < 1 || state > TCP_NSTATES) {
119 +                                               printf("%d\n", state );
120 +                                       } else {
121 +                                               printf("%s\n", tcpstates[state]);
122 +                                       }
123 +
124 +                    if ((vp->type != SNMP_ENDOFMIBVIEW) &&
125 +                        (vp->type != SNMP_NOSUCHOBJECT) &&
126 +                        (vp->type != SNMP_NOSUCHINSTANCE)) {
127 +                        /*
128 +                         * Check if last variable, and if so, save for next request.
129 +                         */
130 +                        if (vp->next_variable == NULL) {
131 +//                                                     printf("old: "); fprint_objid(stdout, tcpConnState_oid, tcpConnState_len);
132 +
133 +                            memmove(tcpConnState_oid, vp->name,
134 +                                    vp->name_length * sizeof(oid));
135 +                            tcpConnState_len = vp->name_length;
136 +
137 +//                                                     printf("new: "); fprint_objid(stdout, tcpConnState_oid, tcpConnState_len);
138 +                        }
139 +                    } else {
140 +                        /*
141 +                         * an exception value, so stop
142 +                         */
143 +                        running = 0;
144 +                    }
145 +                               }
146 +            } else {
147 +                /*
148 +                 * error in response, print it
149 +                 */
150 +                running = 0;
151 +            }
152 +        } else if (status == STAT_TIMEOUT) {
153 +            running = 0;
154 +        } else {                /* status == STAT_ERROR */
155 +            running = 0;
156          }
157 -        
158 -        /* Extract the local/remote information from the index values */
159 -        cp = tmpAddr.data;
160 -        cp[0] = vp->name[ 10 ] & 0xff;
161 -        cp[1] = vp->name[ 11 ] & 0xff;
162 -        cp[2] = vp->name[ 12 ] & 0xff;
163 -        cp[3] = vp->name[ 13 ] & 0xff;
164 -        localAddr.s_addr = tmpAddr.addr.s_addr;
165 -        localPort        = ntohs(vp->name[ 14 ]);
166 -        cp = tmpAddr.data;
167 -        cp[0] = vp->name[ 15 ] & 0xff;
168 -        cp[1] = vp->name[ 16 ] & 0xff;
169 -        cp[2] = vp->name[ 17 ] & 0xff;
170 -        cp[3] = vp->name[ 18 ] & 0xff;
171 -        remoteAddr.s_addr = tmpAddr.addr.s_addr;
172 -        remotePort        = ntohs(vp->name[ 19 ]);
173  
174 -        printf("%-5.5s", name);
175 -        inetprint(&localAddr,  localPort,  name, 1);
176 -        inetprint(&remoteAddr, remotePort, name, 0);
177 -        if ( state < 1 || state > TCP_NSTATES )
178 -            printf("%d\n", state );
179 -        else
180 -            printf("%s\n", tcpstates[ state ]);
181 -    }
182 -    snmp_free_varbind( var );
183 +        if (response) {
184 +            snmp_free_pdu(response);
185 +               }
186 +       }
187  }
188  
189  /*
This page took 0.069499 seconds and 3 git commands to generate.