snmp-notify.c revision f6e214c7418f43af38bd8c3a557e3d0a1d311cfa
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*/
#include <fm/fmd_snmp.h>
#include <fm/libfmevent.h>
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <errno.h>
#include <locale.h>
#include <netdb.h>
#include <signal.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <alloca.h>
#include <priv_utils.h>
#include <zone.h>
#include "libfmnotify.h"
/*
* Debug messages can be enabled by setting the debug property to true
*
* # svccfg -s svc:/system/fm/snmp-notify setprop config/debug=true
*/
#define SVCNAME "system/fm/snmp-notify"
typedef struct ireport_trap {
char *host;
char *msgid;
char *desc;
long long tstamp;
char *fmri;
char *reason;
static const char optstr[] = "dfR:";
static const char SNMP_SUPPCONF[] = "fmd-trapgen";
static int
{
"\t-d enable debug mode\n"
"\t-f stay in foreground\n"
"\t-R specify alternate root\n");
return (1);
}
/*
* If someone does an "svcadm refresh" on us, then this function gets called,
* which rereads our service configuration.
*/
static void
{
int s = 0;
&(nhdl->nh_rootdir));
if (s != 0)
"properties");
}
static void
nd_sighandler(int sig)
{
else
}
static int
{
uint_t n;
int r;
/*
* For SMF state transition events, pref_nvl contain two sets of
* preferences, which will have to be merged.
*
* The "snmp" nvlist currently only supports a single boolean member,
* "active" which will be set to true, if it is true in either set
*/
if (npref == 2) {
&n);
if (r != 0) {
"preferences");
return (-1);
return (-1);
}
} else {
&a1, &n)) {
"preferences");
return (-1);
} else if (!a1[0]) {
return (-1);
}
}
return (0);
}
static void
{
static const oid sunIreportTrap_oid[] =
{ SUNIREPORTTRAP_OID };
const size_t sunIreportTrap_len =
static const oid sunIreportHostname_oid[] =
static const oid sunIreportMsgid_oid[] =
{ SUNIREPORTMSGID_OID };
static const oid sunIreportDescription_oid[] =
static const oid sunIreportTime_oid[] =
{ SUNIREPORTTIME_OID };
static const oid sunIreportSmfFmri_oid[] =
static const oid sunIreportSmfFromState_oid[] =
static const oid sunIreportSmfToState_oid[] =
static const oid sunIreportSmfTransitionReason_oid[] =
const size_t
/*
* We know date_n_time is broken, it returns a buffer from
* its stack. So we copy before we step over it!
*/
for (int i = 0; i < dt_len; ++i)
if (var_len > MAX_OID_LEN) {
return;
}
sizeof (oid));
sunIreport_base_len * sizeof (oid));
sunIreport_base_len * sizeof (oid));
sizeof (oid));
if (t->is_stn_event) {
sunIreport_base_len * sizeof (oid));
sunIreport_base_len * sizeof (oid));
sunIreport_base_len * sizeof (oid));
sunIreport_base_len * sizeof (oid));
}
/*
* Which is sent to a specific destination is determined by the
* configuration file(s).
*/
}
/*ARGSUSED*/
static void
{
static const oid sunFmProblemUUID_oid[] =
static const oid sunFmProblemCode_oid[] =
static const oid sunFmProblemURL_oid[] =
/*
* The format of our trap varbinds' oids is as follows:
*
* +-----------------------+---+--------+----------+------+
* | SUNFMPROBLEMTABLE_OID | 1 | column | uuid_len | uuid |
* +-----------------------+---+--------+----------+------+
* \---- index ----/
*
* A common mistake here is to send the trap with varbinds that
* do not contain the index. All the indices are the same, and
* all the oids are the same length, so the only thing we need to
* do for each varbind is set the table and column parts of the
* variable name.
*/
if (var_len > MAX_OID_LEN)
return;
for (int i = 0; i < uuid_len; i++)
/*
* Ordinarily, we would need to add the OID of the trap itself
* to the head of the variable list; this is required by SNMP v2.
* However, send_enterprise_trap_vars does this for us as a part
* of converting between v1 and v2 traps, so we skip directly to
* the objects we're sending.
*/
sunFmProblem_base_len * sizeof (oid));
sunFmProblem_base_len * sizeof (oid));
sunFmProblem_base_len * sizeof (oid));
/*
* Which is sent to a specific destination is determined by the
* configuration file(s).
*/
}
/*
* The SUN-IREPORT-MIB declares the following enum to represent SMF service
* states.
*
* offline(0), online(1), degraded(2), disabled(3), maintenance(4),
* uninitialized(5)
*
* This function converts a string representation of an SMF service state
* to it's corresponding enum val.
*/
static int
{
*stateval = 0;
*stateval = 1;
*stateval = 2;
*stateval = 3;
*stateval = 4;
*stateval = 5;
else
return (-1);
return (0);
}
/*ARGSUSED*/
static void
{
int ret;
if (ret == SCF_ERROR_NOT_FOUND) {
/*
* No snmp notification preferences specified for this type of
* event, so we're done
*/
return;
} else if (ret != 0) {
"for this event");
return;
}
goto irpt_done;
goto irpt_done;
< 0 ||
goto irpt_done;
}
}
if (ev_info)
}
/*ARGSUSED*/
static void
{
char *uuid;
int ret;
if (ret == SCF_ERROR_NOT_FOUND) {
/*
* No snmp notification preferences specified for this type of
* event, so we're done
*/
return;
} else if (ret != 0) {
"for this event");
return;
}
goto listcb_done;
goto listcb_done;
/*
* If the message payload member is set to 0, then it's an event we
* typically suppress messaging on, so we won't send a trap for it.
*/
goto listcb_done;
}
!= 0 || version > FM_SUSPECT_VERSION) {
goto listcb_done;
}
&uuid);
else
if (ev_info)
}
static int
init_sma(void)
{
int err;
/*
* The only place we could possibly log is syslog, but the
* full agent doesn't normally log there. It would be confusing
* if this agent did so; therefore we disable logging entirely.
*/
/*
* Net-SNMP has a provision for reading an arbitrary number of
* configuration files. A configuration file is read if it has
* had any handlers registered for it, or if it's the value in
* of NETSNMP_DS_LIB_APPTYPE. Our objective here is to read
* both snmpd.conf and fmd-trapgen.conf.
*/
return (err);
init_agent_read_config("snmpd");
return (err);
return (SNMPERR_MALLOC);
if (register_app_config_handler("trap2sink",
NULL)
return (SNMPERR_MALLOC);
return (SNMPERR_MALLOC);
init_traps();
return (SNMPERR_SUCCESS);
}
int
{
char c;
return (1);
}
/*
* In the case where we get started outside of SMF, args passed on the
* command line override SMF property setting
*/
switch (c) {
case 'd':
break;
case 'f':
break;
case 'R':
break;
default:
}
}
}
/*
* Set up a signal handler for SIGTERM (and SIGINT if we'll
* be running in the foreground) to ensure sure we get a chance to exit
* in an orderly fashion. We also catch SIGHUP, which will be sent to
* us by SMF if the service is refreshed.
*/
(void) sigfillset(&set);
if (run_fg) {
} else
/*
* We need to be root initialize our libfmevent handle (because that
* calling __init_daemon_priv.
*/
(void) sleep(5);
}
/*
* If we're in the global zone, reset all of our privilege sets to
* the minimum set of required privileges. We also change our
*
* __init_daemon_priv will also set the process core path for us
*
*/
if (getzoneid() == GLOBAL_ZONEID)
if (__init_daemon_priv(
if (init_sma() != SNMPERR_SUCCESS)
/*
* Set up our event subscriptions. We subscribe to everything and then
* consult libscf when we receive an event to determine what (if any)
* notification to send.
*/
}
NULL) != FMEV_SUCCESS) {
}
/*
* We run until someone kills us
*/
while (nhdl->nh_keep_running)
(void) sigsuspend(&set);
/*
* snmp_shutdown, which we would normally use here, calls free_slots,
* a callback that is supposed to tear down the pkcs11 state; however,
* it abuses C_Finalize, causing fmd to drop core on shutdown. Avoid
* this by shutting down the library piecemeal.
*/
(void) snmp_close_sessions();
shutdown_mib();
return (0);
}