aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/auxstats/main.c
blob: d1c3556193847cdc91b9d319d14981b0f0de5584 (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
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "dat.h"

Biobuf bout;

void
usage(void)
{
	fprint(2, "usage: auxstats [system [executable]]\n");
	exits("usage");
}

int pid;

void
notifyf(void *v, char *msg)
{
	USED(v);

	if(strstr(msg, "child"))
		noted(NCONT);
	postnote(PNPROC, pid, msg);
	exits(nil);
}

void
main(int argc, char **argv)
{
	char *sys, *exe;
	int i;
	Waitmsg *w;

	ARGBEGIN{
	default:
		usage();
	}ARGEND

	notify(notifyf);

	sys = nil;
	exe = nil;
	if(argc > 0)
		sys = argv[0];
	if(argc > 1)
		exe = argv[1];
	if(argc > 2)
		usage();

	if(sys){
		if(exe == nil)
			exe = "auxstats";
		for(;;){
			switch(pid = fork()){
			case -1:
				sysfatal("fork: %r");
			case 0:
				rfork(RFNOTEG);
				execlp("ssh", "ssh", "-nTC", sys, exe, nil);
				_exit(97);
			default:
				if((w = wait()) == nil)
					sysfatal("wait: %r");
				if(w->pid != pid)
					sysfatal("wait got wrong pid");
				if(atoi(w->msg) == 97)
					sysfatal("exec ssh failed");
				free(w);
				fprint(2, "stats: %s hung up; sleeping 60\n", sys);
				sleep(60*1000);
				fprint(2, "stats: redialing %s\n", sys);
				break;
			}
		}
	}

	for(i=0; statfn[i]; i++)
		statfn[i](1);

	Binit(&bout, 1, OWRITE);
	for(;;){
		Bflush(&bout);
		sleep(900);
		for(i=0; statfn[i]; i++)
			statfn[i](0);
	}
}

char buf[16384];
char *line[100];
char *tok[100];
int nline, ntok;

void
readfile(int fd)
{
	int n;

	if(fd == -1){
		nline = 0;
		return;
	}

	seek(fd, 0, 0);
	n = read(fd, buf, sizeof buf-1);
	if(n < 0)
		n = 0;
	buf[n] = 0;

	nline = getfields(buf, line, nelem(line), 0, "\n");
}

void
tokens(int i)
{
	if(i >= nline){
		ntok = 0;
		return;
	}
	ntok = tokenize(line[i], tok, nelem(tok));
}