From f0f4401f0cfc654646bdf21849627ebcbd5d82b5 Mon Sep 17 00:00:00 2001 From: rsc Date: Fri, 23 Apr 2004 05:12:11 +0000 Subject: stats helper --- src/cmd/auxstats/main.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 src/cmd/auxstats/main.c (limited to 'src/cmd/auxstats/main.c') diff --git a/src/cmd/auxstats/main.c b/src/cmd/auxstats/main.c new file mode 100644 index 00000000..a3624824 --- /dev/null +++ b/src/cmd/auxstats/main.c @@ -0,0 +1,123 @@ +#include +#include +#include +#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(strcmp(msg, "child") == 0) + 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", "-C", sys, exe, nil); + _exit(12); + default: + if((w = wait()) == nil) + sysfatal("wait: %r"); + if(w->pid != pid) + sysfatal("wait got wrong pid"); + if(atoi(w->msg) == 12) + 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)); +} + -- cgit v1.2.3