1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It contains the zstat program.
4 * Created by: David C. Jedlinsky
6 * $Id: zstat.c,v 1.21 1999/08/13 00:18:50 danw Exp $
8 * Copyright (c) 1987,1988 by the Massachusetts Institute of Technology.
9 * For copying and distribution information, see the file
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. */
17 #include <sys/socket.h>
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 $";
24 const char *hm_head[] = {
27 "Client packets received:",
28 "Server packets received:",
31 "Looking for a new server:",
36 #define HM_SIZE (sizeof(hm_head) / sizeof (char *))
37 const char *srv_head[] = {
38 "Current server version =",
43 #define SRV_SIZE (sizeof(srv_head) / sizeof (char *))
47 int serveronly = 0,hmonly = 0;
50 void usage(), do_stat();
62 char hostname[MAXHOSTNAMELEN];
68 if ((ret = ZInitialize()) != ZERR_NONE) {
69 com_err("zstat", ret, "initializing");
73 if ((ret = ZOpenPort((u_short *)0)) != ZERR_NONE) {
74 com_err("zstat", ret, "opening port");
78 while ((optchar = getopt(argc, argv, "sh")) != EOF) {
93 if (serveronly && hmonly) {
94 fprintf(stderr,"Only one of -s and -h may be specified\n");
98 sp = getservbyname(SERVER_SVCNAME,"udp");
99 srv_port = (sp) ? sp->s_port : SERVER_SVC_FALLBACK;
101 if (optind == argc) {
102 if (gethostname(hostname, MAXHOSTNAMELEN) < 0) {
103 com_err("zstat",errno,"while finding hostname");
110 for (;optind<argc;optind++)
111 do_stat(argv[optind]);
120 char srv_host[MAXHOSTNAMELEN];
123 (void) srv_stat(host);
127 if (hm_stat(host,srv_host))
131 (void) srv_stat(srv_host);
138 struct in_addr inaddr;
147 #ifdef _POSIX_VERSION
151 if ((inaddr.s_addr = inet_addr(host)) == (unsigned)(-1)) {
152 if ((hp = gethostbyname(host)) == NULL) {
153 fprintf(stderr,"Unknown host: %s\n",host);
156 (void) memcpy((char *) &inaddr, hp->h_addr, hp->h_length);
158 printf("Hostmanager stats: %s\n", hp->h_name);
160 printf("Hostmanager stats: %s\n", host);
163 if ((code = ZhmStat(&inaddr, ¬ice)) != ZERR_NONE) {
164 com_err("zstat", code, "getting hostmanager status");
168 mp = notice.z_message;
169 for (nf=0;mp<notice.z_message+notice.z_message_len;nf++) {
174 (void) strcpy(server,line[0]);
176 printf("HostManager protocol version = %s\n",notice.z_version);
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],
189 printf("%s %s\n",hm_head[i],line[i]);
194 ZFreeNotice(¬ice);
205 struct sockaddr_in sin;
209 #ifdef _POSIX_VERSION
213 (void) memset((char *) &sin, 0, sizeof(struct sockaddr_in));
215 sin.sin_port = srv_port;
217 if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
222 sin.sin_family = AF_INET;
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);
229 (void) memcpy((char *) &sin.sin_addr, hp->h_addr, hp->h_length);
231 printf("Server stats: %s\n", hp->h_name);
233 printf("Server stats: %s\n", host);
236 (void) memset((char *)¬ice, 0, sizeof(notice));
237 notice.z_kind = UNSAFE;
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;
247 if ((ret = ZSetDestAddr(&sin)) != ZERR_NONE) {
248 com_err("zstat", ret, "setting destination");
251 if ((ret = ZSendNotice(¬ice, ZNOAUTH)) != ZERR_NONE) {
252 com_err("zstat", ret, "sending notice");
256 #ifdef _POSIX_VERSION
257 sigemptyset(&sa.sa_mask);
259 sa.sa_handler = timeout;
260 (void) sigaction(SIGALRM, &sa, (struct sigaction *)0);
262 (void) signal(SIGALRM,timeout);
266 if (((ret = ZReceiveNotice(¬ice, (struct sockaddr_in *) 0))
269 com_err("zstat", ret, "receiving notice");
274 fprintf(stderr,"No response after 10 seconds.\n");
278 mp = notice.z_message;
279 for (nf=0;mp<notice.z_message+notice.z_message_len;nf++) {
284 printf("Server protocol version = %s\n",notice.z_version);
286 for (i=0; i < nf; i++) {
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",
299 printf("%s\n",srv_head[i]);
300 printf("%s\n",line[i]);
301 } else printf("%s\n",line[i]);
306 ZFreeNotice(¬ice);
314 fprintf(stderr,"usage: %s [-s] [-h] [host ...]\n",s);