File: jmon.c

package info (click to toggle)
jmon 0.3-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 108 kB
  • ctags: 76
  • sloc: ansic: 569; sh: 77; makefile: 76
file content (415 lines) | stat: -rw-r--r-- 9,623 bytes parent folder | download
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/****************************************************************************

                   jMon - Distributed Resource Monitor

                   Copyright (C) 1999 Jaco Breitenbach

    This code is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This package 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this package; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    Jaco Breitenbach can be contacted at <jjb@dsp.sun.ac.za> 

****************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <ctype.h>
#include <signal.h>
#include <termios.h>
#include <sgtty.h>
#include <sys/ioctl.h>
#include <curses.h>
#include <math.h>


#ifndef MSG_NOSIGNAL
#	define MSG_NOSIGNAL 0x4000
#endif

#define MSGLEN 32

#define RCFILE ".jmonrc"

#define APPNAME "jMon"
#define CREDITS "by Jaco Breitenbach (1999)"
#define LEGEND  "            CPU                      Memory                     Swap            "

#define VERSION "0.3"

 
struct hostitem	{
	char				hostname[64];
	unsigned short int	port;
	int					sock_fd;
	struct sockaddr_in	addr;
	float				cpu, mem, swp;
	char				active;
	struct hostitem		*prev, *next;
};


struct hostitem		*firsthost=NULL, *lasthost=NULL; 
struct hostitem		*topdisphost=NULL, *botdisphost=NULL;
static int          interrupted, waiting, NUM_HOSTS;
attr_t				default_colour;


void connect_socket (struct hostitem *server);
void addhost (const char *hostname, unsigned short int port);
void remove_host (struct hostitem *ptr);
void parse_rcfile (const char *rcfilename);
void bar_calc (char *bar_str, int len, float val, int *colour);
static void show_all(void);
static void finish(int sig);
static void adjust(int sig);
void Usage (void);


void connect_socket (struct hostitem *server)	{
	struct hostent		*hostinfo;
	static int			pos = 0;

	if ((server->sock_fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)	{
		perror("Socket");
		exit(1);
	}

	server->addr.sin_family = AF_INET;
	server->addr.sin_port = htons(server->port);
	hostinfo = gethostbyname(server->hostname);
	if (hostinfo == NULL)	{
		fprintf(stderr, "Unknown host %s.\n", server->hostname);
		exit(1);
	}
	server->addr.sin_addr = *(struct in_addr *) hostinfo->h_addr;
	attrset(default_colour);
	move(pos, 0);
	printw("Connecting to host %s : %d",
		server->hostname, server->port);
	if (connect(server->sock_fd, (struct sockaddr *) &server->addr, 
		sizeof(server->addr)) < 0)	{
		attrset(COLOR_PAIR(3) | A_BOLD);
		move(pos++, 65);
		printw("Failed");
		remove_host(server);
	}
	else	{ 
		attrset(COLOR_PAIR(1) | A_BOLD);
		move(pos++, 65);
		printw("Success");
	}
	refresh();
}


void addhost (const char *hostname, unsigned short int port)	{
	struct hostitem		*ptr;

	if ((ptr=(struct hostitem *) calloc(sizeof(struct hostitem),1)) == NULL) {
		perror("calloc()");
		exit(1);
	}

	if (firsthost == NULL)	{
		firsthost = ptr;
		ptr->prev = NULL;
	}
	else	{ 
		lasthost->next = ptr;
		ptr->prev = lasthost; 
	}

	ptr->next = NULL;
	strncpy(ptr->hostname, hostname, 64);
	ptr->port = port;
	ptr->active = 1;

	lasthost = ptr;
	connect_socket(ptr);
}


void remove_host (struct hostitem *ptr)	{
	if (ptr != NULL)	{
		if (lasthost == ptr) { lasthost = ptr->prev; }
		if (firsthost == ptr) { firsthost = ptr->next; }
		if (ptr->prev != NULL) { ptr->prev->next = ptr->next; }
		if (ptr->next != NULL) { ptr->next->prev = ptr->prev; }
		free(ptr);
	}
}


void parse_rcfile (const char *rcfilename)	{
	FILE				*rcfile;
	static char			hostline[80];
	static char			hostname[64];
	static int			port;

	NUM_HOSTS = 0;

	if (((rcfile=fopen(rcfilename, "r")) == NULL)
            && ((rcfile=fopen("/etc/jmonrc", "r")) == NULL))
	{
		endwin();
		perror("fopen(RCFILE)");
                Usage();
		exit(1);
	}
	while (fgets(hostline, 80, rcfile) != NULL)	{
		if (sscanf(hostline, "%s %d", hostname, &port) == 2)	{
			addhost(hostname, (unsigned short int) port);
			NUM_HOSTS++;
		}
	}
	fclose(rcfile);
}


void bar_calc (char *bar_str, int len, float val, int *colour)	{
	int		num;

	num = ceil(((double) len) * val / 100.);
	num = num > len ? len : num;
	memset(bar_str, '=', num);
	memset(bar_str+num, ' ', len-num);
	*colour = floor(val/33.) + 1;
	*colour = *colour > 3 ? 3 : *colour;
}


static void show_all(void)	{
	struct hostitem		*disphost;
	int					c, n, pos = 3;
	div_t				result;
	char				bar[21];

	result = div(LINES+1, 3);
	bar[20] = '\0';

	scrollok(stdscr, FALSE);
	disphost = topdisphost;

	move(0,0);
	attrset(default_colour | A_BOLD);
	n = floor((80-(strlen(APPNAME)+strlen(VERSION)+strlen(CREDITS)+5))/2);
	printw("%*s (v%s) %s", n+strlen(APPNAME), APPNAME, VERSION, CREDITS);
	move(2,0);
	attrset(default_colour | A_REVERSE);
	printw("%s", LEGEND);

	while ((disphost != NULL) && (pos < 3*result.quot))	{
	    move(pos++, 0);
		attrset(default_colour);
		printw("%s : %d", disphost->hostname, disphost->port);
		clrtoeol();
		move(pos++, 0);
		attrset(default_colour);
		printw("%3.0f%%[", disphost->cpu);
		bar_calc(bar, 20, disphost->cpu, &c);
		attrset(COLOR_PAIR(c) | A_BOLD);
		printw("%s", bar);
		attrset(default_colour);
		printw("]%3.0f%%[", disphost->mem);
		bar_calc(bar, 20, disphost->mem, &c);
		attrset(COLOR_PAIR(c) | A_BOLD);
		printw("%s", bar);
		attrset(default_colour);
		printw("]%3.0f%%[", disphost->swp);
		bar_calc(bar, 20, disphost->swp, &c);
		attrset(COLOR_PAIR(c) | A_BOLD);
		printw("%s", bar);
		attrset(default_colour);
		printw("]");
	    clrtoeol();
		move(pos++, 0);
		clrtoeol();
		disphost = disphost->next;
	}
	clrtobot();
	botdisphost = disphost;
	setscrreg(3, LINES-3);
	scrollok(stdscr, TRUE);
	refresh();
}


static void finish(int sig)	{
	endwin();
	exit(sig);
}


static void adjust(int sig)	{
	if (waiting || sig == 0)	{
	struct winsize size;

		if (ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0)	{
			resizeterm(size.ws_row, size.ws_col);
			wrefresh(curscr);
			topdisphost = firsthost;
			show_all();
		}
		interrupted = FALSE;
	} 
	else	{
		interrupted = TRUE;
	}
	signal(SIGWINCH, (void *) &adjust);
}


void Usage (void)	{
	printf("\nUsage:\tjmon [-f rcfile]\nThe client for the jMon daemon. ");
        printf("Hosts running the jmond daemon are listed in\nthe jmonrc file. ");
	printf("The default rcfile is .jmonrc in the user's home directory.\n");
	printf("The format is as follows:");
	printf("\n\n\thostname\t\t\tport\ne.g.\n");
	printf("\twintermute.dsp.sun.ac.za\t7777\n");
	printf("\tsaiph.dsp.sun.ac.za\t\t7777\n\n");
	exit(1);
}


int main (int argc, char *argv[])	{
	int					done = 0, result, c;
	struct hostitem		*thishost;
	fd_set 				act_rd_set, rd_set;
	float				cpu, mem, swp;
	char				buf[MSGLEN];
	static int			my_bg = COLOR_BLACK;
	int				opt, opts;
	short int			pair;
	char				rcfile[64] = "";
	char				*home;


	opts = 0;

	if ((home = getenv("HOME")) != NULL)	{
		strncpy(rcfile, home, 64);
		if (strlen(rcfile) < 64) strncat(rcfile, "/", 1);
	}
	strncat(rcfile, RCFILE, 64-strlen(rcfile));

	while ((opt=getopt(argc,argv,"f:h")) != EOF)	{
		switch(opt)	{
			case 'f':	{
				strncpy(rcfile, optarg, 64);
				break;
			}
			default:	Usage();
		}	
	}

	initscr();
	keypad(stdscr, TRUE);
	nonl();
	cbreak();
	noecho();
	idlok(stdscr, TRUE);
	nodelay(stdscr, TRUE);
	if (has_colors())	{
		start_color();
		if (use_default_colors() == OK)
			my_bg = -1;
	}
	curs_set(0);
        //	attr_get(&default_colour, &pair, (void *) &opts);
	init_pair(1,2,my_bg);
	init_pair(2,3,my_bg);
	init_pair(3,1,my_bg);
	erase();

	parse_rcfile(rcfile);
	erase();

	if ((thishost = firsthost) != NULL)	{
		FD_ZERO (&act_rd_set);
		while (thishost != NULL)	{
			FD_SET(thishost->sock_fd, &act_rd_set);
			thishost = thishost->next;
		}

		signal(SIGINT, (void *) &finish);
		signal(SIGWINCH, (void *) &adjust);

		topdisphost = firsthost;

		while(!done)	{
			rd_set = act_rd_set;
			result = select(FD_SETSIZE, &rd_set, NULL, NULL, NULL);
			thishost = firsthost;
			while (thishost != NULL)	{
				if (FD_ISSET(thishost->sock_fd, &rd_set))	{
					if (recv(thishost->sock_fd, buf, \
						MSGLEN, MSG_NOSIGNAL) == MSGLEN)	{
						if (sscanf(buf,"cpu:%f mem:%f swp:%f", 
							&cpu, &mem, &swp) == 3)	{
							thishost->cpu = cpu;
							thishost->mem = mem;
							thishost->swp = swp;
						}
					}
				}
				thishost = thishost->next;
			}

			show_all();

			if (interrupted) adjust(0);
			waiting = TRUE;
			c = getch();
			waiting = FALSE;

			switch(c) {
				case KEY_DOWN:
					if ((botdisphost != NULL) && (topdisphost != NULL))	{
						topdisphost = topdisphost->next;
						wscrl(stdscr, 1);
					}
					break;
				case KEY_UP:
					if ((topdisphost!=firsthost) && (topdisphost!=NULL))	{
						topdisphost = topdisphost->prev;
						wscrl(stdscr, -1);
					}
	   				break;
				case 'q':
				    done = TRUE;
	   				break;
				case ERR:
					break;
				default:
				    beep();
			}
			usleep(100000);
		}
	}
	finish(0);
	return 0;
}