]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/clients/zstat/zstat.c
Initial revision
[1ts-debian.git] / zephyr / clients / zstat / zstat.c
1 /* This file is part of the Project Athena Zephyr Notification System.
2  * It contains the zstat program.
3  *
4  *      Created by:     David C. Jedlinsky
5  *
6  *      $Id: zstat.c,v 1.21 1999/08/13 00:18:50 danw Exp $
7  *
8  *      Copyright (c) 1987,1988 by the Massachusetts Institute of Technology.
9  *      For copying and distribution information, see the file
10  *      "mit-copyright.h". 
11  */
12
13 /* There should be library interfaces for the operations in zstat; for now,
14  * however, zstat is more or less internal to the Zephyr system. */
15 #include <internal.h>
16
17 #include <sys/socket.h>
18 #include "zserver.h"
19
20 #if !defined(lint) && !defined(SABER)
21 static const char rcsid_zstat_c[] = "$Id: zstat.c,v 1.21 1999/08/13 00:18:50 danw Exp $";
22 #endif
23
24 const char *hm_head[] = {
25     "Current server =",
26     "Items in queue:",
27     "Client packets received:",
28     "Server packets received:",
29     "Server changes:",
30     "Version:",
31     "Looking for a new server:",
32     "Time running:",
33     "Size:",
34     "Machine type:"
35 };
36 #define HM_SIZE (sizeof(hm_head) / sizeof (char *))
37 const char *srv_head[] = { 
38     "Current server version =",
39     "Packets handled:",
40     "Uptime:",
41     "Server states:",
42 };
43 #define SRV_SIZE        (sizeof(srv_head) / sizeof (char *))
44
45 int outoftime = 0;
46
47 int serveronly = 0,hmonly = 0;
48 u_short srv_port;
49
50 void usage(), do_stat();
51
52 RETSIGTYPE timeout()
53 {
54         outoftime = 1;
55 }
56
57 main(argc, argv)
58         int argc;
59         char *argv[];
60 {
61         Code_t ret;
62         char hostname[MAXHOSTNAMELEN];
63         int optchar;
64         struct servent *sp;
65         extern char *optarg;
66         extern int optind;
67
68         if ((ret = ZInitialize()) != ZERR_NONE) {
69                 com_err("zstat", ret, "initializing");
70                 exit(-1);
71         }
72
73         if ((ret = ZOpenPort((u_short *)0)) != ZERR_NONE) {
74                 com_err("zstat", ret, "opening port");
75                 exit(-1);
76         }
77
78         while ((optchar = getopt(argc, argv, "sh")) != EOF) {
79                 switch(optchar) {
80                 case 's':
81                         serveronly++;
82                         break;
83                 case 'h':
84                         hmonly++;
85                         break;
86                 case '?':
87                 default:
88                         usage(argv[0]);
89                         exit(1);
90                 }
91         }
92
93         if (serveronly && hmonly) {
94                 fprintf(stderr,"Only one of -s and -h may be specified\n");
95                 exit(1);
96         }
97
98         sp = getservbyname(SERVER_SVCNAME,"udp");
99         srv_port = (sp) ? sp->s_port : SERVER_SVC_FALLBACK;
100
101         if (optind == argc) {
102                 if (gethostname(hostname, MAXHOSTNAMELEN) < 0) {
103                         com_err("zstat",errno,"while finding hostname");
104                         exit(-1);
105                 }
106                 do_stat(hostname);
107                 exit(0);
108         }
109
110         for (;optind<argc;optind++)
111                 do_stat(argv[optind]);
112
113         exit(0);
114 }
115
116 void
117 do_stat(host)
118         char *host;
119 {
120         char srv_host[MAXHOSTNAMELEN];
121         
122         if (serveronly) {
123                 (void) srv_stat(host);
124                 return;
125         }
126
127         if (hm_stat(host,srv_host))
128                 return;
129
130         if (!hmonly)
131                 (void) srv_stat(srv_host);
132 }
133
134 int
135 hm_stat(host,server)
136         char *host,*server;
137 {
138         struct in_addr inaddr;
139         Code_t code;
140
141         char *line[20],*mp;
142         int i,nf;
143         struct hostent *hp;
144         time_t runtime;
145         struct tm *tim;
146         ZNotice_t notice;
147 #ifdef _POSIX_VERSION
148         struct sigaction sa;
149 #endif
150         
151         if ((inaddr.s_addr = inet_addr(host)) == (unsigned)(-1)) {
152             if ((hp = gethostbyname(host)) == NULL) {
153                 fprintf(stderr,"Unknown host: %s\n",host);
154                 exit(-1);
155             }
156             (void) memcpy((char *) &inaddr, hp->h_addr, hp->h_length);
157
158             printf("Hostmanager stats: %s\n", hp->h_name);
159         } else {
160             printf("Hostmanager stats: %s\n", host);
161         }
162         
163         if ((code = ZhmStat(&inaddr, &notice)) != ZERR_NONE) {
164             com_err("zstat", code, "getting hostmanager status");
165             exit(-1);
166         }
167         
168         mp = notice.z_message;
169         for (nf=0;mp<notice.z_message+notice.z_message_len;nf++) {
170                 line[nf] = mp;
171                 mp += strlen(mp)+1;
172         }
173
174         (void) strcpy(server,line[0]);
175
176         printf("HostManager protocol version = %s\n",notice.z_version);
177
178         for (i=0; (i < nf) && (i < HM_SIZE); i++) {
179                 if (!strncmp("Time",hm_head[i],4)) {
180                         runtime = atol(line[i]);
181                         tim = gmtime(&runtime);
182                         printf("%s %d days, %02d:%02d:%02d\n", hm_head[i],
183                                 tim->tm_yday,
184                                 tim->tm_hour,
185                                 tim->tm_min,
186                                 tim->tm_sec);
187                 }
188                 else
189                         printf("%s %s\n",hm_head[i],line[i]);
190         }
191
192         printf("\n");
193         
194         ZFreeNotice(&notice);
195         return(0);
196 }
197
198 int
199 srv_stat(host)
200         char *host;
201 {
202         char *line[20],*mp;
203         int sock,i,nf,ret;
204         struct hostent *hp;
205         struct sockaddr_in sin;
206         ZNotice_t notice;
207         time_t runtime;
208         struct tm *tim;
209 #ifdef _POSIX_VERSION
210         struct sigaction sa;
211 #endif
212                 
213         (void) memset((char *) &sin, 0, sizeof(struct sockaddr_in));
214
215         sin.sin_port = srv_port;
216
217         if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
218                 perror("socket:");
219                 exit(-1);
220         }
221         
222         sin.sin_family = AF_INET;
223
224         if ((sin.sin_addr.s_addr = inet_addr(host)) == (unsigned)(-1)) {
225             if ((hp = gethostbyname(host)) == NULL) {
226                 fprintf(stderr,"Unknown host: %s\n",host);
227                 exit(-1);
228             }
229             (void) memcpy((char *) &sin.sin_addr, hp->h_addr, hp->h_length);
230
231             printf("Server stats: %s\n", hp->h_name);
232         } else {
233             printf("Server stats: %s\n", host);
234         }
235         
236         (void) memset((char *)&notice, 0, sizeof(notice));
237         notice.z_kind = UNSAFE;
238         notice.z_port = 0;
239         notice.z_class = ZEPHYR_ADMIN_CLASS;
240         notice.z_class_inst = "";
241         notice.z_opcode = ADMIN_STATUS;
242         notice.z_sender = "";
243         notice.z_recipient = "";
244         notice.z_default_format = "";
245         notice.z_message_len = 0;
246         
247         if ((ret = ZSetDestAddr(&sin)) != ZERR_NONE) {
248                 com_err("zstat", ret, "setting destination");
249                 exit(-1);
250         }
251         if ((ret = ZSendNotice(&notice, ZNOAUTH)) != ZERR_NONE) {
252                 com_err("zstat", ret, "sending notice");
253                 exit(-1);
254         }
255
256 #ifdef _POSIX_VERSION
257         sigemptyset(&sa.sa_mask);
258         sa.sa_flags = 0;
259         sa.sa_handler = timeout;
260         (void) sigaction(SIGALRM, &sa, (struct sigaction *)0);
261 #else
262         (void) signal(SIGALRM,timeout);
263 #endif
264         outoftime = 0;
265         (void) alarm(10);
266         if (((ret = ZReceiveNotice(&notice, (struct sockaddr_in *) 0))
267             != ZERR_NONE) &&
268             ret != EINTR) {
269                 com_err("zstat", ret, "receiving notice");
270                 return (1);
271         }
272         (void) alarm(0);
273         if (outoftime) {
274                 fprintf(stderr,"No response after 10 seconds.\n");
275                 return (1);
276         } 
277         
278         mp = notice.z_message;
279         for (nf=0;mp<notice.z_message+notice.z_message_len;nf++) {
280                 line[nf] = mp;
281                 mp += strlen(mp)+1;
282         }
283
284         printf("Server protocol version = %s\n",notice.z_version);
285         
286         for (i=0; i < nf; i++) {
287                 if (i < 2)
288                         printf("%s %s\n",srv_head[i],line[i]);
289                 else if (i == 2) { /* uptime field */
290                         runtime = atol(line[i]);
291                         tim = gmtime(&runtime);
292                         printf("%s %d days, %02d:%02d:%02d\n",
293                                srv_head[i],
294                                tim->tm_yday,
295                                tim->tm_hour,
296                                tim->tm_min,
297                                tim->tm_sec);
298                 } else if (i == 3) {
299                         printf("%s\n",srv_head[i]);
300                         printf("%s\n",line[i]);
301                 } else printf("%s\n",line[i]);
302         }
303         printf("\n");
304         
305         (void) close(sock);
306         ZFreeNotice(&notice);
307         return(0);
308 }
309
310 void
311 usage(s)
312         char *s;
313 {
314         fprintf(stderr,"usage: %s [-s] [-h] [host ...]\n",s);
315         exit(1);
316 }