yanic/respond/daemon/nodeinfo.go

148 lines
3.5 KiB
Go
Raw Normal View History

2019-04-10 13:18:05 +02:00
package respondd
import (
"fmt"
"io/ioutil"
2019-04-10 20:09:55 +02:00
"net"
2019-04-10 13:18:05 +02:00
"os"
"os/exec"
2019-04-10 13:18:05 +02:00
"runtime"
"strings"
2019-05-19 00:34:21 +02:00
babelParser "github.com/Vivena/babelweb2/parser"
"github.com/bdlm/log"
2019-04-10 13:18:05 +02:00
"github.com/FreifunkBremen/yanic/data"
)
2019-04-10 21:57:31 +02:00
func (d *Daemon) updateNodeinfo(iface string, resp *data.ResponseData) {
2019-04-10 13:18:05 +02:00
config, nodeID := d.getAnswer(iface)
2019-04-10 21:57:31 +02:00
resp.Nodeinfo.NodeID = nodeID
2019-04-10 13:18:05 +02:00
if config.Hostname == "" {
2019-04-10 21:57:31 +02:00
resp.Nodeinfo.Hostname, _ = os.Hostname()
2019-04-10 13:18:05 +02:00
} else {
2019-04-10 21:57:31 +02:00
resp.Nodeinfo.Hostname = config.Hostname
2019-04-10 13:18:05 +02:00
}
2019-04-10 21:57:31 +02:00
resp.Nodeinfo.VPN = config.VPN
resp.Nodeinfo.Location = config.Location
2019-04-10 13:18:05 +02:00
2019-04-10 21:57:31 +02:00
resp.Nodeinfo.System.SiteCode = config.SiteCode
resp.Nodeinfo.System.DomainCode = config.DomainCode
2019-04-10 13:18:05 +02:00
2019-04-10 21:57:31 +02:00
resp.Nodeinfo.Hardware.Nproc = runtime.NumCPU()
2019-04-10 13:18:05 +02:00
if out, err := exec.Command("lsb_release", "-sri").Output(); err == nil {
f := strings.Fields(string(out))
if len(f) == 2 {
resp.Nodeinfo.Software.Firmware.Base = f[0]
resp.Nodeinfo.Software.Firmware.Release = f[1]
}
} else {
log.Errorf("not able to run lsb_release: %s", err)
}
if out, err := exec.Command("fastd", "-v").Output(); err == nil {
resp.Nodeinfo.Software.Fastd.Enabled = true
f := strings.Fields(string(out))
if len(f) >= 2 {
resp.Nodeinfo.Software.Fastd.Version = f[1]
}
} else {
log.Infof("not able to run fastd: %s", err)
}
if v, err := ioutil.ReadFile("/sys/module/batman_adv/version"); err == nil {
resp.Nodeinfo.Software.BatmanAdv.Version = trim(string(v))
}
2019-05-21 09:37:46 +02:00
if babel := d.babelData; babel != nil {
resp.Nodeinfo.Software.Babeld.Version = babel.Version()
2019-05-21 09:37:46 +02:00
}
if config.InterfaceMAC != "" {
i, err := net.InterfaceByName(config.InterfaceMAC)
if err == nil {
resp.Nodeinfo.Network.Mac = i.HardwareAddr.String()
}
}
2019-04-10 21:57:31 +02:00
if resp.Nodeinfo.Network.Mac == "" {
resp.Nodeinfo.Network.Mac = fmt.Sprintf("%s:%s:%s:%s:%s:%s", nodeID[0:2], nodeID[2:4], nodeID[4:6], nodeID[6:8], nodeID[8:10], nodeID[10:12])
2019-04-10 13:18:05 +02:00
}
if iface != "" && len(config.InterfacesAddress) == 0 {
2019-04-10 21:57:31 +02:00
resp.Nodeinfo.Network.Addresses = getAddresses(iface)
2019-05-19 00:51:42 +02:00
} else {
resp.Nodeinfo.Network.Addresses = []string{}
for _, ifname := range config.InterfacesAddress {
resp.Nodeinfo.Network.Addresses = append(resp.Nodeinfo.Network.Addresses, getAddresses(ifname)...)
}
2019-04-10 21:57:31 +02:00
}
resp.Nodeinfo.Network.Mesh = make(map[string]*data.NetworkInterface)
for _, bface := range d.Batman {
2019-04-10 21:57:31 +02:00
b := NewBatman(bface)
if b == nil {
continue
}
2019-04-10 21:57:31 +02:00
mesh := data.NetworkInterface{}
2019-04-10 21:57:31 +02:00
for _, bbface := range b.Interfaces {
addr := b.Address(bbface)
if addr != "" {
mesh.Interfaces.Tunnel = append(mesh.Interfaces.Tunnel, addr)
}
2019-04-10 13:18:05 +02:00
}
2019-04-10 21:57:31 +02:00
resp.Nodeinfo.Network.Mesh[bface] = &mesh
2019-04-10 13:18:05 +02:00
}
2019-05-19 00:34:21 +02:00
if d.babelData == nil {
return
}
meshBabel := data.NetworkInterface{}
resp.Nodeinfo.Network.Mesh["babel"] = &meshBabel
d.babelData.Iter(func(t babelParser.Transition) error {
if t.Table != "interface" {
2019-05-19 00:34:21 +02:00
return nil
}
if t.Data["up"].(bool) {
addrIP := t.Data["ipv6"].(net.IP)
addr := addrIP.String()
2019-05-19 00:34:21 +02:00
meshBabel.Interfaces.Tunnel = append(meshBabel.Interfaces.Tunnel, addr)
2019-05-19 00:51:42 +02:00
resp.Nodeinfo.Network.Addresses = append(resp.Nodeinfo.Network.Addresses, addr)
2019-05-19 00:34:21 +02:00
}
return nil
})
2019-04-10 13:18:05 +02:00
}
func getAddresses(iface string) (addrs []string) {
in, err := net.InterfaceByName(iface)
if err != nil {
return
}
2019-04-10 20:09:55 +02:00
inAddrs, err := in.Addrs()
2019-04-10 13:18:05 +02:00
if err != nil {
return
}
for _, a := range inAddrs {
var ip net.IP
switch v := a.(type) {
2019-04-10 20:09:55 +02:00
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
default:
continue
2019-04-10 13:18:05 +02:00
}
if ip4 := ip.To4(); ip4 == nil {
addrs = append(addrs, ip.String())
}
}
return
}