/* apply include/exclude pattern to existing directory content */ static void apply_dir_pattern(struct sdev_node *dir, char *expr, char *pathleft, int type) { struct sdev_node *dv; /* leaf pattern */ if (pathleft == NULL) { if (type == PROFILE_TYPE_INCLUDE) return; /* nothing to do for include */ (void) sdev_cleandir(dir, expr, SDEV_ENFORCE); return; } /* directory pattern */ rw_enter(&dir->sdev_contents, RW_WRITER); for (dv = SDEV_FIRST_ENTRY(dir); dv; dv = SDEV_NEXT_ENTRY(dir, dv)) { if (gmatch(dv->sdev_name, expr) == 0 || SDEVTOV(dv)->v_type != VDIR) continue; process_rule(dv, dv->sdev_origin, pathleft, NULL, type); } rw_exit(&dir->sdev_contents); }
static void apply_glob_pattern(struct sdev_node *pdir, struct sdev_node *cdir) { char *name; nvpair_t *nvp = NULL; nvlist_t *nvl; struct vnode *vp = SDEVTOV(cdir); int rv = 0; if (vp->v_type != VDIR) return; name = cdir->sdev_name; nvl = pdir->sdev_prof.dev_glob_incdir; while (nvp = nvlist_next_nvpair(nvl, nvp)) { char *pathleft; char *expr = nvpair_name(nvp); if (!gmatch(name, expr)) continue; rv = nvpair_value_string(nvp, &pathleft); if (rv != 0) { cmn_err(CE_WARN, sdev_nvp_val_err, rv, nvpair_name(nvp)); break; } process_rule(cdir, cdir->sdev_origin, pathleft, NULL, PROFILE_TYPE_INCLUDE); } }
bool process_rules() { rule_t* rule = rules; int rule_number; for (rule_number = 0; rule_number < rules_count; rule_number++) { if (rule->from) { if (!process_rule(rule->from, index_from, rule_number, masks_from)) { return false; }; } if (rule->to) { if (!process_rule(rule->to, index_to, rule_number, masks_to)) { return false; }; } rule++; } return true; }
/****************************************************************************** * * * Function: main_discoverer_loop * * * * Purpose: periodically try to find new hosts and services * * * * Parameters: * * * * Return value: * * * * Author: Alexei Vladishev * * * * Comments: executes once per 30 seconds (hardcoded) * * * ******************************************************************************/ void main_discoverer_loop(int num) { int now; DB_RESULT result; DB_ROW row; DB_DRULE rule; zabbix_log( LOG_LEVEL_DEBUG, "In main_discoverer_loop(num:%d)", num); discoverer_num = num; DBconnect(ZBX_DB_CONNECT_NORMAL); for(;;) { zbx_setproctitle("Discoverer finding new hosts and services"); now=time(NULL); result = DBselect("select druleid,iprange,delay,nextcheck,name,status,siteid from drules where status=%d and nextcheck<=%d and " ZBX_SQL_MOD(druleid,%d) "=%d", DRULE_STATUS_MONITORED, now, CONFIG_DISCOVERER_FORKS, discoverer_num-1); while((row=DBfetch(result))) { memset(&rule, 0, sizeof(DB_DRULE)); ZBX_STR2UINT64(rule.druleid,row[0]); rule.iprange = row[1]; rule.delay = atoi(row[2]); rule.nextcheck = atoi(row[3]); rule.name = row[4]; rule.status = atoi(row[5]); ZBX_STR2UINT64(rule.siteid,row[6]); process_rule(&rule); } DBfree_result(result); zbx_setproctitle("Discoverer [sleeping for 30 sec]"); sleep(30); } DBclose(); }
static int process_discovery(int now) { DB_RESULT result; DB_ROW row; DB_DRULE drule; int rule_count = 0; result = DBselect( "select distinct r.druleid,r.iprange,r.name,c.dcheckid" " from drules r" " left join dchecks c" " on c.druleid=r.druleid" " and uniq=1" " where r.proxy_hostid is null" " and r.status=%d" " and (r.nextcheck<=%d or r.nextcheck>%d+r.delay)" " and " ZBX_SQL_MOD(r.druleid,%d) "=%d" ZBX_SQL_NODE, DRULE_STATUS_MONITORED, now, now, CONFIG_DISCOVERER_FORKS, process_num - 1, DBand_node_local("r.druleid")); while (NULL != (row = DBfetch(result))) { memset(&drule, 0, sizeof(drule)); ZBX_STR2UINT64(drule.druleid, row[0]); drule.iprange = row[1]; drule.name = row[2]; ZBX_DBROW2UINT64(drule.unique_dcheckid, row[3]); process_rule(&drule); DBexecute("update drules set nextcheck=%d+delay where druleid=" ZBX_FS_UI64, now, drule.druleid); rule_count++; } DBfree_result(result); return rule_count; /* performance metric */ }
/* * Process profile passed down from libdevinfo. There are four types * of matching rules: * include: export a name or names matching a pattern * exclude: exclude a name or names matching a pattern * symlink: create a local symlink * map: export a device with a name different from the global zone * Note: We may consider supporting VOP_SYMLINK in non-global instances, * because it does not present any security risk. For now, the fs * instance is read only. */ static void sdev_process_profile(struct sdev_data *sdev_data, nvlist_t *profile) { nvpair_t *nvpair; char *nvname, *dname; struct sdev_node *dir, *gdir; char **pair; /* for symlinks and maps */ uint_t nelem; int rv; gdir = sdev_origins->sdev_root; /* root of global /dev */ dir = sdev_data->sdev_root; /* root of current instance */ ASSERT(profile); /* process nvpairs in the list */ nvpair = NULL; while (nvpair = nvlist_next_nvpair(profile, nvpair)) { nvname = nvpair_name(nvpair); ASSERT(nvname != NULL); if (strcmp(nvname, SDEV_NVNAME_INCLUDE) == 0) { rv = nvpair_value_string(nvpair, &dname); if (rv != 0) { cmn_err(CE_WARN, sdev_nvp_val_err, rv, nvpair_name(nvpair)); break; } process_rule(dir, gdir, dname, NULL, PROFILE_TYPE_INCLUDE); } else if (strcmp(nvname, SDEV_NVNAME_EXCLUDE) == 0) { rv = nvpair_value_string(nvpair, &dname); if (rv != 0) { cmn_err(CE_WARN, sdev_nvp_val_err, rv, nvpair_name(nvpair)); break; } process_rule(dir, gdir, dname, NULL, PROFILE_TYPE_EXCLUDE); } else if (strcmp(nvname, SDEV_NVNAME_SYMLINK) == 0) { rv = nvpair_value_string_array(nvpair, &pair, &nelem); if (rv != 0) { cmn_err(CE_WARN, sdev_nvp_val_err, rv, nvpair_name(nvpair)); break; } ASSERT(nelem == 2); process_rule(dir, gdir, pair[0], pair[1], PROFILE_TYPE_SYMLINK); } else if (strcmp(nvname, SDEV_NVNAME_MAP) == 0) { rv = nvpair_value_string_array(nvpair, &pair, &nelem); if (rv != 0) { cmn_err(CE_WARN, sdev_nvp_val_err, rv, nvpair_name(nvpair)); break; } process_rule(dir, gdir, pair[1], pair[0], PROFILE_TYPE_MAP); } else if (strcmp(nvname, SDEV_NVNAME_MOUNTPT) != 0) { cmn_err(CE_WARN, "sdev_process_profile: invalid " "nvpair %s\n", nvname); } } }
/* * process a line from the logfile */ void process_logline() { struct context *this_context; struct rule *this_rule, *this_rule_next; int check_stop=0; if ( logline[0] == '\0' ) return; if ( logline[strlen(logline)-1] == '\n' ) logline[strlen(logline)-1]='\0'; #ifdef DEBUG (void) printf("processing logline: %s\n", logline); #endif if ( (logline_context=(struct context_line *)malloc(sizeof(struct context_line))) == NULL ) { (void) fprintf(stderr, "out of memory processing logling: %s\n", logline); return; } if ( (logline_context->content=strdup(logline)) == NULL ) { (void) fprintf(stderr, "out of memory processing logling: %s\n", logline); (void) free(logline_context); return; } logline_context->linenumber=logline_num; logline_context->timestamp=(long) current_time; logline_context->link_counter=1; /* * first try to store the logline in all matching contexts */ for ( this_context=all_contexts; this_context != NULL; this_context=this_context->next) if ( re_search(this_context->match_regex, logline, strlen(logline), 0, strlen(logline), ®ex_submatches) >= 0 ) { regex_submatches_num=this_context->match_regex->re_nsub; if ( this_context->match_not_regex != NULL ) { if ( re_search(this_context->match_not_regex, logline, strlen(logline), 0, strlen(logline), ®ex_notmatches) == -1 ) { add_to_context(this_context, logline_context); } } else add_to_context(this_context, logline_context); } /* * now check if there is a matching rule for this logline */ for ( this_rule=all_rules; this_rule != NULL ; ) { /* check if the logline matches the first rgex */ if ( re_search(this_rule->match_regex, logline, strlen(logline), 0, strlen(logline), ®ex_submatches) >= 0 ) { regex_submatches_num=this_rule->match_regex->re_nsub; this_rule_next=this_rule->next; /* if we do have a not-match regex test it */ if ( this_rule->match_not_regex != NULL ) { if ( re_search(this_rule->match_not_regex, logline, strlen(logline), 0, strlen(logline), ®ex_notmatches) == -1 ) { check_stop=1; if ( this_rule->do_continue == 0 ) this_rule_next=NULL; process_rule(this_rule); } } else { check_stop=1; if ( this_rule->do_continue == 0 ) this_rule_next=NULL; process_rule(this_rule); } if ( check_stop == 1 ) { /* check for the stop_regex */ if ( this_rule->stop_regex != NULL ) { if ( re_search(this_rule->stop_regex, logline, strlen(logline), 0, strlen(logline), ®ex_submatches) >= 0 ) { /* if we do have a not-match stop-regex test it */ if ( this_rule->stop_not_regex != NULL ) { if ( re_search(this_rule->stop_not_regex, logline, strlen(logline), 0, strlen(logline), ®ex_notmatches) == -1 ) unlink_rule(this_rule); } else unlink_rule(this_rule); } } check_stop=0; } this_rule=this_rule_next; } else this_rule=this_rule->next; } logline_context->link_counter--; /* * free the new line if not used in any context */ destroy_context_line(logline_context); return; }