You are not logged in.

#1 2009-08-18 21:00:47

kowalski
Member
Registered: 2009-05-07
Posts: 82

Script to fill wmii 3.5 rbar

Hi guys,

I changed to wmii lately and used a combination of shell scripts to put some stuff into /rbar/, but just now I was a little bit bored and put everything into one python script with the possibility to change the arrangement and layout in a convenient way.
So, I thougt that there might be some people using wmii as well and having interest in something of this kind.
Here we go:

#! /usr/bin/env python

import datetime
import statgrab
import time
import os

SEPARATOR = ' | '
# sample time for network stats
SAMPLE_TIME = 0.25
# strftime format to display clock (and date or whatever)
DATE_FORMAT = "%a, %d.%m. %H:%M:%S"
# amount of cores for get_cpu_info
NR_OF_CORES = 2
# show core temperatures in get_cpu_info
# this required "lm-sensors" to be installed and working
# (might get messy on a screen with a small width in resolution
# and a lot of cores)
TEMP_READING = True 

def get_load(last = False):
    info = "load: %.2f"%statgrab.sg_get_load_stats()["min1"]+" %.2f"%statgrab.sg_get_load_stats()["min5"]+" %.2f"%statgrab.sg_get_load_stats()["min15"]
    if not last:
        info = info + SEPARATOR
    return info

def get_mem_usage(last = False):
    mem = float(statgrab.sg_get_mem_stats()["used"]-statgrab.sg_get_mem_stats()["cache"])/1024/1024
    info = "mem usage: %.f MB"%mem
    if not last:
        info = info + SEPARATOR
    return info

def get_network_load(last = False):
    iface_stats = statgrab.sg_get_network_iface_stats()
    # first run is necessary so we don't get all packages ever received
    statgrab.sg_get_network_io_stats_diff()

    # the script uses the first interface found that is up
    for i in iface_stats:
        if i["up"] == 1 and i["interface_name"] != "lo":
            iface = i["interface_name"]
            break

    # wait for some time to gather packages, very elegant ...
    time.sleep(SAMPLE_TIME)

    stats = statgrab.sg_get_network_io_stats_diff()

    for i in stats:
        if i["interface_name"] == iface:
            rx = float(i["rx"])/1024*1/SAMPLE_TIME 
            tx = float(i["tx"])/1024*1/SAMPLE_TIME
            info =  iface+": rx: %.2f"%rx+" kB/s tx: %.2f"%tx+" kB/s"
            if not last:
                info = info + SEPARATOR
            break
    return info

def get_time(last = False):
    info = str(datetime.datetime.now().strftime(DATE_FORMAT))
    if not last:
        info = info + SEPARATOR
    return info

def get_cpu_info(last = False):
    info = ""
    for i in range(0, NR_OF_CORES):
        # might be read from /proc/cpuinfo as well
        freq = os.popen("echo -n $(cpufreq-info -fmc "+str(i)+")")
        freq = freq.read()
        if TEMP_READING:
            temp = os.popen('echo -n $(sensors | grep "Core '+str(i)+'" | cut -d"+" -f2 | cut -d" " -f1)')
            temp = temp.read()
            if i < NR_OF_CORES - 1:
                info = info + freq +" @ " + temp + SEPARATOR
            else:
                info = info + freq +" @ " + temp
        else:
            info = info + freq + SEPARATOR

    if not last:
        info = info + SEPARATOR
    return info

# this is were the actual string to be displayed is constructed. the last call has to have True as argument,
# otherwise it will have a separator as well, which looks crappy
print get_load()+get_network_load()+get_cpu_info()+get_mem_usage()+get_time(True)

Supposedly the function get_network_info should work with whatever interface is connected (eth0/wlan0) and use that one (more precisely it uses the first one found and up except for "lo"). But I wasn't able to try this yet since my notebook isn't with me right now :-)

The bar as set in the file looks like this:
http://yfrog.com/18rbarp

The actual layout of the bar is set by the settings on the top of the file and the line on the very bottom which builds the line to be displayed in /rbar/ using the return values of the called functions.

Requirements:
    - python
    - pystatgrab
    - lm-sensors (for temperature readings)
    - cpufrequtils (reading cpu frequency, this might be read from /proc/cpuinfo but I'm too lazy for now)

Maybe someone will find this useful.

Btw: the calls for os.popen are from the old shell scripts there are certainly smarter ways and I will change this someday ....

Comments, remarks are welcome.

Cheers.


He who says A doesn't have to say B. He can also recognize that A was false.

Offline

Board footer

Powered by FluxBB