summaryrefslogtreecommitdiff
path: root/qa/src/hp-mib.c
blob: fb31b936d41f64870b9fb54615004a8757de9835 (plain)
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
/*
 * Copyright (c) 1997-2001 Silicon Graphics, Inc.  All Rights Reserved.
 */

/* short test program for hp-mib metrics */

#include <pcp/pmapi.h>
#include <pcp/impl.h>

static char *metrics[] = {
    "proc.nprocs",
    "proc.psinfo.pid",
    "proc.psinfo.uid",
    "proc.psinfo.ppid",
    "proc.psinfo.nice",
    "proc.psinfo.ttydev",
    "proc.psinfo.gid",
    "proc.psinfo.pgrp",
    "proc.psinfo.pri",
    "proc.psinfo.oldpri",
    "proc.psinfo.addr",
    "proc.psinfo.cpu",
    "proc.psinfo.uname",
    "proc.psinfo.gname",
    "proc.psinfo.ttyname",
    "proc.pstatus.utime",
    "proc.pstatus.stime",
    "proc.psusage.starttime",
    "proc.pstatus.flags",
    "proc.psinfo.state",
    "proc.psinfo.sname",
    "proc.psinfo.wchan",
    "proc.psinfo.fname",
    "proc.psinfo.psargs",
    "proc.psinfo.time",
    "proc.psusage.tstamp",
    "proc.psusage.starttime",
    "proc.psinfo.clname",
    "proc.psinfo.time",
    "proc.psusage.rss",
    "proc.pscred.suid",
    "proc.psinfo.ttydev",
    "proc.memory.virtual.txt",
    "proc.memory.virtual.dat",
    "proc.memory.virtual.bss",
    "proc.memory.virtual.stack",
    "proc.memory.virtual.shm",
    "proc.memory.physical.txt",
    "proc.memory.physical.dat",
    "proc.memory.physical.bss",
    "proc.memory.physical.stack",
    "proc.memory.physical.shm"
};

#define NMETRICS (sizeof(metrics) / sizeof(metrics[0]))
static pmID pmids[NMETRICS];

static int
int_compare(int *a, int *b)
{
    return *a - *b;
}

int
main(int argc, char **argv)
{
    int		c;
    int		sts;
    int		errflag = 0;
    char	*namespace = PM_NS_DEFAULT;
    pmResult	*result;
    pmDesc      desc;
    int		all_n;
    int		*all_inst;
    char	**all_names;
    int		(*int_cmp)() = int_compare;
#ifdef PCP_DEBUG
    static char	*debug = "[-D N]";
#else
    static char	*debug = "";
#endif
    static char	*usage = " [-n namespace]";

    __pmSetProgname(argv[0]);

    while ((c = getopt(argc, argv, "D:n:")) != EOF) {
	switch (c) {
#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 'n':	/* alternative name space file */
	    namespace = optarg;
	    break;

	case '?':
	default:
	    errflag++;
	    break;
	}
    }

    if (errflag) {
	fprintf(stderr, "Usage: %s %s%s\n", pmProgname, debug, usage);
	exit(1);
    }


    /*
     * Startup:
     * a) load the name space
     * b) lookup the metrics in the name space
     * c) establish a context
     * d) get the instance domain identifier
     * [Note: these steps only have to be done once on startup]
     */
    if ((sts = pmLoadNameSpace(namespace)) < 0) {
	printf("%s: Cannot load namespace from \"%s\": %s\n", pmProgname, namespace, pmErrStr(sts));
	exit(1);
    }
    if ((sts = pmLookupName(NMETRICS, metrics, pmids)) < 0) {
	printf("%s: %s\n", pmProgname, pmErrStr(sts));
	exit(1);
    }
    putenv("PMDA_LOCAL_PROC=");
    if ((sts = pmNewContext(PM_CONTEXT_LOCAL, NULL)) < 0) {
	printf("%s: Cannot make local connection: %s\n", pmProgname, pmErrStr(sts));
	exit(1);
    }
    if ((sts = pmLookupDesc(pmids[1], &desc)) < 0) {
	fprintf(stderr, "pmLookupDesc: %s\n", pmErrStr(sts));
	exit(1);
    }



    /*
     * Enumerate the instance domain
     * [This has to be done before every fetch]
     */
    if (desc.indom != PM_INDOM_NULL) {
	all_n = pmGetInDom(desc.indom, &all_inst, &all_names);
	if (all_n < 0) {
	    printf("%s: pmGetInDom: %s\n", pmProgname, pmErrStr(all_n));
	    exit(1);
	}
    }
    else {
	printf("%s: botch: metric %s (%s) should have an instance domain\n", pmProgname, metrics[1], pmIDStr(pmids[1]));
	exit(1);
    }

    /*
     * sort the instance identifiers
     * [This has to be done before every fetch]
     */
    qsort(all_inst, all_n, sizeof(int), int_cmp);

    /*
     * establish an explicit instance profile
     * [This has to be done before every fetch]
     */
    pmDelProfile(desc.indom, 0, (int *)0); /* exclude everything first */
    pmAddProfile(desc.indom, all_n, all_inst); /* and then explicitly include our list of pids */

    /*
     * fetch the desired metrics
     */
    sts = pmFetch(NMETRICS, pmids, &result);
    if (sts < 0) {
	printf("%s: fetch all %d instances : %s\n", pmProgname, all_n, pmErrStr(sts));
	exit(1);
    }



    printf("\n\n-------- Result Dump --------------\n");
    __pmDumpResult(stdout, result);

    /* note: in a loop, you need to free(all_inst); free(all_names); on every iteration */

    free(all_inst);
    free(all_names);
    pmFreeResult(result);
    if ((sts = pmWhichContext()) < 0) {
	printf("%s: pmWhichContext: %s\n", pmProgname, pmErrStr(sts));
	exit(1);
    }
    pmDestroyContext(sts);
    exit(0);
}