| 1 |
#include <net-snmp/net-snmp-config.h> |
| 2 |
#include <net-snmp/net-snmp-features.h> |
| 3 |
#include <net-snmp/net-snmp-includes.h> |
| 4 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 5 |
|
| 6 |
#include <limits.h> |
| 7 |
|
| 8 |
netsnmp_feature_require(container_fifo); |
| 9 |
|
| 10 |
#include "deliverByNotify.h" |
| 11 |
|
| 12 |
/* we should never split beyond this */ |
| 13 |
#define MAX_MESSAGE_COUNT 128 |
| 14 |
#define BASE_PACKET_SIZE 100 /* should be enough to store SNMPv3 msg headers */ |
| 15 |
|
| 16 |
/* if v is !NULL, then estimate it's likely size */ |
| 17 |
#define ESTIMATE_VAR_SIZE(v) (v?(v->name_length + v->val_len + 8):0) |
| 18 |
|
| 19 |
static void parse_deliver_config(const char *, char *); |
| 20 |
static void parse_deliver_maxsize_config(const char *, char *); |
| 21 |
static void parse_data_notification_oid_config(const char *, char *); |
| 22 |
static void parse_periodic_time_oid_config(const char *, char *); |
| 23 |
static void parse_message_number_oid_config(const char *, char *); |
| 24 |
static void parse_max_message_number_oid_config(const char *, char *); |
| 25 |
static void free_deliver_config(void); |
| 26 |
|
| 27 |
static void _schedule_next_execute_time(void); |
| 28 |
|
| 29 |
oid data_notification_oid[MAX_OID_LEN] |
| 30 |
= { 1, 3, 6, 1, 4, 1, 8072, 3, 1, 5, 4, 0, 1 }; |
| 31 |
size_t data_notification_oid_len = 13; |
| 32 |
|
| 33 |
oid netsnmp_periodic_time_oid[MAX_OID_LEN] |
| 34 |
= { 1, 3, 6, 1, 4, 1, 8072, 3, 1, 5, 3, 1, 0 }; |
| 35 |
size_t netsnmp_periodic_time_oid_len = 13; |
| 36 |
|
| 37 |
oid netsnmp_message_number_oid[MAX_OID_LEN] |
| 38 |
= { 1, 3, 6, 1, 4, 1, 8072, 3, 1, 5, 3, 2, 0 }; |
| 39 |
size_t netsnmp_message_number_oid_len = 13; |
| 40 |
|
| 41 |
oid netsnmp_max_message_number_oid[MAX_OID_LEN] |
| 42 |
= { 1, 3, 6, 1, 4, 1, 8072, 3, 1, 5, 3, 3, 0 }; |
| 43 |
size_t netsnmp_max_message_number_oid_len = 13; |
| 44 |
|
| 45 |
oid objid_snmptrap[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 }; |
| 46 |
|
| 47 |
#define DEFAULT_MAX_DELIVER_SIZE -1 |
| 48 |
static int default_max_size; |
| 49 |
unsigned int alarm_reg; |
| 50 |
static netsnmp_container *deliver_container; |
| 51 |
|
| 52 |
static int |
| 53 |
_deliver_compare(const void *p, const void *q) |
| 54 |
{ |
| 55 |
const deliver_by_notify *lhs = p, *rhs = q; |
| 56 |
|
| 57 |
/* sort by the next_run time */ |
| 58 |
if (lhs->next_run < rhs->next_run) |
| 59 |
return -1; |
| 60 |
else |
| 61 |
return 1; |
| 62 |
} |
| 63 |
|
| 64 |
/** Initializes the mteTrigger module */ |
| 65 |
void |
| 66 |
init_deliverByNotify(void) |
| 67 |
{ |
| 68 |
/* register the config tokens */ |
| 69 |
snmpd_register_config_handler("deliverByNotify", |
| 70 |
&parse_deliver_config, &free_deliver_config, |
| 71 |
"[-p] [-m] [-s maxsize] FREQUENCY OID"); |
| 72 |
|
| 73 |
snmpd_register_config_handler("deliverByNotifyMaxPacketSize", |
| 74 |
&parse_deliver_maxsize_config, NULL, |
| 75 |
"sizeInBytes"); |
| 76 |
|
| 77 |
snmpd_register_config_handler("deliverByNotifyOid", |
| 78 |
&parse_data_notification_oid_config, |
| 79 |
NULL, /* XXX: reset to default */ |
| 80 |
"OID"); |
| 81 |
|
| 82 |
snmpd_register_config_handler("deliverByNotifyFrequencyOid", |
| 83 |
&parse_periodic_time_oid_config, |
| 84 |
NULL, /* XXX: reset to default */ |
| 85 |
"OID"); |
| 86 |
|
| 87 |
snmpd_register_config_handler("deliverByNotifyMessageNumberOid", |
| 88 |
&parse_message_number_oid_config, |
| 89 |
NULL, /* XXX: reset to default */ |
| 90 |
"OID"); |
| 91 |
|
| 92 |
snmpd_register_config_handler("deliverByNotifyMaxMessageNumberOid", |
| 93 |
&parse_max_message_number_oid_config, |
| 94 |
NULL, /* XXX: reset to default */ |
| 95 |
"OID"); |
| 96 |
|
| 97 |
/* create the container to store the config objects*/ |
| 98 |
deliver_container = netsnmp_container_find("deliverByNotify:fifo"); |
| 99 |
if (NULL == deliver_container) { |
| 100 |
snmp_log(LOG_ERR, |
| 101 |
"deliverByNotify: failed to initialize our data container\n"); |
| 102 |
return; |
| 103 |
} |
| 104 |
deliver_container->container_name = strdup("deliverByNotify"); |
| 105 |
deliver_container->compare = _deliver_compare; |
| 106 |
|
| 107 |
/* set the defaults */ |
| 108 |
default_max_size = DEFAULT_MAX_DELIVER_SIZE; |
| 109 |
|
| 110 |
alarm_reg = 0; |
| 111 |
} |
| 112 |
|
| 113 |
void |
| 114 |
_parse_config_oid(const char *token, char *line, |
| 115 |
oid *oid_store, size_t *oid_store_len) { |
| 116 |
size_t tmp_len = MAX_OID_LEN; |
| 117 |
/* parse the OID given */ |
| 118 |
|
| 119 |
if (!snmp_parse_oid(line, oid_store, &tmp_len)) { |
| 120 |
char buf[SPRINT_MAX_LEN]; |
| 121 |
snprintf(buf, SPRINT_MAX_LEN-1, "unknown %s OID: %s", token, line); |
| 122 |
config_perror(buf); |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
*oid_store_len = tmp_len; |
| 127 |
} |
| 128 |
|
| 129 |
void |
| 130 |
parse_data_notification_oid_config(const char *token, char *line) { |
| 131 |
_parse_config_oid(token, line, |
| 132 |
data_notification_oid, &data_notification_oid_len); |
| 133 |
} |
| 134 |
|
| 135 |
void |
| 136 |
parse_periodic_time_oid_config(const char *token, char *line) { |
| 137 |
_parse_config_oid(token, line, |
| 138 |
netsnmp_periodic_time_oid, &netsnmp_periodic_time_oid_len); |
| 139 |
} |
| 140 |
|
| 141 |
void |
| 142 |
parse_message_number_oid_config(const char *token, char *line) { |
| 143 |
_parse_config_oid(token, line, |
| 144 |
netsnmp_message_number_oid, |
| 145 |
&netsnmp_message_number_oid_len); |
| 146 |
} |
| 147 |
|
| 148 |
void |
| 149 |
parse_max_message_number_oid_config(const char *token, char *line) { |
| 150 |
_parse_config_oid(token, line, |
| 151 |
netsnmp_max_message_number_oid, |
| 152 |
&netsnmp_max_message_number_oid_len); |
| 153 |
} |
| 154 |
|
| 155 |
void |
| 156 |
parse_deliver_config(const char *token, char *line) { |
| 157 |
const char *cp = line; |
| 158 |
char buf[SPRINT_MAX_LEN]; |
| 159 |
size_t buf_len = SPRINT_MAX_LEN; |
| 160 |
int max_size = DEFAULT_MAX_DELIVER_SIZE; |
| 161 |
int frequency; |
| 162 |
oid target_oid[MAX_OID_LEN]; |
| 163 |
size_t target_oid_len = MAX_OID_LEN; |
| 164 |
deliver_by_notify *new_notify = NULL; |
| 165 |
int flags = 0; |
| 166 |
|
| 167 |
while(cp && *cp == '-') { |
| 168 |
switch (*(cp+1)) { |
| 169 |
case 's': |
| 170 |
cp = skip_token_const(cp); |
| 171 |
if (!cp) { |
| 172 |
config_perror("no argument given to -s"); |
| 173 |
return; |
| 174 |
} |
| 175 |
max_size = atoi(cp); |
| 176 |
break; |
| 177 |
|
| 178 |
case 'p': |
| 179 |
flags = flags | NETSNMP_DELIVER_NO_PERIOD_OID; |
| 180 |
break; |
| 181 |
|
| 182 |
case 'm': |
| 183 |
flags = flags | NETSNMP_DELIVER_NO_MSG_COUNTS; |
| 184 |
break; |
| 185 |
|
| 186 |
default: |
| 187 |
config_perror("unknown flag"); |
| 188 |
return; |
| 189 |
} |
| 190 |
cp = skip_token_const(cp); |
| 191 |
} |
| 192 |
|
| 193 |
if (!cp) { |
| 194 |
config_perror("no frequency given"); |
| 195 |
return; |
| 196 |
} |
| 197 |
copy_nword_const(cp, buf, buf_len); |
| 198 |
frequency = netsnmp_string_time_to_secs(buf); |
| 199 |
cp = skip_token_const(cp); |
| 200 |
|
| 201 |
if (frequency <= 0) { |
| 202 |
config_perror("illegal frequency given"); |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
if (!cp) { |
| 207 |
config_perror("no OID given"); |
| 208 |
return; |
| 209 |
} |
| 210 |
|
| 211 |
/* parse the OID given */ |
| 212 |
if (!snmp_parse_oid(cp, target_oid, &target_oid_len)) { |
| 213 |
config_perror("unknown deliverByNotify OID"); |
| 214 |
DEBUGMSGTL(("deliverByNotify", "The OID with the problem: %s\n", cp)); |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
/* set up the object to store all the data */ |
| 219 |
new_notify = SNMP_MALLOC_TYPEDEF(deliver_by_notify); |
| 220 |
new_notify->frequency = frequency; |
| 221 |
new_notify->max_packet_size = max_size; |
| 222 |
new_notify->last_run = time(NULL); |
| 223 |
new_notify->next_run = new_notify->last_run + frequency; |
| 224 |
new_notify->flags = flags; |
| 225 |
|
| 226 |
new_notify->target = malloc(target_oid_len * sizeof(oid)); |
| 227 |
new_notify->target_len = target_oid_len; |
| 228 |
memcpy(new_notify->target, target_oid, target_oid_len*sizeof(oid)); |
| 229 |
|
| 230 |
/* XXX: need to do the whole container */ |
| 231 |
snmp_alarm_register(calculate_time_until_next_run(new_notify, NULL), 0, |
| 232 |
&deliver_execute, NULL); |
| 233 |
|
| 234 |
/* add it to the container */ |
| 235 |
CONTAINER_INSERT(deliver_container, new_notify); |
| 236 |
_schedule_next_execute_time(); |
| 237 |
} |
| 238 |
|
| 239 |
void |
| 240 |
parse_deliver_maxsize_config(const char *token, char *line) { |
| 241 |
default_max_size = atoi(line); |
| 242 |
} |
| 243 |
|
| 244 |
static void |
| 245 |
_free_deliver_obj(void *p, void *context) |
| 246 |
{ |
| 247 |
deliver_by_notify *obj = p; |
| 248 |
|
| 249 |
netsnmp_assert_or_return(obj != NULL, ); |
| 250 |
SNMP_FREE(obj->target); |
| 251 |
SNMP_FREE(obj); |
| 252 |
} |
| 253 |
|
| 254 |
void |
| 255 |
free_deliver_config(void) { |
| 256 |
default_max_size = DEFAULT_MAX_DELIVER_SIZE; |
| 257 |
CONTAINER_CLEAR(deliver_container, _free_deliver_obj, NULL); |
| 258 |
if (alarm_reg) { |
| 259 |
snmp_alarm_unregister(alarm_reg); |
| 260 |
alarm_reg = 0; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
void |
| 265 |
deliver_execute(unsigned int clientreg, void *clientarg) { |
| 266 |
netsnmp_variable_list *vars, *walker, *deliver_notification, *vartmp; |
| 267 |
netsnmp_variable_list *ready_for_delivery[MAX_MESSAGE_COUNT]; |
| 268 |
netsnmp_session *sess; |
| 269 |
int rc, i; |
| 270 |
deliver_by_notify *obj; |
| 271 |
netsnmp_iterator *iterator; |
| 272 |
time_t now = time(NULL); |
| 273 |
u_long message_count, max_message_count, tmp_long; |
| 274 |
u_long *max_message_count_ptrs[MAX_MESSAGE_COUNT]; |
| 275 |
size_t estimated_pkt_size; |
| 276 |
|
| 277 |
DEBUGMSGTL(("deliverByNotify", "Starting the execute routine\n")); |
| 278 |
|
| 279 |
/* XXX: need to do the whole container */ |
| 280 |
iterator = CONTAINER_ITERATOR(deliver_container); |
| 281 |
netsnmp_assert_or_return(iterator != NULL,); |
| 282 |
|
| 283 |
sess = netsnmp_query_get_default_session(); |
| 284 |
|
| 285 |
for(obj = ITERATOR_FIRST(iterator); obj; |
| 286 |
obj = ITERATOR_NEXT(iterator)) { |
| 287 |
|
| 288 |
/* check if we need to run this one yet */ |
| 289 |
if (obj->next_run > now) |
| 290 |
continue; |
| 291 |
|
| 292 |
max_message_count = 1; |
| 293 |
message_count = 0; |
| 294 |
|
| 295 |
/* fill the varbind list with the target object */ |
| 296 |
vars = SNMP_MALLOC_TYPEDEF( netsnmp_variable_list ); |
| 297 |
snmp_set_var_objid( vars, obj->target, obj->target_len ); |
| 298 |
vars->type = ASN_NULL; |
| 299 |
|
| 300 |
/* walk the OID tree for the data */ |
| 301 |
rc = netsnmp_query_walk(vars, sess); |
| 302 |
if (rc != SNMP_ERR_NOERROR) { |
| 303 |
/* XXX: disable? and reset the next query time point! */ |
| 304 |
snmp_log(LOG_ERR, "deliverByNotify: failed to issue the query"); |
| 305 |
ITERATOR_RELEASE(iterator); |
| 306 |
free(vars); |
| 307 |
return; |
| 308 |
} |
| 309 |
|
| 310 |
walker = vars; |
| 311 |
|
| 312 |
while (walker) { |
| 313 |
|
| 314 |
/* Set up the notification itself */ |
| 315 |
deliver_notification = NULL; |
| 316 |
estimated_pkt_size = BASE_PACKET_SIZE; |
| 317 |
|
| 318 |
/* add in the notification type */ |
| 319 |
snmp_varlist_add_variable(&deliver_notification, |
| 320 |
objid_snmptrap, OID_LENGTH(objid_snmptrap), |
| 321 |
ASN_OBJECT_ID, |
| 322 |
data_notification_oid, |
| 323 |
data_notification_oid_len * sizeof(oid)); |
| 324 |
estimated_pkt_size += ESTIMATE_VAR_SIZE(deliver_notification); |
| 325 |
|
| 326 |
/* add in the current message number in this sequence */ |
| 327 |
if (!(obj->flags & NETSNMP_DELIVER_NO_PERIOD_OID)) { |
| 328 |
tmp_long = obj->frequency; |
| 329 |
snmp_varlist_add_variable(&deliver_notification, |
| 330 |
netsnmp_periodic_time_oid, |
| 331 |
netsnmp_periodic_time_oid_len, |
| 332 |
ASN_UNSIGNED, |
| 333 |
(const void *) &tmp_long, |
| 334 |
sizeof(tmp_long)); |
| 335 |
estimated_pkt_size += ESTIMATE_VAR_SIZE(deliver_notification); |
| 336 |
} |
| 337 |
|
| 338 |
/* add in the current message number in this sequence */ |
| 339 |
message_count++; |
| 340 |
if (message_count > MAX_MESSAGE_COUNT) { |
| 341 |
snmp_log(LOG_ERR, "delivery construct grew too large... giving up\n"); |
| 342 |
/* XXX: disable it */ |
| 343 |
/* XXX: send a notification about it? */ |
| 344 |
ITERATOR_RELEASE(iterator); |
| 345 |
return; |
| 346 |
} |
| 347 |
|
| 348 |
/* store this for later updating and sending */ |
| 349 |
ready_for_delivery[message_count-1] = deliver_notification; |
| 350 |
|
| 351 |
if (!(obj->flags & NETSNMP_DELIVER_NO_MSG_COUNTS)) { |
| 352 |
snmp_varlist_add_variable(&deliver_notification, |
| 353 |
netsnmp_message_number_oid, |
| 354 |
netsnmp_message_number_oid_len, |
| 355 |
ASN_UNSIGNED, |
| 356 |
(const void *) &message_count, |
| 357 |
sizeof(message_count)); |
| 358 |
estimated_pkt_size += ESTIMATE_VAR_SIZE(deliver_notification); |
| 359 |
|
| 360 |
/* add in the max message number count for this sequence */ |
| 361 |
vartmp = snmp_varlist_add_variable(&deliver_notification, |
| 362 |
netsnmp_max_message_number_oid, |
| 363 |
netsnmp_max_message_number_oid_len, |
| 364 |
ASN_UNSIGNED, |
| 365 |
(const void *) &max_message_count, |
| 366 |
sizeof(max_message_count)); |
| 367 |
estimated_pkt_size += ESTIMATE_VAR_SIZE(deliver_notification); |
| 368 |
|
| 369 |
/* we'll need to update this counter later */ |
| 370 |
max_message_count_ptrs[message_count-1] = |
| 371 |
(u_long *) vartmp->val.integer; |
| 372 |
} else { |
| 373 |
/* just to be sure */ |
| 374 |
max_message_count_ptrs[message_count-1] = NULL; |
| 375 |
} |
| 376 |
|
| 377 |
/* copy in the collected data */ |
| 378 |
while(walker) { |
| 379 |
snmp_varlist_add_variable(&deliver_notification, |
| 380 |
walker->name, walker->name_length, |
| 381 |
walker->type, |
| 382 |
walker->val.string, walker->val_len); |
| 383 |
|
| 384 |
/* 8 byte padding for ASN encodings an a few extra OID bytes */ |
| 385 |
estimated_pkt_size += ESTIMATE_VAR_SIZE(walker); |
| 386 |
|
| 387 |
walker = walker->next_variable; |
| 388 |
|
| 389 |
/* if the current size PLUS the next one (which is now |
| 390 |
in 'walker') is greater than the limet then we stop here */ |
| 391 |
if (obj->max_packet_size > 0 && |
| 392 |
estimated_pkt_size + |
| 393 |
ESTIMATE_VAR_SIZE(walker) >= |
| 394 |
obj->max_packet_size) { |
| 395 |
break; |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
/* send out the notification */ |
| 400 |
send_v2trap(deliver_notification); |
| 401 |
} |
| 402 |
|
| 403 |
for(i = 0; i < message_count; i++) { |
| 404 |
/* update the max count pointer */ |
| 405 |
if (max_message_count_ptrs[i]) |
| 406 |
*(max_message_count_ptrs[i]) = message_count; |
| 407 |
|
| 408 |
send_v2trap(ready_for_delivery[i]); |
| 409 |
snmp_free_varbind(ready_for_delivery[i]); |
| 410 |
} |
| 411 |
|
| 412 |
snmp_free_varbind(vars); |
| 413 |
|
| 414 |
/* record this as the time processed */ |
| 415 |
/* XXX: this may creep by a few seconds when processing and maybe we want |
| 416 |
to do the time stamp at the beginning? */ |
| 417 |
obj->last_run = time(NULL); |
| 418 |
} |
| 419 |
ITERATOR_RELEASE(iterator); |
| 420 |
|
| 421 |
/* calculate the next time to sleep for */ |
| 422 |
_schedule_next_execute_time(); |
| 423 |
} |
| 424 |
|
| 425 |
int |
| 426 |
calculate_time_until_next_run(deliver_by_notify *it, time_t *now) { |
| 427 |
time_t local_now; |
| 428 |
|
| 429 |
/* if we weren't passed a valid time, fake it */ |
| 430 |
if (NULL == now) { |
| 431 |
now = &local_now; |
| 432 |
time(&local_now); |
| 433 |
} |
| 434 |
|
| 435 |
netsnmp_assert_or_return(it->last_run != 0, -1); |
| 436 |
|
| 437 |
/* set the timestamp for the next run */ |
| 438 |
it->next_run = it->last_run + it->frequency; |
| 439 |
|
| 440 |
/* how long until the next run? */ |
| 441 |
return it->next_run - *now; |
| 442 |
} |
| 443 |
|
| 444 |
static void |
| 445 |
_schedule_next_execute_time(void) { |
| 446 |
time_t local_now = time(NULL); |
| 447 |
int sleep_for = INT_MAX; |
| 448 |
int next_time; |
| 449 |
netsnmp_iterator *iterator; |
| 450 |
deliver_by_notify *obj; |
| 451 |
|
| 452 |
DEBUGMSGTL(("deliverByNotify", "Calculating scheduling needed\n")); |
| 453 |
|
| 454 |
if (alarm_reg) { |
| 455 |
snmp_alarm_unregister(alarm_reg); |
| 456 |
alarm_reg = 0; |
| 457 |
} |
| 458 |
|
| 459 |
iterator = CONTAINER_ITERATOR(deliver_container); |
| 460 |
if (NULL == iterator) |
| 461 |
return; |
| 462 |
|
| 463 |
for(obj = ITERATOR_FIRST(iterator); obj; |
| 464 |
obj = ITERATOR_NEXT(iterator)) { |
| 465 |
next_time = calculate_time_until_next_run(obj, &local_now); |
| 466 |
DEBUGMSGTL(("deliverByNotify", " obj: %d (last=%lld, next_run=%lld)\n", next_time, (long long)obj->last_run, (long long)obj->next_run)); |
| 467 |
if (next_time < sleep_for) |
| 468 |
sleep_for = next_time; |
| 469 |
} |
| 470 |
|
| 471 |
if (sleep_for != INT_MAX) { |
| 472 |
if (sleep_for < 1) |
| 473 |
sleep_for = 1; /* give at least a small pause */ |
| 474 |
DEBUGMSGTL(("deliverByNotify", "Next execution in %d (max = %d) seconds\n", |
| 475 |
sleep_for, INT_MAX)); |
| 476 |
alarm_reg = snmp_alarm_register(sleep_for, 0, &deliver_execute, NULL); |
| 477 |
} |
| 478 |
|
| 479 |
ITERATOR_RELEASE(iterator); |
| 480 |
} |
| 481 |
|