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