From 60c82cbf80234418205ddb7c8992a9afbab61d7d Mon Sep 17 00:00:00 2001
From: Adam Wujek <adam.wujek@cern.ch>
Date: Thu, 11 Jun 2015 11:31:03 +0200
Subject: [PATCH] userspace/snmpd: add wrsMemoryFreeLow to wrsOSStatusGroup

Notify with error or warning when mamory usage is too high.

Additionally:
--update MIB

Signed-off-by: Adam Wujek <adam.wujek@cern.ch>
---
 userspace/snmpd/WR-SWITCH-MIB.txt  | 15 +++++++++++++++
 userspace/snmpd/wrsOSStatusGroup.c | 31 +++++++++++++++++++++++++++++-
 userspace/snmpd/wrsOSStatusGroup.h |  8 ++++++++
 3 files changed, 53 insertions(+), 1 deletion(-)

diff --git a/userspace/snmpd/WR-SWITCH-MIB.txt b/userspace/snmpd/WR-SWITCH-MIB.txt
index 3242e9f1d..596e72bc8 100644
--- a/userspace/snmpd/WR-SWITCH-MIB.txt
+++ b/userspace/snmpd/WR-SWITCH-MIB.txt
@@ -172,6 +172,21 @@ wrsTemperatureWarning OBJECT-TYPE
             "Warning if temperature exceed threshold levels"
     ::= { wrsOSStatusGroup 2 }
 
+wrsMemoryFreeLow OBJECT-TYPE
+    SYNTAX         INTEGER {
+                        na(0),
+                        ok(1),
+                        error(2),
+                        warning(3),
+                        warningNA(4)
+    }
+    MAX-ACCESS     read-only
+    STATUS         current
+    DESCRIPTION
+            "Status of free memory. Based on wrsMemoryGroup.
+            Error - more than 80% memory used
+            Warning - more than 50% memory used"
+    ::= { wrsOSStatusGroup 3 }
 
 -- wrsTimingStatusGroup (.6.2.2)
 wrsTimingStatusGroup       OBJECT IDENTIFIER ::= { wrsDetailedStatusesGroup 2 }
diff --git a/userspace/snmpd/wrsOSStatusGroup.c b/userspace/snmpd/wrsOSStatusGroup.c
index 48c078fff..03dbe1060 100644
--- a/userspace/snmpd/wrsOSStatusGroup.c
+++ b/userspace/snmpd/wrsOSStatusGroup.c
@@ -1,11 +1,16 @@
 #include "wrsSnmp.h"
 #include "wrsBootStatusGroup.h"
 #include "wrsTemperatureGroup.h"
+#include "wrsMemoryGroup.h"
 #include "wrsOSStatusGroup.h"
 
+#define WRSMEMORYFREELOW_TRESHOLD_ERROR 80
+#define WRSMEMORYFREELOW_TRESHOLD_WARNING 50
+
 static struct pickinfo wrsOSStatus_pickinfo[] = {
 	FIELD(wrsOSStatus_s, ASN_INTEGER, wrsBootSuccessful),
 	FIELD(wrsOSStatus_s, ASN_INTEGER, wrsTemperatureWarning),
+	FIELD(wrsOSStatus_s, ASN_INTEGER, wrsMemoryFreeLow),
 };
 
 struct wrsOSStatus_s wrsOSStatus_s;
@@ -15,13 +20,17 @@ time_t wrsOSStatus_data_fill(void)
 	static time_t time_update; /* time of last update */
 	time_t time_temp; /* time when temperature data was updated */
 	time_t time_boot; /* time when boot data was updated */
+	time_t time_free_mem; /* time when free memory data was updated */
 	struct wrsBootStatus_s *b;
+	struct wrsMemory_s *f;
 
 	time_boot = wrsBootStatus_data_fill();
 	time_temp = wrsTemperature_data_fill();
+	time_free_mem = wrsMemory_data_fill();
 
 	if (time_boot <= time_update
-		&& time_temp <= time_update) {
+		&& time_temp <= time_update
+		&& time_free_mem <= time_update) {
 		/* cache not updated, return last update time */
 		return time_update;
 	}
@@ -127,6 +136,26 @@ time_t wrsOSStatus_data_fill(void)
 		    || (wrsTemperature_s.temp_psr > wrsTemperature_s.temp_psr_thold));
 	}
 
+	/*********************************************************************\
+	|************************* wrsMemoryFreeLow  *************************|
+	\*********************************************************************/
+	/* Check memory usage */
+	f = &wrsMemory_s;
+	if (f->wrsMemoryUsedPerc > WRSMEMORYFREELOW_TRESHOLD_ERROR) {
+		/* Memory usage above error threshold level */
+		wrsOSStatus_s.wrsMemoryFreeLow = WRS_MEMORY_FREE_LOW_ERROR;
+	} else if (f->wrsMemoryUsedPerc > WRSMEMORYFREELOW_TRESHOLD_WARNING) {
+		/* Memory usage above warning threshold level */
+		wrsOSStatus_s.wrsMemoryFreeLow = WRS_MEMORY_FREE_LOW_WARNING;
+	} else if (f->wrsMemoryTotal == 0) {
+		/* Problem with read memory size */
+		wrsOSStatus_s.wrsMemoryFreeLow =
+					WRS_MEMORY_FREE_LOW_WARNING_NA;
+	} else {
+		/* Memory usage below threshold levels */
+		wrsOSStatus_s.wrsMemoryFreeLow = WRS_MEMORY_FREE_LOW_OK;
+	}
+
 	/* there was an update, return current time */
 	return time_update;
 }
diff --git a/userspace/snmpd/wrsOSStatusGroup.h b/userspace/snmpd/wrsOSStatusGroup.h
index cc3900040..c40734115 100644
--- a/userspace/snmpd/wrsOSStatusGroup.h
+++ b/userspace/snmpd/wrsOSStatusGroup.h
@@ -15,9 +15,17 @@
 #define WRS_TEMPERATURE_WARNING_OK 2			/* ok */
 #define WRS_TEMPERATURE_WARNING_TOO_HIGH 3		/* warning */
 
+#define WRS_MEMORY_FREE_LOW_OK 1			/* ok */
+#define WRS_MEMORY_FREE_LOW_ERROR 2			/* error */
+#define WRS_MEMORY_FREE_LOW_WARNING 3			/* warning */
+#define WRS_MEMORY_FREE_LOW_WARNING_NA 4 /* warning, at least one field is
+					  * equal to 0 (NA),shouldn't happen in
+					  * normal operation */
+
 struct wrsOSStatus_s {
 	int wrsBootSuccessful;
 	int wrsTemperatureWarning;
+	int wrsMemoryFreeLow;
 };
 
 extern struct wrsOSStatus_s wrsOSStatus_s;
-- 
GitLab