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