mirror of
https://github.com/netfun2000/lcd4linux.git
synced 2026-02-27 09:44:34 +08:00
[lcd4linux @ 2004-01-21 14:29:03 by reinelt]
new helper 'hash_get_regex' which delivers the sum over regex matched items new function 'disk()' which uses this regex matching git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@333 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
/* $Id: hash.c,v 1.7 2004/01/21 10:48:17 reinelt Exp $
|
||||
/* $Id: hash.c,v 1.8 2004/01/21 14:29:03 reinelt Exp $
|
||||
*
|
||||
* hashes (associative arrays)
|
||||
*
|
||||
@@ -23,6 +23,10 @@
|
||||
*
|
||||
*
|
||||
* $Log: hash.c,v $
|
||||
* Revision 1.8 2004/01/21 14:29:03 reinelt
|
||||
* new helper 'hash_get_regex' which delivers the sum over regex matched items
|
||||
* new function 'disk()' which uses this regex matching
|
||||
*
|
||||
* Revision 1.7 2004/01/21 10:48:17 reinelt
|
||||
* hash_age function added
|
||||
*
|
||||
@@ -65,12 +69,14 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <regex.h>
|
||||
//#include <sys/types.h>
|
||||
|
||||
#include "debug.h"
|
||||
#include "hash.h"
|
||||
|
||||
|
||||
#define FILTER_SLOTS 64
|
||||
#define DELTA_SLOTS 64
|
||||
|
||||
|
||||
// bsearch compare function for hash entries
|
||||
@@ -177,22 +183,22 @@ void hash_set (HASH *Hash, char *key, char *val)
|
||||
|
||||
// insert a string into the hash table
|
||||
// convert it into a number, and store it in the
|
||||
// filter table, too
|
||||
void hash_set_filter (HASH *Hash, char *key, char *val)
|
||||
// delta table, too
|
||||
void hash_set_delta (HASH *Hash, char *key, char *val)
|
||||
{
|
||||
double number=atof(val);
|
||||
HASH_ITEM *Item;
|
||||
|
||||
Item=hash_set_string (Hash, key, val);
|
||||
|
||||
// allocate filter table
|
||||
// allocate delta table
|
||||
if (Item->Slot==NULL) {
|
||||
Item->Slot = malloc(FILTER_SLOTS*sizeof(HASH_SLOT));
|
||||
memset(Item->Slot, 0, FILTER_SLOTS*sizeof(HASH_SLOT));
|
||||
Item->Slot = malloc(DELTA_SLOTS*sizeof(HASH_SLOT));
|
||||
memset(Item->Slot, 0, DELTA_SLOTS*sizeof(HASH_SLOT));
|
||||
}
|
||||
|
||||
// shift filter table
|
||||
memmove (Item->Slot+1, Item->Slot, (FILTER_SLOTS-1)*sizeof(HASH_SLOT));
|
||||
// shift delta table
|
||||
memmove (Item->Slot+1, Item->Slot, (DELTA_SLOTS-1)*sizeof(HASH_SLOT));
|
||||
|
||||
// set first entry
|
||||
gettimeofday(&(Item->Slot[0].time), NULL);
|
||||
@@ -237,8 +243,8 @@ int hash_age (HASH *Hash, char *key, char **value)
|
||||
}
|
||||
|
||||
|
||||
// get a delta value from the filter table
|
||||
double hash_get_filter (HASH *Hash, char *key, int delay)
|
||||
// get a delta value from the delta table
|
||||
double hash_get_delta (HASH *Hash, char *key, int delay)
|
||||
{
|
||||
HASH_ITEM *Item;
|
||||
timeval now, end;
|
||||
@@ -250,6 +256,9 @@ double hash_get_filter (HASH *Hash, char *key, int delay)
|
||||
if (Item==NULL) return 0.0;
|
||||
if (Item->Slot==NULL) return 0.0;
|
||||
|
||||
// if delay is zero, return absolute value
|
||||
if (delay==0) return Item->Slot[0].val;
|
||||
|
||||
// prepare timing values
|
||||
now=Item->Slot[0].time;
|
||||
end.tv_sec = now.tv_sec;
|
||||
@@ -259,8 +268,8 @@ double hash_get_filter (HASH *Hash, char *key, int delay)
|
||||
end.tv_usec += 1000000;
|
||||
}
|
||||
|
||||
// search filter slot
|
||||
for (i=1; i<FILTER_SLOTS; i++) {
|
||||
// search delta slot
|
||||
for (i=1; i<DELTA_SLOTS; i++) {
|
||||
if (Item->Slot[i].time.tv_sec==0) break;
|
||||
if (timercmp(&Item->Slot[i].time, &end, <)) break;
|
||||
dt = (now.tv_sec - Item->Slot[i].time.tv_sec) + (now.tv_usec - Item->Slot[i].time.tv_usec)/1000000.0;
|
||||
@@ -281,6 +290,38 @@ double hash_get_filter (HASH *Hash, char *key, int delay)
|
||||
}
|
||||
|
||||
|
||||
// get a delta value from the delta table
|
||||
// key may contain regular expressions, and the sum
|
||||
// of all matching entries is returned.
|
||||
double hash_get_regex (HASH *Hash, char *key, int delay)
|
||||
{
|
||||
double sum=0.0;
|
||||
regex_t preg;
|
||||
int i, err;
|
||||
|
||||
debug ("Michi: regex=<%s>", key);
|
||||
err=regcomp(&preg, key, REG_ICASE|REG_NOSUB);
|
||||
if (err!=0) {
|
||||
char buffer[32];
|
||||
regerror(err, &preg, buffer, sizeof(buffer));
|
||||
error ("error in regular expression: %s", buffer);
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
// force the table to be sorted by requesting anything
|
||||
hash_lookup(Hash, "", 1);
|
||||
|
||||
for (i=0;i<Hash->nItems; i++) {
|
||||
debug ("Michi: Testing <%s>", Hash->Items[i].key);
|
||||
if (regexec(&preg, Hash->Items[i].key, 0, NULL, 0)==0) {
|
||||
debug ("Michi: MATCHED <%s>", Hash->Items[i].key);
|
||||
sum+=hash_get_delta(Hash, Hash->Items[i].key, delay);
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
|
||||
void hash_destroy (HASH *Hash)
|
||||
{
|
||||
int i;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* $Id: hash.h,v 1.6 2004/01/21 10:48:17 reinelt Exp $
|
||||
/* $Id: hash.h,v 1.7 2004/01/21 14:29:03 reinelt Exp $
|
||||
*
|
||||
* hashes (associative arrays)
|
||||
*
|
||||
@@ -23,6 +23,10 @@
|
||||
*
|
||||
*
|
||||
* $Log: hash.h,v $
|
||||
* Revision 1.7 2004/01/21 14:29:03 reinelt
|
||||
* new helper 'hash_get_regex' which delivers the sum over regex matched items
|
||||
* new function 'disk()' which uses this regex matching
|
||||
*
|
||||
* Revision 1.6 2004/01/21 10:48:17 reinelt
|
||||
* hash_age function added
|
||||
*
|
||||
@@ -80,11 +84,12 @@ typedef struct {
|
||||
} HASH;
|
||||
|
||||
|
||||
void hash_set (HASH *Hash, char *key, char *val);
|
||||
void hash_set_filter (HASH *Hash, char *key, char *val);
|
||||
int hash_age (HASH *Hash, char *key, char **value);
|
||||
char *hash_get (HASH *Hash, char *key);
|
||||
double hash_get_filter (HASH *Hash, char *key, int delay);
|
||||
void hash_destroy (HASH *Hash);
|
||||
void hash_set (HASH *Hash, char *key, char *val);
|
||||
void hash_set_delta (HASH *Hash, char *key, char *val);
|
||||
int hash_age (HASH *Hash, char *key, char **value);
|
||||
char *hash_get (HASH *Hash, char *key);
|
||||
double hash_get_delta (HASH *Hash, char *key, int delay);
|
||||
double hash_get_regex (HASH *Hash, char *key, int delay);
|
||||
void hash_destroy (HASH *Hash);
|
||||
|
||||
#endif
|
||||
|
||||
+35
-8
@@ -1,4 +1,4 @@
|
||||
/* $Id: plugin_proc_stat.c,v 1.8 2004/01/21 11:31:23 reinelt Exp $
|
||||
/* $Id: plugin_proc_stat.c,v 1.9 2004/01/21 14:29:03 reinelt Exp $
|
||||
*
|
||||
* plugin for /proc/stat parsing
|
||||
*
|
||||
@@ -23,6 +23,10 @@
|
||||
*
|
||||
*
|
||||
* $Log: plugin_proc_stat.c,v $
|
||||
* Revision 1.9 2004/01/21 14:29:03 reinelt
|
||||
* new helper 'hash_get_regex' which delivers the sum over regex matched items
|
||||
* new function 'disk()' which uses this regex matching
|
||||
*
|
||||
* Revision 1.8 2004/01/21 11:31:23 reinelt
|
||||
* two bugs with hash_age() ixed
|
||||
*
|
||||
@@ -79,7 +83,7 @@ static HASH Stat = { 0, };
|
||||
|
||||
static void hash_set1 (char *key1, char *val)
|
||||
{
|
||||
hash_set_filter (&Stat, key1, val);
|
||||
hash_set_delta (&Stat, key1, val);
|
||||
}
|
||||
|
||||
|
||||
@@ -198,7 +202,7 @@ static void my_proc_stat (RESULT *result, int argc, RESULT *argv[])
|
||||
SetResult(&result, R_STRING, string);
|
||||
break;
|
||||
case 2:
|
||||
number=hash_get_filter(&Stat, R2S(argv[0]), R2N(argv[1]));
|
||||
number=hash_get_delta(&Stat, R2S(argv[0]), R2N(argv[1]));
|
||||
SetResult(&result, R_NUMBER, &number);
|
||||
break;
|
||||
default:
|
||||
@@ -223,10 +227,10 @@ static void my_cpu (RESULT *result, RESULT *arg1, RESULT *arg2)
|
||||
key = R2S(arg1);
|
||||
delay = R2N(arg2);
|
||||
|
||||
cpu_user = hash_get_filter(&Stat, "cpu.user", delay);
|
||||
cpu_nice = hash_get_filter(&Stat, "cpu.nice", delay);
|
||||
cpu_system = hash_get_filter(&Stat, "cpu.system", delay);
|
||||
cpu_idle = hash_get_filter(&Stat, "cpu.idle", delay);
|
||||
cpu_user = hash_get_delta(&Stat, "cpu.user", delay);
|
||||
cpu_nice = hash_get_delta(&Stat, "cpu.nice", delay);
|
||||
cpu_system = hash_get_delta(&Stat, "cpu.system", delay);
|
||||
cpu_idle = hash_get_delta(&Stat, "cpu.idle", delay);
|
||||
|
||||
cpu_total = cpu_user+cpu_nice+cpu_system+cpu_idle;
|
||||
|
||||
@@ -245,9 +249,32 @@ static void my_cpu (RESULT *result, RESULT *arg1, RESULT *arg2)
|
||||
}
|
||||
|
||||
|
||||
static void my_disk (RESULT *result, RESULT *arg1, RESULT *arg2, RESULT *arg3)
|
||||
{
|
||||
char *dev, *key, buffer[32];
|
||||
int delay;
|
||||
double value;
|
||||
|
||||
if (parse_proc_stat()<0) {
|
||||
SetResult(&result, R_STRING, "");
|
||||
return;
|
||||
}
|
||||
|
||||
dev = R2S(arg1);
|
||||
key = R2S(arg2);
|
||||
delay = R2N(arg3);
|
||||
|
||||
snprintf (buffer, sizeof(buffer), "disk_io\\.%s\\.%s", dev, key);
|
||||
value = hash_get_regex(&Stat, buffer, delay);
|
||||
|
||||
SetResult(&result, R_NUMBER, &value);
|
||||
}
|
||||
|
||||
|
||||
int plugin_init_proc_stat (void)
|
||||
{
|
||||
AddFunction ("proc_stat", -1, my_proc_stat);
|
||||
AddFunction ("cpu", 2, my_cpu);
|
||||
AddFunction ("cpu", 2, my_cpu);
|
||||
AddFunction ("disk", 3, my_disk);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user