📄 notification_log.c
字号:
#include <net-snmp/net-snmp-config.h>#include <sys/types.h>#if HAVE_NETINET_IN_H#include <netinet/in.h>#endif#if HAVE_NETDB_H#include <netdb.h>#endif#include <net-snmp/net-snmp-includes.h>#include <net-snmp/agent/net-snmp-agent-includes.h>#include <net-snmp/agent/ds_agent.h>#include <net-snmp/agent/instance.h>#include <net-snmp/agent/table.h>#include <net-snmp/agent/table_data.h>#include <net-snmp/agent/table_dataset.h>#include "notification_log.h"static u_long num_received = 0;static u_long num_deleted = 0;static u_long max_logged = 1000; /* goes against the mib default of infinite */static u_long max_age = 1440; /* 1440 = 24 hours, which is the mib default */netsnmp_table_data_set *nlmLogTable;netsnmp_table_data_set *nlmLogVarTable;voidnetsnmp_notif_log_remove_oldest(int count){ netsnmp_table_row *deleterow, *tmprow, *deletevarrow; DEBUGMSGTL(("notification_log", "deleting %d log entry(s)\n", count)); deleterow = netsnmp_table_data_set_get_first_row(nlmLogTable); for (; count && deleterow; deleterow = tmprow, --count) { /* * delete contained varbinds * xxx-rks: note that this assumes that only the default * log is used (ie for the first nlmLogTable row, the * first nlmLogVarTable rows will be the right ones). * the right thing to do would be to do a find based on * the nlmLogTable oid. */ DEBUGMSGTL(("9:notification_log", " deleting notification\n")); DEBUGIF("9:notification_log") { DEBUGMSGTL(("9:notification_log", " base oid:")); DEBUGMSGOID(("9:notification_log", deleterow->index_oid, deleterow->index_oid_len)); DEBUGMSG(("9:notification_log", "\n")); } deletevarrow = netsnmp_table_data_set_get_first_row(nlmLogVarTable); for (; deletevarrow; deletevarrow = tmprow) { tmprow = netsnmp_table_data_set_get_next_row(nlmLogVarTable, deletevarrow); DEBUGIF("9:notification_log") { DEBUGMSGTL(("9:notification_log", " :")); DEBUGMSGOID(("9:notification_log", deletevarrow->index_oid, deletevarrow->index_oid_len)); DEBUGMSG(("9:notification_log", "\n")); } if ((deleterow->index_oid_len == deletevarrow->index_oid_len - 1) && snmp_oid_compare(deleterow->index_oid, deleterow->index_oid_len, deletevarrow->index_oid, deleterow->index_oid_len) == 0) { DEBUGMSGTL(("9:notification_log", " deleting varbind\n")); netsnmp_table_dataset_remove_and_delete_row(nlmLogVarTable, deletevarrow); } else break; } /* * delete the master row */ tmprow = netsnmp_table_data_set_get_next_row(nlmLogTable, deleterow); netsnmp_table_dataset_remove_and_delete_row(nlmLogTable, deleterow); num_deleted++; } /** should have deleted all of them */ netsnmp_assert(0 == count);}voidcheck_log_size(unsigned int clientreg, void *clientarg){ netsnmp_table_row *row; netsnmp_table_data_set_storage *data; u_long count = 0; struct timeval now; u_long uptime; gettimeofday(&now, NULL); uptime = netsnmp_timeval_uptime(&now); if (!nlmLogTable || !nlmLogTable->table ) { DEBUGMSGTL(("notification_log", "missing log table\n")); return; } /* * check max allowed count */ count = netsnmp_table_set_num_rows(nlmLogTable); DEBUGMSGTL(("notification_log", "logged notifications %d; max %d\n", count, max_logged)); if (count > max_logged) { count = count - max_logged; DEBUGMSGTL(("notification_log", "removing %d extra notifications\n", count)); netsnmp_notif_log_remove_oldest(count); } /* * check max age */ if (0 == max_age) return; count = 0; for (row = netsnmp_table_data_set_get_first_row(nlmLogTable); row; row = netsnmp_table_data_set_get_next_row(nlmLogTable, row)) { data = (netsnmp_table_data_set_storage *) row->data; data = netsnmp_table_data_set_find_column(data, COLUMN_NLMLOGTIME); if (uptime < ((long)(*(data->data.integer) + max_age * 100 * 60))) break; ++count; } if (count) { DEBUGMSGTL(("notification_log", "removing %d expired notifications\n", count)); netsnmp_notif_log_remove_oldest(count); }}/** Initialize the nlmLogVariableTable table by defining its contents and how it's structured */voidinitialize_table_nlmLogVariableTable(const char * context){ static oid nlmLogVariableTable_oid[] = { 1, 3, 6, 1, 2, 1, 92, 1, 3, 2 }; size_t nlmLogVariableTable_oid_len = OID_LENGTH(nlmLogVariableTable_oid); netsnmp_table_data_set *table_set; netsnmp_handler_registration *reginfo; /* * create the table structure itself */ table_set = netsnmp_create_table_data_set("nlmLogVariableTable"); nlmLogVarTable = table_set; nlmLogVarTable->table->store_indexes = 1; /*************************************************** * Adding indexes */ /* * declaring the nlmLogName index */ DEBUGMSGTL(("initialize_table_nlmLogVariableTable", "adding index nlmLogName of type ASN_OCTET_STR to table nlmLogVariableTable\n")); netsnmp_table_dataset_add_index(table_set, ASN_OCTET_STR); /* * declaring the nlmLogIndex index */ DEBUGMSGTL(("initialize_table_nlmLogVariableTable", "adding index nlmLogIndex of type ASN_UNSIGNED to table nlmLogVariableTable\n")); netsnmp_table_dataset_add_index(table_set, ASN_UNSIGNED); /* * declaring the nlmLogVariableIndex index */ DEBUGMSGTL(("initialize_table_nlmLogVariableTable", "adding index nlmLogVariableIndex of type ASN_UNSIGNED to table nlmLogVariableTable\n")); netsnmp_table_dataset_add_index(table_set, ASN_UNSIGNED); /* * adding column nlmLogVariableIndex of type ASN_UNSIGNED and access * of NoAccess */ DEBUGMSGTL(("initialize_table_nlmLogVariableTable", "adding column nlmLogVariableIndex (#1) of type ASN_UNSIGNED to table nlmLogVariableTable\n")); netsnmp_table_set_add_default_row(table_set, COLUMN_NLMLOGVARIABLEINDEX, ASN_UNSIGNED, 0, NULL, 0); /* * adding column nlmLogVariableID of type ASN_OBJECT_ID and access of * ReadOnly */ DEBUGMSGTL(("initialize_table_nlmLogVariableTable", "adding column nlmLogVariableID (#2) of type ASN_OBJECT_ID to table nlmLogVariableTable\n")); netsnmp_table_set_add_default_row(table_set, COLUMN_NLMLOGVARIABLEID, ASN_OBJECT_ID, 0, NULL, 0); /* * adding column nlmLogVariableValueType of type ASN_INTEGER and * access of ReadOnly */ DEBUGMSGTL(("initialize_table_nlmLogVariableTable", "adding column nlmLogVariableValueType (#3) of type ASN_INTEGER to table nlmLogVariableTable\n")); netsnmp_table_set_add_default_row(table_set, COLUMN_NLMLOGVARIABLEVALUETYPE, ASN_INTEGER, 0, NULL, 0); /* * adding column nlmLogVariableCounter32Val of type ASN_COUNTER and * access of ReadOnly */ DEBUGMSGTL(("initialize_table_nlmLogVariableTable", "adding column nlmLogVariableCounter32Val (#4) of type ASN_COUNTER to table nlmLogVariableTable\n")); netsnmp_table_set_add_default_row(table_set, COLUMN_NLMLOGVARIABLECOUNTER32VAL, ASN_COUNTER, 0, NULL, 0); /* * adding column nlmLogVariableUnsigned32Val of type ASN_UNSIGNED and * access of ReadOnly */ DEBUGMSGTL(("initialize_table_nlmLogVariableTable", "adding column nlmLogVariableUnsigned32Val (#5) of type ASN_UNSIGNED to table nlmLogVariableTable\n")); netsnmp_table_set_add_default_row(table_set, COLUMN_NLMLOGVARIABLEUNSIGNED32VAL, ASN_UNSIGNED, 0, NULL, 0); /* * adding column nlmLogVariableTimeTicksVal of type ASN_TIMETICKS and * access of ReadOnly */ DEBUGMSGTL(("initialize_table_nlmLogVariableTable", "adding column nlmLogVariableTimeTicksVal (#6) of type ASN_TIMETICKS to table nlmLogVariableTable\n")); netsnmp_table_set_add_default_row(table_set, COLUMN_NLMLOGVARIABLETIMETICKSVAL, ASN_TIMETICKS, 0, NULL, 0); /* * adding column nlmLogVariableInteger32Val of type ASN_INTEGER and * access of ReadOnly */ DEBUGMSGTL(("initialize_table_nlmLogVariableTable", "adding column nlmLogVariableInteger32Val (#7) of type ASN_INTEGER to table nlmLogVariableTable\n")); netsnmp_table_set_add_default_row(table_set, COLUMN_NLMLOGVARIABLEINTEGER32VAL, ASN_INTEGER, 0, NULL, 0); /* * adding column nlmLogVariableOctetStringVal of type ASN_OCTET_STR * and access of ReadOnly */ DEBUGMSGTL(("initialize_table_nlmLogVariableTable", "adding column nlmLogVariableOctetStringVal (#8) of type ASN_OCTET_STR to table nlmLogVariableTable\n")); netsnmp_table_set_add_default_row(table_set, COLUMN_NLMLOGVARIABLEOCTETSTRINGVAL, ASN_OCTET_STR, 0, NULL, 0); /* * adding column nlmLogVariableIpAddressVal of type ASN_IPADDRESS and * access of ReadOnly */ DEBUGMSGTL(("initialize_table_nlmLogVariableTable", "adding column nlmLogVariableIpAddressVal (#9) of type ASN_IPADDRESS to table nlmLogVariableTable\n")); netsnmp_table_set_add_default_row(table_set, COLUMN_NLMLOGVARIABLEIPADDRESSVAL, ASN_IPADDRESS, 0, NULL, 0); /* * adding column nlmLogVariableOidVal of type ASN_OBJECT_ID and access * of ReadOnly */ DEBUGMSGTL(("initialize_table_nlmLogVariableTable", "adding column nlmLogVariableOidVal (#10) of type ASN_OBJECT_ID to table nlmLogVariableTable\n")); netsnmp_table_set_add_default_row(table_set, COLUMN_NLMLOGVARIABLEOIDVAL, ASN_OBJECT_ID, 0, NULL, 0); /* * adding column nlmLogVariableCounter64Val of type ASN_COUNTER64 and * access of ReadOnly */ DEBUGMSGTL(("initialize_table_nlmLogVariableTable", "adding column nlmLogVariableCounter64Val (#11) of type ASN_COUNTER64 to table nlmLogVariableTable\n")); netsnmp_table_set_add_default_row(table_set, COLUMN_NLMLOGVARIABLECOUNTER64VAL, ASN_COUNTER64, 0, NULL, 0); /* * adding column nlmLogVariableOpaqueVal of type ASN_OPAQUE and access * of ReadOnly */ DEBUGMSGTL(("initialize_table_nlmLogVariableTable", "adding column nlmLogVariableOpaqueVal (#12) of type ASN_OPAQUE to table nlmLogVariableTable\n")); netsnmp_table_set_add_default_row(table_set, COLUMN_NLMLOGVARIABLEOPAQUEVAL, ASN_OPAQUE, 0, NULL, 0); /* * registering the table with the master agent */ /* * note: if you don't need a subhandler to deal with any aspects of * the request, change nlmLogVariableTable_handler to "NULL" */ reginfo = netsnmp_create_handler_registration ("nlmLogVariableTable", nlmLogVariableTable_handler, nlmLogVariableTable_oid, nlmLogVariableTable_oid_len, HANDLER_CAN_RWRITE); if (NULL != context) reginfo->contextName = strdup(context); netsnmp_register_table_data_set(reginfo, table_set, NULL);}/** Initialize the nlmLogTable table by defining its contents and how it's structured */voidinitialize_table_nlmLogTable(const char * context){ static oid nlmLogTable_oid[] = { 1, 3, 6, 1, 2, 1, 92, 1, 3, 1 }; size_t nlmLogTable_oid_len = OID_LENGTH(nlmLogTable_oid); netsnmp_handler_registration *reginfo; /* * create the table structure itself */ nlmLogTable = netsnmp_create_table_data_set("nlmLogTable"); /*************************************************** * Adding indexes */ /* * declaring the nlmLogIndex index */ DEBUGMSGTL(("initialize_table_nlmLogTable", "adding index nlmLogName of type ASN_OCTET_STR to table nlmLogTable\n")); netsnmp_table_dataset_add_index(nlmLogTable, ASN_OCTET_STR); DEBUGMSGTL(("initialize_table_nlmLogTable", "adding index nlmLogIndex of type ASN_UNSIGNED to table nlmLogTable\n")); netsnmp_table_dataset_add_index(nlmLogTable, ASN_UNSIGNED); /* * adding column nlmLogTime of type ASN_TIMETICKS and access of * ReadOnly */ DEBUGMSGTL(("initialize_table_nlmLogTable", "adding column nlmLogTime (#2) of type ASN_TIMETICKS to table nlmLogTable\n")); netsnmp_table_set_add_default_row(nlmLogTable, COLUMN_NLMLOGTIME, ASN_TIMETICKS, 0, NULL, 0); /* * adding column nlmLogDateAndTime of type ASN_OCTET_STR and access of * ReadOnly */ DEBUGMSGTL(("initialize_table_nlmLogTable", "adding column nlmLogDateAndTime (#3) of type ASN_OCTET_STR to table nlmLogTable\n")); netsnmp_table_set_add_default_row(nlmLogTable, COLUMN_NLMLOGDATEANDTIME, ASN_OCTET_STR, 0, NULL, 0); /* * adding column nlmLogEngineID of type ASN_OCTET_STR and access of * ReadOnly */ DEBUGMSGTL(("initialize_table_nlmLogTable", "adding column nlmLogEngineID (#4) of type ASN_OCTET_STR to table nlmLogTable\n")); netsnmp_table_set_add_default_row(nlmLogTable, COLUMN_NLMLOGENGINEID, ASN_OCTET_STR, 0, NULL, 0); /* * adding column nlmLogEngineTAddress of type ASN_OCTET_STR and access * of ReadOnly */ DEBUGMSGTL(("initialize_table_nlmLogTable", "adding column nlmLogEngineTAddress (#5) of type ASN_OCTET_STR to table nlmLogTable\n"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -