]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/clients/zstat/zstat.c
04951cce1b9cb1f4e5ed8d56749f435abb047b1d
[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$
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$";
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(char *);
51 void do_stat(char *);
52
53 RETSIGTYPE
54 timeout(int ignored)
55 {
56         outoftime = 1;
57 }
58
59 int
60 main(int argc,
61      char *argv[])
62 {
63         Code_t ret;
64         char hostname[MAXHOSTNAMELEN];
65         int optchar;
66         struct servent *sp;
67         extern char *optarg;
68         extern int optind;
69
70         if ((ret = ZInitialize()) != ZERR_NONE) {
71                 com_err("zstat", ret, "initializing");
72                 exit(-1);
73         }
74
75         if ((ret = ZOpenPort((u_short *)0)) != ZERR_NONE) {
76                 com_err("zstat", ret, "opening port");
77                 exit(-1);
78         }
79
80         while ((optchar = getopt(argc, argv, "sh")) != EOF) {
81                 switch(optchar) {
82                 case 's':
83                         serveronly++;
84                         break;
85                 case 'h':
86                         hmonly++;
87                         break;
88                 case '?':
89                 default:
90                         usage(argv[0]);
91                         exit(1);
92                 }
93         }
94
95         if (serveronly && hmonly) {
96                 fprintf(stderr,"Only one of -s and -h may be specified\n");
97                 exit(1);
98         }
99
100         sp = getservbyname(SERVER_SVCNAME,"udp");
101         srv_port = (sp) ? sp->s_port : SERVER_SVC_FALLBACK;
102
103         if (optind == argc) {
104                 if (gethostname(hostname, MAXHOSTNAMELEN) < 0) {
105                         com_err("zstat",errno,"while finding hostname");
106                         exit(-1);
107                 }
108                 do_stat(hostname);
109                 exit(0);
110         }
111
112         for (;optind<argc;optind++)
113                 do_stat(argv[optind]);
114
115         exit(0);
116 }
117
118 void
119 do_stat(char *host)
120 {
121         char srv_host[MAXHOSTNAMELEN];
122         
123         if (serveronly) {
124                 (void) srv_stat(host);
125                 return;
126         }
127
128         if (hm_stat(host,srv_host))
129                 return;
130
131         if (!hmonly)
132                 (void) srv_stat(srv_host);
133 }
134
135 int
136 hm_stat(char *host,
137         char *server)
138 {
139         struct in_addr inaddr;
140         Code_t code;
141
142         char *line[20],*mp;
143         int i,nf;
144         struct hostent *hp;
145         time_t runtime;
146         struct tm *tim;
147         ZNotice_t notice;
148 #ifdef _POSIX_VERSION
149         struct sigaction sa;
150 #endif
151         
152         if ((inaddr.s_addr = inet_addr(host)) == (unsigned)(-1)) {
153             if ((hp = gethostbyname(host)) == NULL) {
154                 fprintf(stderr,"Unknown host: %s\n",host);
155                 exit(-1);
156             }
157             (void) memcpy((char *) &inaddr, hp->h_addr, hp->h_length);
158
159             printf("Hostmanager stats: %s\n", hp->h_name);
160         } else {
161             printf("Hostmanager stats: %s\n", host);
162         }
163         
164         if ((code = ZhmStat(&inaddr, &notice)) != ZERR_NONE) {
165             com_err("zstat", code, "getting hostmanager status");
166             exit(-1);
167         }
168         
169         mp = notice.z_message;
170         for (nf=0;mp<notice.z_message+notice.z_message_len;nf++) {
171                 line[nf] = mp;
172                 mp += strlen(mp)+1;
173         }
174
175         (void) strcpy(server,line[0]);
176
177         printf("HostManager protocol version = %s\n",notice.z_version);
178
179         for (i=0; (i < nf) && (i < HM_SIZE); i++) {
180                 if (!strncmp("Time",hm_head[i],4)) {
181                         runtime = atol(line[i]);
182                         tim = gmtime(&runtime);
183                         printf("%s %d days, %02d:%02d:%02d\n", hm_head[i],
184                                 tim->tm_yday,
185                                 tim->tm_hour,
186                                 tim->tm_min,
187                                 tim->tm_sec);
188                 }
189                 else
190                         printf("%s %s\n",hm_head[i],line[i]);
191         }
192
193         printf("\n");
194         
195         ZFreeNotice(&notice);
196         return(0);
197 }
198
199 int
200 srv_stat(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(char *s)
312 {
313         fprintf(stderr,"usage: %s [-s] [-h] [host ...]\n",s);
314         exit(1);
315 }