Welcome, guest | Sign In | My Account | Store | Cart

Every view months HP provides new SNMP MIB files for their new products (software and hardware). Using the provided HP shell commands mcompile and mxmib, you compile and register each .mib/.cfg file with their management software on a file by file basis. This script automates the process.

Shows: o Automation of shell commands o Using script/shell arguments o Error checking o OS independent file, path, directory handling o Sequential programming

Prerequisites: This script assumes you have HP Systems Insight Manager installed to provide the mcompile and mxmib programs if you wish to run it directly on your system.

Python, 59 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python

import os, shutil, sys

try:
    mibsToCompile = sys.argv[1]
except IndexError:
    print "addmibs.py - This program requires HP Systems Insight Manager"
    print "             to be installed."
    print "             This program adds mib files in a directory to the"
    print "             Systems Insight Manager '/mibs' directory and compiles"
    print "             them and then registers them with the system."
    print
    print "Useage: <python> addmibs.py [directory containing .mib files]"
    sys.exit(1)

# validate input
if os.path.isdir(mibsToCompile):
    mibsToCompile = os.path.abspath(mibsToCompile)
else:
    print mibsToCompile, 'is not a valid directory.'
    sys.exit(1)

# get path to HP SIM's mibs directory
for pathChunk in os.getenv("PATH").split(os.pathsep):
    if pathChunk.count("Systems Insight Manager"):
        hpMibsPath = pathChunk[:pathChunk.rfind(os.sep)+1] + "mibs"
        if os.path.isdir(hpMibsPath):
            break
        else:
            print "No valid mibs directory found in the Systems Insight Manager"
            print "directory."
            sys.exit(1)
    else:
        print "No Systems Insight Manager directory found in your system's"
        print "PATH."
        sys.exit(1)

# parse out mibs
mibFiles = []
for file in os.listdir(mibsToCompile):
    if file.endswith(".mib"):
        mibFiles.append(file)

# move mibs
if mibFiles:
    for file in mibFiles:
        shutil.move(mibsToCompile + os.sep + file, hpMibsPath)
else:
    print "No mib files found in %s" % mibsToCompile
    sys.exit(1)

# compile and register mibs
os.chdir(hpMibsPath)
for file in mibFiles:
    os.system("mcompile %s" % file)
    os.system("mxmib -a %s" % file[:-4] + ".cfg")

print "Done."

Now that you have the basics of how I automated a task. Take the framework and think of ways you can automate some tasks of your own!

Created by Peter Dilley on Mon, 12 Nov 2007 (PSF)
Python recipes (4591)
Peter Dilley's recipes (1)

Required Modules

Other Information and Tasks