From 78bad7b5d52407128d5069cb4003f7d6d0d10dd1 Mon Sep 17 00:00:00 2001
From: Maxim Mamontov <faust.madf@gmail.com>
Date: Fri, 10 Jun 2011 10:52:43 +0300
Subject: [PATCH] Data sensors added

---
 .../stargazer/plugins/other/snmp/Makefile     |   1 +
 .../stargazer/plugins/other/snmp/sensors.cpp  | 194 ++++++++++++++++++
 .../stargazer/plugins/other/snmp/sensors.h    | 172 ++++++++++++++++
 3 files changed, 367 insertions(+)
 create mode 100644 projects/stargazer/plugins/other/snmp/sensors.cpp
 create mode 100644 projects/stargazer/plugins/other/snmp/sensors.h

diff --git a/projects/stargazer/plugins/other/snmp/Makefile b/projects/stargazer/plugins/other/snmp/Makefile
index 047c29ec..d4339824 100644
--- a/projects/stargazer/plugins/other/snmp/Makefile
+++ b/projects/stargazer/plugins/other/snmp/Makefile
@@ -9,6 +9,7 @@ LIBS += $(LIB_THREAD)
 PROG = mod_snmp_agent.so
 
 SRCS =  snmp.cpp \
+	sensors.cpp \
 	asn1/DisplayString.c	\
 	asn1/PhysAddress.c	\
 	asn1/IfEntry.c	\
diff --git a/projects/stargazer/plugins/other/snmp/sensors.cpp b/projects/stargazer/plugins/other/snmp/sensors.cpp
new file mode 100644
index 00000000..a9f8efc7
--- /dev/null
+++ b/projects/stargazer/plugins/other/snmp/sensors.cpp
@@ -0,0 +1,194 @@
+#include "asn1/INTEGER.h"
+
+#include "stg/user.h"
+#include "stg/user_property.h"
+
+#include "sensors.h"
+
+void Int2OS(ObjectSyntax_t * dst, long src)
+{
+dst->present = ObjectSyntax_PR_simple;
+SimpleSyntax_t * simpleSyntax = &dst->choice.simple;
+simpleSyntax->present = SimpleSyntax_PR_number;
+asn_long2INTEGER(&simpleSyntax->choice.number, src);
+}
+
+bool ConnectedUsersSensor::GetValue(ObjectSyntax_t * objectSyntax)
+{
+int handle = users.OpenSearch();
+if (!handle)
+    return false;
+
+USER_PTR user;
+size_t count = 0;
+while (!users.SearchNext(handle, &user))
+    {
+    if (user->GetConnected())
+        ++count;
+    }
+
+users.CloseSearch(handle);
+
+Int2OS(objectSyntax, count);
+return true;
+}
+
+bool AuthorizedUsersSensor::GetValue(ObjectSyntax_t * objectSyntax)
+{
+int handle = users.OpenSearch();
+if (!handle)
+    return false;
+
+USER_PTR user;
+size_t count = 0;
+while (!users.SearchNext(handle, &user))
+    {
+    if (user->GetAuthorized())
+        ++count;
+    }
+
+users.CloseSearch(handle);
+
+Int2OS(objectSyntax, count);
+return true;
+}
+
+bool AlwaysOnlineUsersSensor::GetValue(ObjectSyntax_t * objectSyntax)
+{
+int handle = users.OpenSearch();
+if (!handle)
+    return false;
+
+USER_PTR user;
+size_t count = 0;
+while (!users.SearchNext(handle, &user))
+    {
+    if (user->GetProperty().alwaysOnline)
+        ++count;
+    }
+
+users.CloseSearch(handle);
+
+Int2OS(objectSyntax, count);
+return true;
+}
+
+bool NoCashUsersSensor::GetValue(ObjectSyntax_t * objectSyntax)
+{
+int handle = users.OpenSearch();
+if (!handle)
+    return false;
+
+USER_PTR user;
+size_t count = 0;
+while (!users.SearchNext(handle, &user))
+    {
+    if (user->GetProperty().cash < 0)
+        ++count;
+    }
+
+users.CloseSearch(handle);
+
+Int2OS(objectSyntax, count);
+return true;
+}
+
+bool DisabledDetailStatsUsersSensor::GetValue(ObjectSyntax_t * objectSyntax)
+{
+int handle = users.OpenSearch();
+if (!handle)
+    return false;
+
+USER_PTR user;
+size_t count = 0;
+while (!users.SearchNext(handle, &user))
+    {
+    if (user->GetProperty().disabledDetailStat)
+        ++count;
+    }
+
+users.CloseSearch(handle);
+
+Int2OS(objectSyntax, count);
+return true;
+}
+
+bool DisabledUsersSensor::GetValue(ObjectSyntax_t * objectSyntax)
+{
+int handle = users.OpenSearch();
+if (!handle)
+    return false;
+
+USER_PTR user;
+size_t count = 0;
+while (!users.SearchNext(handle, &user))
+    {
+    if (user->GetProperty().disabled)
+        ++count;
+    }
+
+users.CloseSearch(handle);
+
+Int2OS(objectSyntax, count);
+return true;
+}
+
+bool PassiveUsersSensor::GetValue(ObjectSyntax_t * objectSyntax)
+{
+int handle = users.OpenSearch();
+if (!handle)
+    return false;
+
+USER_PTR user;
+size_t count = 0;
+while (!users.SearchNext(handle, &user))
+    {
+    if (user->GetProperty().passive)
+        ++count;
+    }
+
+users.CloseSearch(handle);
+
+Int2OS(objectSyntax, count);
+return true;
+}
+
+bool CreditUsersSensor::GetValue(ObjectSyntax_t * objectSyntax)
+{
+int handle = users.OpenSearch();
+if (!handle)
+    return false;
+
+USER_PTR user;
+size_t count = 0;
+while (!users.SearchNext(handle, &user))
+    {
+    if (user->GetProperty().credit > 0)
+        ++count;
+    }
+
+users.CloseSearch(handle);
+
+Int2OS(objectSyntax, count);
+return true;
+}
+
+bool FreeMbUsersSensor::GetValue(ObjectSyntax_t * objectSyntax)
+{
+int handle = users.OpenSearch();
+if (!handle)
+    return false;
+
+USER_PTR user;
+size_t count = 0;
+while (!users.SearchNext(handle, &user))
+    {
+    if (user->GetProperty().freeMb > 0)
+        ++count;
+    }
+
+users.CloseSearch(handle);
+
+Int2OS(objectSyntax, count);
+return true;
+}
diff --git a/projects/stargazer/plugins/other/snmp/sensors.h b/projects/stargazer/plugins/other/snmp/sensors.h
new file mode 100644
index 00000000..49a08302
--- /dev/null
+++ b/projects/stargazer/plugins/other/snmp/sensors.h
@@ -0,0 +1,172 @@
+#ifndef __SENSORS_H__
+#define __SENSORS_H__
+
+#include <string>
+#include <map>
+
+#include "stg/users.h"
+#include "stg/tariffs.h"
+
+#include "asn1/ObjectSyntax.h"
+
+class Sensor {
+    public:
+        virtual bool GetValue(ObjectSyntax_t * objectSyntax) = 0;
+};
+
+typedef std::map<std::string, Sensor *> Sensors;
+
+void Int2OS(ObjectSyntax_t * dst, long src);
+
+class TotalUsersSensor : public Sensor {
+    public:
+        TotalUsersSensor(const USERS & u)
+            : users(u)
+        {}
+        virtual ~TotalUsersSensor() {}
+
+        bool GetValue(ObjectSyntax_t * objectSyntax)
+        {
+        Int2OS(objectSyntax, users.GetUserNum());
+        return true;
+        }
+
+    private:
+        const USERS & users;
+};
+
+class ConnectedUsersSensor : public Sensor {
+    public:
+        ConnectedUsersSensor(USERS & u)
+            : users(u)
+        {}
+        virtual ~ConnectedUsersSensor() {}
+
+        bool GetValue(ObjectSyntax_t * objectSyntax);
+
+    private:
+        USERS & users;
+};
+
+class AuthorizedUsersSensor : public Sensor {
+    public:
+        AuthorizedUsersSensor(USERS & u)
+            : users(u)
+        {}
+        virtual ~AuthorizedUsersSensor() {}
+
+        bool GetValue(ObjectSyntax_t * objectSyntax);
+
+    private:
+        USERS & users;
+};
+
+class AlwaysOnlineUsersSensor : public Sensor {
+    public:
+        AlwaysOnlineUsersSensor(USERS & u)
+            : users(u)
+        {}
+        virtual ~AlwaysOnlineUsersSensor() {}
+
+        bool GetValue(ObjectSyntax_t * objectSyntax);
+
+    private:
+        USERS & users;
+};
+
+class NoCashUsersSensor : public Sensor {
+    public:
+        NoCashUsersSensor(USERS & u)
+            : users(u)
+        {}
+        virtual ~NoCashUsersSensor() {}
+
+        bool GetValue(ObjectSyntax_t * objectSyntax);
+
+    private:
+        USERS & users;
+};
+
+class DisabledDetailStatsUsersSensor : public Sensor {
+    public:
+        DisabledDetailStatsUsersSensor(USERS & u)
+            : users(u)
+        {}
+        virtual ~DisabledDetailStatsUsersSensor() {}
+
+        bool GetValue(ObjectSyntax_t * objectSyntax);
+
+    private:
+        USERS & users;
+};
+
+class DisabledUsersSensor : public Sensor {
+    public:
+        DisabledUsersSensor(USERS & u)
+            : users(u)
+        {}
+        virtual ~DisabledUsersSensor() {}
+
+        bool GetValue(ObjectSyntax_t * objectSyntax);
+
+    private:
+        USERS & users;
+};
+
+class PassiveUsersSensor : public Sensor {
+    public:
+        PassiveUsersSensor(USERS & u)
+            : users(u)
+        {}
+        virtual ~PassiveUsersSensor() {}
+
+        bool GetValue(ObjectSyntax_t * objectSyntax);
+
+    private:
+        USERS & users;
+};
+
+class CreditUsersSensor : public Sensor {
+    public:
+        CreditUsersSensor(USERS & u)
+            : users(u)
+        {}
+        virtual ~CreditUsersSensor() {}
+
+        bool GetValue(ObjectSyntax_t * objectSyntax);
+
+    private:
+        USERS & users;
+};
+
+class FreeMbUsersSensor : public Sensor {
+    public:
+        FreeMbUsersSensor(USERS & u)
+            : users(u)
+        {}
+        virtual ~FreeMbUsersSensor() {}
+
+        bool GetValue(ObjectSyntax_t * objectSyntax);
+
+    private:
+        USERS & users;
+};
+
+class TotalTariffsSensor : public Sensor {
+    public:
+        TotalTariffsSensor(const TARIFFS & t)
+            : tariffs(t)
+        {}
+        virtual ~TotalTariffsSensor() {}
+
+        bool GetValue(ObjectSyntax_t * objectSyntax)
+        {
+        Int2OS(objectSyntax, tariffs.GetTariffsNum());
+        return true;
+        }
+
+    private:
+        const TARIFFS & tariffs;
+};
+
+#endif
-- 
2.44.2