#include <sys/fm/protocol.h>
#include <fmd_api.h>
#include <fmd_subr.h>
#include <fmd_string.h>
#include <fmd_protocol.h>
#include <fmd_module.h>
#include <fmd_error.h>
static struct {
fmd_stat_t nosub;
fmd_stat_t module;
} self_stats = {
{ "nosub", FMD_TYPE_UINT64, "event classes with no subscribers seen" },
{ "module", FMD_TYPE_UINT64, "error events received from fmd modules" },
};
typedef struct self_case {
enum { SC_CLASS, SC_MODULE } sc_kind;
char *sc_name;
} self_case_t;
static self_case_t *
self_case_create(fmd_hdl_t *hdl, int kind, const char *name)
{
self_case_t *scp = fmd_hdl_alloc(hdl, sizeof (self_case_t), FMD_SLEEP);
scp->sc_kind = kind;
scp->sc_name = fmd_hdl_strdup(hdl, name, FMD_SLEEP);
return (scp);
}
static void
self_case_destroy(fmd_hdl_t *hdl, self_case_t *scp)
{
fmd_hdl_strfree(hdl, scp->sc_name);
fmd_hdl_free(hdl, scp, sizeof (self_case_t));
}
static fmd_case_t *
self_case_lookup(fmd_hdl_t *hdl, int kind, const char *name)
{
fmd_case_t *cp = NULL;
while ((cp = fmd_case_next(hdl, cp)) != NULL) {
self_case_t *scp = fmd_case_getspecific(hdl, cp);
if (scp->sc_kind == kind && strcmp(scp->sc_name, name) == 0)
break;
}
return (cp);
}
static void
self_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
{
fmd_case_t *cp;
nvlist_t *flt, *mod;
char *name;
int err = 0;
if (strcmp(class, fmd_errclass(EFMD_MODULE)) == 0 &&
nvlist_lookup_nvlist(nvl, FM_EREPORT_DETECTOR, &mod) == 0 &&
nvlist_lookup_string(mod, FM_FMRI_FMD_NAME, &name) == 0) {
if ((cp = self_case_lookup(hdl, SC_MODULE, name)) == NULL) {
cp = fmd_case_open(hdl,
self_case_create(hdl, SC_MODULE, name));
}
fmd_case_add_ereport(hdl, cp, ep);
self_stats.module.fmds_value.ui64++;
(void) nvlist_lookup_int32(nvl, FMD_ERR_MOD_ERRNO, &err);
if (err != EFMD_MOD_FAIL && err != EFMD_MOD_CONF)
return;
if (fmd_case_solved(hdl, cp))
return;
class = err == EFMD_MOD_FAIL ? FMD_FLT_MOD : FMD_FLT_CONF;
flt = fmd_protocol_fault(class, 100, mod, NULL, NULL, NULL);
fmd_case_add_suspect(hdl, cp, flt);
fmd_case_solve(hdl, cp);
return;
}
if (strncmp(class, "ereport.io.ddi.", strlen("ereport.io.ddi.")) == 0)
return;
if (strcmp(class, FM_LIST_SUSPECT_CLASS) == 0 ||
strcmp(class, FM_LIST_ISOLATED_CLASS) == 0 ||
strcmp(class, FM_LIST_UPDATED_CLASS) == 0 ||
strcmp(class, FM_LIST_RESOLVED_CLASS) == 0 ||
strcmp(class, FM_LIST_REPAIRED_CLASS) == 0 ||
strncmp(class, FM_FAULT_CLASS, strlen(FM_FAULT_CLASS)) == 0 ||
strncmp(class, FM_DEFECT_CLASS, strlen(FM_DEFECT_CLASS)) == 0)
return;
if (strncmp(class, FMD_ERR_CLASS, FMD_ERR_CLASS_LEN) == 0)
return;
if (strncmp(class, FMD_RSRC_CLASS, FMD_RSRC_CLASS_LEN) == 0)
return;
if (strncmp(class, SYSEVENT_RSRC_CLASS, SYSEVENT_RSRC_CLASS_LEN) == 0)
return;
if (self_case_lookup(hdl, SC_CLASS, class) != NULL)
return;
if (strncmp(class, FM_IREPORT_CLASS ".",
sizeof (FM_IREPORT_CLASS)) == 0)
return;
cp = fmd_case_open(hdl, self_case_create(hdl, SC_CLASS, class));
fmd_case_add_ereport(hdl, cp, ep);
self_stats.nosub.fmds_value.ui64++;
flt = fmd_protocol_fault(FMD_FLT_NOSUB, 100, NULL, NULL, NULL, NULL);
(void) nvlist_add_string(flt, "nosub_class", class);
fmd_case_add_suspect(hdl, cp, flt);
fmd_case_solve(hdl, cp);
}
static void
self_close(fmd_hdl_t *hdl, fmd_case_t *cp)
{
self_case_destroy(hdl, fmd_case_getspecific(hdl, cp));
}
static const fmd_hdl_ops_t self_ops = {
self_recv,
NULL,
self_close,
NULL,
NULL,
};
void
self_init(fmd_hdl_t *hdl)
{
fmd_module_t *mp = (fmd_module_t *)hdl;
fmd_hdl_info_t info = {
"Fault Manager Self-Diagnosis", "1.0", &self_ops, NULL
};
(void) pthread_mutex_lock(&mp->mod_stats_lock);
mp->mod_stats->ms_ckpt_save.fmds_value.bool = FMD_B_FALSE;
mp->mod_stats->ms_ckpt_restore.fmds_value.bool = FMD_B_FALSE;
(void) pthread_mutex_unlock(&mp->mod_stats_lock);
if (fmd_hdl_register(hdl, FMD_API_VERSION, &info) != 0)
return;
(void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, sizeof (self_stats) /
sizeof (fmd_stat_t), (fmd_stat_t *)&self_stats);
}
void
self_fini(fmd_hdl_t *hdl)
{
fmd_case_t *cp = NULL;
while ((cp = fmd_case_next(hdl, cp)) != NULL)
self_case_destroy(hdl, fmd_case_getspecific(hdl, cp));
}