~ubuntu-branches/ubuntu/breezy/rstatd/breezy

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
/*
 * $Id: rsysinfo.c,v 1.3 2001/08/15 02:35:56 amigus Exp $
 *  rsysinfo version 3.03 rpc ruptime client for use with rpc.rstatd 3.03
 *  Copyright (C) 1995  Adam Migus, Memorial University of Newfoundland
 *	(MUN)
 *  Copyright (C) 2001 Andreas Klingler, University of Erlangen-Nuernberg
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 1, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  If you make modifications to the source, I would be happy to have
 *  them to include in future releases.  Feel free to send them to:
 *      Adam Migus	      				
 *		amigus@cs.mun.ca 
 *      amigus@ucs.mun.ca   
 *
 **************************************************************************/

#include <stdio.h>
#include <rpc/rpc.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <time.h>
#include <netdb.h>
#include "rstat.h"

void bzero();
void bcopy();
char *rindex();

static char * id = "@(#) $Id: rsysinfo.c,v 1.3 2001/08/15 02:35:56 amigus Exp $";
static char *version0 = "@(#)rsysinfo version 3.06 (c) Andreas Klingler 3/5/2001 based on\n";
char *version = "@(#)rsysinfo version 3.03 Copyright (Adam Migus) 1/9/96\n";
char *program_name;

rsysinfo_answer_v3(replyp,raddrp)
struct statstime *replyp;
struct sockaddr_in *raddrp;
{
	struct hostent *hp;
	char *host;
	struct statstime *host_stat;
	struct tm *tmp_time;
	struct tm host_time;
	struct tm host_uptime;
	char days_buf[16];
	char hours_buf[16];

	host_stat = (statstime *)replyp;

	hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
			   sizeof(struct in_addr), AF_INET);
	if (hp)
		host = (char *)hp->h_name;
	else
		host = (char *)inet_ntoa(raddrp->sin_addr);


	tmp_time = localtime((time_t *)&host_stat->curtime.tv_sec);
	host_time = *tmp_time;

	host_stat->curtime.tv_sec -= host_stat->boottime.tv_sec;

	tmp_time = gmtime((time_t *)&host_stat->curtime.tv_sec);
	host_uptime = *tmp_time;


	if (host_uptime.tm_yday != 0)
		sprintf(days_buf, "%3d day%s, ", host_uptime.tm_yday,
			(host_uptime.tm_yday > 1) ? "s" : "");
	else
		days_buf[0] = '\0';

	if (host_uptime.tm_hour != 0)
		sprintf(hours_buf, "%2d:%02d, ",
			host_uptime.tm_hour, host_uptime.tm_min);
	else
		if (host_uptime.tm_min != 0)
			sprintf(hours_buf, "%2d mins, ", host_uptime.tm_min);
		else
			hours_buf[0] = '\0';


	printf("System Information for: %s\n",host);
	printf("uptime: %s%sload average: %.2f %.2f %.2f\n",
		days_buf, hours_buf, host_stat->avenrun[0]/256.0,
		host_stat->avenrun[1]/256.0, host_stat->avenrun[2]/256.0);

	printf("cpu usage (jiffies): user %d  nice %d  system %d  idle %d\n",
		host_stat->cp_time[0], host_stat->cp_time[1], host_stat->cp_time[2], host_stat->cp_time[3]);

	printf("page in: %d  page out: %d   swap in: %d  swap out: %d\n",
		host_stat->v_pgpgin, host_stat->v_pgpgout, host_stat->v_pswpin, host_stat->v_pswpout);
	printf("intr: %d     context switches: %d\n",
		host_stat->v_intr, host_stat->v_swtch);
	printf ("disks: %d %d %d %d\n",
		host_stat->dk_xfer[0],host_stat->dk_xfer[1],host_stat->dk_xfer[2],host_stat->dk_xfer[3]);
	printf("ethernet:  rx: %u   rx-err: %u\n"
			"           tx: %u   tx-err: %u    collisions: %d\n",
		host_stat->if_ipackets, host_stat->if_ierrors, host_stat->if_opackets, host_stat->if_oerrors,
		host_stat->if_collisions);

	return(0);
}

int pointopoint_v3(server_addr)
struct sockaddr_in *server_addr;
{
	struct statstime host_stat;
	struct timeval pertry_timeout, total_timeout;
	enum clnt_stat clnt_stat;
	int sock=RPC_ANYSOCK;
	CLIENT *client;

	pertry_timeout.tv_sec = 3;
	pertry_timeout.tv_usec = 0;
	total_timeout.tv_sec = 10;
	total_timeout.tv_usec = 0;

	if((client = clntudp_create(server_addr, RSTATPROG, RSTATVERS_TIME,
	pertry_timeout, &sock)) == NULL) {
		close(sock);
		return(0);
	}
	clnt_stat = clnt_call(client, RSTATPROC_STATS, (xdrproc_t) xdr_void,0,
	(xdrproc_t) xdr_statstime, (struct statstime *)&host_stat, total_timeout);

	if(clnt_stat == RPC_SUCCESS) {
		rsysinfo_answer_v3(&host_stat, server_addr);
		clnt_destroy(client);
		close(sock);
		return(1);
	}
	else {
		clnt_destroy(client);
		close(sock);
		return(0);
	}
	return(0);
}
void usage()
{
	printf("usage: %s \"host\" | -v\n"\
			"\t-v\tprint version and exit\n", program_name);
}

void main(argc,argv)
int argc; char **argv;
{
	struct hostent *hp;
	struct sockaddr_in server_addr;
	char c;

	program_name = rindex(argv[0],'/');
	if(!(program_name)) 
		program_name = argv[0];
	else 
		program_name++;
	
	if(argc == 1) {
		usage();
		exit(0);
	}

	while((c = getopt(argc,argv,"h?v")) != EOF)
		switch(c) {
			case 'v':
						printf(version0);
						printf(version);

						exit(0);
			case '?':
			case 'h':
			default:
						usage();
						exit(0);
		}

	if((hp = gethostbyname(argv[1])) == NULL) {
		fprintf(stderr,"%s: cannot get hostname %s\n",program_name,argv[1]);
		exit(-1);
	}

	bcopy(hp->h_addr,(caddr_t)&server_addr.sin_addr, hp->h_length);
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = 0;

	if(pointopoint_v3(&server_addr) == 1)
		exit(0);
	else 
		clnt_pcreateerror(program_name);
}