/* initialize timezone */ void zoneInit(void) { int sts; int handle = -1; Archive *a; if (timeZone) { /* TZ from timezone string */ if ((sts = pmNewZone(timeZone)) < 0) fprintf(stderr, "%s: cannot set timezone to %s\n" "pmNewZone failed: %s\n", pmProgname, timeZone, pmErrStr(sts)); } else if (! archives && hostZone) { /* TZ from live host */ if ((handle = pmNewContext(PM_CONTEXT_HOST, dfltHostConn)) < 0) fprintf(stderr, "%s: cannot set timezone from %s\n" "pmNewContext failed: %s\n", pmProgname, findsource(dfltHostConn), pmErrStr(handle)); else if ((sts = pmNewContextZone()) < 0) fprintf(stderr, "%s: cannot set timezone from %s\n" "pmNewContextZone failed: %s\n", pmProgname, findsource(dfltHostConn), pmErrStr(sts)); else fprintf(stdout, "%s: timezone set to local timezone of host %s\n", pmProgname, dfltHostConn); if (handle >= 0) pmDestroyContext(handle); } else if (hostZone) { /* TZ from an archive */ a = archives; while (a) { if (strcmp(dfltHostName, a->hname) == 0) break; a = a->next; } if (! a) fprintf(stderr, "%s: no archive supplied for host %s\n", pmProgname, dfltHostName); else if ((handle = pmNewContext(PM_CONTEXT_ARCHIVE, a->fname)) < 0) fprintf(stderr, "%s: cannot set timezone from %s\npmNewContext failed: %s\n", pmProgname, findsource(dfltHostName), pmErrStr(handle)); else if ((sts = pmNewContextZone()) < 0) fprintf(stderr, "%s: cannot set timezone from %s\n" "pmNewContextZone failed: %s\n", pmProgname, findsource(dfltHostName), pmErrStr(sts)); else fprintf(stdout, "%s: timezone set to local timezone of host %s\n", pmProgname, dfltHostName); if (handle >= 0) pmDestroyContext(handle); } }
static char * rawlocalhost(pmOptions *opts) { int ctxt; char *host; if (opts->nhosts > 0) return opts->hosts[0]; if ((ctxt = pmNewContext(PM_CONTEXT_LOCAL, NULL)) < 0) { fprintf(stderr, "%s: cannot create local context: %s\n", pmProgname, pmErrStr(ctxt)); cleanstop(1); } host = (char *)pmGetContextHostName(ctxt); pmDestroyContext(ctxt); if (host[0] == '\0') { fprintf(stderr, "%s: cannot find local hostname\n", pmProgname); cleanstop(1); } return host; }
void freeFetch(Fetch *f) { if (f->profiles == NULL) { if (f->next) f->next->prev = f->prev; if (f->prev) f->prev->next = f->next; else { f->host->fetches = f->next; freeHost(f->host); } pmDestroyContext(f->handle); if (f->result) pmFreeResult(f->result); if (f->pmids) free(f->pmids); free(f); } }
/* * main */ int main(int argc, char **argv) { int sts; set_proc_fmt(); printf("pid=%" FMT_PID " ppid=%" FMT_PID "\n", getpid(), getppid()); getargs(argc, argv); if (pmnsfile != PM_NS_DEFAULT) { if ((sts = pmLoadNameSpace(pmnsfile)) < 0) { printf("%s: Cannot load pmnsfile from \"%s\": %s\n", pmProgname, pmnsfile, pmErrStr(sts)); exit(1); } } if ((sts = pmNewContext(PM_CONTEXT_HOST, host)) < 0) { printf("%s: Cannot connect to PMCD on host \"%s\": %s\n", pmProgname, host, pmErrStr(sts)); exit(1); } test_PMNS(); test_desc(); test_instance(); test_prof_fetch(); if (!is_hotproc) test_store(); if ((sts = pmWhichContext()) < 0) { printf("%s: pmWhichContext: %s\n", pmProgname, pmErrStr(sts)); exit(1); } pmDestroyContext(sts); exit(0); }
int QmcSource::delContext(int handle) { int i; int sts; for (i = 0; i < my.handles.size(); i++) if (my.handles[i] == handle) break; if (i == my.handles.size()) { if (pmDebug & DBG_TRACE_PMC) { QTextStream cerr(stderr); cerr << "QmcSource::delContext: Attempt to delete " << handle << " from list for " << my.desc << ", but it is not listed" << endl; } return PM_ERR_NOCONTEXT; } sts = pmDestroyContext(my.handles[i]); my.handles.removeAt(i); // If this is a valid source, but no more contexts remain, // then we should delete ourselves if (my.handles.size() == 0 && my.status >= 0) { if (pmDebug & DBG_TRACE_PMC) { QTextStream cerr(stderr); cerr << "QmcSource::delContext: No contexts remain, removing " << my.desc << endl; } delete this; } return sts; }
int zbx_module_pcp_uninit() { return pmDestroyContext(ctx); }
/* Return (in result) a list of active pmlogger ports on the specified machine. * The return value of the function is the number of elements in the array. * The caller must NOT free any part of the result stucture, it's storage is * managed here. Subsequent calls will overwrite the data so the caller should * copy it if persistence is required. */ int __pmLogFindPort(const char *host, int pid, __pmLogPort **lpp) { int ctx, oldctx; char *ctxhost; int sts, numval; int i, j; int findone = pid != PM_LOG_ALL_PIDS; int localcon = 0; /* > 0 for local connection */ pmDesc desc; pmResult *res; char *namelist[] = {"pmcd.pmlogger.port"}; pmID pmid; if (PM_MULTIPLE_THREADS(PM_SCOPE_LOGPORT)) return PM_ERR_THREAD; *lpp = NULL; /* pass null back in event of error */ localcon = __pmIsLocalhost(host); if (localcon > 0) /* do the work here instead of making PMCD do it */ return __pmLogFindLocalPorts(pid, lpp); else if (localcon < 0) return localcon; /* note: there may not be a current context */ ctx = 0; oldctx = pmWhichContext(); /* * Enclose ctxhost in [] in case it is an ipv6 address. This prevents * the first colon from being taken as a port separator by pmNewContext * and does no harm otherwise. */ ctxhost = malloc(strlen(host) + 2 + 1); if (ctxhost == NULL) { sts = -ENOMEM; goto ctxErr; } sprintf(ctxhost, "[%s]", host); ctx = pmNewContext(PM_CONTEXT_HOST, ctxhost); free(ctxhost); if (ctx < 0) return ctx; if ((sts = pmLookupName(1, namelist, &pmid)) < 0) goto ctxErr; if ((sts = pmLookupDesc(pmid, &desc)) < 0) goto ctxErr; if ((sts = pmFetch(1, &pmid, &res) < 0)) goto ctxErr; if ((sts = numval = res->vset[0]->numval) < 0) goto resErr; j = 0; if (numval) { if (resize_logports(findone ? 1 : numval) < 0) { sts = -oserror(); goto resErr; } /* scan the pmResult, copying matching pid(s) to logport */ for (i = j = 0; i < numval; i++) { __pmLogPort *p = &logport[j]; pmValue *vp = &res->vset[0]->vlist[i]; if (vp->inst == 1) /* old vcr instance (pseudo-init) */ continue; if (findone && vp->inst != pid) continue; p->pid = vp->inst; p->port = vp->value.lval; sts = pmNameInDom(desc.indom, p->pid, &p->name); if (sts < 0) { p->name = NULL; goto resErr; } j++; if (findone) /* found one, stop searching */ break; } *lpp = logport; } sts = j; /* the number actually added */ resErr: pmFreeResult(res); ctxErr: if (oldctx >= 0) pmUseContext(oldctx); if (ctx >= 0) pmDestroyContext(ctx); return sts; }
void rawarchive(pmOptions *opts, const char *name) { struct tm *tp; time_t timenow; char tmp[MAXPATHLEN]; char path[MAXPATHLEN]; char *logdir, *py, *host; int sep = __pmPathSeparator(); int sts, len = (name? strlen(name) : 0); if (len == 0) return rawfolio(opts); /* see if a valid archive exists as specified */ if ((sts = pmNewContext(PM_CONTEXT_ARCHIVE, name)) >= 0) { pmDestroyContext(sts); __pmAddOptArchive(opts, (char * )name); return; } /* see if a valid folio exists as specified */ strncpy(tmp, name, sizeof(tmp)); tmp[sizeof(tmp)-1] = '\0'; if (access(tmp, R_OK) == 0) { __pmAddOptArchiveFolio(opts, tmp); return; } snprintf(path, sizeof(path), "%s/%s.folio", name, basename(tmp)); path[sizeof(path)-1] = '\0'; if (access(path, R_OK) == 0) { __pmAddOptArchiveFolio(opts, path); return; } /* else go hunting in the system locations... */ if ((logdir = pmGetOptionalConfig("PCP_LOG_DIR")) == NULL) { fprintf(stderr, "%s: cannot find PCP_LOG_DIR\n", pmProgname); cleanstop(1); } host = rawlocalhost(opts); /* ** Use original rawread() algorithms for specifying dates */ if (len == 8 && lookslikedatetome(name)) { snprintf(path, sizeof(path), "%s%c%s%c%s%c%s", logdir, sep, "pmlogger", sep, host, sep, name); __pmAddOptArchive(opts, (char * )path); } /* ** if one or more 'y' (yesterday) characters are used and that ** string is not known as an existing file, the standard logfile ** is shown from N days ago (N is determined by the number ** of y's). */ else { /* ** make a string existing of y's to compare with */ py = malloc(len+1); ptrverify(py, "Malloc failed for 'yes' sequence\n"); memset(py, 'y', len); *(py+len) = '\0'; if ( strcmp(name, py) == 0 ) { timenow = time(0); timenow -= len*3600*24; tp = localtime(&timenow); snprintf(path, sizeof(path), "%s%c%s%c%s%c%04u%02u%02u", logdir, sep, "pmlogger", sep, host, sep, tp->tm_year+1900, tp->tm_mon+1, tp->tm_mday); __pmAddOptArchive(opts, (char * )path); } else { fprintf(stderr, "%s: cannot find archive from \"%s\"\n", pmProgname, name); cleanstop(1); } free(py); } }
/* reinitialize Metric - only for live host */ int /* 1: ok, 0: try again later, -1: fail */ reinitMetric(Metric *m) { char *hname = symName(m->hname); char *mname = symName(m->mname); char **inames; int *iids; int handle; int ret = 1; int sts; int i, j; /* set up temporary context */ if ((handle = newContext(hname)) < 0) return 0; host_state_changed(hname, STATE_RECONN); if ((sts = pmLookupName(1, &mname, &m->desc.pmid)) < 0) { ret = 0; goto end; } /* fill in performance metric descriptor */ if ((sts = pmLookupDesc(m->desc.pmid, &m->desc)) < 0) { ret = 0; goto end; } if (m->desc.type == PM_TYPE_STRING || m->desc.type == PM_TYPE_AGGREGATE || m->desc.type == PM_TYPE_AGGREGATE_STATIC || m->desc.type == PM_TYPE_EVENT || m->desc.type == PM_TYPE_HIGHRES_EVENT || m->desc.type == PM_TYPE_UNKNOWN) { fprintf(stderr, "%s: metric %s has non-numeric type\n", pmProgname, mname); ret = -1; } else if (m->desc.indom == PM_INDOM_NULL) { if (m->specinst != 0) { fprintf(stderr, "%s: metric %s has no instances\n", pmProgname, mname); ret = -1; } else m->m_idom = 1; } else { if ((sts = pmGetInDom(m->desc.indom, &iids, &inames)) < 0) { /* full profile */ ret = 0; } else { if (m->specinst == 0) { /* all instances */ m->iids = iids; m->m_idom = sts; m->inames = alloc(m->m_idom*sizeof(char *)); for (i = 0; i < m->m_idom; i++) { m->inames[i] = sdup(inames[i]); } } else { /* explicit instance profile */ m->m_idom = 0; for (i = 0; i < m->specinst; i++) { /* look for first matching instance name */ for (j = 0; j < sts; j++) { if (eqinst(m->inames[i], inames[j])) { m->iids[i] = iids[j]; m->m_idom++; break; } } if (j == sts) { m->iids[i] = PM_IN_NULL; ret = 0; } } if (sts > 0) { /* * pmGetInDom or pmGetInDomArchive returned some * instances above */ free(iids); } /* * if specinst != m_idom, then some not found ... move these * to the end of the list */ for (j = m->specinst-1; j >= 0; j--) { if (m->iids[j] != PM_IN_NULL) break; } for (i = 0; i < j; i++) { if (m->iids[i] == PM_IN_NULL) { /* need to swap */ char *tp; tp = m->inames[i]; m->inames[i] = m->inames[j]; m->iids[i] = m->iids[j]; m->inames[j] = tp; m->iids[j] = PM_IN_NULL; j--; } } } #if PCP_DEBUG if (pmDebug & DBG_TRACE_APPL1) { int numinst; fprintf(stderr, "reinitMetric: %s from %s: instance domain specinst=%d\n", mname, hname, m->specinst); if (m->m_idom < 1) fprintf(stderr, " %d instances!\n", m->m_idom); if (m->specinst == 0) numinst = m->m_idom; else numinst = m->specinst; for (i = 0; i < numinst; i++) { fprintf(stderr, " indom[%d]", i); if (m->iids[i] == PM_IN_NULL) fprintf(stderr, " ?missing"); else fprintf(stderr, " %d", m->iids[i]); fprintf(stderr, " \"%s\"\n", m->inames[i]); } } #endif if (sts > 0) { /* * pmGetInDom or pmGetInDomArchive returned some instances * above */ free(inames); } } } if (ret == 1) { /* compute conversion factor into canonical units - non-zero conversion factor flags initialized metric */ m->conv = scale(m->desc.units); /* automatic rate computation */ if (m->desc.sem == PM_SEM_COUNTER) { m->vals = (double *) ralloc(m->vals, m->m_idom * sizeof(double)); for (j = 0; j < m->m_idom; j++) m->vals[j] = 0; } } if (ret >= 0) { /* * re-shape, starting here are working up the expression until * we reach the top of the tree or the designated metrics * associated with the node are not the same */ Expr *x = m->expr; while (x) { /* * only re-shape expressions that may have set values */ if (x->op == CND_FETCH || x->op == CND_NEG || x->op == CND_ADD || x->op == CND_SUB || x->op == CND_MUL || x->op == CND_DIV || x->op == CND_SUM_HOST || x->op == CND_SUM_INST || x->op == CND_SUM_TIME || x->op == CND_AVG_HOST || x->op == CND_AVG_INST || x->op == CND_AVG_TIME || x->op == CND_MAX_HOST || x->op == CND_MAX_INST || x->op == CND_MAX_TIME || x->op == CND_MIN_HOST || x->op == CND_MIN_INST || x->op == CND_MIN_TIME || x->op == CND_EQ || x->op == CND_NEQ || x->op == CND_LT || x->op == CND_LTE || x->op == CND_GT || x->op == CND_GTE || x->op == CND_NOT || x->op == CND_AND || x->op == CND_OR || x->op == CND_RISE || x->op == CND_FALL || x->op == CND_INSTANT || x->op == CND_MATCH || x->op == CND_NOMATCH) { instFetchExpr(x); findEval(x); #if PCP_DEBUG if (pmDebug & DBG_TRACE_APPL1) { fprintf(stderr, "reinitMetric: re-shaped ...\n"); dumpExpr(x); } #endif } if (x->parent) { x = x->parent; if (x->metrics == m) continue; } break; } } end: /* destroy temporary context */ pmDestroyContext(handle); return ret; }
/* initialize Metric */ int /* 1: ok, 0: try again later, -1: fail */ initMetric(Metric *m) { char *hname = symName(m->hname); char *mname = symName(m->mname); char **inames; int *iids; int handle; int ret = 1; int sts; int i, j; /* set up temporary context */ if ((handle = newContext(hname)) < 0) return 0; host_state_changed(hname, STATE_RECONN); if ((sts = pmLookupName(1, &mname, &m->desc.pmid)) < 0) { fprintf(stderr, "%s: metric %s not in namespace for %s\n" "pmLookupName failed: %s\n", pmProgname, mname, findsource(hname), pmErrStr(sts)); ret = 0; goto end; } /* fill in performance metric descriptor */ if ((sts = pmLookupDesc(m->desc.pmid, &m->desc)) < 0) { fprintf(stderr, "%s: metric %s not currently available from %s\n" "pmLookupDesc failed: %s\n", pmProgname, mname, findsource(hname), pmErrStr(sts)); ret = 0; goto end; } if (m->desc.type == PM_TYPE_STRING || m->desc.type == PM_TYPE_AGGREGATE || m->desc.type == PM_TYPE_AGGREGATE_STATIC || m->desc.type == PM_TYPE_EVENT || m->desc.type == PM_TYPE_HIGHRES_EVENT || m->desc.type == PM_TYPE_UNKNOWN) { fprintf(stderr, "%s: metric %s has non-numeric type\n", pmProgname, mname); ret = -1; } else if (m->desc.indom == PM_INDOM_NULL) { if (m->specinst != 0) { fprintf(stderr, "%s: metric %s has no instances\n", pmProgname, mname); ret = -1; } else m->m_idom = 1; } else { /* metric has instances, get full instance profile */ if (archives) { if ((sts = pmGetInDomArchive(m->desc.indom, &iids, &inames)) < 0) { fprintf(stderr, "Metric %s from %s - instance domain not " "available in archive\npmGetInDomArchive failed: %s\n", mname, findsource(hname), pmErrStr(sts)); ret = -1; } } else if ((sts = pmGetInDom(m->desc.indom, &iids, &inames)) < 0) { fprintf(stderr, "Instance domain for metric %s from %s not (currently) available\n" "pmGetIndom failed: %s\n", mname, findsource(hname), pmErrStr(sts)); ret = 0; } if (ret == 1) { /* got instance profile */ if (m->specinst == 0) { /* all instances */ m->iids = iids; m->m_idom = sts; m->inames = alloc(m->m_idom*sizeof(char *)); for (i = 0; i < m->m_idom; i++) { m->inames[i] = sdup(inames[i]); } } else { /* selected instances only */ m->m_idom = 0; for (i = 0; i < m->specinst; i++) { /* look for first matching instance name */ for (j = 0; j < sts; j++) { if (eqinst(m->inames[i], inames[j])) { m->iids[i] = iids[j]; m->m_idom++; break; } } if (j == sts) { __pmNotifyErr(LOG_ERR, "metric %s from %s does not " "(currently) have instance \"%s\"\n", mname, findsource(hname), m->inames[i]); m->iids[i] = PM_IN_NULL; ret = 0; } } if (sts > 0) { /* * pmGetInDom or pmGetInDomArchive returned some * instances above */ free(iids); } /* * if specinst != m_idom, then some not found ... move these * to the end of the list */ for (j = m->specinst-1; j >= 0; j--) { if (m->iids[j] != PM_IN_NULL) break; } for (i = 0; i < j; i++) { if (m->iids[i] == PM_IN_NULL) { /* need to swap */ char *tp; tp = m->inames[i]; m->inames[i] = m->inames[j]; m->iids[i] = m->iids[j]; m->inames[j] = tp; m->iids[j] = PM_IN_NULL; j--; } } } #if PCP_DEBUG if (pmDebug & DBG_TRACE_APPL1) { int numinst; fprintf(stderr, "initMetric: %s from %s: instance domain specinst=%d\n", mname, hname, m->specinst); if (m->m_idom < 1) fprintf(stderr, " %d instances!\n", m->m_idom); numinst = m->specinst == 0 ? m->m_idom : m->specinst; for (i = 0; i < numinst; i++) { fprintf(stderr, " indom[%d]", i); if (m->iids[i] == PM_IN_NULL) fprintf(stderr, " ?missing"); else fprintf(stderr, " %d", m->iids[i]); fprintf(stderr, " \"%s\"\n", m->inames[i]); } } #endif if (sts > 0) { /* * pmGetInDom or pmGetInDomArchive returned some instances * above */ free(inames); } } } if (ret == 1) { /* compute conversion factor into canonical units - non-zero conversion factor flags initialized metric */ m->conv = scale(m->desc.units); /* automatic rate computation */ if (m->desc.sem == PM_SEM_COUNTER) { m->vals = (double *) ralloc(m->vals, m->m_idom * sizeof(double)); for (j = 0; j < m->m_idom; j++) m->vals[j] = 0; } } end: /* destroy temporary context */ pmDestroyContext(handle); /* retry not meaningful for archives */ if (archives && (ret == 0)) ret = -1; return ret; }
/* initialize access to archive */ int initArchive(Archive *a) { pmLogLabel label; struct timeval tv; int sts; int handle; Archive *b; const char *tmp; /* setup temorary context for the archive */ if ((sts = pmNewContext(PM_CONTEXT_ARCHIVE, a->fname)) < 0) { fprintf(stderr, "%s: cannot open archive %s\n" "pmNewContext failed: %s\n", pmProgname, a->fname, pmErrStr(sts)); return 0; } handle = sts; tmp = pmGetContextHostName(handle); if (strlen(tmp) == 0) { fprintf(stderr, "%s: pmGetContextHostName(%d) failed\n", pmProgname, handle); return 0; } if ((a->hname = strdup(tmp)) == NULL) __pmNoMem("host name copy", strlen(tmp)+1, PM_FATAL_ERR); /* get the goodies from archive label */ if ((sts = pmGetArchiveLabel(&label)) < 0) { fprintf(stderr, "%s: cannot read label from archive %s\n" "pmGetArchiveLabel failed: %s\n", pmProgname, a->fname, pmErrStr(sts)); pmDestroyContext(handle); return 0; } a->first = __pmtimevalToReal(&label.ll_start); if ((sts = pmGetArchiveEnd(&tv)) < 0) { fprintf(stderr, "%s: archive %s is corrupted\n" "pmGetArchiveEnd failed: %s\n", pmProgname, a->fname, pmErrStr(sts)); pmDestroyContext(handle); return 0; } a->last = __pmtimevalToReal(&tv); /* check for duplicate host */ b = archives; while (b) { if (strcmp(a->hname, b->hname) == 0) { fprintf(stderr, "%s: Error: archive %s not legal - archive %s is already open " "for host %s\n", pmProgname, a->fname, b->fname, b->hname); pmDestroyContext(handle); return 0; } b = b->next; } /* put archive record on the archives list */ a->next = archives; archives = a; /* update first and last available data points */ if (first == -1 || a->first < first) first = a->first; if (a->last > last) last = a->last; pmDestroyContext(handle); return 1; }
int main(int argc, char **argv) { int sts; int ch; int errflag = 0; int a, b, c; __pmSetProgname(argv[0]); while ((ch = getopt(argc, argv, "D:?")) != EOF) { switch (ch) { #ifdef PCP_DEBUG case 'D': /* debug flag */ sts = __pmParseDebug(optarg); if (sts < 0) { fprintf(stderr, "%s: unrecognized debug flag specification (%s)\n", pmProgname, optarg); errflag++; } else pmDebug |= sts; break; #endif case '?': default: errflag++; break; } } if (errflag || optind != argc-2) { fprintf(stderr, "Usage: %s archive1 archive2\n", pmProgname); exit(1); } a = pmNewContext(PM_CONTEXT_ARCHIVE, argv[optind]); if (a < 0) { fprintf(stderr, "%s: first pmNewContext(..., %s): %s\n", pmProgname, argv[optind], pmErrStr(a)); exit(1); } pmDestroyContext(a); b = pmNewContext(PM_CONTEXT_HOST, "localhost"); if (b < 0) { fprintf(stderr, "%s: pmNewContext(..., localhost): %s\n", pmProgname, pmErrStr(b)); exit(1); } c = pmNewContext(PM_CONTEXT_ARCHIVE, argv[optind+1]); if (c < 0) { fprintf(stderr, "%s: second pmNewContext(..., %s): %s\n", pmProgname, argv[optind+1], pmErrStr(c)); exit(1); } exit(0); }