source: trunk/debathena/debathena/larvnet/busytest.c @ 23126

Revision 23126, 2.7 KB checked in by ghudson, 16 years ago (diff)
Add larvnet client-side monitoring package.
RevLine 
[23126]1/* Copyright 2008 by the Massachusetts Institute of Technology.
2 *
3 * Permission to use, copy, modify, and distribute this
4 * software and its documentation for any purpose and without
5 * fee is hereby granted, provided that the above copyright
6 * notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting
8 * documentation, and that the name of M.I.T. not be used in
9 * advertising or publicity pertaining to distribution of the
10 * software without specific, written prior permission.
11 * M.I.T. makes no representations about the suitability of
12 * this software for any purpose.  It is provided "as is"
13 * without express or implied warranty.
14 */
15
16/* busytest.c - Simple diagnostic program to query busy information
17 * from an Athena machine
18 */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <errno.h>
24#include <sys/types.h>
25#include <sys/socket.h>
26#include <netinet/in.h>
27#include <arpa/inet.h>
28#include <netdb.h>
29
30#define BUSYPOLL 49154
31
32int main(int argc, char **argv)
33{
34  struct hostent *host;
35  struct sockaddr_in sin;
36  int s, count, i;
37  char dummy = 0, buf[1024], *name, *arch;
38  socklen_t len;
39
40  /* Verify and resolve the hostname argument. */
41  if (argc != 2)
42    {
43      fprintf(stderr, "Usage: %s hostname\n", argv[0]);
44      exit(1);
45    }
46  host = gethostbyname(argv[1]);
47  if (!host)
48    {
49      fprintf(stderr, "Could not resolve hostname %s: %s\n", argv[1],
50              strerror(errno));
51      exit(1);
52    }
53
54  /* Construct the address. */
55  memset(&sin, 0, sizeof(sin));
56  sin.sin_family = AF_INET;
57  memcpy(&sin.sin_addr, host->h_addr, sizeof(sin.sin_addr));
58  sin.sin_port = htons(BUSYPOLL);
59
60  s = socket(AF_INET, SOCK_DGRAM, 0);
61  if (s < 0)
62    {
63      fprintf(stderr, "Could not create socket: %s\n", strerror(errno));
64      exit(1);
65    }
66
67  sendto(s, &dummy, 1, 0, (struct sockaddr *) &sin, sizeof(sin));
68  printf("Packet sent to %s, waiting for response...\n", argv[1]);
69  count = recvfrom(s, buf, sizeof(buf) - 1, 0, (struct sockaddr *) &sin,
70                   &len);
71  if (len < 0)
72    {
73      fprintf(stderr, "Error receiving response: %s\n", strerror(errno));
74      exit(1);
75    }
76  printf("Response received from %s\n", inet_ntoa(sin.sin_addr));
77  if (len == 0)
78    {
79      fprintf(stderr, "Empty packet\n");
80      exit(1);
81    }
82  buf[count] = 0;
83  name = buf + 1;
84  arch = name + strlen(name) + 1;
85  if (arch >= buf + count)
86    {
87      fprintf(stderr, "Invalid packet\n");
88      for (i = 0; i < count; i++)
89        fprintf(stderr, "%03d: %03d (%c)\n", i, buf[i], buf[i]);
90      exit(1);
91    }
92  printf("Busy state: %s\n", (buf[0] == '1') ? "busy" :
93         (buf[0] == '0') ? "free" : "invalid");
94  printf("Hostname: %s\n", name);
95  printf("Arch: %s\n", arch);
96  return 0;
97}
Note: See TracBrowser for help on using the repository browser.