sammler/plugins/stat.c

155 lines
3.9 KiB
C
Raw Permalink 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>
2006-10-08 16:33:07 +02:00
#include "helper.h"
2007-04-01 15:45:31 +02:00
#include "logging.h"
2006-06-13 21:34:36 +02:00
#include "plugins.h"
2007-04-01 14:30:05 +02:00
#include "probe.h"
2006-06-13 21:34:36 +02:00
#define DS_CPU 1
#define DS_PROC 2
2007-04-01 15:23:09 +02:00
#define BUFSIZE 1024
2006-06-13 21:34:36 +02:00
struct sammler_plugin plugin;
2007-04-01 15:23:09 +02:00
static char *buffer;
2006-06-13 21:34:36 +02:00
2007-03-31 22:31:07 +02:00
static const char *cpu_ds_def = {
"DS:user:COUNTER:15:0:U "
"DS:nice:COUNTER:15:0:U "
2006-09-30 23:25:59 +02:00
"DS:system:COUNTER:15:0:U "
"DS:idle:COUNTER:15:0:U "
2006-09-30 23:25:59 +02:00
"DS:iowait:COUNTER:15:0:U "
"DS:irq:COUNTER:15:0:U "
"DS:softirq:COUNTER:15:0:U "
"DS:steal:COUNTER:15:0:U "
2006-06-13 21:34:36 +02:00
};
2007-03-31 22:31:07 +02:00
static const 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
};
2007-03-31 22:31:07 +02:00
static const 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;
};
2006-10-07 20:37:30 +02:00
static int probe(void)
2006-06-13 21:34:36 +02:00
{
FILE *fp;
struct proc_ proc;
2006-09-30 21:31:53 +02:00
memset(&proc, 0, sizeof(proc));
2006-06-13 21:34:36 +02:00
fp = fopen("/proc/stat", "r");
if (fp == NULL) {
log_print(LOG_WARN, "plugin stat");
2006-10-07 20:37:30 +02:00
return -1;
2006-06-13 21:34:36 +02:00
}
2007-04-01 15:23:09 +02:00
while (fgets(buffer, BUFSIZE, fp) != NULL) {
if (!strncmp(buffer, "cpu", 3)) {
2006-06-13 21:34:36 +02:00
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
2007-04-01 15:23:09 +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));
}
2007-04-01 15:23:09 +02:00
numfields = strsplit(buffer, " \t\n", val, 9);
2006-06-13 21:34:36 +02:00
if (numfields < 5)
continue;
2006-10-09 14:40:38 +02:00
else if (numfields == 5)
val[5] = val[6] = val[7] = val[8] = "0";
2007-03-31 22:31:07 +02:00
2006-10-09 14:40:38 +02:00
else if (numfields == 8)
val[8] = "0";
2007-03-31 22:31:07 +02:00
2006-10-09 14:40:38 +02:00
probe_submit(&plugin, filename, DS_CPU,
"%s:%s:%s:%s:%s:%s:%s:%s",
val[1], val[2], val[3], val[4],
val[5], val[6], val[7], val[8]);
2006-06-13 21:34:36 +02:00
2007-04-01 15:23:09 +02:00
} else if (!strncmp(buffer, "intr", 4)) {
proc.intr = atoll(buffer + 5);
2006-06-13 21:34:36 +02:00
2007-04-01 15:23:09 +02:00
} else if (!strncmp(buffer, "ctxt", 4)) {
proc.ctxt = atoll(buffer + 5);
2006-06-13 21:34:36 +02:00
2007-04-01 15:23:09 +02:00
} else if (!strncmp(buffer, "processes", 9)) {
proc.fork = atoll(buffer + 10);
2006-06-13 21:34:36 +02:00
}
}
probe_submit(&plugin, "proc.rrd", DS_PROC, "%llu:%llu:%llu",
proc.intr, proc.ctxt, proc.fork);
fclose(fp);
2006-10-07 20:37:30 +02:00
return 0;
}
2007-04-01 15:23:09 +02:00
static int init(void)
{
buffer = malloc(BUFSIZE);
return (buffer == NULL) ? -1 : 0;
}
static int fini(void)
{
free(buffer);
return 0;
}
2006-06-13 21:34:36 +02:00
struct sammler_plugin plugin = {
.name = "stat",
.interval = 10,
2007-04-01 15:23:09 +02:00
.init = &init,
.fini = &fini,
2006-06-13 21:34:36 +02:00
.probe = &probe,
.get_ds = &get_ds,
};