Sample script to set Custom Attribute/Field from a managed host without agent_tools (339 Views)
Reply
Valued Contributor
Dimiter Todorov
Posts: 109
Registered: ‎03-02-2010
Message 1 of 1 (339 Views)

Sample script to set Custom Attribute/Field from a managed host without agent_tools

So,

We had a requirement to push some custom fields and attributes as part of some scripts.

Without having to install the agent tools, this is what I came up with.

The first is a bat script that calls the PY script below.

The PY script will also work on UNIX systems, just need to set some environment variables.

 

@echo off
setlocal
pushd %SystemDrive%\Windows\itr
set PATH=%SystemDrive%\Program Files\Opsware\agent\lcpython15;%SystemDrive%\Program Files\Opsware\agent\bin;%PATH%
set PYTHONPATH=%SystemDrive%\Program Files\Opsware\agent\pylibs
python set_virtual_column.py %*

 

This is the Python script. Not much error checking is involved here.

This is meant to run from the context of a managed server only.

 

 

# Set Virtual Column on Local Server Object.
# Dimiter Todorov - 2013

import os
import re
import subprocess
import os
import sys
import getopt

from optparse import OptionParser


if (sys.platform == 'win32'):
    pytwist_dir = (os.environ['SystemDrive'] + '\\Program Files\\Opsware\\smopylibs2')
    pylibs_dir = (os.environ['SystemDrive'] + '\\Program Files\\Opsware\\agent\\pylibs')
    if os.path.isdir(pylibs_dir):
        pylibs_dir = (os.environ['SystemDrive'] + '\\Program Files\\Loudcloud\\Blackshadow')
        if os.path.isdir(pylibs_dir):
            raise 'The Opsware pylibs modules cannot be located'
else:
    pytwist_dir = '/opt/opsware/pylibs'
    pylibs_dir = '/opt/opsware/agent/pylibs'
    if os.path.isdir(pylibs_dir):
        pylibs_dir = '/lc/blackshadow/coglib'
        if os.path.isdir(pylibs_dir):
            raise 'The Opsware pylibs modules cannot be located'
sys.path.append(pylibs_dir)
sys.path.append(pytwist_dir)
from pytwist import *
from pytwist.com.opsware.search import Filter
ts = twistserver.TwistServer()


def set_server_custom_attribute(ts, key, value):
    filter = Filter()
    filter.expression = ""
    servers = ts.server.ServerService.findServerRefs(filter)
    server_ref = servers[0]
    result = []
    result.append(ts.server.ServerService.setCustAttr(server_ref, key, value))
    return result

def set_server_custom_field(ts, key, value):
    filter = Filter()
    filter.expression = ""
    servers = ts.server.ServerService.findServerRefs(filter)
    server_ref = servers[0]
    result = []
    result.append(ts.server.ServerService.setCustomField(server_ref, key, value))
    return result


def usage():
    print "Usage Here"

if (__name__ == '__main__'):
    parser = OptionParser(description=__doc__, version="0.0.1",
            usage='%prog [--type "CUSTOM_FIELD" | "CUSTOM_ATTRIBUTE"] [--key "KEY"] [--value "VALUE"]')
    parser.add_option("-t", "--type", action="store", dest="vcol_type", metavar="CUSTOM_FIELD", default="CUSTOM_ATTRIBUTE",
        help="CUSTOM_FIELD or CUSTOM_ATTRIBUTE")
    parser.add_option("-k", "--key", action="store", dest="vcol_key", metavar="VCOL Key",
        help="Key for custom virtual Column")
    parser.add_option("-v", "--value", action="store", dest="vcol_value", default=False,
        help="Value for custom virtual Column")
    

    try:
        (opts, args) = parser.parse_args(sys.argv[1:])
    except getopt.GetoptError:
        parser.print_help()
        sys.exit(2)

    if(opts.vcol_key=="" or opts.vcol_value=='' or opts.vcol_value==None or opts.vcol_key==None):
        print "KEY and VALUE are required."
        parser.print_help()
        sys.exit(2)
    if(opts.vcol_type=="CUSTOM_ATTRIBUTE"):
        set_server_custom_attribute(ts,opts.vcol_key,opts.vcol_value)
    elif(opts.vcol_type=="CUSTOM_FIELD"):
        set_server_custom_field(ts,opts.vcol_key,opts.vcol_value)
    else:
        print "Invalid setting for type."
        parser.print_help()
        sys.exit(2)

 

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.