Commit 5255a83c authored by Adam Wujek's avatar Adam Wujek 💬

userspace/snmpd: add new wrsMemoryGroup

Add new wrsMemoryGroup with objects:
-- wrsMemoryTotal
-- wrsMemoryUsed
-- wrsMemoryUsedPerc (% of used memory)
-- wrsMemoryFree

Additionally:
-- update of MIB
-- update Makefile
-- update init.c

NOTE: I tried to implement these objects with sysinfo function. However,
sysinfo doesn't provide information about amount cached memory, which is
included in used memory.
Signed-off-by: Adam Wujek's avatarAdam Wujek <adam.wujek@cern.ch>
parent bae18a50
......@@ -38,6 +38,7 @@ SOURCES = \
wrsCurrentTimeGroup.c \
wrsBootStatusGroup.c \
wrsTemperatureGroup.c \
wrsMemoryGroup.c \
wrsStartCntGroup.c \
wrsSpllVersionGroup.c \
wrsSpllStatusGroup.c \
......
......@@ -722,6 +722,41 @@ wrsTempThresholdPSR OBJECT-TYPE
"Threshold level for Power Supply Right (PSR) temperature"
::= { wrsTemperatureGroup 8 }
-- wrsMemoryGroup (.7.1.4)
wrsMemoryGroup OBJECT IDENTIFIER ::= { wrsOperationStatus 4 }
wrsMemoryTotal OBJECT-TYPE
SYNTAX Integer32
MAX-ACCESS read-only
STATUS current
DESCRIPTION
"Ram size in kB"
::= { wrsMemoryGroup 1 }
wrsMemoryUsed OBJECT-TYPE
SYNTAX Integer32
MAX-ACCESS read-only
STATUS current
DESCRIPTION
"Used RAM in kB (Cache and buffers are not counted)"
::= { wrsMemoryGroup 2 }
wrsMemoryUsedPerc OBJECT-TYPE
SYNTAX Integer32
MAX-ACCESS read-only
STATUS current
DESCRIPTION
"Percentage of used RAM"
::= { wrsMemoryGroup 3 }
wrsMemoryFree OBJECT-TYPE
SYNTAX Integer32
MAX-ACCESS read-only
STATUS current
DESCRIPTION
"Free RAM in kB"
::= { wrsMemoryGroup 4 }
wrsStartCntGroup OBJECT IDENTIFIER ::= { wrsExpertStatus 2 }
-- wrsStartCntGroup (.7.2)
......
......@@ -17,6 +17,7 @@
#include "wrsCurrentTimeGroup.h"
#include "wrsBootStatusGroup.h"
#include "wrsTemperatureGroup.h"
#include "wrsMemoryGroup.h"
#include "wrsStartCntGroup.h"
#include "wrsSpllVersionGroup.h"
#include "wrsSpllStatusGroup.h"
......@@ -46,6 +47,7 @@ void init_wrsSnmp(void)
init_wrsCurrentTimeGroup();
init_wrsBootStatusGroup();
init_wrsTemperatureGroup();
init_wrsMemoryGroup();
init_wrsStartCntGroup();
init_wrsSpllVersionGroup();
init_wrsSpllStatusGroup();
......
#include "wrsSnmp.h"
#include "wrsMemoryGroup.h"
#define MEMINFO_FILE "/proc/meminfo"
#define MEMINFO_ENTRIES 4 /* How many meminfo are interesting for us, used to
* speed up reading meminfo file */
static struct pickinfo wrsMemory_pickinfo[] = {
FIELD(wrsMemory_s, ASN_INTEGER, wrsMemoryTotal),
FIELD(wrsMemory_s, ASN_INTEGER, wrsMemoryUsed),
FIELD(wrsMemory_s, ASN_INTEGER, wrsMemoryUsedPerc),
FIELD(wrsMemory_s, ASN_INTEGER, wrsMemoryFree),
};
struct wrsMemory_s wrsMemory_s;
time_t wrsMemory_data_fill(void)
{
static time_t time_update;
time_t time_cur;
unsigned long value;
unsigned long mem_total = 0;
unsigned long mem_free = 0;
unsigned long mem_cached = 0;
unsigned long mem_buffers = 0;
int found = 0;
FILE *f;
int ret = 0;
char key[41]; /* 1 for null char */
time_cur = time(NULL);
if (time_update
&& time_cur - time_update < WRSMEMORY_CACHE_TIMEOUT) {
/* cache not updated, return last update time */
return time_update;
}
time_update = time_cur;
memset(&wrsMemory_s, 0, sizeof(wrsMemory_s));
f = fopen(MEMINFO_FILE, "r");
if (!f) {
snmp_log(LOG_ERR, "SNMP: wrsMemoryGroup filed to open "
MEMINFO_FILE"\n");
/* notify snmp about error in kernel modules */
return time_update;
}
while (ret != EOF && found < MEMINFO_ENTRIES) {
/* read first word from line (module name) ignore rest of
* the line */
ret = fscanf(f, "%40s %lu %*[^\n]", key, &value);
if (ret != 2)
continue; /* error... or EOF */
if (!strcmp(key, "MemTotal:")) {
mem_total = value;
found++;
} else if (!strcmp(key, "MemFree:")) {
mem_free = value;
found++;
} else if (!strcmp(key, "Buffers:")) {
mem_buffers = value;
found++;
} else if (!strcmp(key, "Cached:")) {
mem_cached = value;
found++;
}
}
if (found == MEMINFO_ENTRIES && mem_total > 0) { /* avoid div 0 */
wrsMemory_s.wrsMemoryTotal = (int) mem_total;
wrsMemory_s.wrsMemoryUsed = (int) (mem_total - mem_free
- mem_buffers - mem_cached);
wrsMemory_s.wrsMemoryUsedPerc = (int) ((mem_total - mem_free
- mem_buffers - mem_cached)
* 100 / mem_total);
wrsMemory_s.wrsMemoryFree = (int) (mem_free + mem_buffers
+ mem_cached);
} else { /* if not enough entries found */
snmp_log(LOG_ERR, "SNMP: wrsMemoryGroup error while reading "
"values from "MEMINFO_FILE"\n");
}
/* there was an update, return current time */
return time_update;
}
#define GT_OID WRSMEMORY_OID
#define GT_PICKINFO wrsMemory_pickinfo
#define GT_DATA_FILL_FUNC wrsMemory_data_fill
#define GT_DATA_STRUCT wrsMemory_s
#define GT_GROUP_NAME "wrsMemoryGroup"
#define GT_INIT_FUNC init_wrsMemoryGroup
#include "wrsGroupTemplate.h"
#ifndef WRS_MEMORY_GROUP_H
#define WRS_MEMORY_GROUP_H
#define WRSMEMORY_CACHE_TIMEOUT 5
#define WRSMEMORY_OID WRS_OID, 7, 1, 4
struct wrsMemory_s {
int wrsMemoryTotal;
int wrsMemoryUsed;
int wrsMemoryUsedPerc;
int wrsMemoryFree;
};
extern struct wrsMemory_s wrsMemory_s;
time_t wrsMemory_data_fill(void);
void init_wrsMemoryGroup(void);
#endif /* WRS_MEMORY_GROUP_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment