Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.bin / rup / rup.c
1 /*-
2  * Copyright (c) 1993, John Brezak
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD: src/usr.bin/rup/rup.c,v 1.11.2.2 2001/07/02 23:43:04 mikeh Exp $
34  * $DragonFly: src/usr.bin/rup/rup.c,v 1.2 2003/06/17 04:29:31 dillon Exp $
35  */
36
37 #include <err.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <time.h>
42 #include <unistd.h>
43 #include <sys/param.h>
44 #include <sys/socket.h>
45 #include <netdb.h>
46 #include <rpc/rpc.h>
47 #include <rpc/pmap_clnt.h>
48 #include <arpa/inet.h>
49 #undef FSHIFT                   /* Use protocol's shift and scale values */
50 #undef FSCALE
51 #include <rpcsvc/rstat.h>
52
53 #define HOST_WIDTH 15
54
55 struct host_list {
56         struct host_list *next;
57         struct in_addr addr;
58 } *hosts;
59
60 int search_host(struct in_addr addr)
61 {
62         struct host_list *hp;
63
64         if (!hosts)
65                 return(0);
66
67         for (hp = hosts; hp != NULL; hp = hp->next) {
68                 if (hp->addr.s_addr == addr.s_addr)
69                         return(1);
70         }
71         return(0);
72 }
73
74 void remember_host(struct in_addr addr)
75 {
76         struct host_list *hp;
77
78         if (!(hp = (struct host_list *)malloc(sizeof(struct host_list))))
79                 errx(1, "no memory");
80         hp->addr.s_addr = addr.s_addr;
81         hp->next = hosts;
82         hosts = hp;
83 }
84
85 int
86 rstat_reply(char *replyp, struct sockaddr_in *raddrp)
87 {
88         struct tm *tmp_time;
89         struct tm host_time;
90         struct tm host_uptime;
91         char days_buf[16];
92         char hours_buf[16];
93         struct hostent *hp;
94         char *host;
95         statstime *host_stat = (statstime *)replyp;
96
97         if (search_host(raddrp->sin_addr))
98                 return(0);
99
100         hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
101                            sizeof(struct in_addr), AF_INET);
102         if (hp)
103                 host = hp->h_name;
104         else
105                 host = inet_ntoa(raddrp->sin_addr);
106
107         /* truncate hostname to fit nicely into field */
108         if (strlen(host) > HOST_WIDTH)
109                 host[HOST_WIDTH] = '\0';
110
111         printf("%-*s\t", HOST_WIDTH, host);
112
113         tmp_time = localtime((time_t *)&host_stat->curtime.tv_sec);
114         host_time = *tmp_time;
115
116         host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec;
117
118         tmp_time = gmtime((time_t *)&host_stat->curtime.tv_sec);
119         host_uptime = *tmp_time;
120
121         #define updays (host_stat->curtime.tv_sec  / 86400)
122         if (host_uptime.tm_yday != 0)
123                 sprintf(days_buf, "%3d day%s, ", updays,
124                         (updays > 1) ? "s" : "");
125         else
126                 days_buf[0] = '\0';
127
128         if (host_uptime.tm_hour != 0)
129                 sprintf(hours_buf, "%2d:%02d, ",
130                         host_uptime.tm_hour, host_uptime.tm_min);
131         else
132                 if (host_uptime.tm_min != 0)
133                         sprintf(hours_buf, "%2d mins, ", host_uptime.tm_min);
134                 else
135                         hours_buf[0] = '\0';
136
137         printf(" %2d:%02d%cm  up %9.9s%9.9s load average: %.2f %.2f %.2f\n",
138                 (host_time.tm_hour % 12) ? host_time.tm_hour % 12 : 12,
139                 host_time.tm_min,
140                 (host_time.tm_hour >= 12) ? 'p' : 'a',
141                 days_buf,
142                 hours_buf,
143                 (double)host_stat->avenrun[0]/FSCALE,
144                 (double)host_stat->avenrun[1]/FSCALE,
145                 (double)host_stat->avenrun[2]/FSCALE);
146
147         remember_host(raddrp->sin_addr);
148         return(0);
149 }
150
151 int
152 onehost(char *host)
153 {
154         CLIENT *rstat_clnt;
155         statstime host_stat;
156         struct sockaddr_in addr;
157         struct hostent *hp;
158         struct timeval tv;
159
160         hp = gethostbyname(host);
161         if (hp == NULL) {
162                 warnx("unknown host \"%s\"", host);
163                 return(-1);
164         }
165
166         rstat_clnt = clnt_create(host, RSTATPROG, RSTATVERS_TIME, "udp");
167         if (rstat_clnt == NULL) {
168                 warnx("%s %s", host, clnt_spcreateerror(""));
169                 return(-1);
170         }
171
172         bzero((char *)&host_stat, sizeof(host_stat));
173         tv.tv_sec = 15; /* XXX ??? */
174         tv.tv_usec = 0;
175         if (clnt_call(rstat_clnt, RSTATPROC_STATS, xdr_void, NULL, xdr_statstime, &host_stat, tv) != RPC_SUCCESS) {
176                 warnx("%s: %s", host, clnt_sperror(rstat_clnt, host));
177                 clnt_destroy(rstat_clnt);
178                 return(-1);
179         }
180
181         addr.sin_addr.s_addr = *(int *)hp->h_addr;
182         rstat_reply((char *)&host_stat, &addr);
183         clnt_destroy(rstat_clnt);
184         return (0);
185 }
186
187 void
188 allhosts()
189 {
190         statstime host_stat;
191         enum clnt_stat clnt_stat;
192
193         clnt_stat = clnt_broadcast(RSTATPROG, RSTATVERS_TIME, RSTATPROC_STATS,
194                                    xdr_void, NULL,
195                                    xdr_statstime, (char *)&host_stat, rstat_reply);
196         if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT)
197                 errx(1, "%s", clnt_sperrno(clnt_stat));
198 }
199
200 static void
201 usage()
202 {
203         fprintf(stderr, "usage: rup [hosts ...]\n");
204         exit(1);
205 }
206
207 int
208 main(int argc, char *argv[])
209 {
210         int ch;
211
212         while ((ch = getopt(argc, argv, "?")) != -1)
213                 switch (ch) {
214                 default:
215                         usage();
216                         /*NOTREACHED*/
217                 }
218
219         setlinebuf(stdout);
220         if (argc == optind)
221                 allhosts();
222         else {
223                 for (; optind < argc; optind++)
224                         (void) onehost(argv[optind]);
225         }
226         exit(0);
227 }