📄 mtetriggerconf.c
字号:
/* * DisMan Event MIB: * Implementation of the trigger 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 "utilities/iquery.h"#include "disman/event/mteObjects.h"#include "disman/event/mteTrigger.h"#include "disman/event/mteTriggerConf.h"#include <ctype.h>/** Initializes the mteTriggerConf module */voidinit_mteTriggerConf(void){ init_trigger_table_data(); /* * Register config handler for user-level (fixed) triggers ... */ snmpd_register_config_handler("monitor", parse_mteMonitor, NULL, "triggername [-I] [-i OID | -o OID]* [-e event] expression "); snmpd_register_config_handler("defaultMonitors", parse_default_mteMonitors, NULL, "yes|no"); snmpd_register_config_handler("linkUpDownNotifications", parse_linkUpDown_traps, NULL, "yes|no"); /* * ... for persistent storage of various event table entries ... */ snmpd_register_config_handler("_mteTTable", parse_mteTTable, NULL, NULL); snmpd_register_config_handler("_mteTDTable", parse_mteTDTable, NULL, NULL); snmpd_register_config_handler("_mteTExTable", parse_mteTExTable, NULL, NULL); snmpd_register_config_handler("_mteTBlTable", parse_mteTBlTable, NULL, NULL); snmpd_register_config_handler("_mteTThTable", parse_mteTThTable, NULL, NULL); /* * ... and backwards compatability with the previous implementation. */ snmpd_register_config_handler("mteTriggerTable", parse_mteTriggerTable, NULL, NULL); /* * Register to save (non-fixed) entries when the agent shuts down */ snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_STORE_DATA, store_mteTTable, NULL); snmp_register_callback(SNMP_CALLBACK_APPLICATION, SNMPD_CALLBACK_PRE_UPDATE_CONFIG, clear_mteTTable, NULL);}/* ============================== * * utility routines * * ============================== */ /* * Find or create the specified trigger entry */struct mteTrigger *_find_mteTrigger_entry( char *owner, char *tname ){ netsnmp_variable_list owner_var, tname_var; netsnmp_tdata_row *row; /* * If there's already an existing entry, * then use that... */ memset(&owner_var, 0, sizeof(netsnmp_variable_list)); memset(&tname_var, 0, sizeof(netsnmp_variable_list)); snmp_set_var_typed_value(&owner_var, ASN_OCTET_STR, owner, strlen(owner)); snmp_set_var_typed_value(&tname_var, ASN_PRIV_IMPLIED_OCTET_STR, tname, strlen(tname)); owner_var.next_variable = &tname_var; row = netsnmp_tdata_row_get_byidx( trigger_table_data, &owner_var ); /* * ... otherwise, create a new one */ if (!row) row = mteTrigger_createEntry( owner, tname, 0 ); if (!row) return NULL; /* return (struct mteTrigger *)netsnmp_tdata_row_entry( row ); */ return (struct mteTrigger *)row->data;}struct mteTrigger *_find_typed_mteTrigger_entry( char *owner, char *tname, int type ){ struct mteTrigger *entry = _find_mteTrigger_entry( owner, tname ); 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 Existence/Boolean/Threshold trigger. */ if ( entry && (entry->flags & MTE_TRIGGER_FLAG_VALID) && (entry->mteTriggerTest & type )) { config_perror("duplicate trigger name"); return NULL; } return entry;}/* ================================================ * * Handlers for user-configured (static) triggers * * ================================================ */int_mteTrigger_callback_enable( int majorID, int minorID, void *serverargs, void *clientarg){ struct mteTrigger *entry = (struct mteTrigger *)clientarg; mteTrigger_enable( entry ); return 0;}voidparse_mteMonitor(const char *token, char *line){ char buf[ SPRINT_MAX_LEN]; char tname[MTE_STR1_LEN+1]; char *cp; long test = 0; char ename[MTE_STR1_LEN+1]; long flags = MTE_TRIGGER_FLAG_ENABLED | MTE_TRIGGER_FLAG_ACTIVE | MTE_TRIGGER_FLAG_FIXED | MTE_TRIGGER_FLAG_VWILD | MTE_TRIGGER_FLAG_SYSUPT | MTE_TRIGGER_FLAG_VALID; long idx = 0; long startup = 1; /* ??? or 0 */ long repeat = 600; netsnmp_session *sess = NULL; int seen_name = 0; char oid_name_buf[SPRINT_MAX_LEN]; oid name_buf[MAX_OID_LEN]; size_t name_buf_len; long op = 0; long value = 0; struct mteObject *object; struct mteTrigger *entry; DEBUGMSGTL(("disman:event:conf", "Parsing disman monitor config (%s)\n", line)); /* * Before parsing the configuration fully, first * skim through the config line in order to: * a) locate the name for the trigger, and * b) identify the type of trigger test * * This information will be used both for creating the trigger * entry, and registering any additional payload objects. */ memset( buf, 0, sizeof(buf)); memset( tname, 0, sizeof(tname)); memset( ename, 0, sizeof(ename)); for (cp = copy_nword(line, buf, SPRINT_MAX_LEN); ; cp = copy_nword(cp, buf, SPRINT_MAX_LEN)) { if ( buf[0] == '-' ) { switch (buf[1]) { case 't': /* No longer necessary */ break; case 'd': case 'e': case 'o': case 'r': case 'u': /* skip option parameter */ cp = skip_token( cp ); break; case 'D': case 'I': case 's': case 'S': /* flag options */ break; case 'i': /* * '-i' can act as a flag or take a parameter. * Handle either case. */ if (cp && *cp != '-') cp = skip_token( cp ); break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': /* accept negative values */ case '\0': /* and '-' placeholder value */ break; default: config_perror("unrecognised option"); return; } } else { /* * Save the first non-option parameter as the trigger name. * * This name will also be used to register entries in the * mteObjectsTable, so insert a distinguishing prefix. * This will ensure combined trigger entries don't clash with * each other, or with a similarly-named notification event. */ if ( !tname[0] ) { tname[0] = '_'; tname[1] = '_'; /* Placeholder */ memcpy( tname+2, buf, MTE_STR1_LEN-2 ); } else { /* * This marks the beginning of the monitor expression, * so we don't need to scan any further */ break; } } if (!cp) break; } /* * Now let's examine the expression to determine the type of * monitor being configured. There are four possible forms: * != OID (or ! OID) (existence test) * OID (existence test) * OID op VALUE (boolean test) * OID MIN MAX (threshold test) */ if ( *buf == '!' ) { /* * If the expression starts with '!=' or '!', then * it must be the first style of existence test. */ test = MTE_TRIGGER_EXISTENCE; } else { /* * Otherwise the first token is the OID to be monitored. * Skip it and look at the next token (if any). */ cp = copy_nword(cp, buf, SPRINT_MAX_LEN); if (cp) { /* * If this is a numeric value, then it'll be the MIN * field of a threshold test (the fourth form) * Otherwise it'll be the operation field of a * boolean test (the third form) */ if ( isdigit(buf[0]) || buf[0] == '-' ) test = MTE_TRIGGER_THRESHOLD; else test = MTE_TRIGGER_BOOLEAN; } else { /* * If there isn't a "next token", then this * must be the second style of existence test. */ test = MTE_TRIGGER_EXISTENCE; } } /* * Use the type of trigger test to update the trigger name buffer */ switch (test) { case MTE_TRIGGER_BOOLEAN: tname[1] = 'B'; break; case MTE_TRIGGER_THRESHOLD: tname[1] = 'T'; break; case MTE_TRIGGER_EXISTENCE: tname[1] = 'X'; break; } /* * Now start parsing again at the beginning of the directive, * extracting the various options... */ for (cp = copy_nword(line, buf, SPRINT_MAX_LEN); ; cp = copy_nword(cp, buf, SPRINT_MAX_LEN)) { if (buf[0] == '-' ) { switch (buf[1]) { case 'D': /* delta sample value */ flags |= MTE_TRIGGER_FLAG_DELTA; break; case 'd': /* discontinuity OID (implies delta sample) */ flags |= MTE_TRIGGER_FLAG_DELTA; if (buf[2] != 'i') flags |= MTE_TRIGGER_FLAG_DWILD; memset( oid_name_buf, 0, sizeof(oid_name_buf)); memset( name_buf, 0, sizeof( name_buf)); name_buf_len = MAX_OID_LEN; cp = copy_nword(cp, oid_name_buf, MTE_STR1_LEN); if (!snmp_parse_oid(oid_name_buf, name_buf, &name_buf_len)) { snmp_log(LOG_ERR, "discontinuity OID: %s\n", oid_name_buf); config_perror("unknown discontinuity OID"); mteObjects_removeEntries( "snmpd.conf", tname ); return; } if ( snmp_oid_compare( name_buf, name_buf_len, _sysUpTime_instance, _sysUpTime_inst_len) != 0 ) flags &= ~MTE_TRIGGER_FLAG_SYSUPT; break; case 'e': /* event */ cp = copy_nword(cp, ename, MTE_STR1_LEN); break; case 'I': /* value instance */ flags &= ~MTE_TRIGGER_FLAG_VWILD; break; /* * "instance" flag: * either non-wildcarded mteTriggerValueID * (backwards compatability - see '-I') * or exact payload OID * (c.f. notificationEvent config) */ case 'i': if ( *cp == '-' ) { /* Backwards compatibility - now '-I' */ flags &= ~MTE_TRIGGER_FLAG_VWILD; continue; } idx++; cp = copy_nword(cp, buf, SPRINT_MAX_LEN);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -