Plan 9 from Bell Labs’s /usr/web/sources/contrib/stallion/src/collectd/swap.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"

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

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

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

void
swapproc(void *arg)
{
	char *p;
	Channel *c;
	Biobuf *bp;
	Swap swap;
	ulong pagesize;
	double used, total;

	/*
	 * Swap(8) is rarely used these days; skip this plugin for
	 * systems not making use of it.
	 */
	p = getenv("swap");
	if(p == nil)
		exits(nil);
	free(p);

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

		pagesize = 0;
		used = total = 0;
		while(bp = readswap(bp, "/dev/swap", &swap)){
			if(strcmp(swap.name, "pagesize") == 0)
				pagesize = swap.n;

			else if(strcmp(swap.name, "swap") == 0){
				used = (double)swap.n * pagesize;
				total = (double)swap.m * pagesize;
			}
		}
		submit(c, "used", used);
		submit(c, "free", total-used);
	}
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.