13778
![]() |
|
|
|
|
|||||||
| Userland Programming & Scripting C, Shell, Perl, Sed & Awk |
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
#1
|
|||
|
|||
|
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 ""
|
|
#2
|
||||
|
||||
|
Or you could just edit SabNZB to call sysctl(3) instead of reading procfs.
|
|
#3
|
|||
|
|||
|
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 ""
|
| The Following User Says Thank You to grump For This Useful Post: | ||
Nordmann (May 18th, 2010) | ||
|
#4
|
|||
|
|||
|
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
|
|
#5
|
|||
|
|||
|
(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 |
|
#6
|
|||
|
|||
|
Quote:
Ah, try changing Code:
command = ['sysctl', 'vm.loadavg'] Code:
command = ['/sbin/sysctl', 'vm.loadavg'] |
|
#7
|
|||
|
|||
|
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 |
| The Following User Says Thank You to john_doe For This Useful Post: | ||
grump (May 18th, 2010) | ||
|
#8
|
|||
|
|||
|
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.. |
|
#9
|
|||
|
|||
|
Nordmann, wasn't the example sufficient enough? Either
Code:
import os
def loadavg():
return '%.2f, %.2f, %.2f' % os.getloadavg()
Code:
import os loadavg = lambda: '%.2f, %.2f, %.2f' % os.getloadavg() Code:
print(loadavg()) It doesn't check os.getloadavg() presence, though. I don't remember how to do it in python. |
| The Following User Says Thank You to john_doe For This Useful Post: | ||
Nordmann (May 19th, 2010) | ||
|
#10
|
|||
|
|||
|
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()
Thank you all for your help getting this to work.
|
![]() |
| Tags |
| loadavg, python, sabnzb |
| Thread Tools | |
| Display Modes | |
|
|
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 |