diff options
author | reinelt <reinelt@3ae390bd-cb1e-0410-b409-cd5a39f66f1f> | 2004-01-16 07:26:25 +0000 |
---|---|---|
committer | reinelt <reinelt@3ae390bd-cb1e-0410-b409-cd5a39f66f1f> | 2004-01-16 07:26:25 +0000 |
commit | 193ebdb96e9608291a84af0958993da091fa3558 (patch) | |
tree | 90bdb480a89d1bfa29f2296d363b93b13e62b39f | |
parent | 5c148f2a8e46983d905053d3e09d67ef6948ccf4 (diff) | |
download | lcd4linux-193ebdb96e9608291a84af0958993da091fa3558.tar.gz |
[lcd4linux @ 2004-01-16 07:26:25 by reinelt]
moved various /proc parsing to own functions
made some progress with /proc/stat parsing
git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@315 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
-rw-r--r-- | hash.c | 93 | ||||
-rw-r--r-- | hash.h | 16 | ||||
-rw-r--r-- | plugin_cpuinfo.c | 90 | ||||
-rw-r--r-- | plugin_meminfo.c | 99 | ||||
-rw-r--r-- | plugin_proc_stat.c | 44 |
5 files changed, 146 insertions, 196 deletions
@@ -1,4 +1,4 @@ -/* $Id: hash.c,v 1.2 2004/01/16 05:04:53 reinelt Exp $ +/* $Id: hash.c,v 1.3 2004/01/16 07:26:25 reinelt Exp $ * * hashes (associative arrays) * @@ -23,6 +23,10 @@ * * * $Log: hash.c,v $ + * Revision 1.3 2004/01/16 07:26:25 reinelt + * moved various /proc parsing to own functions + * made some progress with /proc/stat parsing + * * Revision 1.2 2004/01/16 05:04:53 reinelt * started plugin proc_stat which should parse /proc/stat * which again is a paint in the a** @@ -62,16 +66,6 @@ static int hash_lookup_f (const void *a, const void *b) } -// bsearch compare function for filter entries -static int filter_lookup_f (const void *a, const void *b) -{ - char *key=(char*)a; - FILTER_ITEM *item=(FILTER_ITEM*)b; - - return strcmp(key, item->key); -} - - // qsort compare function for hash tables static int hash_sort_f (const void *a, const void *b) { @@ -82,17 +76,6 @@ static int hash_sort_f (const void *a, const void *b) } -// qsort compare function for filter tables -static int filter_sort_f (const void *a, const void *b) -{ - FILTER_ITEM *ha=(FILTER_ITEM*)a; - FILTER_ITEM *hb=(FILTER_ITEM*)b; - - return strcasecmp(ha->key, hb->key); -} - - - // insert a key/val pair into the hash table // the tbale will be searched linearly if the entry // does already exist, for the table may not be sorted. @@ -155,69 +138,3 @@ void hash_destroy (HASH *Hash) Hash->Items=NULL; } - -// insert a key/val pair into the filter table -// the tbale will be searched linearly if the entry -// does already exist, for the table may not be sorted. -// the table will be flagged 'unsorted' afterwards -void filter_set (FILTER *Filter, char *key, char *val) -{ - int i; - - // entry already exists? - for (i=0;i<Filter->nItems; i++) { - if (strcmp(key, Filter->Items[i].key)==0) { - // if (Filter->Items[i].val) free (Filter->Items[i].val); - // Filter->Items[i].val=strdup(val); - return; - } - } - - // add entry - Filter->sorted=0; - Filter->nItems++; - Filter->Items=realloc(Filter->Items,Filter->nItems*sizeof(FILTER_ITEM)); - Filter->Items[i].key=strdup(key); - - - Filter->Items[i].Slots=strdup(val); -} - - -double filter_get (FILTER *Filter, char *key) -{ - FILTER_ITEM *Item; - - if (!Filter->sorted) { - qsort(Filter->Items, Filter->nItems, sizeof(FILTER_ITEM), filter_sort_f); - Filter->sorted=1; - } - - Item=bsearch(key, Filter->Items, Filter->nItems, sizeof(FILTER_ITEM), filter_lookup_f); - if (Item==NULL) return 0.0; - if (Item->Slots==NULL) return 0.0; - return Item->Slots[0].val; - -} - - -void filter_destroy (FILTER *Filter) -{ - int i; - - if (Filter->Items) { - - // free all entries - for (i=0;i<Filter->nItems; i++) { - if (Filter->Items[i].key) free (Filter->Items[i].key); - if (Filter->Items[i].Slots) free (Filter->Items[i].Slots); - } - - // free filter table - free (Filter->Items); - } - - Filter->nItems=0; - Filter->sorted=0; - Filter->Items=NULL; -} @@ -1,4 +1,4 @@ -/* $Id: hash.h,v 1.2 2004/01/16 05:04:53 reinelt Exp $ +/* $Id: hash.h,v 1.3 2004/01/16 07:26:25 reinelt Exp $ * * hashes (associative arrays) * @@ -23,6 +23,10 @@ * * * $Log: hash.h,v $ + * Revision 1.3 2004/01/16 07:26:25 reinelt + * moved various /proc parsing to own functions + * made some progress with /proc/stat parsing + * * Revision 1.2 2004/01/16 05:04:53 reinelt * started plugin proc_stat which should parse /proc/stat * which again is a paint in the a** @@ -62,18 +66,8 @@ typedef struct { FILTER_SLOT *Slots; } FILTER_ITEM; -typedef struct { - int sorted; - int nItems; - FILTER_ITEM *Items; -} FILTER; - void hash_set (HASH *Hash, char *key, char *val); char *hash_get (HASH *Hash, char *key); void hash_destroy (HASH *Hash); -void filter_set (FILTER *Filter, char *key, double val); -double filter_get (FILTER *Filter, char *key); -void filter_destroy (FILTER *Filter); - #endif diff --git a/plugin_cpuinfo.c b/plugin_cpuinfo.c index e9d4036..efa5d4a 100644 --- a/plugin_cpuinfo.c +++ b/plugin_cpuinfo.c @@ -1,4 +1,4 @@ -/* $Id: plugin_cpuinfo.c,v 1.3 2004/01/15 04:29:45 reinelt Exp $ +/* $Id: plugin_cpuinfo.c,v 1.4 2004/01/16 07:26:25 reinelt Exp $ * * plugin for /proc/cpuinfo parsing * @@ -23,6 +23,10 @@ * * * $Log: plugin_cpuinfo.c,v $ + * Revision 1.4 2004/01/16 07:26:25 reinelt + * moved various /proc parsing to own functions + * made some progress with /proc/stat parsing + * * Revision 1.3 2004/01/15 04:29:45 reinelt * moved lcd4linux.conf.sample to *.old * lcd4linux.conf.sample with new layout @@ -61,56 +65,64 @@ #include "hash.h" -static void my_cpuinfo (RESULT *result, RESULT *arg1) +static HASH CPUinfo = { 0, 0, NULL }; + + +static int parse_cpuinfo (void) { - static HASH CPUinfo = { 0, 0, NULL }; - static time_t now=0; - char *key, *val; - + FILE *stream; + // reread every second only - if (time(NULL)!=now) { - FILE *stream; + if (time(NULL)==now) return 0; + time(&now); + - // destroy previous hash table - hash_destroy (&CPUinfo); - - stream=fopen("/proc/cpuinfo", "r"); - if (stream==NULL) { - error ("fopen(/proc/cpuinfo) failed: %s", strerror(errno)); - SetResult(&result, R_STRING, ""); - return; - } + stream=fopen("/proc/cpuinfo", "r"); + if (stream==NULL) { + error ("fopen(/proc/cpuinfo) failed: %s", strerror(errno)); + return -1; + } - while (!feof(stream)) { - char buffer[256]; - char *c; - fgets (buffer, sizeof(buffer), stream); - c=strchr(buffer, ':'); - if (c==NULL) continue; - key=buffer; val=c+1; - // strip leading blanks from key - while (isspace(*key)) *key++='\0'; - // strip trailing blanks from key - do *c='\0'; while (isspace(*--c)); - // strip leading blanks from value - while (isspace(*val)) *val++='\0'; - // strip trailing blanks from value - for (c=val; *c!='\0';c++); - while (isspace(*--c)) *c='\0'; + while (!feof(stream)) { + char buffer[256]; + char *c, *key, *val; + fgets (buffer, sizeof(buffer), stream); + c=strchr(buffer, ':'); + if (c==NULL) continue; + key=buffer; val=c+1; + // strip leading blanks from key + while (isspace(*key)) *key++='\0'; + // strip trailing blanks from key + do *c='\0'; while (isspace(*--c)); + // strip leading blanks from value + while (isspace(*val)) *val++='\0'; + // strip trailing blanks from value + for (c=val; *c!='\0';c++); + while (isspace(*--c)) *c='\0'; - // add entry to hash table - hash_set (&CPUinfo, key, val); + // add entry to hash table + hash_set (&CPUinfo, key, val); - } - fclose (stream); - time(&now); + } + fclose (stream); + return 0; +} + + +static void my_cpuinfo (RESULT *result, RESULT *arg1) +{ + char *key, *val; + + if (parse_cpuinfo()<0) { + SetResult(&result, R_STRING, ""); + return; } key=R2S(arg1); val=hash_get(&CPUinfo, key); if (val==NULL) val=""; - + SetResult(&result, R_STRING, val); } diff --git a/plugin_meminfo.c b/plugin_meminfo.c index 6115515..ec8e611 100644 --- a/plugin_meminfo.c +++ b/plugin_meminfo.c @@ -1,4 +1,4 @@ -/* $Id: plugin_meminfo.c,v 1.1 2004/01/15 04:29:45 reinelt Exp $ +/* $Id: plugin_meminfo.c,v 1.2 2004/01/16 07:26:25 reinelt Exp $ * * plugin for /proc/meminfo parsing * @@ -23,6 +23,10 @@ * * * $Log: plugin_meminfo.c,v $ + * Revision 1.2 2004/01/16 07:26:25 reinelt + * moved various /proc parsing to own functions + * made some progress with /proc/stat parsing + * * Revision 1.1 2004/01/15 04:29:45 reinelt * moved lcd4linux.conf.sample to *.old * lcd4linux.conf.sample with new layout @@ -49,64 +53,71 @@ #include "debug.h" #include "plugin.h" + #include "hash.h" -static void my_meminfo (RESULT *result, RESULT *arg1) +static HASH MemInfo = { 0, 0, NULL }; + + +static int parse_meminfo (void) { - static HASH MemInfo = { 0, 0, NULL }; - static time_t now=0; - char *key, *val; + FILE *stream; int line; // reread every second only - if (time(NULL)!=now) { - FILE *stream; - - // destroy previous hash table - hash_destroy (&MemInfo); - - stream=fopen("/proc/meminfo", "r"); - if (stream==NULL) { - error ("fopen(/proc/meminfo) failed: %s", strerror(errno)); - SetResult(&result, R_STRING, ""); - return; - } + if (time(NULL)==now) return 0; + time(&now); + + stream=fopen("/proc/meminfo", "r"); + if (stream==NULL) { + error ("fopen(/proc/meminfo) failed: %s", strerror(errno)); + return -1; + } - line=0; - while (!feof(stream)) { - char buffer[256]; - char *c; - fgets (buffer, sizeof(buffer), stream); - c=strchr(buffer, ':'); - if (c==NULL) continue; - key=buffer; val=c+1; - // strip leading blanks from key - while (isspace(*key)) *key++='\0'; - // strip trailing blanks from key - do *c='\0'; while (isspace(*--c)); - // strip leading blanks from value - while (isspace(*val)) *val++='\0'; - // strip trailing blanks from value - for (c=val; *c!='\0';c++); - while (isspace(*--c)) *c='\0'; - // skip lines that do not end with " kB" - if (*c=='B' && *(c-1)=='k' && *(c-2)==' ') { - // strip trailing " kB" from value - *(c-2)='\0'; - // add entry to hash table - hash_set (&MemInfo, key, val); - } + line=0; + while (!feof(stream)) { + char buffer[256]; + char *c, *key, *val; + fgets (buffer, sizeof(buffer), stream); + c=strchr(buffer, ':'); + if (c==NULL) continue; + key=buffer; val=c+1; + // strip leading blanks from key + while (isspace(*key)) *key++='\0'; + // strip trailing blanks from key + do *c='\0'; while (isspace(*--c)); + // strip leading blanks from value + while (isspace(*val)) *val++='\0'; + // strip trailing blanks from value + for (c=val; *c!='\0';c++); + while (isspace(*--c)) *c='\0'; + // skip lines that do not end with " kB" + if (*c=='B' && *(c-1)=='k' && *(c-2)==' ') { + // strip trailing " kB" from value + *(c-2)='\0'; + // add entry to hash table + hash_set (&MemInfo, key, val); } - fclose (stream); - time(&now); + } + fclose (stream); + return 0; +} + +static void my_meminfo (RESULT *result, RESULT *arg1) +{ + char *key, *val; + + if (parse_meminfo()<0) { + SetResult(&result, R_STRING, ""); + return; } key=R2S(arg1); val=hash_get(&MemInfo, key); if (val==NULL) val=""; - + SetResult(&result, R_STRING, val); } diff --git a/plugin_proc_stat.c b/plugin_proc_stat.c index 1e7ec0b..e7b9bf8 100644 --- a/plugin_proc_stat.c +++ b/plugin_proc_stat.c @@ -1,4 +1,4 @@ -/* $Id: plugin_proc_stat.c,v 1.1 2004/01/16 05:04:53 reinelt Exp $ +/* $Id: plugin_proc_stat.c,v 1.2 2004/01/16 07:26:25 reinelt Exp $ * * plugin for /proc/stat parsing * @@ -23,6 +23,10 @@ * * * $Log: plugin_proc_stat.c,v $ + * Revision 1.2 2004/01/16 07:26:25 reinelt + * moved various /proc parsing to own functions + * made some progress with /proc/stat parsing + * * Revision 1.1 2004/01/16 05:04:53 reinelt * started plugin proc_stat which should parse /proc/stat * which again is a paint in the a** @@ -75,6 +79,16 @@ static int renew(int msec) } +static void my_hash_set (char *key1, char *key2, char *val) +{ + char key[16]; + + snprintf (key, sizeof(key), "%s.%s", key1, key2); + debug ("Michi: hash_set(%s, %s)", key, val); + hash_set (&Stat, key, val); +} + + static int parse_proc_stat (void) { FILE *stream; @@ -82,9 +96,6 @@ static int parse_proc_stat (void) // update every 10 msec if (!renew(10)) return 0; - // destroy previous hash table - hash_destroy (&Stat); - stream=fopen("/proc/stat", "r"); if (stream==NULL) { error ("fopen(/proc/stat) failed: %s", strerror(errno)); @@ -94,13 +105,14 @@ static int parse_proc_stat (void) while (!feof(stream)) { char buffer[256]; fgets (buffer, sizeof(buffer), stream); - + if (strncmp(buffer, "cpu", 3)==0) { - unsigned long v1, v2, v3, v4; - debug ("Michi: buffer=<%s>", buffer); - if (sscanf(buffer+4, " %lu %lu %lu %lu", &v1, &v2, &v3, &v4)==4) { - debug ("Michi: v1=<%ld> v2=<%ld> v3=<%ld> v4=<%ld>", v1, v2, v3, v4); - } + char *cpu; + cpu=strtok(buffer, " \t"); + my_hash_set (cpu, "user", strtok(NULL, " \t")); + my_hash_set (cpu, "nice", strtok(NULL, " \t")); + my_hash_set (cpu, "system", strtok(NULL, " \t")); + my_hash_set (cpu, "idle", strtok(NULL, " \t")); } else if (strncmp(buffer, "page ", 5)==0) { @@ -129,12 +141,16 @@ static void my_proc_stat (RESULT *result, RESULT *arg1) { char *key, *val; - parse_proc_stat(); + if (parse_proc_stat()<0) { + SetResult(&result, R_STRING, ""); + return; + } key=R2S(arg1); - val=""; - - SetResult(&result, R_STRING, ""); + val=hash_get(&Stat, key); + if (val==NULL) val=""; + + SetResult(&result, R_STRING, val); } |