From b2f2c9e02dfd43dc90d57128304e9a681672e19b Mon Sep 17 00:00:00 2001 From: Anders Selhammer Date: Tue, 21 Aug 2018 10:53:08 +0200 Subject: [PATCH] snmp4lptp: Add snmp sub agent for linuxptp The sub agent use net-snmp library and AgentX protocol for the communication towards the snmp master agent. snmpd.c should only include general setup needed for handling snmp. Supported MIBs should be implemented in separate files. Makefile will include snmpd if libsnmp is available during compilation. configs/snmpd.conf should be placed in /etc/snmp/ Signed-off-by: Anders Selhammer --- .gitignore | 1 + configs/snmpd.conf | 26 +++++++++++++++++++++ makefile | 13 +++++++++++ snmp4lptp.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++ snmpflg.sh | 42 ++++++++++++++++++++++++++++++++++ 5 files changed, 139 insertions(+) create mode 100644 configs/snmpd.conf create mode 100644 snmp4lptp.c create mode 100755 snmpflg.sh diff --git a/.gitignore b/.gitignore index 6d288ae..3dbcbfa 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ /pmc /ptp4l /phc_ctl +/snmp4lptp /timemaster 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..eec49af --- /dev/null +++ b/snmp4lptp.c @@ -0,0 +1,57 @@ +/** + * @file snmp4lptp.c + * @brief Implements SNMP sub agent program for linuxptp + * @note Copyright (C) 2018 Anders Selhammer + * + * 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 +#include +#include + +#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 +# +# 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)"