Repository: nifi-minifi-cpp Updated Branches: refs/heads/master 19b74bf5f -> e9cfbfe0f
MINIFI-159: Create AppendHostInfo processor This closes #27. Signed-off-by: Aldrin Piri <ald...@apache.org> Project: http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/repo Commit: http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/commit/e9cfbfe0 Tree: http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/tree/e9cfbfe0 Diff: http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/diff/e9cfbfe0 Branch: refs/heads/master Commit: e9cfbfe0f0e2bd96e47e46fade63864fda251958 Parents: 19b74bf Author: Randy Gelhausen <rgel...@gmail.com> Authored: Wed Dec 7 02:24:14 2016 -0500 Committer: Aldrin Piri <ald...@apache.org> Committed: Fri Dec 16 16:38:01 2016 -0500 ---------------------------------------------------------------------- libminifi/include/AppendHostInfo.h | 67 ++++++++++++++++++++++ libminifi/include/FlowController.h | 1 + libminifi/src/AppendHostInfo.cpp | 99 +++++++++++++++++++++++++++++++++ libminifi/src/FlowController.cpp | 4 ++ 4 files changed, 171 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/e9cfbfe0/libminifi/include/AppendHostInfo.h ---------------------------------------------------------------------- diff --git a/libminifi/include/AppendHostInfo.h b/libminifi/include/AppendHostInfo.h new file mode 100644 index 0000000..8cc1d41 --- /dev/null +++ b/libminifi/include/AppendHostInfo.h @@ -0,0 +1,67 @@ +/** + * @file AppendHostInfo.h + * AppendHostInfo class declaration + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __APPEND_HOSTINFO_H__ +#define __APPEND_HOSTINFO_H__ + +#include "FlowFileRecord.h" +#include "Processor.h" +#include "ProcessSession.h" + +//! AppendHostInfo Class +class AppendHostInfo : public Processor +{ +public: + //! Constructor + /*! + * Create a new processor + */ + AppendHostInfo(std::string name, uuid_t uuid = NULL) + : Processor(name, uuid) + { + _logger = Logger::getLogger(); + } + //! Destructor + virtual ~AppendHostInfo() + { + } + //! Processor Name + static const std::string ProcessorName; + //! Supported Properties + static Property InterfaceName; + static Property HostAttribute; + static Property IPAttribute; + + //! Supported Relationships + static Relationship Success; + +public: + //! OnTrigger method, implemented by NiFi AppendHostInfo + virtual void onTrigger(ProcessContext *context, ProcessSession *session); + //! Initialize, over write by NiFi AppendHostInfo + virtual void initialize(void); + +protected: + +private: + //! Logger + Logger *_logger; +}; + +#endif http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/e9cfbfe0/libminifi/include/FlowController.h ---------------------------------------------------------------------- diff --git a/libminifi/include/FlowController.h b/libminifi/include/FlowController.h index b02a83c..49629a2 100644 --- a/libminifi/include/FlowController.h +++ b/libminifi/include/FlowController.h @@ -52,6 +52,7 @@ #include "TailFile.h" #include "ListenSyslog.h" #include "ExecuteProcess.h" +#include "AppendHostInfo.h" //! Default NiFi Root Group Name #define DEFAULT_ROOT_GROUP_NAME "" http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/e9cfbfe0/libminifi/src/AppendHostInfo.cpp ---------------------------------------------------------------------- diff --git a/libminifi/src/AppendHostInfo.cpp b/libminifi/src/AppendHostInfo.cpp new file mode 100644 index 0000000..c9ce932 --- /dev/null +++ b/libminifi/src/AppendHostInfo.cpp @@ -0,0 +1,99 @@ +/** + * @file AppendHostInfo.cpp + * AppendHostInfo class implementation + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include <set> +#include <sys/time.h> +#include <string.h> +#include "AppendHostInfo.h" +#include "ProcessContext.h" +#include "ProcessSession.h" + +#include <netdb.h> +#include <netinet/in.h> +#include <sys/socket.h> +#include <sys/ioctl.h> +#include <net/if.h> +#include <arpa/inet.h> + +#define __USE_POSIX +#include <limits.h> + +#ifndef HOST_NAME_MAX +#define HOST_NAME_MAX 255 +#endif + +const std::string AppendHostInfo::ProcessorName("AppendHostInfo"); +Property AppendHostInfo::InterfaceName("Network Interface Name", "Network interface from which to read an IP v4 address", "eth0"); +Property AppendHostInfo::HostAttribute("Hostname Attribute", "Flowfile attribute to used to record the agent's hostname", "source.hostname"); +Property AppendHostInfo::IPAttribute("IP Attribute", "Flowfile attribute to used to record the agent's IP address", "source.ipv4"); +Relationship AppendHostInfo::Success("success", "success operational on the flow record"); + +void AppendHostInfo::initialize() +{ + //! Set the supported properties + std::set<Property> properties; + properties.insert(InterfaceName); + properties.insert(HostAttribute); + properties.insert(IPAttribute); + setSupportedProperties(properties); + + //! Set the supported relationships + std::set<Relationship> relationships; + relationships.insert(Success); + setSupportedRelationships(relationships); +} + +void AppendHostInfo::onTrigger(ProcessContext *context, ProcessSession *session) +{ + FlowFileRecord *flow = session->get(); + if (!flow) + return; + + //Get Hostname + char hostname[HOST_NAME_MAX]; + hostname[HOST_NAME_MAX-1] = '\0'; + gethostname(hostname, HOST_NAME_MAX-1); + struct hostent* h; + h = gethostbyname(hostname); + std::string hostAttribute; + context->getProperty(HostAttribute.getName(), hostAttribute); + flow->addAttribute(hostAttribute.c_str(), h->h_name); + + //Get IP address for the specified interface + std::string iface; + context->getProperty(InterfaceName.getName(), iface); + //Confirm the specified interface name exists on this device + if (if_nametoindex(iface.c_str()) != 0){ + struct ifreq ifr; + int fd = socket(AF_INET, SOCK_DGRAM, 0); + //Type of address to retrieve - IPv4 IP address + ifr.ifr_addr.sa_family = AF_INET; + //Copy the interface name in the ifreq structure + strncpy(ifr.ifr_name , iface.c_str(), IFNAMSIZ-1); + ioctl(fd, SIOCGIFADDR, &ifr); + close(fd); + + std::string ipAttribute; + context->getProperty(IPAttribute.getName(), ipAttribute); + flow->addAttribute(ipAttribute.c_str(), inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr)); + } + + // Transfer to the relationship + session->transfer(flow, Success); +} http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/e9cfbfe0/libminifi/src/FlowController.cpp ---------------------------------------------------------------------- diff --git a/libminifi/src/FlowController.cpp b/libminifi/src/FlowController.cpp index 3598716..6f74373 100644 --- a/libminifi/src/FlowController.cpp +++ b/libminifi/src/FlowController.cpp @@ -195,6 +195,10 @@ Processor *FlowController::createProcessor(std::string name, uuid_t uuid) { processor = new ExecuteProcess(name, uuid); } + else if (name == AppendHostInfo::ProcessorName) + { + processor = new AppendHostInfo(name, uuid); + } else { _logger->log_error("No Processor defined for %s", name.c_str());