Add a per-process network information plugin
AcceptedPublic

Authored by ahiemstra on Thu, Aug 22, 10:59 AM.

Details

Reviewers
meven
Group Reviewers
Plasma
Summary

This adds a ksysguard process plugin that provides information about
how much data is being used by a certain process. Since there is no
unpriviledged API available (yet?) for this information, a small
helper application is used that uses libpcap to capture packet sizes,
which is then mapped to individual processes by comparing sockets to
entries in /proc.

The helper application is intentionally kept small and should not
require anything else than cap_net_raw.

Depends on D23287

Test Plan

Ensure ksgrd_network_helper has cap_net_raw set, by running
setcap cap_net_raw+ep ksgrd_network_helper. After that, start
ksysguard. The "upload" and "download" columns should be
automatically enabled and display network traffic for a certain
process.

Diff Detail

Repository
R106 KSysguard
Branch
network_plugin
Lint
No Linters Available
Unit
No Unit Test Coverage
Build Status
Buildable 15992
Build 16010: arc lint + arc unit
ahiemstra created this revision.Thu, Aug 22, 10:59 AM
Restricted Application added a project: Plasma. · View Herald TranscriptThu, Aug 22, 10:59 AM
Restricted Application added a subscriber: plasma-devel. · View Herald Transcript
ahiemstra requested review of this revision.Thu, Aug 22, 10:59 AM
davidedmundson added inline comments.
plugins/process/network/network.cpp
75

why 2? We call parts.at(6)

Also code style {}

plugins/process/network/network.h
29–30

These can go go now

ahiemstra updated this revision to Diff 64293.Thu, Aug 22, 11:15 AM
  • Add missing header and remove unneeded forward declares
  • Code style fixes
ahiemstra updated this revision to Diff 64295.Thu, Aug 22, 11:20 AM
  • Reject lines <7 elements instead of <2
ahiemstra marked 2 inline comments as done.Thu, Aug 22, 11:21 AM
ahiemstra added inline comments.
plugins/process/network/network.cpp
75

Good point. I cannot recall why I used 2 so changed it to 7 now.

plugins/process/network/helper/CMakeLists.txt
22

This isn't set.

libcap_FOUND is

27

Can we use KDE_INSTALL_LIBEXECDIR?

ahiemstra marked an inline comment as done.Thu, Aug 22, 1:49 PM
ahiemstra added inline comments.
plugins/process/network/helper/CMakeLists.txt
27

We can, if there is a decent way to find executables in libexec, because I couldn't find anything in QStandardPaths.

davidedmundson added inline comments.Thu, Aug 22, 1:52 PM
plugins/process/network/helper/CMakeLists.txt
27

There's nothing at runtime, but we know KDE_INSTALL_LIBEXECDIR_FULL at configure time, so we can pass that to the plugin.

ahiemstra added inline comments.Thu, Aug 22, 2:09 PM
plugins/process/network/helper/CMakeLists.txt
27

Oh, that's a good idea, I totally did not think about that.

ahiemstra updated this revision to Diff 64310.Thu, Aug 22, 2:30 PM
  • Use libexec dir for the helper install location
  • Don't use nonexistant variables
ahiemstra marked 2 inline comments as done.Thu, Aug 22, 2:32 PM
meven added a subscriber: meven.Sun, Sep 1, 6:45 AM

It works nicely.
I thought it missed a screenshot here.

meven requested changes to this revision.Sun, Sep 1, 6:52 AM

The command sudo setcap cap_net_raw+ep ksgrd_network_helper needs to be documented outside of just the commit message, in plugins/process/network/README.md at least.
This could be run on package install by distributions, or we could add an action somewhere to let the user run it on demand.

This revision now requires changes to proceed.Sun, Sep 1, 6:52 AM

This could be run on package install by distributions, or we could add an action somewhere to let the user run it on demand.

It is run automatically in the cmake.

See
plugins/process/network/helper/CMakeLists.txt

I think that should be enough for distros

meven added a comment.Sun, Sep 1, 3:34 PM

This could be run on package install by distributions, or we could add an action somewhere to let the user run it on demand.

It is run automatically in the cmake.

See
plugins/process/network/helper/CMakeLists.txt

I think that should be enough for distros

I had to run the command setcap cap_net_raw+ep as root, cmake is not run a root so if I understand correctly it won't work.

ahiemstra updated this revision to Diff 65214.Mon, Sep 2, 9:49 AM
  • Add a line that explains using setcap for cap_net_raw

The readme already mentions it needs cap_net_raw. But I've now added a line on how to ensure that it has that.

meven accepted this revision.Mon, Sep 2, 10:29 AM

Seems good to me !
I am using this now, and it is great.

But my review does not weight much as I am not a ksysguard maintainer/developer.

It makes me want to add a network graph by process in ksysguard.

plugins/process/network/helper/main.cpp
97

Could the refresh rate be a parameter or at least a constant instead of an hardcoded value.

This revision is now accepted and ready to land.Mon, Sep 2, 10:29 AM
meven added a comment.Thu, Sep 5, 5:12 AM

According to my testing, it does not measure traffic for root's processes such as apt.
Am I missing something ?
Can we improve on the situation ?

That's correct. The reason for that is that a non-root process is not allowed to read which inodes root process has open, thus there is no way for the helper to map from incoming port to process ID. The only way I know of solving that is by having the helper run as root, which I would prefer to avoid.

plugins/process/network/helper/main.cpp
97

The helper reports a download/upload rate per second. This is done by capturing the amount of incoming/outgoing bytes and each second reporting that value, then resetting the counter. Changing this value would thus result in changing the reported unit, so I'd rather keep it a hardcoded constant here.