include-afsconfig-before-param-h-20010712
[openafs.git] / src / libadmin / samples / rxstat_get_peer.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * This file contains sample code for the rxstats interface 
12  */
13
14 #include <afsconfig.h>
15 #include <afs/param.h>
16
17 RCSID("$Header$");
18
19 #ifdef AFS_NT40_ENV
20 #include <winsock2.h>
21 #include <pthread.h>
22 #endif
23 #include <afs/afs_Admin.h>
24 #include <afs/afs_AdminErrors.h>
25 #include <afs/afs_clientAdmin.h>
26 #include <afs/afs_utilAdmin.h>
27 #include <rx/rxstat.h>
28 #include <afs/afsint.h>
29 #define FSINT_COMMON_XG
30 #include <afs/afscbint.h>
31 #include <afs/kauth.h>
32 #include <afs/kautils.h>
33 #include <afs/ptint.h>
34 #include <afs/ptserver.h>
35 #include <afs/vldbint.h>
36 #include <afs/bosint.h>
37 #include <afs/volint.h>
38 #include <afs/volser.h>
39 #include <afs/bosint.h>
40 #include <ubik.h>
41 #include <ubik_int.h>
42 #ifndef AFS_NT40_ENV
43 #include <arpa/inet.h>  /* for inet_ntoa() */
44 #endif
45
46 extern int RXSTATS_RetrievePeerRPCStats();
47
48 void Usage()
49 {
50     fprintf(stderr,
51             "Usage: rxstat_get_peer <cell> <host> <port>\n");
52     exit(1);
53 }
54
55 void ParseArgs(
56     int argc,
57     char *argv[],
58     char **srvrName,
59     long *srvrPort)
60 {
61     char **argp = argv;
62
63     if (!*(++argp))
64         Usage();
65     *srvrName = *(argp++);
66     if (!*(argp))
67         Usage();
68     *srvrPort = strtol(*(argp++), NULL, 0);
69     if (*srvrPort <= 0 || *srvrPort >= 65536)
70         Usage();
71     if (*(argp))
72         Usage();
73 }
74
75 void GetPrintStrings(
76     afs_RPCStats_p statp,
77     char *ifName,
78     char *ifRole,
79     char *hostName,
80     const char ***funcList,
81     int *funcListLen)
82 {
83     struct in_addr ina;
84
85     ina.s_addr = htonl(statp->s.stats_v1.remote_peer);
86     strcpy(hostName, inet_ntoa(ina));
87
88     if (statp->s.stats_v1.remote_is_server) {
89         strcpy(ifRole, "client");
90     } else {
91         strcpy(ifRole, "server");
92     }
93
94     switch(statp->s.stats_v1.interfaceId) {
95       case RXSTATS_STATINDEX:
96         strcpy(ifName, "rxstats interface");
97         *funcList = RXSTATS_function_names;
98         *funcListLen = RXSTATS_NO_OF_STAT_FUNCS;
99         break;
100       case RXAFS_STATINDEX:
101         strcpy(ifName, "fileserver interface");
102         *funcList = RXAFS_function_names;
103         *funcListLen = RXAFS_NO_OF_STAT_FUNCS;
104         break;
105       case RXAFSCB_STATINDEX:
106         strcpy(ifName, "callback interface");
107         *funcList = RXAFSCB_function_names;
108         *funcListLen = RXAFSCB_NO_OF_STAT_FUNCS;
109         break;
110       case PR_STATINDEX:
111         strcpy(ifName, "ptserver interface");
112         *funcList = PR_function_names;
113         *funcListLen = PR_NO_OF_STAT_FUNCS;
114         break;
115       case DISK_STATINDEX:
116         strcpy(ifName, "ubik disk interface");
117         *funcList = DISK_function_names;
118         *funcListLen = DISK_NO_OF_STAT_FUNCS;
119         break;
120       case VOTE_STATINDEX:
121         strcpy(ifName, "ubik vote interface");
122         *funcList = VOTE_function_names;
123         *funcListLen = VOTE_NO_OF_STAT_FUNCS;
124         break;
125       case VL_STATINDEX:
126         strcpy(ifName, "volserver interface");
127         *funcList = VL_function_names;
128         *funcListLen = VL_NO_OF_STAT_FUNCS;
129         break;
130       case AFSVolSTATINDEX:
131         strcpy(ifName, "volserver interface");
132         *funcList = AFSVolfunction_names;
133         *funcListLen = AFSVolNO_OF_STAT_FUNCS;
134         break;
135       case BOZO_STATINDEX:
136         strcpy(ifName, "bosserver interface");
137         *funcList = BOZO_function_names;
138         *funcListLen = BOZO_NO_OF_STAT_FUNCS;
139         break;
140       case KAA_STATINDEX:
141         strcpy(ifName, "KAA interface");
142         *funcList = KAA_function_names;
143         *funcListLen = KAA_NO_OF_STAT_FUNCS;
144         break;
145       case KAM_STATINDEX:
146         strcpy(ifName, "KAM interface");
147         *funcList = KAM_function_names;
148         *funcListLen = KAM_NO_OF_STAT_FUNCS;
149         break;
150       case KAT_STATINDEX:
151         strcpy(ifName, "KAT interface");
152         *funcList = KAT_function_names;
153         *funcListLen = KAT_NO_OF_STAT_FUNCS;
154         break;
155       default:
156         sprintf(ifName, "interface 0x%x", statp->s.stats_v1.interfaceId);
157         *funcList = NULL;
158         *funcListLen = 0;
159         break;
160     }
161 }
162
163 int main(int argc, char *argv[])
164 {
165     int rc;
166     afs_status_t st = 0;
167     struct rx_connection *conn;
168     char *srvrName;
169     long srvrPort;
170     void *cellHandle;
171     void *iterator;
172     afs_RPCStats_t stats;
173     char ifName[128];
174     char role[8];
175     char hostName[128];
176     const char **funcList;
177     int funcListLen;
178     int index;
179
180     ParseArgs(argc, argv, &srvrName, &srvrPort);
181
182     rc = afsclient_Init(&st);
183     if (!rc) {
184         fprintf(stderr, "afsclient_Init, status %d\n", st);
185         exit(1);
186     }
187
188     rc = afsclient_NullCellOpen(&cellHandle, &st);
189     if (!rc) {
190         fprintf(stderr, "afsclient_NullCellOpen, status %d\n", st);
191         exit(1);
192     }
193
194     rc = afsclient_RPCStatOpenPort(cellHandle, srvrName, srvrPort, &conn, &st);
195     if (!rc) {
196         fprintf(stderr, "afsclient_RPCStatOpenPort, status %d\n", st);
197         exit(1);
198     }
199
200     rc = util_RPCStatsGetBegin(conn, RXSTATS_RetrievePeerRPCStats,
201                                &iterator, &st);
202     if (!rc) {
203         fprintf(stderr, "util_RPCStatsGetBegin, status %d\n", st);
204         exit(1);
205     }
206
207     while (util_RPCStatsGetNext(iterator, &stats, &st)) {
208         index = stats.s.stats_v1.func_index;
209
210         if (index == 0) {
211             GetPrintStrings(&stats, ifName, role, hostName,
212                             &funcList, &funcListLen);
213             printf("\nPeer stats for %s accessed as a %s\n"
214                    "    host %s, port %d\n\n",
215                    ifName, role, hostName, stats.s.stats_v1.remote_port);
216         }
217
218         if (index >= funcListLen) {
219             printf("    Function index %d\n", index);
220         } else {
221             printf("    %s\n", funcList[index]);
222         }
223
224         if (!hiszero(stats.s.stats_v1.invocations)) {
225             printf("\tinvoc (%u.%u) bytes_sent (%u.%u) bytes_rcvd (%u.%u)\n",
226                    hgethi(stats.s.stats_v1.invocations),
227                    hgetlo(stats.s.stats_v1.invocations),
228                    hgethi(stats.s.stats_v1.bytes_sent),
229                    hgetlo(stats.s.stats_v1.bytes_sent),
230                    hgethi(stats.s.stats_v1.bytes_rcvd),
231                    hgetlo(stats.s.stats_v1.bytes_rcvd));
232             printf("\tqsum %d.%06d qsqr %d.%06d"
233                    " qmin %d.%06d qmax %d.%06d\n",
234                    stats.s.stats_v1.queue_time_sum.sec,
235                    stats.s.stats_v1.queue_time_sum.usec,
236                    stats.s.stats_v1.queue_time_sum_sqr.sec,
237                    stats.s.stats_v1.queue_time_sum_sqr.usec,
238                    stats.s.stats_v1.queue_time_min.sec,
239                    stats.s.stats_v1.queue_time_min.usec,
240                    stats.s.stats_v1.queue_time_max.sec,
241                    stats.s.stats_v1.queue_time_max.usec);
242             printf("\txsum %d.%06d xsqr %d.%06d"
243                    " xmin %d.%06d xmax %d.%06d\n",
244                    stats.s.stats_v1.execution_time_sum.sec,
245                    stats.s.stats_v1.execution_time_sum.usec,
246                    stats.s.stats_v1.execution_time_sum_sqr.sec,
247                    stats.s.stats_v1.execution_time_sum_sqr.usec,
248                    stats.s.stats_v1.execution_time_min.sec,
249                    stats.s.stats_v1.execution_time_min.usec,
250                    stats.s.stats_v1.execution_time_max.sec,
251                    stats.s.stats_v1.execution_time_max.usec);
252         } else {
253             printf("\tNever invoked\n");
254         }
255     }
256     if (st != ADMITERATORDONE) {
257         fprintf(stderr, "util_RPCStatsGetNext, status %d\n", st);
258         exit(1);
259     }
260     printf("\n");
261
262     rc = util_RPCStatsGetDone(iterator, &st);
263     if (!rc) {
264         fprintf(stderr, "util_RPCStatsGetDone, status %d\n", st);
265         exit(1);
266     }
267
268     rc = afsclient_RPCStatClose(conn, &st);
269     if (!rc) {
270         fprintf(stderr, "afsclient_RPCStatClose, status %d\n", st);
271         exit(1);
272     }
273
274     rc = afsclient_CellClose(cellHandle, &st);
275     if (!rc) {
276         fprintf(stderr, "afsclient_CellClose, status %d\n", st);
277         exit(1);
278     }
279
280     exit(0);
281 }