13778 /proc/loadavg alternative and SabNZB Python script? - The FreeBSD Forums
The FreeBSD Forums  

Go Back   The FreeBSD Forums > Development > Userland Programming & Scripting

Userland Programming & Scripting C, Shell, Perl, Sed & Awk

Reply
 
Thread Tools Display Modes
  #1  
Old May 17th, 2010, 23:18
Nordmann Nordmann is offline
Junior Member
 
Join Date: May 2010
Posts: 7
Thanks: 4
Thanked 0 Times in 0 Posts
Question /proc/loadavg alternative and SabNZB Python script?

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?



Code:
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 ""
Reply With Quote
  #2  
Old May 18th, 2010, 01:15
expl's Avatar
expl expl is offline
Member
 
Join Date: Oct 2009
Location: In your shell, stealing your cookies.
Posts: 639
Thanks: 0
Thanked 113 Times in 104 Posts
Default

Or you could just edit SabNZB to call sysctl(3) instead of reading procfs.
Reply With Quote
  #3  
Old May 18th, 2010, 08:05
grump grump is offline
Junior Member
 
Join Date: Nov 2008
Posts: 2
Thanks: 1
Thanked 1 Time in 1 Post
Default

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

Code:
def loadavg():

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

    try:
        pipe = subprocess.Popen(command, stdout=subprocess.PIPE)
        a1, a5, a15 = map(float, pipe.stdout.read().split(' ')[2:5])
        return "%.2f, %.2f, %.2f" % (a1, a5, a15)
    except:
        return ""
I don't think calling that function every page hit is such a big deal.
Reply With Quote
The Following User Says Thank You to grump For This Useful Post:
Nordmann (May 18th, 2010)
  #4  
Old May 18th, 2010, 12:56
Nordmann Nordmann is offline
Junior Member
 
Join Date: May 2010
Posts: 7
Thanks: 4
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #5  
Old May 18th, 2010, 13:15
Nordmann Nordmann is offline
Junior Member
 
Join Date: May 2010
Posts: 7
Thanks: 4
Thanked 0 Times in 0 Posts
Default

(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
Reply With Quote
  #6  
Old May 18th, 2010, 20:41
grump grump is offline
Junior Member
 
Join Date: Nov 2008
Posts: 2
Thanks: 1
Thanked 1 Time in 1 Post
Default

Quote:
Originally Posted by Nordmann View 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

Ah, try changing
Code:
    command = ['sysctl', 'vm.loadavg']
to
Code:
    command = ['/sbin/sysctl', 'vm.loadavg']
Reply With Quote
  #7  
Old May 18th, 2010, 16:30
john_doe john_doe is offline
Banned
 
Join Date: Aug 2009
Posts: 78
Thanks: 1
Thanked 22 Times in 18 Posts
Default

Code:
$ 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
Reply With Quote
The Following User Says Thank You to john_doe For This Useful Post:
grump (May 18th, 2010)
  #8  
Old May 18th, 2010, 18:57
Nordmann Nordmann is offline
Junior Member
 
Join Date: May 2010
Posts: 7
Thanks: 4
Thanked 0 Times in 0 Posts
Default

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..
Reply With Quote
  #9  
Old May 18th, 2010, 20:45
john_doe john_doe is offline
Banned
 
Join Date: Aug 2009
Posts: 78
Thanks: 1
Thanked 22 Times in 18 Posts
Default

Nordmann, wasn't the example sufficient enough? Either
Code:
import os
def loadavg():
    return '%.2f, %.2f, %.2f' % os.getloadavg()
or
Code:
import os
loadavg = lambda: '%.2f, %.2f, %.2f' % os.getloadavg()
Then you can call it
Code:
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.
Reply With Quote
The Following User Says Thank You to john_doe For This Useful Post:
Nordmann (May 19th, 2010)
  #10  
Old May 19th, 2010, 16:00
Nordmann Nordmann is offline
Junior Member
 
Join Date: May 2010
Posts: 7
Thanks: 4
Thanked 0 Times in 0 Posts
Talking 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

Code:
def loadavg():

        return "%.2f, %.2f, %.2f" % os.getloadavg()
and it works like a charm!

Thank you all for your help getting this to work.
Reply With Quote
Reply

Tags
loadavg, python, sabnzb

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Python script which checks for syntax error in rc.conf Kami Userland Programming & Scripting 23 May 5th, 2010 17:07
[Solved] Alternative to portversion ikbendeman Installation and Maintenance of FreeBSD Ports or Packages 8 March 4th, 2010 08:18
VTUN alternative LoZio Networking 8 November 6th, 2009 09:27
[Solved] Create user from python script? solshark Userland Programming & Scripting 2 September 2nd, 2009 09:53
Check connection with timeout, script telnet web server in script bsddaemon Web & Network Services 5 December 7th, 2008 14:03


All times are GMT +1. The time now is 02:17.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
The mark FreeBSD is a registered trademark of The FreeBSD Foundation and is used by The FreeBSD Project with the permission of The FreeBSD Foundation.
Web protection and acceleration provided by CloudFlare
0