Spelunking Linux – procfs or is that sysctl?Posted: December 22, 2015
Last time around, I introduced some simple things with lj2procfs. Being able to simply access the contents of the various files within procfs is a bit of convenience. Really what lj2procfs is doing is just giving you a common interface to the data in those files. Everything shows up as simple lua values, typically tables, with strings, and numbers. That’s great for most of what you’d be doing with procfs, just taking a look at things.
But, on Linux, procfs has another capability. The /proc/sys directory contains a few interesting directories of its own:
abi/ debug/ dev/ fs/ kernel/ net/ vm/
And if you look into these directories, you find some more interesting files. For example, in the ‘kernel/’ directory, we can see a little bit of this:
hostname hotplug hung_task_check_count hung_task_panic hung_task_timeout_secs hung_task_warnings io_delay_type kexec_load_disabled keys kptr_restrict kstack_depth_to_print max_lock_depth modprobe . . .
Now, these are looking kind of interesting. These files contain typically tunable portions of the kernel. On other unices, these values might be controlled through the sysctl() function call. On Linux, that function would just manipulate the contents of these files. So, why not just use lj2procfs to do the same.
Let’s take a look at a few relatively simple tasks. First, I want to get the version of the OS running on my machine. This can be obtained through the file /proc/sys/kernel/version
local procfs = require("lj2procfs.procfs") print(procfs.sys.kernel.version) $ #15-Ubuntu SMP Thu Apr 16 23:32:37 UTC 2015
This is the same string returned from the call ‘uname -v’
And, to get the hostname of the machine:
print(procfs.sys.kernel.hostname) $ ubuntu
Which is what the ‘hostname’ command returns on my machine.
And what about setting the hostname? First of all, you’ll want to do this as root, but it’s equally simple:
procfs.sys.kernel.hostname = 'alfredo'
Keep in mind that setting the hostname in this way is transient, and it will seriously mess up things, like your about to sudo after this. But, there you have it.
Any value under /proc/sys can be retrieved or set using the fairly simple mechanism. I find this to be very valuable for two reasons. First of all, spelunking these values makes for great discovery. More importantly, being able to capture and set the values makes for a fairly easily tunable system.
An example of how this can be used for system diagnostics and tuning, you can capture the kernel values, using a simple command that just dumps what you want into a table. Send that table to anyone else for analysis. Similarly, if someone has come up with a system configuration that is great for a particular task, tuning the VM allocations, networking values, and the like, they can send you the configuration (just a string value that is a lua table) and you can apply it to your system.
This is a tad better than simply trying to look at system logs to determine after the fact what might be going on with a system. Perhaps the combination of these live values, as well as correlation with system logs, makes it easier to automate the process of diagnosing and tuning a system.
Well, there you have it. The lj2procfs thing is getting more concise, as well as becoming more usable at the same time.