コード例 #1
0
ファイル: events.c プロジェクト: damirboss/canopsis-nagios
int
n2a_event_host_check (int event_type __attribute__ ((__unused__)), void *data)
{
  //logger(LG_DEBUG, "Event: event_service_check");
  nebstruct_host_check_data *c = (nebstruct_host_check_data *) data;

  if (c->type == NEBTYPE_HOSTCHECK_PROCESSED)
    {
      //logger(LG_DEBUG, "HOSTCHECK_PROCESSED: %s", c->host_name);
      char *buffer = NULL, *key = NULL;

      size_t l = xstrlen(g_options.connector) + xstrlen(g_options.eventsource_name) + xstrlen(c->host_name) + 20; 

      nebstruct_host_check_data_to_json(&buffer, c); 

      // DO NOT FREE !!!
      xalloca(key, xmin(g_options.max_size, (int)l) * sizeof(char));

      snprintf(key, xmin(g_options.max_size, (int)l),
                 "%s.%s.check.component.%s", g_options.connector,
                 g_options.eventsource_name, c->host_name);

      if (c_size == -10000 || c_size / 2 == 0)
          amqp_publish(key, buffer);
      else
          n2a_record_cache (key, buffer);

      xfree(buffer);
    }

  return 0;
}
コード例 #2
0
ファイル: wildcards.c プロジェクト: ziirish/shelldone
static char *
escape_char (char **src, char esc)
{
    if (src == NULL || *src == NULL)
        return src == NULL ? NULL : *src;

    int backslash = 0, i, j;
    unsigned int found = FALSE;
    char *string = *src, *tmp = NULL;
    size_t len = xstrlen (string);
    for (i = 0; string[i] != '\0'; i++)
    {
        if (string[i] == '\\' && (i < 1 ||
            string[i-1] != '\\' ||
            (string[i-1] == '\\' && backslash != 0 && backslash % 2 == 0)))
            backslash++;
        if (string[i] == esc && (i < 1 ||
            string[i-1] != '\\' ||
            (string[i-1] =='\\' && backslash != 0 && backslash % 2 == 0)))
        {
            len++;
            found = TRUE;
        }
    }

    if (!found)
        return *src;

    xalloca (tmp, len + 1);
    for (i = 0, j = 0; string[i] != '\0' && j < (int) (len + 1); i++, j++)
    {
        if (string[i] == esc && (i < 1 ||
            string[i-1] != '\\' ||
            (string[i-1] =='\\' && backslash != 0 && backslash % 2 == 0)))
        {
            tmp[j] = '\\';
            j++;
            tmp[j] = string[i];
        }
        else
            tmp[j] = string[i];
    }
    tmp[j] = '\0';
    xfree (*src);
    *src = xstrdup (tmp);
    return *src;
}
コード例 #3
0
ファイル: tree-diff.c プロジェクト: 1416759661/git
static struct combine_diff_path *ll_diff_tree_paths(
	struct combine_diff_path *p, const unsigned char *sha1,
	const unsigned char **parents_sha1, int nparent,
	struct strbuf *base, struct diff_options *opt)
{
	struct tree_desc t, *tp;
	void *ttree, **tptree;
	int i;

	tp     = xalloca(nparent * sizeof(tp[0]));
	tptree = xalloca(nparent * sizeof(tptree[0]));

	/*
	 * load parents first, as they are probably already cached.
	 *
	 * ( log_tree_diff() parses commit->parent before calling here via
	 *   diff_tree_sha1(parent, commit) )
	 */
	for (i = 0; i < nparent; ++i)
		tptree[i] = fill_tree_descriptor(&tp[i], parents_sha1[i]);
	ttree = fill_tree_descriptor(&t, sha1);

	/* Enable recursion indefinitely */
	opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);

	for (;;) {
		int imin, cmp;

		if (diff_can_quit_early(opt))
			break;

		if (opt->pathspec.nr) {
			skip_uninteresting(&t, base, opt);
			for (i = 0; i < nparent; i++)
				skip_uninteresting(&tp[i], base, opt);
		}

		/* comparing is finished when all trees are done */
		if (!t.size) {
			int done = 1;
			for (i = 0; i < nparent; ++i)
				if (tp[i].size) {
					done = 0;
					break;
				}
			if (done)
				break;
		}

		/*
		 * lookup imin = argmin(p1...pn),
		 * mark entries whether they =p[imin] along the way
		 */
		imin = 0;
		tp[0].entry.mode &= ~S_IFXMIN_NEQ;

		for (i = 1; i < nparent; ++i) {
			cmp = tree_entry_pathcmp(&tp[i], &tp[imin]);
			if (cmp < 0) {
				imin = i;
				tp[i].entry.mode &= ~S_IFXMIN_NEQ;
			}
			else if (cmp == 0) {
				tp[i].entry.mode &= ~S_IFXMIN_NEQ;
			}
			else {
				tp[i].entry.mode |= S_IFXMIN_NEQ;
			}
		}

		/* fixup markings for entries before imin */
		for (i = 0; i < imin; ++i)
			tp[i].entry.mode |= S_IFXMIN_NEQ;	/* pi > p[imin] */



		/* compare t vs p[imin] */
		cmp = tree_entry_pathcmp(&t, &tp[imin]);

		/* t = p[imin] */
		if (cmp == 0) {
			/* are either pi > p[imin] or diff(t,pi) != ø ? */
			if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER)) {
				for (i = 0; i < nparent; ++i) {
					/* p[i] > p[imin] */
					if (tp[i].entry.mode & S_IFXMIN_NEQ)
						continue;

					/* diff(t,pi) != ø */
					if (oidcmp(t.entry.oid, tp[i].entry.oid) ||
					    (t.entry.mode != tp[i].entry.mode))
						continue;

					goto skip_emit_t_tp;
				}
			}

			/* D += {δ(t,pi) if pi=p[imin];  "+a" if pi > p[imin]} */
			p = emit_path(p, base, opt, nparent,
					&t, tp, imin);

		skip_emit_t_tp:
			/* t↓,  ∀ pi=p[imin]  pi↓ */
			update_tree_entry(&t);
			update_tp_entries(tp, nparent);
		}

		/* t < p[imin] */
		else if (cmp < 0) {
			/* D += "+t" */
			p = emit_path(p, base, opt, nparent,
					&t, /*tp=*/NULL, -1);

			/* t↓ */
			update_tree_entry(&t);
		}

		/* t > p[imin] */
		else {
			/* ∀i pi=p[imin] -> D += "-p[imin]" */
			if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER)) {
				for (i = 0; i < nparent; ++i)
					if (tp[i].entry.mode & S_IFXMIN_NEQ)
						goto skip_emit_tp;
			}

			p = emit_path(p, base, opt, nparent,
					/*t=*/NULL, tp, imin);

		skip_emit_tp:
			/* ∀ pi=p[imin]  pi↓ */
			update_tp_entries(tp, nparent);
		}
	}

	free(ttree);
	for (i = nparent-1; i >= 0; i--)
		free(tptree[i]);
	xalloca_free(tptree);
	xalloca_free(tp);

	return p;
}
コード例 #4
0
ファイル: tree-diff.c プロジェクト: 1416759661/git
/*
 * new path should be added to combine diff
 *
 * 3 cases on how/when it should be called and behaves:
 *
 *	 t, !tp		-> path added, all parents lack it
 *	!t,  tp		-> path removed from all parents
 *	 t,  tp		-> path modified/added
 *			   (M for tp[i]=tp[imin], A otherwise)
 */
static struct combine_diff_path *emit_path(struct combine_diff_path *p,
	struct strbuf *base, struct diff_options *opt, int nparent,
	struct tree_desc *t, struct tree_desc *tp,
	int imin)
{
	unsigned mode;
	const char *path;
	const unsigned char *sha1;
	int pathlen;
	int old_baselen = base->len;
	int i, isdir, recurse = 0, emitthis = 1;

	/* at least something has to be valid */
	assert(t || tp);

	if (t) {
		/* path present in resulting tree */
		sha1 = tree_entry_extract(t, &path, &mode)->hash;
		pathlen = tree_entry_len(&t->entry);
		isdir = S_ISDIR(mode);
	} else {
		/*
		 * a path was removed - take path from imin parent. Also take
		 * mode from that parent, to decide on recursion(1).
		 *
		 * 1) all modes for tp[i]=tp[imin] should be the same wrt
		 *    S_ISDIR, thanks to base_name_compare().
		 */
		tree_entry_extract(&tp[imin], &path, &mode);
		pathlen = tree_entry_len(&tp[imin].entry);

		isdir = S_ISDIR(mode);
		sha1 = NULL;
		mode = 0;
	}

	if (DIFF_OPT_TST(opt, RECURSIVE) && isdir) {
		recurse = 1;
		emitthis = DIFF_OPT_TST(opt, TREE_IN_RECURSIVE);
	}

	if (emitthis) {
		int keep;
		struct combine_diff_path *pprev = p;
		p = path_appendnew(p, nparent, base, path, pathlen, mode, sha1);

		for (i = 0; i < nparent; ++i) {
			/*
			 * tp[i] is valid, if present and if tp[i]==tp[imin] -
			 * otherwise, we should ignore it.
			 */
			int tpi_valid = tp && !(tp[i].entry.mode & S_IFXMIN_NEQ);

			const unsigned char *sha1_i;
			unsigned mode_i;

			p->parent[i].status =
				!t ? DIFF_STATUS_DELETED :
					tpi_valid ?
						DIFF_STATUS_MODIFIED :
						DIFF_STATUS_ADDED;

			if (tpi_valid) {
				sha1_i = tp[i].entry.oid->hash;
				mode_i = tp[i].entry.mode;
			}
			else {
				sha1_i = NULL;
				mode_i = 0;
			}

			p->parent[i].mode = mode_i;
			hashcpy(p->parent[i].oid.hash, sha1_i ? sha1_i : null_sha1);
		}

		keep = 1;
		if (opt->pathchange)
			keep = opt->pathchange(opt, p);

		/*
		 * If a path was filtered or consumed - we don't need to add it
		 * to the list and can reuse its memory, leaving it as
		 * pre-allocated element on the tail.
		 *
		 * On the other hand, if path needs to be kept, we need to
		 * correct its .next to NULL, as it was pre-initialized to how
		 * much memory was allocated.
		 *
		 * see path_appendnew() for details.
		 */
		if (!keep)
			p = pprev;
		else
			p->next = NULL;
	}

	if (recurse) {
		const unsigned char **parents_sha1;

		parents_sha1 = xalloca(nparent * sizeof(parents_sha1[0]));
		for (i = 0; i < nparent; ++i) {
			/* same rule as in emitthis */
			int tpi_valid = tp && !(tp[i].entry.mode & S_IFXMIN_NEQ);

			parents_sha1[i] = tpi_valid ? tp[i].entry.oid->hash
						    : NULL;
		}

		strbuf_add(base, path, pathlen);
		strbuf_addch(base, '/');
		p = ll_diff_tree_paths(p, sha1, parents_sha1, nparent, base, opt);
		xalloca_free(parents_sha1);
	}

	strbuf_setlen(base, old_baselen);
	return p;
}
コード例 #5
0
ファイル: events.c プロジェクト: damirboss/canopsis-nagios
int
n2a_event_service_check (int event_type __attribute__ ((__unused__)), void *data)
{
  //logger(LG_DEBUG, "Event: event_host_check");
  nebstruct_service_check_data *c = (nebstruct_service_check_data *) data;

  if (c->type == NEBTYPE_SERVICECHECK_PROCESSED)
    {
      //logger(LG_DEBUG, "SERVICECHECK_PROCESSED: %s->%s", c->host_name, c->service_description);
      char *buffer = NULL, *key = NULL;

      size_t l = xstrlen(g_options.connector) +
      xstrlen(g_options.eventsource_name) + xstrlen(c->host_name) + xstrlen(c->service_description) + 20;
      // "..check.ressource.." + \0 = 20 chars

      json_t *jdata = NULL;
      size_t message_size = 0;

      int nbmsg = nebstruct_service_check_data_to_json(c, &jdata, &message_size); 

      // DO NOT FREE !!!
      xalloca(key, xmin(g_options.max_size, (int)l) * sizeof(char));

      snprintf(key, xmin(g_options.max_size, (int)l),
                 "%s.%s.check.resource.%s.%s", g_options.connector,
                 g_options.eventsource_name, c->host_name,
                 c->service_description);

      if (nbmsg == 1) {
          char *json = json_dumps(jdata, 0);
          buffer = xmalloc (message_size + 1);

          snprintf (buffer, message_size + 1, "%s", json);

          if (c_size == -10000 || c_size / 2 == 0) 
              amqp_publish(key, buffer);
          else
              n2a_record_cache (key, buffer);

          xfree(buffer);
          xfree (json);
      } else {
          int left = g_options.max_size - (int)message_size;
          size_t l_out = xstrlen(c->long_output);
          size_t out = xstrlen(c->output);
          size_t perf = xstrlen(c->perf_data);
          int msgs = ((int)l_out/left + 1) + ((int)out/left + 1) + ((int)perf/left + 1);
          n2a_logger(LG_INFO, "Data too long... sending %d messages for host: %s, service: %s", msgs, c->host_name, c->service_description);
          int i, temp;
          split_message(c->long_output, "long_output");
          split_message(c->output, "output");
          split_message(c->perf_data, "perf_data");
      }

      if (jdata != NULL)
          json_decref (jdata);

    }

  return 0;
}