Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create processor that adds host IP addresses as tags #3756

Closed
todaygood opened this issue Feb 6, 2018 · 7 comments
Closed

Create processor that adds host IP addresses as tags #3756

todaygood opened this issue Feb 6, 2018 · 7 comments
Labels
feature request Requests for new plugin and for new features to existing plugins

Comments

@todaygood
Copy link

Feature Request

telegraf is lack of an input plugin which can get all ip address of interfaces.

Proposal:

new an input plugin which can get all ip address of interfaces.

Current behavior:

no plugin which can get all ip address of interfaces.

Use case: [Why is this important (helps with prioritizing requests)]

I used telegraf +Influxdb+ grafana to monitor all instances in an openstack , but I found telegraf use hostname as index of measurements, not ip address. Now an issue is that I can't find the instance though I know the broken instance's hostname when monitoring on grafana web, because hostname of instance is permitted to modify by customer or tenant. I want an plugin which can get all ip addresses of interfaces in in instance.

@influxdata influxdata deleted a comment from derenma Feb 6, 2018
@danielnelson
Copy link
Contributor

It might be better to set a global tag with the information you would need to find the instance, you could potentially use an environment variable to set the value.

The reason I suggest this is that this would tag all metrics with the information, and you wouldn't need to perform a separate lookup step. Also, InfluxDB does not excel at storing relation data or lists of values, so it would be difficult to come up with a good schema for storing the data separately, since as you know an interface can have many ip addresses.

@todaygood
Copy link
Author

I solved this issue temporarily with global tag. but I still thought we need to consider the issue which hostname may be not the best option. From the standpoint of maintainers, need to know
IP1, IP2, IP3, ... of the monitored target to further login on it then take some actions whether they are located one interface or not.

@danielnelson
Copy link
Contributor

I might be misunderstanding, but what about just adding multiple global_tags?

[global_tags]
  ip1 = "192.168.0.1"
  ip2 = "192.168.0.2"
  ip3 = "192.168.0.3"

If we wanted to automate adding this, I believe the way to go about it would be to have a processor plugin that can add these tags automatically.

@danielnelson danielnelson changed the title [FR] Get all ip addresses of interfaces Create processor that adds host IP addresses as tags Mar 23, 2018
@aixeshunter
Copy link

IP address will be changed, so we need a processor plugin that can add ip address as tag automatically.

@glinton glinton added the feature request Requests for new plugin and for new features to existing plugins label Apr 22, 2019
@danielnelson
Copy link
Contributor

One question I still have is how should we decide which addresses to add in the processor? Would it make sense to create tags for all non-local addresses? Or perhaps have the user provide a list of networks to select:

networks = ["192.168.0.0/24", "10.0.0.0/8"]

Maybe we should use a dns lookup instead? This does introduce a network operation, normally not allowed by the processor interface, but should be fast and can be cached periodically so maybe it would be okay. In the future I think we need a "heavyweight" processor type for this type of operation.

Another item to be considered is how will we name the tags if there are multiple addresses. The most query friendly is to duplicate the metric:

cpu,ip=192.168.0.1 usage_idle=42
cpu,ip=192.168.0.2 usage_idle=42
cpu,ip=192.168.0.3 usage_idle=42

This allows you to select the data with any address, but is not very storage friendly. If we set the addresses like so then it will be hard to query because you must know which tag it is stored in:

cpu,ip1=192.168.0.1,ip2=192.168.0.2,ip3=192.168.0.3 usage_idle=42

@aixeshunter
Copy link

aixeshunter commented Apr 23, 2019

Hi guys, can we have a new measurement to store it?

I wrote a piece of incomplete code about interface ip obtain:

package main

import (
	"fmt"
	"net"
	"strings"
)

func main() {

	//----------------------
	// Get the local machine IP address
	// https://www.socketloop.com/tutorials/golang-how-do-I-get-the-local-ip-non-loopback-address
	//----------------------

	var currentIP, currentNetworkHardwareName string

	// get all the system's or local machine's network interfaces

	interfaces, _ := net.Interfaces()
	for _, interf := range interfaces {

		if addrs, err := interf.Addrs(); err == nil {
			for _, addr := range addrs {
				// check the address type and if it is not a loopback the display it
				// = GET LOCAL IP ADDRESS
				if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
					if ipnet.IP.To4() != nil {
						currentIP = ipnet.IP.String()

						// only interested in the name with current IP address`
						if strings.Contains(addr.String(), currentIP) {
							currentNetworkHardwareName = interf.Name
						}

						macAddress := interf.HardwareAddr
						hwAddr, err := net.ParseMAC(macAddress.String())
						if err != nil {
							continue
						}

						currentNetworkHardwareName = strings.Replace(currentNetworkHardwareName, " ", "", -1)

						fmt.Printf("interface,name=%s,ip_address=%s,mac_address=%s status=1\n",
							currentNetworkHardwareName, currentIP, hwAddr)
					}
				}
			}
		}
	}
}

Compile it:

go build -o telegraf_plugin

and it can be configured in telegraf.conf:

[[inputs.exec]]
#   ## Commands array
   commands = [
     "/usr/bin/telegraf_plugin"
   ]

  # name_suffix = "_cac"
   data_format = "influx"

The output like:

interface,host=localhost.localdomain,ip_address=172.17.0.1,mac_address=02:42:af:84:5c:47,name=docker0 status=1 1555919116000000000
interface,host=localhost.localdomain,ip_address=10.33.46.103,mac_address=0c:c4:7a:e2:25:3e,name=ovsbr1 status=1 1555919116000000000

@reimda
Copy link
Contributor

reimda commented Aug 2, 2022

Closing this since there is a workaround to use aixeshunter's code with the exec plugin. Thanks!

@reimda reimda closed this as completed Aug 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Requests for new plugin and for new features to existing plugins
Projects
None yet
Development

No branches or pull requests

5 participants