/var/lib/pcp/testsuite/src/lookupnametest.c is in pcp-testsuite 4.0.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | /*
* Copyright (c) 1995-2001 Silicon Graphics, Inc. All Rights Reserved.
* Copyright (c) 2015 Ken McDonell. All Rights Reserved.
* Copyright (c) 2015 Martins Innus. All Rights Reserved.
*/
#include <pcp/pmapi.h>
#define BUILD_STANDALONE 1
int
main(int argc, char **argv)
{
int c;
int sts;
int errflag = 0;
int type = 0;
int mode = PM_MODE_INTERP; /* mode for archives */
int i;
int numpmid;
char *host = NULL; /* pander to gcc */
char *pmnsfile = PM_NS_DEFAULT;
char **namelist;
pmID *pmidlist;
char *name;
/* trim cmd name of leading directory components */
pmSetProgname(argv[0]);
while ((c = getopt(argc, argv, "a:D:h:Ln:x?")) != EOF) {
switch (c) {
case 'a': /* archive name */
if (type != 0) {
#ifdef BUILD_STANDALONE
fprintf(stderr, "%s: at most one of -a, -h, -L and -x allowed\n", pmGetProgname());
#else
fprintf(stderr, "%s: at most one of -a, -h and -x allowed\n", pmGetProgname());
#endif
errflag++;
}
type = PM_CONTEXT_ARCHIVE;
host = optarg;
printf("Using archive context: %s\n", host);
break;
case 'D': /* debug options */
sts = pmSetDebug(optarg);
if (sts < 0) {
fprintf(stderr, "%s: unrecognized debug options specification (%s)\n",
pmGetProgname(), optarg);
errflag++;
}
break;
case 'h': /* contact PMCD on this hostname */
if (type != 0) {
#ifdef BUILD_STANDALONE
fprintf(stderr, "%s: at most one of -a, -h, -L and -x allowed\n", pmGetProgname());
#else
fprintf(stderr, "%s: at most one of -a, -h and -x allowed\n", pmGetProgname());
#endif
errflag++;
}
host = optarg;
type = PM_CONTEXT_HOST;
printf("Using host context: %s\n", host);
break;
#ifdef BUILD_STANDALONE
case 'L': /* LOCAL, no PMCD */
if (type != 0) {
fprintf(stderr, "%s: at most one of -a, -h, -L and -x allowed\n", pmGetProgname());
errflag++;
}
host = NULL;
type = PM_CONTEXT_LOCAL;
putenv("PMDA_LOCAL_PROC="); /* if proc PMDA needed */
putenv("PMDA_LOCAL_SAMPLE="); /* if sampledso PMDA needed */
printf("Using PM_CONTEXT_LOCAL context\n");
break;
#endif
case 'n': /* alternative name space file */
pmnsfile = optarg;
printf("Using PMNS: %s\n", pmnsfile);
break;
case 'x': /* no pmNewContext */
if (type != 0) {
#ifdef BUILD_STANDALONE
fprintf(stderr, "%s: at most one of -a, -h, -L and -x allowed\n", pmGetProgname());
#else
fprintf(stderr, "%s: at most one of -a, -h and -x allowed\n", pmGetProgname());
#endif
errflag++;
}
type = -1;
printf("Using no context\n");
break;
case '?':
default:
errflag++;
break;
}
}
if (errflag) {
fprintf(stderr,
"Usage: %s [options] metricname ...\n\
\n\
Options:\n\
-a archive metrics source is a PCP log archive\n\
-h host metrics source is PMCD on host\n"
#ifdef BUILD_STANDALONE
" -L use local context instead of PMCD\n"
#endif
" -n pmnsfile use an alternative PMNS\n\
-x don't call pmNewContext\n",
pmGetProgname());
exit(1);
}
if (pmnsfile != PM_NS_DEFAULT && (sts = pmLoadASCIINameSpace(pmnsfile, 1)) < 0) {
printf("%s: Cannot load namespace from \"%s\": %s\n", pmGetProgname(),
pmnsfile, pmErrStr(sts));
exit(1);
}
if (type >= 0) {
if (type == 0) {
type = PM_CONTEXT_HOST;
host = "local:";
}
if ((sts = pmNewContext(type, host)) < 0) {
if (type == PM_CONTEXT_HOST)
fprintf(stderr, "%s: Cannot connect to PMCD on host \"%s\": %s\n",
pmGetProgname(), host, pmErrStr(sts));
else
fprintf(stderr, "%s: Cannot open archive \"%s\": %s\n",
pmGetProgname(), host, pmErrStr(sts));
exit(1);
}
if (type == PM_CONTEXT_ARCHIVE) {
pmLogLabel label;
if ((sts = pmGetArchiveLabel(&label)) < 0) {
fprintf(stderr, "%s: Cannot get archive label record: %s\n",
pmGetProgname(), pmErrStr(sts));
exit(1);
}
if (mode != PM_MODE_INTERP) {
if ((sts = pmSetMode(mode, &label.ll_start, 0)) < 0) {
fprintf(stderr, "%s: pmSetMode: %s\n", pmGetProgname(), pmErrStr(sts));
exit(1);
}
}
}
}
/* metricname args are argv[optind] ... argv[argc-1] */
numpmid = argc - optind;
namelist = (char **)malloc(numpmid*sizeof(namelist[0]));
if (namelist == NULL) {
fprintf(stderr, "%s: namelist malloc(%d) failed\n", pmGetProgname(), (int)(numpmid*sizeof(namelist[0])));
exit(1);
}
pmidlist = (pmID *)malloc(numpmid*sizeof(pmidlist[0]));
if (pmidlist == NULL) {
fprintf(stderr, "%s: pmidlist malloc(%d) failed\n", pmGetProgname(), (int)(numpmid*sizeof(pmidlist[0])));
exit(1);
}
for (i = 0; i < numpmid; i++) {
namelist[i] = argv[optind+i];
/* bogus pmID to be sure real values are set below pmLookupName() */
pmidlist[i] = pmID_build(i+1, i+1, i+1);
}
sts = pmLookupName(numpmid, namelist, pmidlist);
printf("pmLookupName -> %d", sts);
if (sts < 0)
printf(": %s\n", pmErrStr(sts));
else
putchar('\n');
for (i = 0; i < numpmid; i++) {
printf("[%d] %s", i, namelist[i]);
printf(" %s", pmIDStr(pmidlist[i]));
if (pmidlist[i] == PM_ID_NULL) {
/* this one failed */
sts = pmLookupName(1, &namelist[i], &pmidlist[i]);
printf(" (%s)\n", pmErrStr(sts));
}
else {
/*
* success ... expect reverse lookups to work
*/
char **names;
int match = 0;
sts = pmNameAll(pmidlist[i], &names);
if (sts < 0)
printf(" pmNameAll: %s", pmErrStr(sts));
else {
int j;
for (j = 0; j < sts; j++) {
if (strcmp(namelist[i], names[j]) == 0) {
match++;
break;
}
}
if (match != 1) {
/* oops, no matching name or more than one match! */
printf(" botch pmNameAll ->");
for (j = 0; j < sts; j++)
printf(" %s", names[j]);
}
free(names);
}
sts = pmNameID(pmidlist[i], &name);
if (sts < 0)
printf(" pmNameID: %s", pmErrStr(sts));
else {
if (strcmp(namelist[i], name) != 0) {
/*
* mismatch is OK if dups in PMNS and correct one
* returned by pmNameAll
*/
if (match != 1)
printf(" botch pmNameID -> %s", name);
}
free(name);
}
putchar('\n');
}
}
return 0;
}
|