Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 587495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:13:06+00:00 2026-05-13T15:13:06+00:00

I wrote this simple Munin plugin to graph average fan speed and I want

  • 0

I wrote this simple Munin plugin to graph average fan speed and I want to redo it to OOP – strictly as a learning exercise. Don’t have a clue where to start though. Anyone feel like offering some guidance or even an example of what this script should look like when done. I will use it to redo some other scripts into an OOP style as well; again for learning purposes.

import sys
import subprocess

CMD = "/usr/sbin/omreport chassis fans".split()

# Munin populates sys.argv[1] with "" (an empty argument), lets remove it.
sys.argv = [x for x in sys.argv if x]

if len(sys.argv) > 1:
    if sys.argv[1].lower() == "autoconfig":
        print "autoconfig"
    elif sys.argv[1].lower() == "config":
        print "graph_title Average Fan Speed"
        print "graph_args --base 1000 -l 0"
        print "graph_vlabel speed (RPM)"
        print "graph_category Chassis"
        print "graph_info This graph shows the average speed of all fans"
        print "graph_period second"
        print "speed.label speed"
        print "speed.info Average fan speed for the five minutes."
else:
    try:
        data = subprocess.Popen(CMD,stdout=subprocess.PIPE).stdout.readlines()
    except OSError, e:
        print >> sys.stderr, "Error running '%s', %s" % (" ".join(cmd), e)
        sys.exit(1)

    count = total = 0
    for item in data:
        if "Reading" in item:
            # Extract variable length fan speed, without regex.
            total += int(item.split(":")[1].split()[0])
            count += 1
    # Sometimes omreport returns zero output if omsa services aren't started.
    if not count or not total:
        print >> sys.stderr, 'Error: "omreport chassis fans" returned 0 output.'
        print >> sys.stderr, 'OMSA running? Try: "srvadmin-services.sh status".'
        sys.exit(1)

    avg = (total / count)
    print "speed.value %s" % avg
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:13:06+00:00Added an answer on May 13, 2026 at 3:13 pm

    You remake it in OOP by identifying code and data that goes together. These you then merge into “classes”.

    You actual data above seems to be the output of a process.
    The code is iterating over it. I guess you can make a class out of that if you want to, but it’s a bit silly. 🙂

    So, something like this (obviously completely untested code):

    import sys
    import subprocess
    
    class Fanspeed(object):
    
        def __init__(self, command):
            self.command = command.split()
    
        def average_fan_speed(self):
            data = subprocess.Popen(CMD,stdout=subprocess.PIPE).stdout.readlines()
    
            count = total = 0
            for item in data:
                if "Reading" in item:
                    # Extract variable length fan speed, without regex.
                    total += int(item.split(":")[1].split()[0])
                    count += 1
            # Sometimes omreport returns zero output if omsa services aren't started.
            if not count or not total:
                raise ValueError("I found no fans. Is OMSA services started?"
    
            avg = (total / count)
            return % avg
    
    if __main__ == '__main__':
        # Munin populates sys.argv[1] with "" (an empty argument), lets remove it.
        sys.argv = [x for x in sys.argv if x]
    
        if len(sys.argv) > 1:
            if sys.argv[1].lower() == "autoconfig":
                print "autoconfig"
            elif sys.argv[1].lower() == "config":
                print "graph_title Average Fan Speed"
                print "graph_args --base 1000 -l 0"
                print "graph_vlabel speed (RPM)"
                print "graph_category Chassis"
                print "graph_info This graph shows the average speed of all fans"
                print "graph_period second"
                print "speed.label speed"
                print "speed.info Average fan speed for the five minutes."
        else:
            try:
                cmd = "/usr/sbin/omreport chassis fans"
                fanspeed = Fanspeed(cmd)
                average = fanspeed.average_fan_speed()
            except OSError, e:
                print >> sys.stderr, "Error running '%s', %s" % (cmd, e)
                sys.exit(1)
            except ValueError, e:
                # Sometimes omreport returns zero output if omsa services aren't started.
                print >> sys.stderr, 'Error: "omreport chassis fans" returned 0 output.'
                print >> sys.stderr, 'OMSA running? Try: "srvadmin-services.sh status".'
                sys.exit(1)
    

    But YMMV. It’s perhaps a bit clearer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning socket programming in c, I wrote this simple program to accept
I wrote simple custom validation on javascript based on this doc . I want
First a little intro: Last year i wrote this http://dragan.yourtree.org/code/canvas-3d-graph/ Now, i want to
I wrote this simple test code, by adapting a piece from a book, to
I wrote this simple piece of code to prevent a form from being submitted
I wrote this simple query statement: INSERT INTO merchants ('firstName','lastName') VALUES ('Bob','Smith') Sounds very
I wrote this simple linq-to-xml query and it seems that null exception could not
I wrote this simple program: void sig_ha(int signum) { cout<<received SIGINT\n; } int main()
So I wrote this simple console app to aid in my question asking. What
I've wrote this simple piece of code. And I have a slight problem with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.