Пример #1
0
void
service_tag_copy(struct service_tag_s *dst, struct service_tag_s *src)
{
	if (!dst || !src)
		return;

	g_strlcpy(dst->name, src->name, sizeof(dst->name));

	switch (src->type) {
	case STVT_I64:
		service_tag_set_value_i64(dst, src->value.i);
		return;
	case STVT_REAL:
		service_tag_set_value_float(dst, src->value.r);
		return;
	case STVT_BOOL:
		service_tag_set_value_boolean(dst, src->value.b);
		return;
	case STVT_STR:
		service_tag_set_value_string(dst, src->value.s);
		return;
	case STVT_BUF:
		service_tag_set_value_string(dst, src->value.buf);
		return;
	}
}
static struct service_tag_s *
_srvtag_load_json (const gchar *name, struct json_object *obj)
{
	struct service_tag_s *tag = g_malloc0(sizeof(struct service_tag_s));
	g_strlcpy(tag->name, name, sizeof(tag->name));
	if (json_object_is_type(obj, json_type_int)) {
		service_tag_set_value_i64(tag, json_object_get_int64(obj));
	} else if (json_object_is_type(obj, json_type_string)) {
		service_tag_set_value_string(tag, json_object_get_string(obj));
	} else if (json_object_is_type(obj, json_type_double)) {
		service_tag_set_value_float(tag, json_object_get_double(obj));
	} else if (json_object_is_type(obj, json_type_boolean)) {
		service_tag_set_value_boolean(tag, json_object_get_boolean(obj));
	} else {
		service_tag_set_value_boolean(tag, FALSE);
	}
	return tag;
}
Пример #3
0
static void
parse_output(const gchar *cmd, service_info_t *si)
{
	int fd;
	FILE *stream_in;
	gchar line[1024];
	gchar cmd_with_args[4096] = {0,0,0};

	g_snprintf(cmd_with_args, sizeof(cmd_with_args), "%s %s", cmd, svc_id);
	INFO("Executing [%s]", cmd_with_args);
	if (0 > (fd = command_get_pipe(cmd_with_args))) {
		WARN("Exec failed: %s", strerror(errno));
		return;
	}

	if (!(stream_in = fdopen(fd, "r"))) {
		WARN("fdopen failed: %s", strerror(errno));
		metautils_pclose(&fd);
		return;
	}

	while (!feof(stream_in) && !ferror(stream_in)) {
		GMatchInfo *mi = NULL;

		bzero(line, sizeof(line));
		if (!fgets(line, sizeof(line), stream_in)) {
			break;
		}

		/* chomp the line */
		my_chomp(line);

		if (!g_regex_match(regex_tag, line, 0, &mi)) {
			NOTICE("Unrecognized pattern for output line [%s]", line);
		} else {
			struct service_tag_s *tag;
			gchar *str_type, *str_name, *str_value;

			str_name = g_match_info_fetch(mi, 1);
			str_type = g_match_info_fetch(mi, 2);
			str_value = g_match_info_fetch(mi, 4);

			if (!g_ascii_strcasecmp(str_type, "tag")) {
				tag = service_info_ensure_tag(si->tags, str_name);
				service_tag_set_value_string(tag, str_value);
			}
			else if (!g_ascii_strcasecmp(str_type, "stat")) {
				gdouble dval;

				dval = g_ascii_strtod(str_value, NULL);
				tag = service_info_ensure_tag(si->tags, str_name);
				service_tag_set_value_float(tag, dval);
			}

			g_free(str_value);
			g_free(str_type);
			g_free(str_name);
		}
		g_match_info_free(mi);
	}

	fclose(stream_in);
}