Browse Source

simple plugins: use fscanf

master
Olaf Rempel 15 years ago
parent
commit
e476640b74
  1. 20
      plugins/load.c
  2. 20
      plugins/random.c
  3. 20
      plugins/uptime.c

20
plugins/load.c

@ -20,7 +20,6 @@
#include <stdio.h>
#include <stdlib.h>
#include "helper.h"
#include "logging.h"
#include "plugins.h"
#include "probe.h"
@ -40,28 +39,21 @@ static const char * get_ds(int ds_id)
static int probe(void)
{
FILE *fp;
char buffer[32];
char *val[3];
fp = fopen("/proc/loadavg", "r");
FILE *fp = fopen("/proc/loadavg", "r");
if (fp == NULL) {
log_print(LOG_WARN, "plugin load");
log_print(LOG_WARN, "plugin load: fopen()");
return -1;
}
if (fgets(buffer, sizeof(buffer), fp) == NULL) {
log_print(LOG_WARN, "plugin load");
double load1, load5, load15;
if (fscanf(fp, "%lf %lf %lf", &load1, &load5, &load15) != 3) {
log_print(LOG_WARN, "plugin load: fscanf()");
fclose(fp);
return -1;
}
fclose(fp);
if (strsplit(buffer, " \t\n", val, 3) != 3)
return -1;
probe_submit(&plugin, "load.rrd", 0, "%s:%s:%s", val[0], val[1], val[2]);
probe_submit(&plugin, "load.rrd", 0, "%.02lf:%.02lf:%.02lf", load1, load5, load15);
return 0;
}

20
plugins/random.c

@ -20,7 +20,6 @@
#include <stdio.h>
#include <stdlib.h>
#include "helper.h"
#include "logging.h"
#include "plugins.h"
#include "probe.h"
@ -38,28 +37,21 @@ static const char * get_ds(int ds_id)
static int probe(void)
{
FILE *fp;
char buffer[32];
char *val[1];
fp = fopen("/proc/sys/kernel/random/entropy_avail", "r");
FILE *fp = fopen("/proc/sys/kernel/random/entropy_avail", "r");
if (fp == NULL) {
log_print(LOG_WARN, "plugin random");
log_print(LOG_WARN, "plugin random: fopen()");
return -1;
}
if (fgets(buffer, sizeof(buffer), fp) == NULL) {
log_print(LOG_WARN, "plugin random");
int entropy;
if (fscanf(fp, "%d", &entropy) != 1) {
log_print(LOG_WARN, "plugin random: fopen()");
fclose(fp);
return -1;
}
fclose(fp);
if (strsplit(buffer, " \t\n", val, 1) != 1)
return -1;
probe_submit(&plugin, "random.rrd", 0, "%s", val[0]);
probe_submit(&plugin, "random.rrd", 0, "%d", entropy);
return 0;
}

20
plugins/uptime.c

@ -20,7 +20,6 @@
#include <stdio.h>
#include <stdlib.h>
#include "helper.h"
#include "logging.h"
#include "plugins.h"
#include "probe.h"
@ -39,28 +38,21 @@ static const char * get_ds(int ds_id)
static int probe(void)
{
FILE *fp;
char buffer[32];
char *val[2];
fp = fopen("/proc/uptime", "r");
FILE *fp = fopen("/proc/uptime", "r");
if (fp == NULL) {
log_print(LOG_WARN, "plugin uptime");
log_print(LOG_WARN, "plugin uptime: fopen()");
return -1;
}
if (fgets(buffer, sizeof(buffer), fp) == NULL) {
log_print(LOG_WARN, "plugin uptime");
double uptime, idletime;
if (fscanf(fp, "%lf %lf\n", &uptime, &idletime) != 2) {
log_print(LOG_WARN, "plugin uptime: fscanf()");
fclose(fp);
return -1;
}
fclose(fp);
if (strsplit(buffer, " \t\n", val, 2) != 2)
return -1;
probe_submit(&plugin, "uptime.rrd", 0, "%s:%s", val[0], val[1]);
probe_submit(&plugin, "uptime.rrd", 0, "%.02lf:%.02lf", uptime, idletime);
return 0;
}

Loading…
Cancel
Save