From: Anders Selhammer <anders.selham...@est.tech>

The sub agent use net-snmp library and AgentX protocol for the
communication towards the snmp master agent.
snmp4lptp.c should only include general setup needed for handling snmp.
Supported MIBs should be implemented in separate files.
Makefile will include snmp4lptp if libsnmp is available during compilation.
configs/snmpd.conf should be placed in /etc/snmp/

[ RC: re-worked libsnmp detection and build rules. ]

Signed-off-by: Anders Selhammer <anders.selham...@est.tech>
Signed-off-by: Richard Cochran <richardcoch...@gmail.com>
---
 configs/snmpd.conf | 26 +++++++++++++++++++++++++
 makefile           | 13 +++++++++++++
 snmp4lptp.c        | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 snmpflg.sh         | 42 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 138 insertions(+)
 create mode 100644 configs/snmpd.conf
 create mode 100644 snmp4lptp.c
 create mode 100755 snmpflg.sh

diff --git a/configs/snmpd.conf b/configs/snmpd.conf
new file mode 100644
index 0000000..8a0d929
--- /dev/null
+++ b/configs/snmpd.conf
@@ -0,0 +1,26 @@
+# Map 'linuxptp' community to the 'LinuxPtpUser'
+# Map 'public' community to the 'AllUser'
+#       sec.name        source          community
+com2sec LinuxPtpUser    default         linuxptp
+com2sec AllUser         default         public
+
+# Map 'LinuxPtpUser' to 'LinuxPtpGroup' for SNMP Version 2c
+# Map 'AllUser' to 'AllGroup' for SNMP Version 2c
+#                       sec.model       sec.name
+group   LinuxPtpGroup   v2c             LinuxPtpUser
+group   AllGroup        v2c             AllUser
+
+# Define 'SystemView', which includes everything under .1.3.6.1.2.1.241
+# Define 'AllView', which includes everything under .1
+#                       incl/excl       subtree
+view    SystemView      included        .1.3.6.1.2.1.241
+view    AllView         included        .1
+
+# Give 'ConfigGroup' read access to objects in the view 'SystemView'
+# Give 'AllGroup' read access to objects in the view 'AllView'
+#                       context model   level   prefix  read            write  
 notify
+access  LinuxPtpGroup   ""      any     noauth  exact   SystemView      none   
 none
+access  AllGroup        ""      any     noauth  exact   AllView         none   
 none
+
+# turn on the AgentX master agent support
+master agentx
diff --git a/makefile b/makefile
index 693e75d..b6a96d0 100644
--- a/makefile
+++ b/makefile
@@ -35,9 +35,16 @@ SRC  = $(OBJECTS:.o=.c)
 DEPEND = $(OBJECTS:.o=.d)
 srcdir := $(dir $(lastword $(MAKEFILE_LIST)))
 incdefs := $(shell $(srcdir)/incdefs.sh)
+snmpflg        := $(shell $(srcdir)/snmpflg.sh)
 version := $(shell $(srcdir)/version.sh $(srcdir))
 VPATH  = $(srcdir)
 
+ifneq (,$(findstring -DHAVE_NET_SNMP,$(snmpflg)))
+PRG    += snmp4lptp
+OBJECTS        += snmp4lptp.o
+snmplib        := $(shell net-snmp-config --netsnmp-agent-libs)
+endif
+
 prefix = /usr/local
 sbindir        = $(prefix)/sbin
 mandir = $(prefix)/man
@@ -61,6 +68,12 @@ hwstamp_ctl: hwstamp_ctl.o version.o
 
 phc_ctl: phc_ctl.o phc.o sk.o util.o clockadj.o sysoff.o print.o version.o
 
+snmp4lptp: print.o sk.o snmp4lptp.o util.o
+       $(CC) $^ $(LDFLAGS) $(LOADLIBES) $(LDLIBS) $(snmplib) -o $@
+
+snmp4lptp.o: snmp4lptp.c
+       $(CC) $(CPPFLAGS) $(CFLAGS) $(snmpflg) -c $<
+
 timemaster: print.o rtnl.o sk.o timemaster.o util.o version.o
 
 version.o: .version version.sh $(filter-out version.d,$(DEPEND))
diff --git a/snmp4lptp.c b/snmp4lptp.c
new file mode 100644
index 0000000..dc17a4f
--- /dev/null
+++ b/snmp4lptp.c
@@ -0,0 +1,57 @@
+/**
+ * @file snmp4lptp.c
+ * @brief Implements linuxptp SNMP sub agent program
+ * @note Copyright (C) 2018 Anders Selhammer <anders.selham...@est.tech>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#include <net-snmp/net-snmp-config.h>
+#include <net-snmp/net-snmp-includes.h>
+#include <net-snmp/agent/net-snmp-agent-includes.h>
+
+#include "util.h"
+
+static int open_snmp()
+{
+       snmp_enable_calllog();
+       netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
+                              NETSNMP_DS_AGENT_ROLE, 1);
+       init_agent("linuxptpAgent");
+
+       init_snmp("linuxptpAgent");
+
+       return 0;
+}
+
+int main(int argc, char *argv[])
+{
+       int err = 0;
+
+       if (handle_term_signals()) {
+               return -1;
+       }
+
+       if (open_snmp()) {
+               return -1;
+       }
+
+       while (is_running()) {
+               agent_check_and_process(1);
+       }
+
+       snmp_shutdown("linuxptpAgent");
+
+       return err;
+}
diff --git a/snmpflg.sh b/snmpflg.sh
new file mode 100755
index 0000000..3c58bcb
--- /dev/null
+++ b/snmpflg.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+#
+# Discover the SNMP CFLAGS to use during compilation.
+#
+# Copyright (C) 2018 Anders Selhammer <anders.selham...@est.tech>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+#
+# Look for libsnmp presence.  When cross compiling, the user will
+# specify the include path in EXTRA_CFLAGS.
+#
+snmp_flags()
+{
+       # Get list of directories searched for header files.
+       dirs=$(echo "" | ${CROSS_COMPILE}cpp ${EXTRA_CFLAGS} -Wp,-v 2>&1 
>/dev/null | grep ^" /")
+
+       # Look for libsnmp presence
+       for d in $dirs; do
+               files=$(find $d -type f -name net-snmp-agent-includes.h)
+               for f in $files; do
+                       if grep -q NET_SNMP_AGENT_INCLUDES_H $f; then
+                               printf " -DHAVE_NET_SNMP `net-snmp-config 
--cflags`"
+                               break 2
+                       fi
+               done
+       done
+}
+
+echo "$(snmp_flags)"
-- 
2.11.0


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Linuxptp-devel mailing list
Linuxptp-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel

Reply via email to