Add a per-process network information plugin
ClosedPublic

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

Details

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
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.
ahiemstra created this revision.Aug 22 2019, 10:59 AM
Restricted Application added a project: Plasma. · View Herald TranscriptAug 22 2019, 10:59 AM
Restricted Application added a subscriber: plasma-devel. · View Herald Transcript
ahiemstra requested review of this revision.Aug 22 2019, 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.Aug 22 2019, 11:15 AM
  • Add missing header and remove unneeded forward declares
  • Code style fixes
ahiemstra updated this revision to Diff 64295.Aug 22 2019, 11:20 AM
  • Reject lines <7 elements instead of <2
ahiemstra marked 2 inline comments as done.Aug 22 2019, 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.Aug 22 2019, 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.Aug 22 2019, 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.Aug 22 2019, 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.Aug 22 2019, 2:30 PM
  • Use libexec dir for the helper install location
  • Don't use nonexistant variables
ahiemstra marked 2 inline comments as done.Aug 22 2019, 2:32 PM
meven added a subscriber: meven.Sep 1 2019, 6:45 AM

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

meven requested changes to this revision.Sep 1 2019, 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.Sep 1 2019, 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.Sep 1 2019, 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.Sep 2 2019, 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.Sep 2 2019, 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
98

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.Sep 2 2019, 10:29 AM
meven added a comment.Sep 5 2019, 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
98

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.

This revision was automatically updated to reflect the committed changes.

Please see https://build.kde.org/job/Plasma/job/ksysguard/job/kf5-qt5%20FreeBSDQt5.13/10/console

This has now caused Dependency Builds to fail, which means CI for Plasma is now unmaintainable.

Could we please get this fixed?