summaryrefslogtreecommitdiff
path: root/clients/zstat/zstat.c
blob: ed0a925ff152cd397b4e16cf7c32ad288c283fbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* This file is part of the Project Athena Zephyr Notification System.
 * It contains the zstat program.
 *
 *      Created by:     David C. Jedlinsky
 *
 *      $Source$
 *      $Author$
 *
 *      Copyright (c) 1987 by the Massachusetts Institute of Technology.
 *      For copying and distribution information, see the file
 *      "mit-copyright.h". 
 */

#include <zephyr/zephyr.h>
#include "../server/zserver.h"
#include <sys/param.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>

#ifndef lint
#ifndef SABER
static char rcsid_zstat_c[] = "$Header$";
#endif SABER
#endif lint
		     
char *head[20] = { "Current server =",
		     "Items in queue:",
		     "Client packets received:",
		     "Server packets received:",
		     "Server changes:",
		     "Version:",
		     "Looking for a new server:",
		     "Time running:",
		     "Size:",
		     "Machine type:"
};
char *srv_head[20] = { 
	"Current server version =",
	"Packets handled:",
	"Uptime:",
	"Server states:",
};

int serveronly = 0,hmonly = 0;
int outoftime = 0;
u_short hm_port,srv_port;

main(argc, argv)
	int argc;
	char *argv[];
{
	ZNotice_t notice;
	Code_t ret;
	caddr_t packet, mp;
	char hostname[MAXHOSTNAMELEN];
	char *host = NULL;
	char *srv_name;
	int optchar;
	struct servent *sp;
	int ml, i, nitems;
	extern char *optarg;
	extern int optind;

	if ((ret = ZInitialize()) != ZERR_NONE) {
		com_err("zstat", ret, "initializing");
		exit(-1);
	}

	if ((ret = ZOpenPort(0)) != ZERR_NONE) {
		com_err("zstat", ret, "opening port");
		exit(-1);
	}

	while ((optchar = getopt(argc, argv, "sh")) != EOF) {
		switch(optchar) {
		case 's':
			serveronly++;
			break;
		case 'h':
			hmonly++;
			break;
		case '?':
		default:
			usage(argv[0]);
			exit(1);
		}
	}

	if (serveronly && hmonly) {
		fprintf(stderr,"Only one of -s and -h may be specified\n");
		exit(1);
	}

	if (!(sp = getservbyname("zephyr-hm","udp"))) {
		fprintf(stderr,"zephyr-hm/udp: unknown service\n");
		exit(-1);
	}

	hm_port = sp->s_port;

	if (!(sp = getservbyname("zephyr-clt","udp"))) {
		fprintf(stderr,"zephyr-clt/udp: unknown service\n");
		exit(-1);
	}

	srv_port = sp->s_port;

	if (optind == argc) {
		if (gethostname(hostname, MAXHOSTNAMELEN) < 0) {
			com_err("zstat",errno,"while finding hostname");
			exit(-1);
		}
		do_stat(hostname);
		exit(0);
	}

	for (;optind<argc;optind++)
		do_stat(argv[optind]);

	exit(0);
}

do_stat(host)
	char *host;
{
	char srv_host[MAXHOSTNAMELEN];
	
	if (serveronly) {
		srv_stat(host);
		return;
	}

	if (hm_stat(host,srv_host))
		return;

	if (!hmonly)
		srv_stat(srv_host);
}

hm_stat(host,server)
	char *host,*server;
{
	char *line[20],*mp;
	int sock,i,nf,ret;
	struct hostent *hp;
	struct sockaddr_in sin;
	long runtime;
	struct tm *tim;
	ZNotice_t notice;
	ZPacket_t packet;
	extern int timeout();
	
	bzero(&sin,sizeof(struct sockaddr_in));

	sin.sin_port = hm_port;

	if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
		perror("socket:");
		exit(-1);
	}
	
	sin.sin_family = AF_INET;

	if ((hp = gethostbyname(host)) == NULL) {
		fprintf(stderr,"Unknown host: %s\n",host);
		exit(-1);
	}
	bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);

	printf("Hostmanager stats: %s\n",hp->h_name);
	
	notice.z_kind = STAT;
	notice.z_port = 0;
	notice.z_class = HM_STAT_CLASS;
	notice.z_class_inst = HM_STAT_CLIENT;
	notice.z_opcode = HM_GIMMESTATS;
	notice.z_sender = "";
	notice.z_recipient = "";
	notice.z_default_format = "";
	notice.z_message_len = 0;
	
	if ((ret = ZSetDestAddr(&sin)) != ZERR_NONE) {
		com_err("zstat", ret, "setting destination");
		exit(-1);
	}
	if ((ret = ZSendNotice(&notice, ZNOAUTH)) != ZERR_NONE) {
		com_err("zstat", ret, "sending notice");
		exit(-1);
	}

	signal(SIGALRM,timeout);
	outoftime = 0;
	alarm(10);
	if (((ret = ZReceiveNotice(packet, sizeof packet, &notice,
				   NULL)) != ZERR_NONE) &&
	    ret != EINTR) {
		com_err("zstat", ret, "receiving notice");
		return (1);
	}
	alarm(0);
	if (outoftime) {
		fprintf(stderr,"No response after 10 seconds.\n");
		return (1);
	}
	
	mp = notice.z_message;
	for (nf=0;mp<notice.z_message+notice.z_message_len;nf++) {
		line[nf] = mp;
		mp += strlen(mp)+1;
	}

	strcpy(server,line[0]);

	for (i=0;i<nf;i++) {
		if (!strncmp("Time",head[i],4)) {
			runtime = atol(line[i]);
			tim = gmtime(&runtime);
			printf("%s %d days, %02d:%02d:%02d\n", head[i],
				tim->tm_yday,
				tim->tm_hour,
				tim->tm_min,
				tim->tm_sec);
		}
		else
			printf("%s %s\n",head[i],line[i]);
	}

	printf("\n");
	
	close(sock);
}

srv_stat(host)
	char *host;
{
	char *line[20],*mp;
	int sock,i,nf,ret;
	struct hostent *hp;
	struct sockaddr_in sin;
	ZNotice_t notice;
	ZPacket_t packet;
	long runtime;
	struct tm *tim;
	extern int timeout();
	
	bzero(&sin,sizeof(struct sockaddr_in));

	sin.sin_port = srv_port;

	if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
		perror("socket:");
		exit(-1);
	}
	
	sin.sin_family = AF_INET;

	if ((hp = gethostbyname(host)) == NULL) {
		fprintf(stderr,"Unknown host: %s\n",host);
		exit(-1);
	}
	bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);

	printf("Server stats: %s\n",hp->h_name);
	
	notice.z_kind = UNACKED;
	notice.z_port = 0;
	notice.z_class = ZEPHYR_ADMIN_CLASS;
	notice.z_class_inst = "";
	notice.z_opcode = ADMIN_STATUS;
	notice.z_sender = "";
	notice.z_recipient = "";
	notice.z_default_format = "";
	notice.z_message_len = 0;
	
	if ((ret = ZSetDestAddr(&sin)) != ZERR_NONE) {
		com_err("zstat", ret, "setting destination");
		exit(-1);
	}
	if ((ret = ZSendNotice(&notice, ZNOAUTH)) != ZERR_NONE) {
		com_err("zstat", ret, "sending notice");
		exit(-1);
	}

	signal(SIGALRM,timeout);
	outoftime = 0;
	alarm(10);
	if (((ret = ZReceiveNotice(packet, sizeof packet, &notice,
				   NULL)) != ZERR_NONE) &&
	    ret != EINTR) {
		com_err("zstat", ret, "receiving notice");
		return (1);
	}
	alarm(0);
	if (outoftime) {
		fprintf(stderr,"No response after 10 seconds.\n");
		return (1);
	} 
	
	mp = notice.z_message;
	for (nf=0;mp<notice.z_message+notice.z_message_len;nf++) {
		line[nf] = mp;
		mp += strlen(mp)+1;
	}

	for (i=0; i<nf; i++) {
		if (i < 2)
			printf("%s %s\n",srv_head[i],line[i]);
		else if (i == 2) { /* uptime field */
			runtime = atol(line[i]);
			tim = gmtime(&runtime);
			printf("%s %d days, %02d:%02d:%02d\n",
			       srv_head[i],
			       tim->tm_yday,
			       tim->tm_hour,
			       tim->tm_min,
			       tim->tm_sec);
		} else if (i == 3) {
			printf("%s\n",srv_head[i]);
			printf("%s\n",line[i]);
		} else printf("%s\n",line[i]);
	}
	printf("\n");
	
	close(sock);
}

usage(s)
	char *s;
{
	fprintf(stderr,"usage: %s [-s] [-h] [host ...]\n",s);
	exit(1);
}

timeout()
{
	outoftime = 1;
}