This file is indexed.

/usr/share/pcp/demos/pmclient/pmclient_fg.c is in pcp 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
 * pmclient_fg - sample, simpler PMAPI/fetchgroup client
 *
 * Copyright (c) 2013-2015 Red Hat.
 * Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "pmapi.h"
#include "libpcp.h"
#include <assert.h>

pmLongOptions longopts[] = {
    PMAPI_GENERAL_OPTIONS,
    PMAPI_OPTIONS_HEADER("Reporting options"),
    { "pause", 0, 'P', 0, "pause between updates for archive replay" },
    PMAPI_OPTIONS_END
};

pmOptions opts = {
    .flags = PM_OPTFLAG_STDOUT_TZ | PM_OPTFLAG_BOUNDARIES,
    .short_options = PMAPI_OPTIONS "P",
    .long_options = longopts,
};

typedef struct {
    struct timeval	timestamp;	/* last fetched time */
    double		cpu_util;	/* aggregate CPU utilization, usr+sys */
    int			peak_cpu;	/* most utilized CPU, if > 1 CPU */
    double		peak_cpu_util;	/* utilization for most utilized CPU */
    pmAtomValue		freemem;	/* free memory (Mbytes) */
    pmAtomValue		dkiops;		/* aggregate disk I/O's per second */
    pmAtomValue		load1;		/* 1 minute load average */
    pmAtomValue		load15;		/* 15 minute load average */
    pmAtomValue		ncpu;		/* number of cpus */
    unsigned int	last_ncpu;	/* last seen number of cpus */
} info_t;

static info_t		info;
static pmFG		pmfg;

static void
get_sample(void)
{
    enum { indom_maxnum = 1024 };
    static int		cpu_user_inst[indom_maxnum]; 
    static pmAtomValue 	cpu_user[indom_maxnum];
    static unsigned	num_cpu_user;
    static int		cpu_sys_inst[indom_maxnum];
    static pmAtomValue	cpu_sys[indom_maxnum];
    static unsigned	num_cpu_sys;
    static int		setup;
    int			sts;
    int			i;

    if (!setup) {
	setup = 1;

	if ((sts = pmExtendFetchGroup_item(pmfg, "hinv.ncpu", NULL, NULL,
					   &info.ncpu, PM_TYPE_32, &sts)) < 0) {
	    fprintf(stderr, "%s: Failed hinv.ncpu ExtendFetchGroup: %s\n",
		    pmGetProgname(), pmErrStr(sts));
	    exit(1);
	}
	
	/*
	 * Because of pmfg_item's willingness to scan to the end of an
	 * archive to do metric/instance resolution, we don't have to
	 * specially handle the PM_CONTEXT_ARCHIVE case here.
	 */

	if ((sts = pmExtendFetchGroup_item(pmfg,
				"kernel.all.load", "1 minute", NULL,
				&info.load1, PM_TYPE_DOUBLE, NULL)) < 0) {
	    fprintf(stderr, "%s: Failed kernel.all.load[1] "
			    "ExtendFetchGroup: %s\n",
		    pmGetProgname(), pmErrStr(sts));
	    exit(1);
	}
	if ((sts = pmExtendFetchGroup_item(pmfg,
				"kernel.all.load", "15 minute", NULL,
				&info.load15, PM_TYPE_DOUBLE, NULL)) < 0) {
	    fprintf(stderr, "%s: Failed kernel.all.load[15] "
			    "ExtendFetchGroup: %s\n",
		    pmGetProgname(), pmErrStr(sts));
	    exit(1);
	}
	if ((sts = pmExtendFetchGroup_indom(pmfg,
				"kernel.percpu.cpu.user", "second/second",
				cpu_user_inst, NULL, cpu_user, PM_TYPE_DOUBLE,
				NULL, indom_maxnum, &num_cpu_user, NULL)) < 0) {
	    fprintf(stderr, "%s: Failed kernel.percpu.cpu.user "
				"ExtendFetchGroup: %s\n",
		    pmGetProgname(), pmErrStr(sts));
	    exit(1);
	}

	if ((sts = pmExtendFetchGroup_indom(pmfg,
				"kernel.percpu.cpu.sys", "second/second",
				cpu_sys_inst, NULL, cpu_sys, PM_TYPE_DOUBLE,
				NULL, indom_maxnum, &num_cpu_sys, NULL)) < 0) {
	    fprintf(stderr, "%s: Failed kernel.percpu.cpu.sys "
				"ExtendFetchGroup: %s\n",
		    pmGetProgname(), pmErrStr(sts));
	    exit(1);
	}
	if ((sts = pmExtendFetchGroup_item(pmfg,
				"mem.freemem", NULL, "Mbyte",
				&info.freemem, PM_TYPE_DOUBLE, NULL)) < 0) {
	    fprintf(stderr, "%s: Failed mem.freemem "
				"ExtendFetchGroup: %s\n",
		    pmGetProgname(), pmErrStr(sts));
	    exit(1);
	}
	if ((sts = pmExtendFetchGroup_item(pmfg,
				"disk.all.total", NULL, "count/second",
				&info.dkiops, PM_TYPE_32, NULL)) < 0) {
	    fprintf(stderr, "%s: Failed disk.all.total "
				"ExtendFetchGroup: %s\n",
		    pmGetProgname(), pmErrStr(sts));
	    exit(1);
	}
	if ((sts = pmExtendFetchGroup_timestamp(pmfg, &info.timestamp)) < 0) {
	    fprintf(stderr, "%s: Failed ExtendFetchGroup: %s\n",
		    pmGetProgname(), pmErrStr(sts));
	    exit(1);
	}

	/*
	 * Since we don't have a "last" call, we will have some
	 * some memory at exit, namely the cpu_sys and cpu_user
	 * arrays, and the object hiding behind pmfg.
	 */
    }

    /*
     * Fetch the current metrics; fill many info.* fields.  Since we
     * passed NULLs to most fetchgroup status int*'s, we'll get
     * PM_TYPE_DOUBLE fetch/conversion errors represented by NaN's.
     */
    sts = pmFetchGroup(pmfg);
    if (sts < 0) {
	fprintf(stderr, "%s: pmFetchGroup: %s\n", pmGetProgname(), pmErrStr(sts));
	exit(1);
    }

    /* compute rate-converted values */
    info.cpu_util = 0;
    info.peak_cpu_util = -1;	/* force re-assignment at first CPU */

    /*
     * Safely assume that the cpu_user and cpu_sys indoms are identical
     * and that each has a corresponding set of values, so we zip them
     * up pairwise with one iteration and no auxiliary data structures.
     */
    assert(num_cpu_user == num_cpu_sys);
    for (i = 0; i < num_cpu_user; i++) {
	double util;

	/* corresponding instances */
	assert(cpu_user_inst[i] == cpu_sys_inst[i]);

	util = cpu_user[i].d + cpu_sys[i].d; /* already rate-converted */
	if (util > 1.0)
	    /* small errors are possible, so clip the utilization at 1.0 */
	    util = 1.0;
	info.cpu_util += util;
	if (util > info.peak_cpu_util) {
	    info.peak_cpu_util = util;
	    /* NB: i is indom instance, not result index */
	    info.peak_cpu = cpu_user_inst[i];
	}
    }
    assert(info.ncpu.l != 0);
    info.cpu_util /= info.ncpu.l;
}

int
main(int argc, char **argv)
{
    int			c;
    int			sts;
    int			samples;
    int			pauseFlag = 0;
    int			lines = 0;
    char		*source;
    const char		*host;
    char		timebuf[26];	/* for pmCtime result */

    setlinebuf(stdout);

    while ((c = pmGetOptions(argc, argv, &opts)) != EOF) {
	switch (c) {
	case 'P':
	    pauseFlag++;
	    break;
	default:
	    opts.errors++;
	    break;
	}
    }

    if (pauseFlag && opts.context != PM_CONTEXT_ARCHIVE) {
	pmprintf("%s: pause can only be used with archives\n", pmGetProgname());
	opts.errors++;
    }

    if (opts.errors || opts.optind < argc - 1) {
	pmUsageMessage(&opts);
	exit(1);
    }

    if (opts.context == PM_CONTEXT_ARCHIVE) {
	source = opts.archives[0];
    } else if (opts.context == PM_CONTEXT_HOST) {
	source = opts.hosts[0];
    } else {
	opts.context = PM_CONTEXT_HOST;
	source = "local:";
    }

    sts = pmCreateFetchGroup(& pmfg, opts.context, source);
    if (sts < 0) {
	if (opts.context == PM_CONTEXT_HOST)
	    fprintf(stderr, "%s: Cannot connect to PMCD on host \"%s\": %s\n",
		    pmGetProgname(), source, pmErrStr(sts));
	else
	    fprintf(stderr, "%s: Cannot open archive \"%s\": %s\n",
		    pmGetProgname(), source, pmErrStr(sts));
	exit(1);
    }
    c = pmGetFetchGroupContext(pmfg);
    
    /* complete TZ and time window option (origin) setup */
    if (pmGetContextOptions(c, &opts)) {
	pmflush();
	exit(1);
    }

    host = pmGetContextHostName(c);

    /* set a default sampling interval if none has been requested */
    if (opts.interval.tv_sec == 0 && opts.interval.tv_usec == 0)
	opts.interval.tv_sec = 5;

    if (opts.context == PM_CONTEXT_ARCHIVE) {
	if ((sts = pmSetMode(PM_MODE_INTERP, &opts.start, (int)(opts.interval.tv_sec*1000 + opts.interval.tv_usec/1000))) < 0) {
	    fprintf(stderr, "%s: pmSetMode failed: %s\n",
		    pmGetProgname(), pmErrStr(sts));
	    exit(1);
	}
    }

    if (opts.context == PM_CONTEXT_ARCHIVE)
	get_sample(); /* fetch the separate early ncpu record */
    get_sample(); /* fetch other rate metrics */

    /* set sampling loop termination via the command line options */
    samples = opts.samples ? opts.samples : -1;

    while (samples == -1 || samples-- > 0) {
	if (lines % 15 == 0) {
	    time_t	now = info.timestamp.tv_sec;
	    if (opts.context == PM_CONTEXT_ARCHIVE)
		printf("Archive: %s, ", opts.archives[0]);
	    printf("Host: %s, %d cpu(s), %s",
		   host, info.ncpu.l,
		   pmCtime(&now, timebuf));
/* - report format
  CPU  Busy    Busy  Free Mem   Disk     Load Average
 Util   CPU    Util  (Mbytes)   IOPS    1 Min  15 Min
X.XXX   XXX   X.XXX XXXXX.XXX XXXXXX  XXXX.XX XXXX.XX
*/
	    printf("  CPU");
	    if (info.ncpu.l > 1)
		printf("  Busy    Busy");
	    printf("  Free Mem   Disk     Load Average\n");
	    printf(" Util");
	    if (info.ncpu.l > 1)
		printf("   CPU    Util");
	    printf("  (Mbytes)   IOPS    1 Min  15 Min\n");
	}
	if (opts.context != PM_CONTEXT_ARCHIVE || pauseFlag)
	    __pmtimevalSleep(opts.interval);
	get_sample();
	printf("%5.2f", info.cpu_util);
	if (info.ncpu.l > 1)
	    printf("   %3d   %5.2f", info.peak_cpu, info.peak_cpu_util);
	printf(" %9.3f", info.freemem.d);
	printf(" %6d", info.dkiops.l);
	printf("  %7.2f %7.2f\n", info.load1.d, info.load15.d);
 	lines++;
    }
    exit(0);
}