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

feature: monitor traffic in openstack router #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 65 additions & 3 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
"syscall"
"time"

"net"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"github.com/vishvananda/netns"
Expand All @@ -23,6 +23,9 @@ const (
collectorSubsystem = "network"
netnsLabel = "netns"
deviceLabel = "device"
router = "router"
host = "host"
deviceIP = "deviceIP"
)

type Collector struct {
Expand All @@ -44,7 +47,7 @@ func NewCollector(config *NetnsExporterConfig, logger *logrus.Logger) *Collector
intfMetrics[metric] = prometheus.NewDesc(
prometheus.BuildFQName(collectorNamespace, collectorSubsystem, metric+"_total"),
"Interface statistics in the network namespace",
[]string{netnsLabel, deviceLabel},
[]string{netnsLabel, deviceLabel, router, host, deviceIP},
nil,
)
}
Expand Down Expand Up @@ -187,9 +190,20 @@ func (c *Collector) getMetricsFromNamespace(namespace string, wg *LimitedWaitGro

c.logger.Debugf("Start getting statistics for interface %s in namespace %s", ifFile.Name(), namespace)

// get ip address of device in namespace
device_addr, err := c.getIPfromNS(namespace, ifFile.Name())
if err != nil {
c.logger.Errorf("Failed to get IP of device: %s", ifFile.Name)
}

// parse routerID from namespace
routerID := strings.Replace(namespace, "qrouter-", "", -1)
// get current hostname
hostname := c.getHostname()

for metricName, desc := range c.intfMetrics {
value := c.getMetricFromFile(namespace, InterfaceStatPath+ifFile.Name()+"/statistics/"+metricName)
ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, value, namespace, ifFile.Name())
ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, value, namespace, ifFile.Name(), routerID, hostname, device_addr)
}
}

Expand Down Expand Up @@ -250,3 +264,51 @@ func (c *Collector) filterNsFiles(nsFiles []os.FileInfo) []os.FileInfo {

return nsFiles
}


func (c *Collector) getHostname() (string) {
hostname, err := os.Hostname()
if err != nil {
c.logger.Debugf("Fail to get current hostname")
return ""
}
return hostname
}

func (c *Collector) getIPfromNS(namespace, device string) (IP string, err error) {

ns, err := netns.GetFromName(namespace)
if err != nil {
c.logger.Errorf("Failed to open namespace:", err)
return "nil", err
}
defer ns.Close()

netns.Set(ns)
defer netns.Set(netns.None())

// Get IP from specific ip adress
iface, err := net.InterfaceByName(device)
if err != nil {
c.logger.Errorf("Failed to retrieve interfaces:", err)
return "nil", err
}
addrs, err := iface.Addrs()
if err != nil {
c.logger.Errorf("Failed to retrieve addresses for interface", iface.Name, ":", err)
return "nil", err
}
if len(addrs) > 0 {
ip, _, err := net.ParseCIDR(addrs[0].String())
if err != nil {
c.logger.Errorf("Failed to parse IP address:", err)
return "nil", err
}
c.logger.Debugf("IP address %s %s in namespace %s", iface.Name, ip, namespace)
return ip.String(), nil

}
c.logger.Debugf("Interface %s no IP", iface.Name)
return "", nil

}