Skip to content

Commit

Permalink
fix(linux): clean up network connections sensor
Browse files Browse the repository at this point in the history
- fix BSSID parsing
- adjust and decorate trace/debug logging
  • Loading branch information
joshuar committed Oct 8, 2023
1 parent e3ef8f2 commit 78dc843
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions internal/linux/networkConnectionSensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ func getActiveConns(ctx context.Context) []dbus.ObjectPath {
// sensor.STATE_UNKNOWN.
func getConnType(ctx context.Context, p dbus.ObjectPath) string {
if !p.IsValid() {
log.Debug().Msgf("Invalid D-Bus object path %s.", p)
log.Trace().Str("path", string(p)).Msg("Invalid D-Bus object path.")
return sensor.StateUnknown
}
v, err := NewBusRequest(ctx, SystemBus).
Path(p).
Destination(dBusNMObj).
GetProp(dBusPropConnType)
if err != nil {
log.Debug().Err(err).Msg("Could not fetch type of connection.")
log.Trace().Err(err).Msg("Could not fetch type of connection.")
return sensor.StateUnknown
} else {
return string(variantToValue[[]uint8](v))
Expand Down Expand Up @@ -208,10 +208,12 @@ func getIPAddr(ctx context.Context, p dbus.ObjectPath, ver int) string {
// getWifiProp will fetch the appropriate value for the given wifi sensor type
// from D-Bus. If it cannot find the type, it returns sensor.STATE_UNKNOWN.
func getWifiProp(ctx context.Context, p dbus.ObjectPath, t sensorType) interface{} {
log.Info().Msg("in wifi prop")
if !p.IsValid() {
log.Debug().Msgf("Invalid D-Bus object path %s.", p)
return sensor.StateUnknown
}
log.Info().Msg("HERE")
var prop string
switch t {
case wifiSSID:
Expand Down Expand Up @@ -263,7 +265,7 @@ func getWifiProp(ctx context.Context, p dbus.ObjectPath, t sensorType) interface
// connection type.
func handleConn(ctx context.Context, p dbus.ObjectPath, t device.SensorTracker) error {
s := newNetworkSensor(ctx, p, connectionState, nil)
if s.sensorGroup == "lo" {
if s.sensorGroup == "lo" || s.ID() == "unknown_connection_state" {
log.Trace().Caller().Msgf("Ignoring state update for connection %s.", s.sensorGroup)
} else {
return t.UpdateSensors(ctx, s)
Expand Down Expand Up @@ -317,10 +319,10 @@ func handleProps(ctx context.Context, p dbus.ObjectPath, props map[string]dbus.V
switch propName {
case "Ssid":
propType = wifiSSID
value = variantToValue[[]uint8](propValue)
value = variantToValue[string](propValue)
case "HwAddress":
propType = wifiHWAddress
value = variantToValue[[]uint8](propValue)
value = variantToValue[string](propValue)
case "Frequency":
propType = wifiFrequency
value = variantToValue[uint32](propValue)
Expand All @@ -331,13 +333,14 @@ func handleProps(ctx context.Context, p dbus.ObjectPath, props map[string]dbus.V
propType = wifiStrength
value = variantToValue[uint32](propValue)
default:
log.Trace().Caller().
Msgf("Unhandled property %v changed to %v (%s).", propName, propValue, p)
log.Trace().
Str("prop", propName).Interface("value", propValue).Str("path", string(p)).
Msg("Unhandled property.")
return
}
if err := tracker.UpdateSensors(ctx, newNetworkSensor(ctx, p, propType, value)); err != nil {
log.Error().Err(err).Str("prop", propName).
Msg("Could not update connection property")
Msg("Could not update property.")
}
}
}
Expand All @@ -363,14 +366,17 @@ func newNetworkSensor(ctx context.Context, p dbus.ObjectPath, asType sensorType,
if value == nil {
s.value = getConnState(ctx, s.objectPath)
}
log.Trace().Str("id", s.ID()).Msg("Getting IP address details.")
s.attributes.Ipv4 = getIPAddr(ctx, s.objectPath, 4)
s.attributes.Ipv6 = getIPAddr(ctx, s.objectPath, 6)
log.Trace().Str("id", s.ID()).Msg("Getting connection type.")
s.attributes.ConnectionType = getConnType(ctx, s.objectPath)
case wifiFrequency, wifiSSID, wifiSpeed, wifiHWAddress, wifiStrength:
s.sensorGroup = "wifi"
if value == nil {
s.value = getWifiProp(ctx, p, s.sensorType)
}
// if value == nil {
// log.Trace().Str("id", s.ID()).Msg("Fetching Wi-Fi property.")
// s.value = getWifiProp(ctx, p, s.sensorType)
// }
}
return s
}
Expand Down Expand Up @@ -480,24 +486,26 @@ func NetworkConnectionsUpdater(ctx context.Context, tracker device.SensorTracker
Event("org.freedesktop.DBus.Properties.PropertiesChanged").
Handler(func(s *dbus.Signal) {
if !s.Path.IsValid() || s.Path == "/" {
log.Trace().Caller().Msgf("Invalid D-Bus object path %s.", s.Path)
log.Trace().Str("path", string(s.Path)).
Msg("Invalid D-Bus object path.")
return
}
if len(s.Body) == 0 {
log.Trace().Caller().Msg("No signal body received.")
log.Trace().Msg("No signal body received.")
return
}
obj, ok := s.Body[0].(string)
if !ok {
log.Trace().Caller().Msgf("Unhandled signal body of type %T (%v).", obj, s)
log.Trace().Type("type", obj).Interface("value", s.Body).
Msg("Unhandled signal.")
return
}
switch obj {
case dBusNMObj:
// TODO: handle this object
case dBusObjConn:
if err := handleConn(ctx, s.Path, tracker); err != nil {
log.Error().Err(err).Str("dBusPath", string(s.Path)).
log.Error().Err(err).Str("path", string(s.Path)).
Msg("Could not process connection.")
}
case dBusObjDev:
Expand All @@ -511,7 +519,8 @@ func NetworkConnectionsUpdater(ctx context.Context, tracker device.SensorTracker
case "org.freedesktop.NetworkManager.Device.Statistics":
// no-op, too noisy
default:
log.Trace().Caller().Msgf("Unhandled object %s.", obj)
log.Trace().Str("value", obj).
Msg("Unhandled object.")
}
}).
AddWatch(ctx)
Expand Down

0 comments on commit 78dc843

Please sign in to comment.