📄 mteeventconf.c
字号:
/* * DisMan Event MIB: * Implementation of the event table configure handling */#include <net-snmp/net-snmp-config.h>#include <net-snmp/net-snmp-includes.h>#include <net-snmp/agent/net-snmp-agent-includes.h>#include <net-snmp/agent/agent_callbacks.h>#include "disman/event/mteObjects.h"#include "disman/event/mteEvent.h"#include "disman/event/mteEventConf.h"/** Initializes the mteEventsConf module */voidinit_mteEventConf(void){ init_event_table_data(); /* * Register config handlers for user-level (fixed) events.... */ snmpd_register_config_handler("notificationEvent", parse_notificationEvent, NULL, "eventname notifyOID [-m] [-i OID|-o OID]*"); snmpd_register_config_handler("setEvent", parse_setEvent, NULL, "eventname [-I] OID = value"); netsnmp_ds_register_config(ASN_BOOLEAN, netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_APPTYPE), "strictDisman", NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_STRICT_DISMAN); /* * ... and for persistent storage of dynamic event table entries. * * (The previous implementation didn't store these entries, * so we don't need to worry about backwards compatability) */ snmpd_register_config_handler("_mteETable", parse_mteETable, NULL, NULL); snmpd_register_config_handler("_mteENotTable", parse_mteENotTable, NULL, NULL); snmpd_register_config_handler("_mteESetTable", parse_mteESetTable, NULL, NULL); /* * Register to save (non-fixed) entries when the agent shuts down */ snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_STORE_DATA, store_mteETable, NULL); snmp_register_callback(SNMP_CALLBACK_APPLICATION, SNMPD_CALLBACK_PRE_UPDATE_CONFIG, clear_mteETable, NULL);}/* ============================== * * utility routines * * ============================== */ /* * Find or create the specified event entry */struct mteEvent *_find_mteEvent_entry( char *owner, char *ename ){ netsnmp_variable_list owner_var, ename_var; netsnmp_tdata_row *row; /* * If there's already an existing entry, * then use that... */ memset(&owner_var, 0, sizeof(netsnmp_variable_list)); memset(&ename_var, 0, sizeof(netsnmp_variable_list)); snmp_set_var_typed_value(&owner_var, ASN_OCTET_STR, owner, strlen(owner)); snmp_set_var_typed_value(&ename_var, ASN_PRIV_IMPLIED_OCTET_STR, ename, strlen(ename)); owner_var.next_variable = &ename_var; row = netsnmp_tdata_row_get_byidx( event_table_data, &owner_var ); /* * ... otherwise, create a new one */ if (!row) row = mteEvent_createEntry( owner, ename, 0 ); if (!row) return NULL; /* return (struct mteEvent *)netsnmp_tdata_row_entry( row ); */ return (struct mteEvent *)row->data;}struct mteEvent *_find_typed_mteEvent_entry( char *owner, char *ename, int type ){ struct mteEvent *entry = _find_mteEvent_entry( owner, ename ); if (!entry) return NULL; /* * If this is an existing (i.e. valid) entry of the * same type, then throw an error and discard it. * But allow combined Set/Notification events. */ if ( entry && (entry->flags & MTE_EVENT_FLAG_VALID) && (entry->mteEventActions & type )) { config_perror("error: duplicate event name"); return NULL; } return entry;}/* ============================== * * User-configured (static) events * * ============================== */voidparse_notificationEvent( const char *token, char *line ){ char *owner = "snmpd.conf"; char ename[MTE_STR1_LEN+1]; char buf[SPRINT_MAX_LEN]; oid name_buf[MAX_OID_LEN]; size_t name_buf_len; struct mteEvent *entry; struct mteObject *object; int wild = 1; int idx = 0; char *cp;#ifndef NETSNMP_DISABLE_MIB_LOADING struct tree *tp;#endif struct varbind_list *var; DEBUGMSGTL(("disman:event:conf", "Parsing notificationEvent config\n")); /* * The event name could be used directly to index the mteObjectsTable. * But it's quite possible that the same name could also be used to * set up a mteTriggerTable entry (with trigger-specific objects). * * To avoid such a clash, we'll add a prefix ("_E"). */ memset(ename, 0, sizeof(ename)); ename[0] = '_'; ename[1] = 'E'; cp = copy_nword(line, ename+2, MTE_STR1_LEN-2); if (!cp || ename[2] == '\0') { config_perror("syntax error: no event name"); return; } /* * Parse the notification OID field ... */ cp = copy_nword(cp, buf, SPRINT_MAX_LEN); if ( buf[0] == '\0' ) { config_perror("syntax error: no notification OID"); return; } name_buf_len = MAX_OID_LEN; if (!snmp_parse_oid(buf, name_buf, &name_buf_len)) { snmp_log(LOG_ERR, "notificationEvent OID: %s\n", buf); config_perror("unknown notification OID"); return; } /* * ... and the relevant object/instances. */ if ( cp && *cp=='-' && *(cp+1)=='m' ) {#ifdef NETSNMP_DISABLE_MIB_LOADING config_perror("Can't use -m if MIB loading is disabled"); return;#else /* * Use the MIB definition to add the standard * notification payload to the mteObjectsTable. */ cp = skip_token( cp ); tp = get_tree( name_buf, name_buf_len, get_tree_head()); if (!tp) { config_perror("Can't locate notification payload info"); return; } for (var = tp->varbinds; var; var=var->next) { idx++; object = mteObjects_addOID( "snmpd.conf", ename, idx, var->vblabel, wild ); idx = object->mteOIndex; }#endif } while (cp) { if ( *cp == '-' ) { switch (*(cp+1)) { case 'm': config_perror("-m option must come first"); return; case 'i': /* exact instance */ case 'w': /* "not-wild" (backward compatability) */ wild = 0; break; case 'o': /* wildcarded object */ wild = 1; break; default: config_perror("unrecognised option"); return; } cp = skip_token( cp ); if (!cp) { config_perror("missing parameter"); return; } } idx++; cp = copy_nword(cp, buf, SPRINT_MAX_LEN); object = mteObjects_addOID( "snmpd.conf", ename, idx, buf, wild ); idx = object->mteOIndex; wild = 1; /* default to wildcarded objects */ } /* * If the entry has parsed successfully, then create, * populate and activate the new event entry. */ entry = _find_typed_mteEvent_entry(owner, ename+2, MTE_EVENT_NOTIFICATION); if (!entry) { mteObjects_removeEntries( "snmpd.conf", ename ); return; } entry->mteNotification_len = name_buf_len; memcpy( entry->mteNotification, name_buf, name_buf_len*sizeof(oid)); memcpy( entry->mteNotifyOwner, "snmpd.conf", 10 ); memcpy( entry->mteNotifyObjects, ename, MTE_STR1_LEN ); entry->mteEventActions |= MTE_EVENT_NOTIFICATION; entry->flags |= MTE_EVENT_FLAG_ENABLED | MTE_EVENT_FLAG_ACTIVE | MTE_EVENT_FLAG_FIXED | MTE_EVENT_FLAG_VALID; return;}voidparse_setEvent( const char *token, char *line ){ char *owner = "snmpd.conf"; char ename[MTE_STR1_LEN+1]; char buf[SPRINT_MAX_LEN]; oid name_buf[MAX_OID_LEN]; size_t name_buf_len; long value; int wild = 1; struct mteEvent *entry; char *cp; DEBUGMSGTL(("disman:event:conf", "Parsing setEvent config... ")); memset( ename, 0, sizeof(ename)); cp = copy_nword(line, ename, MTE_STR1_LEN); if (!cp || ename[0] == '\0') { config_perror("syntax error: no event name"); return; } if (cp && *cp=='-' && *(cp+1)=='I') { wild = 0; /* an instance assignment */ cp = skip_token( cp ); } /* * Parse the SET assignment in the form "OID = value" */ cp = copy_nword(cp, buf, SPRINT_MAX_LEN); if ( buf[0] == '\0' ) { config_perror("syntax error: no set OID"); return; } name_buf_len = MAX_OID_LEN; if (!snmp_parse_oid(buf, name_buf, &name_buf_len)) { snmp_log(LOG_ERR, "setEvent OID: %s\n", buf);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -