示例#1
0
文件: jsonnodesold.go 项目: hwhw/mesh
// Assemble data elements for a mesh node from database.
// This operation assumes the database is already locked by the caller.
func (db *NodeDB) getNodesOldJSONData(tx *bolt.Tx, nmeta *store.Meta, nodeid string, offlineDuration time.Duration) (*NodesOldJSONData, error) {
	data := &NodesOldJSONData{}

	ninfo := &NodeInfo{}
	if err := nmeta.GetItem(ninfo); err != nil {
		return data, err
	}
	nodeinfo := *ninfo // make a copy

	data.Name = &nodeinfo.Data.Hostname
	data.Id = nodeid
	if nodeinfo.Data.Location != nil {
		data.Geo = []float64{nodeinfo.Data.Location.Latitude, nodeinfo.Data.Location.Longitude}
	}
	if nodeinfo.Data.Software != nil {
		if nodeinfo.Data.Software.Firmware != nil {
			data.Firmware = &nodeinfo.Data.Software.Firmware.Release
			data.GluonBase = nodeinfo.Data.Software.Firmware.Base
		}
		if nodeinfo.Data.Software.BatmanAdv != nil {
			data.BatmanVersion = &nodeinfo.Data.Software.BatmanAdv.Version
		}
		if nodeinfo.Data.Software.AutoUpdater != nil {
			data.AutoUpdaterState = nodeinfo.Data.Software.AutoUpdater.Enabled
			data.AutoUpdaterBranch = nodeinfo.Data.Software.AutoUpdater.Branch
		}
	}
	if nodeinfo.Data.Hardware != nil {
		data.Hardware = nodeinfo.Data.Hardware.Model
	}
	if nodeinfo.Data.Network != nil {
		data.Addresses = nodeinfo.Data.Network.Addresses
	}

	// latest datestamp is the "last seen" time
	lastseen := nmeta.Updated

	statistics := &Statistics{}
	smeta := store.NewMeta(statistics)
	if db.Main.Get(tx, nmeta.Key(), smeta) == nil {
		if lastseen.Before(smeta.Updated) {
			lastseen = smeta.Updated
		}

		if smeta.GetItem(statistics) == nil {
			statdata := statistics.Data
			data.Uptime = statdata.Uptime
			if statdata.Clients != nil {
				data.ClientCount = statdata.Clients.Total
			}
			if statdata.Gateway != nil {
				data.Gateway = statdata.Gateway
			}
		}
	}

	vis := &VisData{}
	vmeta := store.NewMeta(vis)
	if db.Main.Get(tx, nmeta.Key(), vmeta) == nil {
		if lastseen.Before(vmeta.Updated) {
			lastseen = vmeta.Updated
		}
	}

	data.LastSeen = lastseen.Unix()

	// set gateway flag when we have the node's address in
	// our list of gateways
	data.Flags.Gateway = db.Main.Exists(tx, nmeta.Key(), &Gateway{})

	// online state is determined by the time we have last
	// seen a mesh node
	offline := time.Now().Sub(time.Time(lastseen))
	if offline < offlineDuration {
		data.Flags.Online = true
	} else {
		data.Flags.Online = false
	}

	return data, nil
}
示例#2
0
文件: jsonnodes.go 项目: hwhw/mesh
// Assemble data elements for a mesh node from database.
// This operation assumes the database is already locked by the caller.
func (db *NodeDB) getNodesJSONData(tx *bolt.Tx, nmeta *store.Meta, offlineDuration time.Duration) (*NodesJSONData, error) {
	data := &NodesJSONData{}

	nodeinfo := &NodeInfo{}
	if err := nmeta.GetItem(nodeinfo); err != nil {
		return data, err
	}

	data.NodeInfo = *nodeinfo.Data // make a copy

	// earliest datestamp is the "first seen" time,
	// latest datestamp is the "last seen" time
	firstseen := nmeta.Created
	lastseen := nmeta.Updated

	statistics := &Statistics{}
	smeta := store.NewMeta(statistics)
	if db.Main.Get(tx, nmeta.Key(), smeta) == nil {
		if smeta.Created.Before(firstseen) {
			firstseen = smeta.Created
		}
		if lastseen.Before(smeta.Updated) {
			lastseen = smeta.Updated
		}

		if smeta.GetItem(statistics) == nil {
			statdata := statistics.Data
			if statdata.Memory != nil {
				if statdata.Memory.Total != 0 {
					// this calculation is a bit stupid, but compatible with ffmap-backend:
					data.Statistics.MemoryUsage = 1.0 - (float64(statdata.Memory.Free) / float64(statdata.Memory.Total))
				} else {
					data.Statistics.MemoryUsage = 1
				}
			}
			data.Statistics.Uptime = statdata.Uptime
			if statdata.Clients != nil {
				data.Statistics.Clients = statdata.Clients.Total
			}
			if statdata.Gateway != nil {
				data.Statistics.Gateway = statdata.Gateway
			}
			data.Statistics.LoadAvg = statdata.LoadAvg
			data.Statistics.RootFSUsage = statdata.RootFSUsage
		}
	}

	vis := &VisData{}
	vmeta := store.NewMeta(vis)
	if db.Main.Get(tx, nmeta.Key(), vmeta) == nil {
		if vmeta.Created.Before(firstseen) {
			firstseen = vmeta.Created
		}
		if lastseen.Before(vmeta.Updated) {
			lastseen = vmeta.Updated
		}
	}

	data.FirstSeen = NodesJSONTime(firstseen)
	data.LastSeen = NodesJSONTime(lastseen)

	// set gateway flag when we have the node's address in
	// our list of gateways
	nodeid, _ := db.ResolveNodeID(tx, alfred.HardwareAddr(nmeta.Key()))
	data.Flags.Gateway = db.Main.Exists(tx, []byte(nodeid), &Gateway{})

	// online state is determined by the time we have last
	// seen a mesh node
	offline := time.Now().Sub(time.Time(data.LastSeen))
	if offline < offlineDuration {
		data.Flags.Online = true
	} else {
		data.Flags.Online = false
	}

	return data, nil
}