vmware / pyvmomi-community-samples

A place for community contributed samples for the pyVmomi library.
Apache License 2.0
1.02k stars 922 forks source link

sample script to add and remove port mirroring session #445

Open aimin-tang opened 6 years ago

aimin-tang commented 6 years ago

Can someone point me to the sample script to configure a port mirroring session? I can manually configure a session and it works. With the pyVmomi script below, I can create a session that appears on vsphere client just like the manual entry, but it does not work! In addition, I cannot find a proper way to remove it.

creates a session but doesn't seem to work:

s_key = '112063'
s_port = vim.dvs.VmwareDistributedVirtualSwitch.VspanPorts(portKey=s_key)
d_key = '112072'
d_port = vim.dvs.VmwareDistributedVirtualSwitch.VspanPorts(portKey=d_key)
configVersion = dvs.config.configVersion
session = vim.dvs.VmwareDistributedVirtualSwitch.VspanSession(name="some-name", 
    enabled=True, stripOriginalVlan=True, normalTrafficAllowed=True, 
    sourcePortTransmitted=s_port, sourcePortReceived=s_port, destinationPort=d_port, 
    sessionType='dvPortMirror', samplingRate=1)
s_spec = vim.dvs.VmwareDistributedVirtualSwitch.VspanConfigSpec(vspanSession=session, 
    operation="add")
c_spec = vim.dvs.VmwareDistributedVirtualSwitch.ConfigSpec(vspanConfigSpec=[s_spec], 
    configVersion=configVersion)
t = dvs.ReconfigureDvs_Task(c_spec)

exception when trying to remove it:

configVersion = dvs.config.configVersion
session = vim.dvs.VmwareDistributedVirtualSwitch.VspanSession(name="some-name",)
s_spec = vim.dvs.VmwareDistributedVirtualSwitch.VspanConfigSpec(vspanSession=session, 
    operation="remove")
c_spec = vim.dvs.VmwareDistributedVirtualSwitch.ConfigSpec(vspanConfigSpec=[s_spec], 
    configVersion=configVersion)
t = dvs.ReconfigureDvs_Task(c_spec)

I appreciate the help!

petergyorgy commented 6 years ago

Could you solve this issue?

Akvinikym commented 4 years ago

True for me as well, could you please share, if it was solved?

I've spotted, that the mirror session actually works after you go vSphere client, click edit on this session and immediately save. However, it's not a solution for sure

Akvinikym commented 4 years ago

To however reads this after me: I think I've managed to solve it - when I did that edit-save thing, vSphere set value of parameter mirroredPacketLength to -1, even though documentation tells that value must not be less than 60. I did the same in my script, and the mirror session works perfectly now.

TL;DR: add mirroredPacketLength=-1 to the list of mirror session creation arguments