Thursday, 31 December 2020

Working with the Linux /proc directory

This strange directory contains some interesting & useful stuff.


Accessing data contained in its files is pretty easy.

So in this post I explore its treasures with some simple Gambas code.

The /proc directory on Linux is a strange beast. It is created when Linux boots and contains many apparently 'empty' files. These files are more like data access points.

For example we can read data from /proc/version by typing in a terminal:-

cat /proc/version

On my laptop this gives an output like this:-

steve@steve-Orion:~$ cat /proc/version
Linux version 5.4.0-58-generic (buildd@lgw01-amd64-040) (gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)) #64~18.04.1-Ubuntu SMP Wed Dec 9 17:11:11 UTC 2020


Its very simple to work with this data in a Gambas program:-

Const PROC_VERSION As String = "/proc/version"

Public Sub Form_Open()

  TextArea1.Text = File.Load(PROC_VERSION)


Note: you must include the gb.Util component in your project to be able to use File.Load()

Many of the Linux command-line programs use the /proc files to present data to the user. For example, the terminal command: free

steve@steve-Orion:~$ free
              total        used        free      shared  buff/cache   available
Mem:       15623868     1072508    12593532      436516     1957828    13817372
Swap:       2097148           0     2097148

The data source for this is: /proc/meminfo

And if you type in a terminal: cat /proc/meminfo  you will see a large, detailed list about system memory.


One final example; /proc/net/wireless

steve@steve-Orion:~$ cat /proc/net/wireless
Inter-| sta-|   Quality        |   Discarded packets               | Missed | WE
 face | tus | link level noise |  nwid  crypt   frag  retry   misc | beacon | 22
wlp6s0: 0000   53.  -57.  -256        0      0      0      0    139        0

Once again, you could display this information in Gambas quite simply:-

Const PROC_WIRELESS As String = "/proc/net/wireless"

Public Sub Form_Open()

  TextArea1.Text = File.Load(PROC_WIRELESS)


Tip: set TextArea1.Wrap = True

Let's suppose that we would like to create a small program to show Wifi quality (Link Quality is the single most useful parameter when it comes to assessing your wifi link, as its a measure of the number of data transmission retries).

  • We only need one figure, which is 53. in the example above.
  • As the ":" appears to be unique, we can remove everything up to that point.
  • Then we can remove the "." after the 53 as this is no use to us, unless the data could read (say) 53.6 then we would need to keep it.
  • Finally, we can turn what is left into a string array, and then just pick out the required field.

Here is a Gambas example which requires a ProgressBar and Timer, where the Timer is Enabled and set for an interval of 1 second:-

Const PROC_WIRELESS As String = "/proc/net/wireless"

Public Sub Timer1_Timer()
Dim strWifiData As String
Dim arrData As String[]

  strWifiData = File.Load(PROC_WIRELESS)
  strWifiData = Mid(strWifiData, InStr(strWifiData, ":"))   'delete everything upto the :
  strWifiData = Replace(strWifiData, ". ", "")   'only replace the dot if its followed by a space
  arrData = Split(strWifiData, " ", "", True)    'Split using spaces as the seperator, but then ignore spaces
  pbQuality.Value = CInt(arrData[2]) / 100      'pick out the third item and scale it from 0 to 1 for the ProgressBar



When I run this program, my tiny window looks like this:-



Sometimes it is easier to use a /proc file to gather system data than it is to run a basic Linux command using the Gambas commands Exec or Shell.

No comments:

Post a Comment