sammler/p_stat.c

143 lines
3.7 KiB
C
Raw Normal View History

2006-06-13 21:34:36 +02:00
/***************************************************************************
* Copyright (C) 06/2006 by Olaf Rempel *
* razzor@kopf-tisch.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "plugins.h"
#define BUFSIZE 1024
#define DS_CPU 1
#define DS_PROC 2
struct sammler_plugin plugin;
static char *cpu_ds_def = {
"DS:user:COUNTER:15:0:U "
"DS:nice:COUNTER:15:0:U "
"DS:syst:COUNTER:15:0:U "
"DS:idle:COUNTER:15:0:U "
"DS:wait:COUNTER:15:0:U "
"DS:intr:COUNTER:15:0:U "
"DS:sitr:COUNTER:15:0:U "
2006-06-13 21:34:36 +02:00
};
static char *proc_ds_def = {
"DS:intr:COUNTER:15:0:U "
"DS:ctxt:COUNTER:15:0:U "
"DS:fork:COUNTER:15:0:U "
2006-06-13 21:34:36 +02:00
};
static char * get_ds(int ds_id)
2006-06-13 21:34:36 +02:00
{
switch (ds_id) {
case DS_CPU:
return cpu_ds_def;
case DS_PROC:
return proc_ds_def;
default:
return NULL;
}
}
struct proc_ {
unsigned long long intr;
unsigned long long ctxt;
unsigned long long fork;
};
static void probe(void)
{
FILE *fp;
char *buffer;
struct proc_ proc;
buffer = malloc(BUFSIZE);
if (buffer == NULL) {
log_print(LOG_WARN, "plugin stat: out of memory");
return;
}
fp = fopen("/proc/stat", "r");
if (fp == NULL) {
log_print(LOG_WARN, "plugin stat");
free(buffer);
return;
}
while (fgets(buffer, BUFSIZE, fp) != NULL) {
if (!strncmp(buffer, "cpu", 3)) {
char *val[9], filename[16];
2006-06-22 20:33:30 +02:00
int numfields, len, cpu;
2006-06-13 21:34:36 +02:00
if ((buffer[3] >= '0') && (buffer[3] <= '9')) {
cpu = atoi(buffer +3);
2006-06-22 20:33:30 +02:00
len = snprintf(filename, sizeof(filename), "cpu-%d.rrd", cpu);
if (len < 0 || len >= sizeof(filename))
continue;
2006-06-13 21:34:36 +02:00
} else {
strncpy(filename, "cpu.rrd", sizeof(filename));
}
numfields = strsplit(buffer, val, 9);
if (numfields < 5)
continue;
if (numfields >= 8) {
probe_submit(&plugin, filename, DS_CPU,
"%s:%s:%s:%s:%s:%s:%s",
val[1], val[2], val[3], val[4],
val[5], val[6], val[7]);
} else {
probe_submit(&plugin, filename, DS_CPU,
"%s:%s:%s:%s:0:0:0",
val[1], val[2], val[3], val[4]);
}
} else if (!strncmp(buffer, "intr", 4)) {
proc.intr = atoll(buffer + 5);
} else if (!strncmp(buffer, "ctxt", 4)) {
proc.ctxt = atoll(buffer + 5);
} else if (!strncmp(buffer, "processes", 9)) {
proc.fork = atoll(buffer + 10);
}
}
probe_submit(&plugin, "proc.rrd", DS_PROC, "%llu:%llu:%llu",
proc.intr, proc.ctxt, proc.fork);
fclose(fp);
free(buffer);
}
struct sammler_plugin plugin = {
.name = "stat",
.interval = 10,
2006-06-13 21:34:36 +02:00
.probe = &probe,
.get_ds = &get_ds,
};