1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It contains the zstat program.
4 * Created by: David C. Jedlinsky
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>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
22 #if !defined(lint) && !defined(SABER)
23 static const char rcsid_zstat_c[] = "$Id$";
26 const char *hm_head[] = {
29 "Client packets received:",
30 "Server packets received:",
33 "Looking for a new server:",
38 #define HM_SIZE (sizeof(hm_head) / sizeof (char *))
39 const char *srv_head[] = {
40 "Current server version =",
45 #define SRV_SIZE (sizeof(srv_head) / sizeof (char *))
49 int serveronly = 0,hmonly = 0;
55 int hm_stat(char *, char *);
68 char hostname[MAXHOSTNAMELEN];
74 if ((ret = ZInitialize()) != ZERR_NONE) {
75 com_err("zstat", ret, "initializing");
79 if ((ret = ZOpenPort((u_short *)0)) != ZERR_NONE) {
80 com_err("zstat", ret, "opening port");
84 while ((optchar = getopt(argc, argv, "sh")) != EOF) {
99 if (serveronly && hmonly) {
100 fprintf(stderr,"Only one of -s and -h may be specified\n");
104 sp = getservbyname(SERVER_SVCNAME,"udp");
105 srv_port = (sp) ? sp->s_port : SERVER_SVC_FALLBACK;
107 if (optind == argc) {
108 if (gethostname(hostname, MAXHOSTNAMELEN) < 0) {
109 com_err("zstat",errno,"while finding hostname");
116 for (;optind<argc;optind++)
117 do_stat(argv[optind]);
125 char srv_host[MAXHOSTNAMELEN];
128 (void) srv_stat(host);
132 if (hm_stat(host,srv_host))
136 (void) srv_stat(srv_host);
143 struct in_addr inaddr;
153 if ((inaddr.s_addr = inet_addr(host)) == (unsigned)(-1)) {
154 if ((hp = gethostbyname(host)) == NULL) {
155 fprintf(stderr,"Unknown host: %s\n",host);
158 (void) memcpy((char *) &inaddr, hp->h_addr, hp->h_length);
160 printf("Hostmanager stats: %s\n", hp->h_name);
162 printf("Hostmanager stats: %s\n", host);
165 if ((code = ZhmStat(&inaddr, ¬ice)) != ZERR_NONE) {
166 com_err("zstat", code, "getting hostmanager status");
170 mp = notice.z_message;
171 for (nf=0;mp<notice.z_message+notice.z_message_len;nf++) {
176 (void) strcpy(server,line[0]);
178 printf("HostManager protocol version = %s\n",notice.z_version);
180 for (i=0; (i < nf) && (i < HM_SIZE); i++) {
181 if (!strncmp("Time",hm_head[i],4)) {
182 runtime = atol(line[i]);
183 tim = gmtime(&runtime);
184 printf("%s %d days, %02d:%02d:%02d\n", hm_head[i],
191 printf("%s %s\n",hm_head[i],line[i]);
196 ZFreeNotice(¬ice);
206 struct sockaddr_in sin;
210 #ifdef _POSIX_VERSION
214 (void) memset((char *) &sin, 0, sizeof(struct sockaddr_in));
216 sin.sin_port = srv_port;
218 if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
223 sin.sin_family = AF_INET;
225 if ((sin.sin_addr.s_addr = inet_addr(host)) == (unsigned)(-1)) {
226 if ((hp = gethostbyname(host)) == NULL) {
227 fprintf(stderr,"Unknown host: %s\n",host);
230 (void) memcpy((char *) &sin.sin_addr, hp->h_addr, hp->h_length);
232 printf("Server stats: %s\n", hp->h_name);
234 printf("Server stats: %s\n", host);
237 (void) memset((char *)¬ice, 0, sizeof(notice));
238 notice.z_kind = UNSAFE;
240 notice.z_class = ZEPHYR_ADMIN_CLASS;
241 notice.z_class_inst = "";
242 notice.z_opcode = ADMIN_STATUS;
243 notice.z_sender = "";
244 notice.z_recipient = "";
245 notice.z_default_format = "";
246 notice.z_message_len = 0;
248 if ((ret = ZSetDestAddr(&sin)) != ZERR_NONE) {
249 com_err("zstat", ret, "setting destination");
252 if ((ret = ZSendNotice(¬ice, ZNOAUTH)) != ZERR_NONE) {
253 com_err("zstat", ret, "sending notice");
257 #ifdef _POSIX_VERSION
258 sigemptyset(&sa.sa_mask);
260 sa.sa_handler = timeout;
261 (void) sigaction(SIGALRM, &sa, (struct sigaction *)0);
263 (void) signal(SIGALRM,timeout);
267 if (((ret = ZReceiveNotice(¬ice, (struct sockaddr_in *) 0))
270 com_err("zstat", ret, "receiving notice");
275 fprintf(stderr,"No response after 10 seconds.\n");
279 mp = notice.z_message;
280 for (nf=0;mp<notice.z_message+notice.z_message_len;nf++) {
285 printf("Server protocol version = %s\n",notice.z_version);
287 for (i=0; i < nf; i++) {
289 printf("%s %s\n",srv_head[i],line[i]);
290 else if (i == 2) { /* uptime field */
291 runtime = atol(line[i]);
292 tim = gmtime(&runtime);
293 printf("%s %d days, %02d:%02d:%02d\n",
300 printf("%s\n",srv_head[i]);
301 printf("%s\n",line[i]);
302 } else printf("%s\n",line[i]);
307 ZFreeNotice(¬ice);
314 fprintf(stderr,"usage: %s [-s] [-h] [host ...]\n",s);