int dump_metrics(const char *dest_file) { FILE *fp; if (mdisk == NULL || read_mdisk(mdisk) < 0) { errno = ENOMEDIUM; return -1; } if (dest_file) { fp = fopen(dest_file, "w"); if (fp == NULL) { libmsg("Error, unable to dump metrics: %s\n", strerror(errno)); return -1; } } else { fp = stdout; } if (fwrite(mdisk->buffer, 1, mdisk->length, fp) != mdisk->length) { libmsg("Error, unable to export metrics to file:%s - error:%s\n", dest_file ? dest_file : "stdout", strerror(errno)); } if (dest_file) fclose(fp); return 0; }
destination_implObj::cups_dinfo_t_wrapper ::cups_dinfo_t_wrapper(http_t *http, cups_dest_t *dest) : info{cupsCopyDestInfo(http, dest)} { if (!info) throw EXCEPTION(libmsg(_("Print destination not available."))); }
static int metric_value_str_to_type(metric *mdef, char *str) { switch (mdef->type) { case M_INT32: mdef->value.i32 = atoi(str); break; case M_UINT32: mdef->value.ui32 = atoi(str); break; case M_INT64: mdef->value.ui64 = atoll(str); break; case M_UINT64: mdef->value.ui64 = atoll(str); break; case M_REAL32: mdef->value.r32 = atof(str); break; case M_REAL64: mdef->value.r64 = atof(str); break; case M_STRING: mdef->value.str = (char *)(mdef) + sizeof(metric); memcpy(mdef->value.str, str, strlen(str) + 1); break; default: libmsg("%s() Unknown type, can not convert:%x\n", __func__, mdef->type); break; } return 0; }
definitionPropertiesObj::definitionPropertiesObj(std::ostream &outputStreamArg) noexcept : definitionbaseObj(0, "properties", 0, libmsg(_txt("List application properties (\"=all\" - show hidden options)")), ""), outputStream(outputStreamArg) { }
destination_implObj::cups_http_t_wrapper::cups_http_t_wrapper(cups_dest_t *dest) : http{cupsConnectDest(dest, CUPS_DEST_FLAGS_NONE, connect_timeout.get(), NULL, NULL, 0, NULL, NULL)} { if (!http) throw EXCEPTION(libmsg(_("Print queue connection failed."))); }
/* * dump metrics from virtio serial port to xml formatted file */ int dump_virtio_metrics(const char *dest_file) { FILE *fp = stdout; char *response = NULL; size_t len; response = get_virtio_metrics(); if (response == NULL) goto error; len = strlen(response); if (dest_file) { fp = fopen(dest_file, "w"); if (fp == NULL) { libmsg("%s(), unable to dump metrics: fopen(%s) %s\n", __func__, dest_file, strerror(errno)); goto error; } } if (fwrite(response, 1UL, len, fp) != len) { libmsg("%s(), unable to export metrics to file:%s %s\n", __func__, dest_file ? dest_file : "stdout", strerror(errno)); goto error; } if (response) free(response); return 0; error: if (dest_file && fp) fclose(fp); if (response) free(response); return -1; }
int definitionPropertiesObj::set(parserObj &parserArg, const std::string &valueArg) const noexcept { bool all=valueArg == libmsg(_txt("all")); std::map<std::string, std::string> properties; property::enumerate_properties(properties); std::map<std::string, std::string>::iterator b, e; for (b=properties.begin(), e=properties.end(); b != e; ++b) { if (!all && b->first.find('@') != b->first.npos) continue; outputStream << b->first << "=" << b->second << std::endl; } return option::parser::base::err_builtin; }
static int get_dom_id() { struct xs_handle *xsh = NULL; char *domid = NULL; int domID = -1; unsigned int len; xsh = xs_domain_open(); if (xsh == NULL) { libmsg("xs_domain_open: cannot open xenstore\n"); return -1; } domid = xs_read(xsh, XBT_NULL, "domid", &len); if (domid) { domID = atoi(domid); free(domid); } return domID; }
static int get_dom_uuid() { FILE *fp; char *cp = NULL; if (mdisk == NULL) return -1; /* Get the UUID of this VM */ fp = fopen("/sys/hypervisor/uuid", "r"); if (fp != NULL) { fread (mdisk->uuid, sizeof (mdisk->uuid) - 1, 1, fp); fclose (fp); if ((cp = strrchr(mdisk->uuid, '\n'))) *cp = '\0'; } #ifdef WITH_XENSTORE else if ((fp = popen("xenstore-read vm", "r"))) { char buffer[256]; if (fread(buffer, 1, 256, fp)) { if ((cp = strrchr(buffer, '/'))) { cp++; strcpy(mdisk->uuid, cp); } } } #endif else { libmsg("%s(): Error, unable to determine VM uuid\n", __func__); goto error; } return 0; error: return -1; }
void cant_clone_writelock() { throw EXCEPTION(libmsg(_txt("Attempt to clone a write lock on a hier container"))); }
/* * dump metrics from virtio serial port to buffer */ static char *get_virtio_metrics(void) { const char request[] = "GET /metrics/XML\n\n", end_token[] = "\n\n"; const char dev[] = "/dev/virtio-ports/org.github.vhostmd.1"; char *response = NULL; int fd = -1; size_t pos; size_t buf_size = (1 << 16); const size_t req_len = (size_t) strlen(request); const time_t timeout = 5; time_t end_time; response = calloc(1UL, buf_size); if (response == NULL) goto error; fd = open(dev, O_RDWR | O_NONBLOCK); if (fd < 0) { libmsg("%s(): Unable to export metrics: open(%s) %s\n", __func__, dev, strerror(errno)); goto error; } pos = 0; end_time = time(NULL) + timeout; while (pos < req_len) { ssize_t len = write(fd, &request[pos], req_len - pos); if (len > 0) pos += (size_t) len; else { if (errno == EAGAIN) { usleep(10000); if (time(NULL) > end_time) { libmsg("%s(): Unable to send metrics request" " - timeout after %us\n", __func__, timeout); goto error; } } else goto error; } } pos = 0; end_time = time(NULL) + timeout; do { ssize_t len = read(fd, &response[pos], buf_size - pos - 1); if (len > 0) { pos += (size_t) len; response[pos] = 0; if ((pos + 1) >= buf_size) { buf_size = buf_size << 1; /* increase response buffer */ if (buf_size > (1 << 24)) /* max 16MB */ goto error; response = realloc(response, buf_size); if (response == NULL) goto error; memset(&response[pos], 0, buf_size - pos); } } else { if (errno == EAGAIN) { usleep(10000); if (time(NULL) > end_time) { libmsg("%s(): Unable to read metrics" " - timeout after %us\n", __func__, timeout); goto error; } } else goto error; } } while ((pos < (size_t) strlen(end_token) || strcmp(end_token, &response[pos - (size_t) strlen(end_token)]) != 0) && pos < buf_size); if (fd >= 0) close(fd); return response; error: if (fd >= 0) close(fd); if (response) free(response); libmsg("%s(): Unable to read metrics\n", __func__); return NULL; }
/* * dump metrics received from xenstore to the dest file */ int dump_xenstore_metrics(const char *dest_file) { char *buf = NULL, *path = NULL, *metrics = NULL; struct xs_handle *xsh = NULL; unsigned int len; int ret = 0; xmlParserCtxtPtr pctxt = NULL; xmlDocPtr doc = NULL; int domid; FILE *fp; if (dest_file) { fp = fopen(dest_file, "w"); if (fp == NULL) { libmsg("Error, unable to dump metrics from xenstore: %s\n", strerror(errno)); return -1; } } else { fp = stdout; } if ((domid = get_dom_id()) == -1) { libmsg("Unable to derive domID.\n" ); ret = -1; goto out; } xsh = xs_domain_open(); if (xsh == NULL) { libmsg("xs_domain_open() error. errno: %d.\n", errno); ret = -1; goto out; } path = xs_get_domain_path(xsh, domid); if (path == NULL) { libmsg("xs_get_domain_path() error. domid %d.\n", 0); ret = -1; goto out; } asprintf(&buf, "%s/metrics", path); metrics = xs_read(xsh, XBT_NULL, buf, &len); if (metrics == NULL) { libmsg("xs_read(): uuid get error. %s.\n", buf); ret = -1; goto out; } pctxt = xmlNewParserCtxt(); if (!pctxt || !pctxt->sax) { libmsg("%s(): failed to create parser \n", __func__); ret = -1; goto out; } doc = xmlCtxtReadMemory(pctxt, metrics, strlen(metrics), "mdisk.xml", NULL, XML_PARSE_NOENT | XML_PARSE_NONET | XML_PARSE_NOWARNING); if (!doc) { libmsg("%s(): libxml failed to xenstore metrics attribute\n", __func__); ret = -1; goto out; } xmlDocFormatDump(fp, doc, 1); out: if (fp && fp != stdout) fclose(fp); if (doc) xmlFreeDoc(doc); if (pctxt) xmlFreeParserCtxt(pctxt); free(path); free(buf); free(metrics); return ret; }
/* * Read metrics disk and populate mdisk * Location of metrics disk is derived by looking at all block * devices and then reading until a valid metrics disk signature * is found. */ static int read_mdisk(metric_disk *mdisk) { mdisk_header md_header; uint32_t busy; uint32_t sig; int fd; char *path; DIR* dir; struct dirent* entry; dir = opendir(SYS_BLOCK); if (dir == NULL) goto error; while((entry = readdir(dir))) { retry: #ifndef DEBUG_FROM_DOM0 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue; asprintf(&path, "/dev/%s", entry->d_name); #else path = strdup("/dev/shm/vhostmd0"); #endif /* Open with O_DIRECT to avoid kernel keeping old copies around * in the cache. */ fd = open (path, O_RDONLY|O_DIRECT); if (fd == -1) { free (path); continue; } if (odirect_read (fd, &md_header, 0, sizeof md_header) == -1) { free (path); close (fd); continue; } if ((sig = ntohl(md_header.sig)) == MDISK_SIGNATURE) { busy = ntohl(md_header.busy); if (busy) { close(fd); free(path); sleep(1); goto retry; } mdisk->sum = ntohl(md_header.sum); mdisk->length = ntohl(md_header.length); mdisk->buffer = malloc(mdisk->length); mdisk->disk_name = strdup(path); /* XXX check return value */ odirect_read (fd, mdisk->buffer, sizeof md_header, mdisk->length); free(path); /* Verify data still valid */ if (odirect_read (fd, &md_header, 0, sizeof md_header) == -1) { mdisk_content_free(); close (fd); sleep(1); goto retry; } busy = ntohl(md_header.busy); if (busy || mdisk->sum != ntohl(md_header.sum)) { mdisk_content_free(); close (fd); sleep(1); goto retry; } close (fd); break; } close (fd); } if (mdisk->buffer == NULL) goto error; /* Set up a parser context */ mdisk->pctxt = xmlNewParserCtxt(); if (!mdisk->pctxt || !mdisk->pctxt->sax) { goto error; } mdisk->doc = xmlCtxtReadMemory(mdisk->pctxt, mdisk->buffer, mdisk->length, "mdisk.xml", NULL, XML_PARSE_NOENT | XML_PARSE_NONET | XML_PARSE_NOWARNING); if (!mdisk->doc) { libmsg("%s(): libxml failed to parse mdisk.xml buffer\n", __func__); goto error; } closedir(dir); return 0; error: if (dir) closedir(dir); libmsg("%s(): Unable to read metrics disk\n", __func__); return -1; }
/* * Get metric from the xml buffer, value set in pmdef */ static int get_mdef(metric_disk *mdisk, private_metric *pmdef) { xmlXPathContextPtr ctxt = NULL; xmlXPathObjectPtr obj; xmlNodePtr node; char *str; char *xpath; int ret = 0; ctxt = xmlXPathNewContext(mdisk->doc); if (!ctxt) { return -1; } /* Get the matching metric node type */ asprintf(&xpath, "//metrics/metric[name='%s'][@context='%s']", pmdef->name, pmdef->context); obj = xmlXPathEval(BAD_CAST xpath, ctxt); free(xpath); if ((obj == NULL) || (obj->type != XPATH_NODESET)) { libmsg("%s(): No metrics found that matches %s in context:%s or malformed definition\n", __func__, pmdef->name, pmdef->context); ret = -1; goto out; } if (xmlXPathNodeSetGetLength(obj->nodesetval) != 1) { libmsg("%s(): No metrics found that matches %s in context:%s or malformed definition\n", __func__, pmdef->name, pmdef->context); ret = -1; goto out; } node = obj->nodesetval->nodeTab[0]; if ((str = (char *)xmlGetProp(node, BAD_CAST "type")) == NULL) { libmsg("%s(): Metric type not specified\n", __func__); ret = -1; goto out; } metric_type_from_str((char *)str, &(pmdef->type)); free(str); xmlXPathFreeObject(obj); /* Get the matching metric node value */ asprintf(&xpath, "//metrics/metric[name='%s'][@context='%s']/value/text()", pmdef->name, pmdef->context); obj = xmlXPathEval( BAD_CAST xpath, ctxt); /* worked but no nodes */ free(xpath); if ((obj == NULL) || (obj->type != XPATH_NODESET)) { libmsg("%s(): No metrics value found!\n", __func__); ret = -1; goto out; } /* Get the nodes value content */ node = obj->nodesetval->nodeTab[0]; str = (char *)xmlNodeListGetString(mdisk->doc, node, 1); pmdef->value = strdup(str); free(str); out: if (obj) xmlXPathFreeObject(obj); if (ctxt) xmlXPathFreeContext(ctxt); return ret; }