aboutsummaryrefslogtreecommitdiffstats
path: root/utils/vmon.c
blob: e1015b4cad091da56a81f32845ff0cb20d1703a2 (plain)
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/* Verbal monitor system status.
 * $Id: vmon.c 1.2 Mon, 24 Mar 1997 12:25:37 -0500 dyfet $
 * Copyright (c) 1997 by Tycho Softworks.
 * For conditions on distribution and reuse see product license.
 *
 * Abstract:
 *	Monitor system status in background.  This service is normally
 *	started with the speech server.  It may also be ran as 'vstat'
 *	to perform immediate notification.
 */

#include <std/time.h>
#include <other/string.h>
#include <proc/process.h>
#include <other/config.h>
#include <net/stream.h>
#include <std/utmp.h>

/* For systems that use statfs! */

#ifdef	__linux__
#include <sys/vfs.h>
#define	__statfs__
#endif

typedef	struct	_MAILBOX
{
	struct	_MAILBOX	*next;
	time_t	update;
	ulong	size;
	char	name[0];
}	MAILBOX;

static	int	freq = 3600;
static	int	timer = 3600;
static	int	interval = 300;
static	int	iter;
static	int	orig_timer;
static	int	alert = 90;	/* warn on 90% full */
static	char	alertmsg[] = "emergency alert              emergency alert         \r\n"; 
static	char	**fs;
static	char	host[128] = "localhost";
static	int	mute[2] = {24, 0};

static	char	*vmailbox(MAILBOX *mbox)
{
	struct	stat	ino;
	static	char	buf[80];

	if(stat(mbox->name, &ino))
		return NULL;

	if(!ino.st_size)
	{
		mbox->size = 0;
		return NULL;
	}

	if(ino.st_size == mbox->size)
		return NULL;

	if(mbox->size == -1)
	{
		mbox->size = ino.st_size;
		return NULL;
	}

	mbox->size = ino.st_size;
	sprintf(buf, "User %s has new mail\n", mbox->name);
	return	buf;
}		
		
static	void	vmbox(MAILBOX *box, int port)
{
	STREAM	fp = NULL;
	char	*p;
	time_t	now;
	struct	tm	*dt;
	char	buf[128];
	
	time(&now);
	dt = localtime(&now);	
	if(dt->tm_hour > mute[0] || dt->tm_hour < mute[1])
		return;
	
	while(box)
	{
		p = vmailbox(box);
		if(!p)
		{
			box = box->next;
			continue;
		}
		if(!fp)
		{
			fp = opentcp(host, port);
			if(!fp)
				return;

			gettcp(buf, sizeof(buf) - 1, fp);
		}
		puttcp(p, fp);
		box = box->next;
	}
	if(fp)
		closetcp(fp);
}

static	int	vstat(int port)
{
	int	i = 0;
	char	*f;
	long	btotal = 0;
	long	bfree = 0;
	bool	failed = FALSE;
	STREAM	fp = NULL;
#ifdef	__statfs__
	struct	statfs	fnode;
#endif
	int	blocks, inodes;
	char	buf[128];
	int	newtimer = orig_timer;
	time_t	now;
	static	prior = -1;
	struct	tm	*dt;
	int	apm, hour;
	char	*apmstr[] = {" A M ", " P M "};
	int	fd;
	struct	utmp	*utmp, uptime;
	int	users = 0;
	int	utcnt = 0;

#ifdef	__statfs__
	while(fs[i])
	{
		if(!statfs(fs[i], &fnode)) 
		{
			btotal += fnode.f_blocks;
			bfree += fnode.f_bfree;
			blocks = 100 - (int)(fnode.f_ffree / (fnode.f_files / 100l));	
			inodes = 100 - (int)(fnode.f_bfree / (fnode.f_blocks / 100l));
			if((blocks < alert) && (inodes < alert))
			{
				++i;
				continue;
			}
		}
		else
		{
			inodes = 0;
			blocks = 0;
			failed = TRUE;
		}	
		if(!fp)
		{
			fp = opentcp(host, port);
			if(!fp)
			{
				newtimer = 300;
				break;
			}
			gettcp(buf, 128, fp);
			puttcp(alertmsg, fp);
		}

		if(blocks > alert)
			fprintf(fp, "File system %s is %d percent full.\r\n", fs[i], blocks);

		if(inodes > alert)
			fprintf(fp, "File system %s i-node table is %d percent full.\r\n", fs[i], inodes);

		if(failed)
			fprintf(fp, "File system %s is dismounted or failed\r\n", fs[i]);

		fflush(fp);
		newtimer = 60;
		++i;
	}
#endif
	
	if(fp)
		closetcp(fp);
	
	time(&now);
	if(prior == (now / freq))
		return newtimer;

	if(prior == -1)
		prior = now / freq - 1;
	else
		prior = now / freq;

	dt = localtime(&now);	
	hour = dt->tm_hour % 12;
	if(dt->tm_hour > mute[0] || dt->tm_hour < mute[1])
		return newtimer;

	if(!hour)
		hour = 12;

	if(dt->tm_hour > 11)
		apm = 1;
	else
		apm = 0;

	fp = opentcp(host, port);
	if(!fp)
		return newtimer;

	gettcp(buf, 128, fp);
	fprintf(fp, "It is %d:%d %s \r\n", hour, dt->tm_min, apmstr[apm]);
	fflush(fp);

	fd = open("/proc/uptime", O_RDONLY);
	hour = -1;
	if(fd > -1)
	{
		read(fd, buf, 128);
		close(fd);
		hour = atol(buf) / 3600;
	}

#if defined(BOOT_TIME) || defined(RUN_LVL)
	if(hour == -1)
	{
		utmp = NULL;
		memset(&uptime, 0, sizeof(uptime));
#ifdef	BOOT_TIME
		setutent();
		uptime.ut_type = BOOT_TIME;
		utmp = getutid(&uptime);
#endif
#ifdef	RUN_LVL
		if(!utmp)
		{
			setutent();
			uptime.ut_type = RUN_LVL;
			utmp = getutid(&uptime);			
		}
#endif
		if(utmp)
			hour = (now - utmp->ut_time) / 3600l;

		endutent();
	}
#endif

	if(hour > -1)
	{
		if(hour == 1)
			fprintf(fp, "The system has been up 1 hour\r\n");
		else if(hour < 48)
			fprintf(fp, "The system has been up %d hours\r\n", hour);
		else
			fprintf(fp, "The system has been up %d days\r\n", hour / 24);
		fflush(fp);
	}

	setutent();
	while(NULL != (utmp = getutent()))
	{
		++utcnt;

#ifdef	USER_PROCESS
		if(utmp->ut_type != USER_PROCESS)
			continue;
#endif

		utmp->ut_user[sizeof(utmp->ut_user)] = 0;
		if(!strcmp(utmp->ut_user, "root"))
			continue;
			
		if(!utmp->ut_user[0])
			continue;

		if(!utmp->ut_line[0])
			continue;

#ifdef	USER_PROCESS			
		if(kill(utmp->ut_pid, 0))
			continue;
#endif

		++users;
	}
	endutent();

	if(utcnt)
	{
		if(!users)
			fprintf(fp, "There are no users online\r\n");
		else if(users == 1)
			fprintf(fp, "There is 1 user online\r\n");
		else
			fprintf(fp, "There are %d users online\r\n", users);
		fflush(fp);
	}		

	if(btotal)
	{
		fprintf(fp, "The main system drives are at %d percent of capacity\r\n",
			100 - (int)(bfree / (btotal / 100)));
		fflush(fp);
	}
			
	closetcp(fp);
	return newtimer;
}
	
void	main(int argc, char **argv)
{
	int	port = getservice("speak");
	char	*maildir = getenv("MAILDIR");
	MAILBOX	*mbox = NULL, *newbox;
	char	*prog = basename(*argv);
	CONFIG	*cfg = sys_config("speak");
	time_t	now;
	char	*p;

	if(argc < 2)
		fatal(EX_USAGE, "use %s filesystems...\n", prog);

	if(!maildir)
		maildir = "/var/spool/mail";

	fs = ++argv;

	if(!port)
		fatal(EX_UNAVAILABLE, "%s: speak: service unavailable\n", prog);

	if(!cfg)
		fatal(EX_UNAVAILABLE, "%s: speak.conf: missing\n", prog);

	seek_config(cfg, "vmon");
	while(read_config(cfg))
	{
		if(NULL != (p = get_config(cfg, "mailbox")))
		{
			p = strtok(p, __SPACES);
			while(p)
			{
				newbox = (MAILBOX *)malloc(sizeof(MAILBOX) + strlen(p) + 1);
				newbox->next = mbox;
				time(&newbox->update);
				newbox->size = -1l;
				strcpy(newbox->name, p);
				mbox = newbox;
				p = strtok(NULL, __SPACES);
			}
			continue;
		}				

		if(NULL != (p = get_config(cfg, "interval")))
		{
			interval = atoi(p) * 60;
			continue;
		}

		if(NULL != (p = get_config(cfg, "frequency")))
		{
			timer = 3600 / atoi(p);
			continue;
		}
		
		if(NULL != (p = get_config(cfg, "server")))
		{
			strcpy(host, p);
			continue;
		}

		if(NULL != (p = get_config(cfg, "host")))
		{
			strcpy(host, p);
			continue;
		}

		if(NULL != (p = get_config(cfg, "alert")))
		{
			alert = atoi(p);
			continue;
		}

		if(NULL != (p = get_config(cfg, "timer")))
		{
			timer = atoi(p) * 60;
			continue;
		}

		if(NULL != (p = get_config(cfg, "mute")))
		{
			mute[0] = atoi(p);
			p = strchr(p, '-');
			if(p)
				mute[1] = atoi(strtrim(++p, __SPACES));
			continue;
		}
		
		if(NULL != (p = get_config(cfg, "maildir")))
		{
			maildir = strdup(p);
			continue;
		}
			
	}
	close_config(cfg);

	if(stricmp(prog, "vmon"))
	{
		vstat(port);
		exit(0);
	}

	pdetach(D_KEEPNONE);

	chdir(maildir);
	freq = timer;
	orig_timer = timer;
	time(&now);
	for(;;)
	{
		timer = vstat(port);
		iter = interval;
		for(;;)
		{
			vmbox(mbox, port);
			sleep(iter);
			time(&now);
			iter = timer - (now % timer) - 1;
			
			if(iter > interval)
				iter = interval;

			if(iter < 2)
			{
				sleep(iter + 2);
				break;
			}
		}			
	}
}