1 /**************************************************************
2 * Copyright (C) 2001 Tali Rozin, Optical Access
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation for any purpose and without fee is hereby granted,
8 * provided that the above copyright notice appear in all copies and that
9 * both that copyright notice and this permission notice appear in
10 * supporting documentation.
12 * TALI ROZIN DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
13 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
14 * ALEX ROZIN BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
15 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
16 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
17 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
19 ******************************************************************/
24 #include <net-snmp/net-snmp-config.h>
25 #include <net-snmp/net-snmp-includes.h>
26 #include <net-snmp/agent/net-snmp-agent-includes.h>
27 #include "util_funcs.h"
28 #include "statistics.h"
30 * Implementation headers
32 #include "agutil_api.h"
35 * File scope definitions section
38 * from MIB compilation
40 #define MIB_DESCR "EthStat"
41 #define etherStatsEntryFirstIndexBegin 11
42 #define IDetherStatsDroppedFrames 1
43 #define IDetherStatsCreateTime 2
44 #define IDetherStatsIndex 3
45 #define IDetherStatsDataSource 4
46 #define IDetherStatsDropEvents 5
47 #define IDetherStatsOctets 6
48 #define IDetherStatsPkts 7
49 #define IDetherStatsBroadcastPkts 8
50 #define IDetherStatsMulticastPkts 9
51 #define IDetherStatsCRCAlignErrors 10
52 #define IDetherStatsUndersizePkts 11
53 #define IDetherStatsOversizePkts 12
54 #define IDetherStatsFragments 13
55 #define IDetherStatsJabbers 14
56 #define IDetherStatsCollisions 15
57 #define IDetherStatsPkts64Octets 16
58 #define IDetherStatsPkts65to127Octets 17
59 #define IDetherStatsPkts128to255Octets 18
60 #define IDetherStatsPkts256to511Octets 19
61 #define IDetherStatsPkts512to1023Octets 20
62 #define IDetherStatsPkts1024to1518Octets 21
63 #define IDetherStatsOwner 22
64 #define IDetherStatsStatus 23
65 #define Leaf_etherStatsDataSource 2
66 #define Leaf_etherStatsOwner 20
67 #define Leaf_etherStatsStatus 21
68 #define MIN_etherStatsIndex 1
69 #define MAX_etherStatsIndex 65535
83 static TABLE_DEFINTION_T
85 static TABLE_DEFINTION_T *
90 * Control Table RowApi Callbacks
94 stat_Create(RMON_ENTRY_T * eptr)
95 { /* create the body: alloc it and set defaults */
97 static VAR_OID_T data_src_if_index_1 =
98 { 11, {1, 3, 6, 1, 2, 1, 2, 2, 1, 1, 1} };
100 eptr->body = AGMALLOC(sizeof(CRTL_ENTRY_T));
103 body = (CRTL_ENTRY_T *) eptr->body;
108 memcpy(&body->data_source, &data_src_if_index_1, sizeof(VAR_OID_T));
109 body->data_source.objid[body->data_source.length - 1] =
111 eptr->owner = AGSTRDUP("Startup Mgmt");
112 memset(&body->eth, 0, sizeof(ETH_STATS_T));
118 stat_Validate(RMON_ENTRY_T * eptr)
121 * T.B.D. (system dependent) check valid inteface in body->data_source;
128 stat_Activate(RMON_ENTRY_T * eptr)
130 CRTL_ENTRY_T *body = (CRTL_ENTRY_T *) eptr->body;
132 body->etherStatsCreateTime = AGUTIL_sys_up_time();
138 stat_Copy(RMON_ENTRY_T * eptr)
140 CRTL_ENTRY_T *body = (CRTL_ENTRY_T *) eptr->body;
141 CRTL_ENTRY_T *clone = (CRTL_ENTRY_T *) eptr->tmp;
144 (clone->data_source.objid, clone->data_source.length,
145 body->data_source.objid, body->data_source.length)) {
146 memcpy(&body->data_source, &clone->data_source, sizeof(VAR_OID_T));
153 stat_Deactivate(RMON_ENTRY_T * eptr)
155 CRTL_ENTRY_T *body = (CRTL_ENTRY_T *) eptr->body;
156 memset(&body->eth, 0, sizeof(ETH_STATS_T));
161 /***************************************************
162 * Function:var_etherStats2Entry
163 * Purpose: Handles the request for etherStats2Entry variable instances
164 ***************************************************/
166 var_etherStats2Entry(struct variable * vp, oid * name, size_t * length,
167 int exact, size_t * var_len,
168 WriteMethod ** write_method)
170 static long long_return;
171 static CRTL_ENTRY_T theEntry;
174 *write_method = NULL;
176 hdr = ROWAPI_header_ControlEntry(vp, name, length, exact, var_len,
178 &theEntry, sizeof(CRTL_ENTRY_T));
182 *var_len = sizeof(long); /* default */
185 case IDetherStatsDroppedFrames:
187 return (u_char *) & long_return;
188 case IDetherStatsCreateTime:
189 long_return = theEntry.etherStatsCreateTime;
190 return (u_char *) & long_return;
192 ag_trace("%s: unknown vp->magic=%d", table_ptr->name,
195 }; /* of switch by 'vp->magic' */
201 /***************************************************
202 * Function:write_etherStatsEntry
203 ***************************************************/
205 write_etherStatsEntry(int action, u_char * var_val, u_char var_val_type,
206 size_t var_val_len, u_char * statP,
207 oid * name, size_t name_len)
210 int leaf_id, snmp_status;
211 static int prev_action = COMMIT;
213 CRTL_ENTRY_T *cloned_body;
224 ROWAPI_do_another_action(name, etherStatsEntryFirstIndexBegin,
225 action, &prev_action, table_ptr,
226 sizeof(CRTL_ENTRY_T));
227 if (SNMP_ERR_NOERROR != snmp_status) {
228 ag_trace("failed action %d with %d", action, snmp_status);
234 * get values from PDU, check them and save them in the cloned entry
236 long_temp = name[etherStatsEntryFirstIndexBegin];
237 leaf_id = (int) name[etherStatsEntryFirstIndexBegin - 1];
238 hdr = ROWAPI_find(table_ptr, long_temp); /* it MUST be OK */
239 cloned_body = (CRTL_ENTRY_T *) hdr->tmp;
240 body = (CRTL_ENTRY_T *) hdr->body;
242 case Leaf_etherStatsDataSource:
243 snmp_status = AGUTIL_get_oid_value(var_val, var_val_type,
245 &cloned_body->data_source);
246 if (SNMP_ERR_NOERROR != snmp_status) {
249 if (RMON1_ENTRY_UNDER_CREATION != hdr->status &&
250 snmp_oid_compare(cloned_body->data_source.objid,
251 cloned_body->data_source.length,
252 body->data_source.objid,
253 body->data_source.length))
254 return SNMP_ERR_BADVALUE;
258 case Leaf_etherStatsOwner:
260 AGFREE(hdr->new_owner);
261 hdr->new_owner = AGMALLOC(MAX_OWNERSTRING);;
263 return SNMP_ERR_TOOBIG;
264 snmp_status = AGUTIL_get_string_value(var_val, var_val_type,
267 1, NULL, hdr->new_owner);
268 if (SNMP_ERR_NOERROR != snmp_status) {
272 case Leaf_etherStatsStatus:
273 snmp_status = AGUTIL_get_int_value(var_val, var_val_type,
278 if (SNMP_ERR_NOERROR != snmp_status) {
279 ag_trace("cannot browse etherStatsStatus");
282 hdr->new_status = long_temp;
286 ag_trace("%s:unknown leaf_id=%d\n", table_ptr->name,
288 return SNMP_ERR_NOSUCHNAME;
289 } /* of switch by 'leaf_id' */
291 } /* of switch by 'action' */
293 prev_action = action;
294 return SNMP_ERR_NOERROR;
297 /***************************************************
298 * Function:var_etherStatsEntry
299 * Purpose: Handles the request for etherStatsEntry variable instances
300 ***************************************************/
302 var_etherStatsEntry(struct variable * vp, oid * name, size_t * length,
303 int exact, size_t * var_len,
304 WriteMethod ** write_method)
306 static long long_return;
307 static CRTL_ENTRY_T theEntry;
310 *write_method = write_etherStatsEntry;
311 hdr = ROWAPI_header_ControlEntry(vp, name, length, exact, var_len,
313 &theEntry, sizeof(CRTL_ENTRY_T));
317 if (RMON1_ENTRY_VALID == hdr->status)
318 SYSTEM_get_eth_statistics(&theEntry.data_source, &theEntry.eth);
320 *var_len = sizeof(long);
323 case IDetherStatsIndex:
324 long_return = hdr->ctrl_index;
325 return (u_char *) & long_return;
326 case IDetherStatsDataSource:
327 *var_len = sizeof(oid) * theEntry.data_source.length;
328 return (unsigned char *) theEntry.data_source.objid;
329 case IDetherStatsDropEvents:
330 long_return = 0; /* theEntry.eth.etherStatsDropEvents; */
331 return (u_char *) & long_return;
332 case IDetherStatsOctets:
333 long_return = theEntry.eth.octets;
334 return (u_char *) & long_return;
335 case IDetherStatsPkts:
336 long_return = theEntry.eth.packets;
337 return (u_char *) & long_return;
338 case IDetherStatsBroadcastPkts:
339 long_return = theEntry.eth.bcast_pkts;
340 return (u_char *) & long_return;
341 case IDetherStatsMulticastPkts:
342 long_return = theEntry.eth.mcast_pkts;
343 return (u_char *) & long_return;
344 case IDetherStatsCRCAlignErrors:
345 long_return = theEntry.eth.crc_align;
346 return (u_char *) & long_return;
347 case IDetherStatsUndersizePkts:
348 long_return = theEntry.eth.undersize;
349 return (u_char *) & long_return;
350 case IDetherStatsOversizePkts:
351 long_return = theEntry.eth.oversize;
352 return (u_char *) & long_return;
353 case IDetherStatsFragments:
354 long_return = theEntry.eth.fragments;
355 return (u_char *) & long_return;
356 case IDetherStatsJabbers:
357 long_return = theEntry.eth.jabbers;
358 return (u_char *) & long_return;
359 case IDetherStatsCollisions:
360 long_return = theEntry.eth.collisions;
361 return (u_char *) & long_return;
362 case IDetherStatsPkts64Octets:
363 long_return = theEntry.eth.pkts_64;
364 return (u_char *) & long_return;
365 case IDetherStatsPkts65to127Octets:
366 long_return = theEntry.eth.pkts_65_127;
367 return (u_char *) & long_return;
368 case IDetherStatsPkts128to255Octets:
369 long_return = theEntry.eth.pkts_128_255;
370 return (u_char *) & long_return;
371 case IDetherStatsPkts256to511Octets:
372 long_return = theEntry.eth.pkts_256_511;
373 return (u_char *) & long_return;
374 case IDetherStatsPkts512to1023Octets:
375 long_return = theEntry.eth.pkts_512_1023;
376 return (u_char *) & long_return;
377 case IDetherStatsPkts1024to1518Octets:
378 long_return = theEntry.eth.pkts_1024_1518;
379 return (u_char *) & long_return;
380 case IDetherStatsOwner:
382 *var_len = strlen(hdr->owner);
383 return (unsigned char *) hdr->owner;
386 return (unsigned char *) "";
388 case IDetherStatsStatus:
389 long_return = hdr->status;
390 return (u_char *) & long_return;
393 }; /* of switch by 'vp->magic' */
398 #if 1 /* debug, but may be used for init. TBD: may be token snmpd.conf ? */
400 add_statistics_entry(int ctrl_index, int ifIndex)
404 ierr = ROWAPI_new(table_ptr, ctrl_index);
407 ag_trace("max. number exedes\n");
410 ag_trace("malloc failed");
413 ag_trace("ClbkCreate failed");
418 ag_trace("Unknown code %d", ierr);
423 register RMON_ENTRY_T *eptr = ROWAPI_find(table_ptr, ctrl_index);
425 ag_trace("cannot find it");
428 CRTL_ENTRY_T *body = (CRTL_ENTRY_T *) eptr->body;
430 body->data_source.objid[body->data_source.length - 1] =
433 eptr->new_status = RMON1_ENTRY_VALID;
434 ierr = ROWAPI_commit(table_ptr, ctrl_index);
436 ag_trace("ROWAPI_commit returned %d", ierr);
445 /***************************************************
446 * define Variables callbacks
447 ***************************************************/
448 oid oidstatisticsVariablesOid[] = { 1, 3, 6, 1, 2, 1, 16, 1 };
450 struct variable7 oidstatisticsVariables[] = {
451 {IDetherStatsIndex, ASN_INTEGER, RONLY, var_etherStatsEntry, 3,
453 {IDetherStatsDataSource, ASN_OBJECT_ID, RWRITE, var_etherStatsEntry, 3,
455 {IDetherStatsDropEvents, ASN_COUNTER, RONLY, var_etherStatsEntry, 3,
457 {IDetherStatsOctets, ASN_COUNTER, RONLY, var_etherStatsEntry, 3,
459 {IDetherStatsPkts, ASN_COUNTER, RONLY, var_etherStatsEntry, 3,
461 {IDetherStatsBroadcastPkts, ASN_COUNTER, RONLY, var_etherStatsEntry, 3,
463 {IDetherStatsMulticastPkts, ASN_COUNTER, RONLY, var_etherStatsEntry, 3,
465 {IDetherStatsCRCAlignErrors, ASN_COUNTER, RONLY, var_etherStatsEntry,
467 {IDetherStatsUndersizePkts, ASN_COUNTER, RONLY, var_etherStatsEntry, 3,
469 {IDetherStatsOversizePkts, ASN_COUNTER, RONLY, var_etherStatsEntry, 3,
471 {IDetherStatsFragments, ASN_COUNTER, RONLY, var_etherStatsEntry, 3,
473 {IDetherStatsJabbers, ASN_COUNTER, RONLY, var_etherStatsEntry, 3,
475 {IDetherStatsCollisions, ASN_COUNTER, RONLY, var_etherStatsEntry, 3,
477 {IDetherStatsPkts64Octets, ASN_COUNTER, RONLY, var_etherStatsEntry, 3,
479 {IDetherStatsPkts65to127Octets, ASN_COUNTER, RONLY,
480 var_etherStatsEntry, 3, {1, 1, 15}},
481 {IDetherStatsPkts128to255Octets, ASN_COUNTER, RONLY,
482 var_etherStatsEntry, 3, {1, 1, 16}},
483 {IDetherStatsPkts256to511Octets, ASN_COUNTER, RONLY,
484 var_etherStatsEntry, 3, {1, 1, 17}},
485 {IDetherStatsPkts512to1023Octets, ASN_COUNTER, RONLY,
486 var_etherStatsEntry, 3, {1, 1, 18}},
487 {IDetherStatsPkts1024to1518Octets, ASN_COUNTER, RONLY,
488 var_etherStatsEntry, 3, {1, 1, 19}},
489 {IDetherStatsOwner, ASN_OCTET_STR, RWRITE, var_etherStatsEntry, 3,
491 {IDetherStatsStatus, ASN_INTEGER, RWRITE, var_etherStatsEntry, 3,
493 {IDetherStatsDroppedFrames, ASN_COUNTER, RONLY, var_etherStats2Entry,
495 {IDetherStatsCreateTime, ASN_TIMETICKS, RONLY, var_etherStats2Entry, 3,
499 /***************************************************
500 * Function:init_statistics
501 * Purpose: register statistics objects in the agent
502 ***************************************************/
504 init_statistics(void)
506 REGISTER_MIB(MIB_DESCR, oidstatisticsVariables, variable7,
507 oidstatisticsVariablesOid);
509 ROWAPI_init_table(&StatCtrlTable, MIB_DESCR, 0, &stat_Create, NULL, /* &stat_Clone, */
510 NULL, /* &stat_Delete, */
512 &stat_Activate, &stat_Deactivate, &stat_Copy);
517 for (iii = 1; iii < 6; iii++) {
518 add_statistics_entry(iii, iii);
521 add_statistics_entry(10, 16);
522 add_statistics_entry(12, 11);
528 * end of file statistics.c