Skip to content

Create a custom Server Plugin (example)

cocoon edited this page May 18, 2018 · 2 revisions

Server Plugin example

This example python script will have two functions and will use a specified argument that can be submitted from the client.

The script

Copy this python script "FcInfoScript" on each XCP-NG server as /etc/xapi.d/plugins/FcInfoScript

#!/usr/bin/python

# put this script on XCP or XenServer as: /etc/xapi.d/plugins/FcInfoScript

import subprocess
import XenAPIPlugin


def FcInfo(session, arg_dict):
    prefix = arg_dict['CustomPrefix']
    #wwn = subprocess.check_output("systool -c fc_host -A port_name", shell=True).strip()
    fc = subprocess.check_output("systool -c fc_host -v", shell=True).strip()
    return "Custom prefix given as argument: " + prefix + " FcInfo: " + fc

def HostGUIDs(session, arg_dict):
    return repr(session.xenapi.host.get_all())

if __name__ == "__main__":
    XenAPIPlugin.dispatch(
        {"FcInfo": FcInfo, "HostGUIDs": HostGUIDs}
    )

make it executable with:

chmod +x /etc/xapi.d/plugins/FcInfoScript

Using the script from a client

Call the plugin in the XCP-NG Console (c#)

This is how you can get all host GUIDs as string in csharp code:

Dictionary<string, string> PluginArgs = new Dictionary<string, string> { { "CustomPrefix", "My custom prefix text" } };
string PluginResult = String.Empty;
try
{
    PluginResult = XenAPI.Host.call_plugin(host.Connection.Session, host.opaque_ref, "FcInfoScript", "HostGUIDs", PluginArgs);
}
catch(Exception ex)
{
    PluginResult = "[ERROR] " + ex.ToString();
}

This is how you can get the fibre channel information as string in csharp code:

Dictionary<string, string> PluginArgs = new Dictionary<string, string> { { "CustomPrefix", "My other custom prefix text" } };
string PluginResult = String.Empty;
try
{
    PluginResult = XenAPI.Host.call_plugin(host.Connection.Session, host.opaque_ref, "FcInfoScript", "FcInfo", PluginArgs);
}
catch(Exception ex)
{
    PluginResult = "[ERROR] " + ex.ToString();
}

Call the plugin from Linux

Put this python script "xentest.py" on any machine with python

import sys
import XenAPI
import ssl

#UNSECURE UNCOMMENT ONLY FOR TEST TO ALLOW UNTRUSTED CERTIFICATES CAUSING THE FOLLOWING ERROR:
#ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)

#ssl._create_default_https_context = ssl._create_unverified_context



script_file_name = sys.argv[0]

targethost = sys.argv[1]
session_pw = sys.argv[2]
session = XenAPI.Session('https://' + targethost)

#session.slave_local_login_with_password should usually only be used iif master failed or if intended
session.slave_local_login_with_password('root', session_pw)

hosts = session.xenapi.host.get_all()
host = hosts[0]

host_name = session.xenapi.host.get_name_label(host)
host_address = session.xenapi.host.get_address(host)

print 'host name: ' + host_name + ' address: ' + host_address

print session.xenapi.host.call_plugin(host, 'FcInfoScript', 'FcInfo', {'CustomPrefix' : 'FC Info: '})


### Plugin working only with using Master

#session.login_with_password is only allowed to the master! Will return error and IP of master if targeting a slave
try:
        session.login_with_password('root', session_pw)
        print session.xenapi.host.call_plugin(host, 'FcInfoScript', 'HostGUIDs', {'CustomPrefix' : 'My Hosts: '})
except XenAPI.Failure as e:
        print 'catching XenAPI.Failure: '
        for k,v in e._details_map().iteritems():
            print(k, v)
        #get master from error message
        if len(e._details_map()) == 2:
                xenmaster = e._details_map()['1']
                session = XenAPI.Session('https://' + xenmaster)
                session.login_with_password('root', session_pw)
                print session.xenapi.host.call_plugin(host, 'FcInfoScript', 'HostGUIDs', {'CustomPrefix' : 'My Hosts: '})
        else:
                print 'error message was in wrong format'
else:
        print 'other error occured'

install the xenapi with the command:

pip install xenapi

Then you should be able to call the script with:

python xentest.py YOURXCPNGSERVERIPorFQDN YOURXCPNGROOTPASSWORD