# /proc/loadavg alternative and SabNZB Python script?



## Nordmann (May 17, 2010)

Hiyas

I've recently switched serving LAMP/samba/NFS/FTP from linux to FreeBSD 8.0 CURRENT on my home network and love it more and more every day but there's one thing I miss.. namely /proc/loadavg

One of the services I run on this server is SabNZBd, a automated usenet client/downloader. This Python script should display sysload status at the bottom of the page but as FreeBSD has very limited procfs support and from what I can gather, is working on removing it's support in entirely (wikipedia) I'm looking for an alternative source of load average I can inject into SabNZB.


Now. yes I know about 'sysctl vm.loadavg' but I don't know how I should go about getting that into SabNZB. besides..  executing a program every time a page is accessed has to be a bad practice 
(ps: this page is accessible from the internet.)

So, I was thinking.
How about I make a cron job that executes sysctl 'vm.loadavg' once every minute and saves the output to a file, which then the SabNZB script can read?
and how can I make sysctl's output fit SabNZB, or vice versa?




```
def loadavg():
    """ Return 1, 5 and 15 minute load average of host or "" if not supported
    """
    try:
        loadavgstr = open('/proc/loadavg', 'r').readline().strip()
    except:
        return ""
    data = loadavgstr.split()
    try:
        a1, a5, a15 = map(float, data[:3])
        return "%.2f, %.2f, %.2f" % (a1, a5, a15)
    except:
        return ""
```


----------



## expl (May 18, 2010)

Or you could just edit SabNZB to call sysctl(3) instead of reading procfs.


----------



## grump (May 18, 2010)

Maybe replace the original loadavg() with something like this:


```
def loadavg():

    command = ['sysctl', 'vm.loadavg']

    try:
        pipe = subprocess.Popen(command, stdout=subprocess.PIPE)
        a1, a5, a15 = map(float, pipe.stdout.read().split(' ')[2])
        return "%.2f, %.2f, %.2f" % (a1, a5, a15)
    except:
        return ""
```

I don't think calling that function every page hit is such a big deal.


----------



## Nordmann (May 18, 2010)

Thank you!
That did the trick.

After I replaced the original code with your code SabNZB now shows sysload correctly again 

This is during a forkbomb lol


----------



## Nordmann (May 18, 2010)

(where's the edit post button??)
Sorry for this double post.

only odd thing left now is that SabNZB doesn't show load averages when the server boots and runs SabNZB from it's dedicated user's crontab, but if I log in that user with putty, shut down SabNZB and then run it again through screen it shows the load averages?  weird


----------



## john_doe (May 18, 2010)

```
$ python
>>> import os
>>> os.getloadavg()
(0.02392578125, 0.76318359375, 0.69287109375)
>>> '%.2f %.2f %.2f' % os.getloadavg()
'0.02 0.76 0.69'

$ pydoc os.getloadavg
$ man getloadavg
```


----------



## Nordmann (May 18, 2010)

Hi

that looks like the correct way of getting loadavg in python, but I'm afraid I have no clue on how to use this..


----------



## grump (May 18, 2010)

Nordmann said:
			
		

> only odd thing left now is that SabNZB doesn't show load averages when the server boots and runs SabNZB from it's dedicated user's crontab, but if I log in that user with putty, shut down SabNZB and then run it again through screen it shows the load averages?  weird




Ah, try changing

```
command = ['sysctl', 'vm.loadavg']
```

to

```
command = ['/sbin/sysctl', 'vm.loadavg']
```


----------



## john_doe (May 18, 2010)

*Nordmann*, wasn't the example sufficient enough? Either 
	
	



```
import os
def loadavg():
    return '%.2f, %.2f, %.2f' % os.getloadavg()
```
or
	
	



```
import os
loadavg = lambda: '%.2f, %.2f, %.2f' % os.getloadavg()
```
Then you can call it
	
	



```
print(loadavg())
```
but this should be obvious if you've read python tutorial, at least the begining.

It doesn't check os.getloadavg() presence, though. I don't remember how to do it in python.


----------



## Nordmann (May 19, 2010)

*fixed!*

I've never read any programming tutorials, nor did I have any plans for doing that. 

I checked the beginning of the script file and it had 'import os' there, so I figured it wasn't required to do that again, so I I've replaced the entire loadavg section with just this


```
def loadavg():

        return "%.2f, %.2f, %.2f" % os.getloadavg()
```

and it works like a charm!

Thank you all for your help getting this to work.


----------

