Plan 9 from Bell Labs’s /usr/web/sources/contrib/stallion/src/collectd/uptime.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <libc.h>
#include <thread.h>
#include <bio.h>
#include "collectd.h"

typedef struct Time Time;
struct Time {
	ulong	sec;
	uvlong	nsec;
	uvlong	ticks;
	uvlong	hz;
};

static void
gettime(char *file, Time *time)
{
	Biobuf *bp;
	char *p, *f[4];
	int nf;

	bp = Bopen(file, OREAD);
	if(bp == nil)
		sysfatal("couldn't open %s: %r", file);

	p = Brdstr(bp, '\n', 1);
	if(p == nil)
		sysfatal("unexpected time output");

	nf = tokenize(p, f, nelem(f));
	if(nf < nelem(f))
		sysfatal("unexpected time output");

	time->sec = strtoul(f[0], nil, 0);
	time->nsec = strtoull(f[1], nil, 0);
	time->ticks = strtoull(f[2], nil, 0);
	time->hz = strtoull(f[3], nil, 0);

	free(p);
	Bterm(bp);
}

static void
submit(Channel *c, double value)
{
	Packet *pp;

	pp = palloc();
	pp->host = estrdup(hostname);
	pp->interval = interval;
	pp->time = time(nil);
	pp->plugin = estrdup("uptime");
	pp->type = estrdup("uptime");
	addgauge(pp, value);

	if(nbsendp(c, pp) < 1)
		pfree(pp);
}

void
uptimeproc(void *arg)
{
	Channel *c;
	Time time;

	c = arg;
	for(;;){
		snooze();

		gettime("/dev/time", &time);
		submit(c, (double)time.ticks / time.hz);
	}
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].